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:
Sasha
2024-12-20 22:13:28 +02:00
committed by GitHub
parent 99481cbc7d
commit 4e953530df
10 changed files with 72 additions and 31 deletions

View File

@@ -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) {

View File

@@ -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,

View File

@@ -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) {

View File

@@ -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,
}

View File

@@ -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']