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:
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user