feat(db-sqlite): add autoIncrement option (#9427)
### What? Exposes ability to enable [AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for Primary Keys which ensures that the same ID cannot be reused from previously deleted rows. ```ts sqliteAdapter({ autoIncrement: true }) ``` ### Why? This may be essential for some systems. Enabled `autoIncrement: true` also for the SQLite Adapter in our tests, which can be useful when testing whether the doc was deleted or not when you also have other create operations. ### How? Uses Drizzle's `autoIncrement` option. WARNING: This cannot be enabled in an existing project without a custom migration, as it completely changes how primary keys are stored in the database.
This commit is contained in:
@@ -88,7 +88,13 @@ export const columnToCodeConverter: ColumnToCodeConverter = ({
|
||||
}
|
||||
|
||||
if (column.primaryKey) {
|
||||
code = `${code}.primaryKey()`
|
||||
let arg = ''
|
||||
|
||||
if (column.type === 'integer' && column.autoIncrement) {
|
||||
arg = `{ autoIncrement: true }`
|
||||
}
|
||||
|
||||
code = `${code}.primaryKey(${arg})`
|
||||
}
|
||||
|
||||
if (defaultStatement) {
|
||||
|
||||
@@ -86,6 +86,7 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
|
||||
return createDatabaseAdapter<SQLiteAdapter>({
|
||||
name: 'sqlite',
|
||||
afterSchemaInit: args.afterSchemaInit ?? [],
|
||||
autoIncrement: args.autoIncrement ?? false,
|
||||
beforeSchemaInit: args.beforeSchemaInit ?? [],
|
||||
client: undefined,
|
||||
clientConfig: args.client,
|
||||
|
||||
@@ -87,7 +87,13 @@ export const buildDrizzleTable: BuildDrizzleTable = ({ adapter, locales, rawTabl
|
||||
}
|
||||
|
||||
if (column.primaryKey) {
|
||||
columns[key].primaryKey()
|
||||
let args: Record<string, unknown> | undefined = undefined
|
||||
|
||||
if (column.type === 'integer' && column.autoIncrement) {
|
||||
args = { autoIncrement: true }
|
||||
}
|
||||
|
||||
columns[key].primaryKey(args)
|
||||
}
|
||||
|
||||
if (column.notNull) {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import type { SetColumnID } from '@payloadcms/drizzle/types'
|
||||
|
||||
import type { SQLiteAdapter } from '../types.js'
|
||||
|
||||
export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
|
||||
const idField = fields.find((field) => field.name === 'id')
|
||||
if (idField) {
|
||||
@@ -36,6 +38,7 @@ export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
|
||||
columns.id = {
|
||||
name: 'id',
|
||||
type: 'integer',
|
||||
autoIncrement: (adapter as unknown as SQLiteAdapter).autoIncrement,
|
||||
primaryKey: true,
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,11 @@ export type Args = {
|
||||
* Examples may include: composite indices, generated columns, vectors
|
||||
*/
|
||||
afterSchemaInit?: SQLiteSchemaHook[]
|
||||
/**
|
||||
* Enable [AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for Primary Keys.
|
||||
* This ensures that the same ID cannot be reused from previously deleted rows.
|
||||
*/
|
||||
autoIncrement?: boolean
|
||||
/**
|
||||
* Transform the schema before it's built.
|
||||
* You can use it to preserve an existing database schema and if there are any collissions Payload will override them.
|
||||
@@ -124,6 +129,7 @@ type Drizzle = { $client: Client } & LibSQLDatabase<ResolveSchemaType<GeneratedD
|
||||
|
||||
export type SQLiteAdapter = {
|
||||
afterSchemaInit: SQLiteSchemaHook[]
|
||||
autoIncrement: boolean
|
||||
beforeSchemaInit: SQLiteSchemaHook[]
|
||||
client: Client
|
||||
clientConfig: Args['client']
|
||||
|
||||
Reference in New Issue
Block a user