Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deduplicate and abstract SQL schema building #9987

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/db-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export function postgresAdapter(args: Args): DatabaseAdapterObj<PostgresAdapter>
packageName: '@payloadcms/db-postgres',
payload,
queryDrafts,
rawRelations: {},
rawTables: {},
rejectInitializing,
requireDrizzleKit,
resolveInitializing,
Expand Down
2 changes: 2 additions & 0 deletions packages/db-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
operators,
prodMigrations: args.prodMigrations,
push: args.push,
rawRelations: {},
rawTables: {},
relations: {},
relationshipsSuffix: args.relationshipsSuffix || '_rels',
schema: {},
Expand Down
138 changes: 19 additions & 119 deletions packages/db-sqlite/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,138 +1,38 @@
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
import type { Init, SanitizedCollectionConfig } from 'payload'
import type { Init } from 'payload'

import { createTableName, executeSchemaHooks } from '@payloadcms/drizzle'
import { uniqueIndex } from 'drizzle-orm/sqlite-core'
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'
import toSnakeCase from 'to-snake-case'
import { buildDrizzleRelations, buildRawSchema, executeSchemaHooks } from '@payloadcms/drizzle'

import type { BaseExtraConfig } from './schema/build.js'
import type { SQLiteAdapter } from './types.js'

import { buildTable } from './schema/build.js'
import { buildDrizzleTable } from './schema/buildDrizzleTable.js'
import { setColumnID } from './schema/setColumnID.js'

export const init: Init = async function init(this: SQLiteAdapter) {
let locales: [string, ...string[]] | undefined
await executeSchemaHooks({ type: 'beforeSchemaInit', adapter: this })
let locales: string[] | undefined

this.rawRelations = {}
this.rawTables = {}

if (this.payload.config.localization) {
locales = this.payload.config.localization.locales.map(({ code }) => code) as [
string,
...string[],
]
locales = this.payload.config.localization.locales.map(({ code }) => code)
}

this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
createTableName({
adapter: this as unknown as DrizzleAdapter,
config: collection,
})

if (collection.versions) {
createTableName({
adapter: this as unknown as DrizzleAdapter,
config: collection,
versions: true,
versionsCustomName: true,
})
}
})
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))
const config = this.payload.config

const baseExtraConfig: BaseExtraConfig = {}

if (collection.upload.filenameCompoundIndex) {
const indexName = `${tableName}_filename_compound_idx`

baseExtraConfig.filename_compound_index = (cols) => {
const colsConstraint = collection.upload.filenameCompoundIndex.map((f) => {
return cols[f]
})
return uniqueIndex(indexName).on(colsConstraint[0], ...colsConstraint.slice(1))
}
}
const adapter = this as unknown as DrizzleAdapter

if (collection.upload.filenameCompoundIndex) {
const indexName = `${tableName}_filename_compound_idx`

baseExtraConfig.filename_compound_index = (cols) => {
const colsConstraint = collection.upload.filenameCompoundIndex.map((f) => {
return cols[f]
})
return uniqueIndex(indexName).on(colsConstraint[0], ...colsConstraint.slice(1))
}
}

buildTable({
adapter: this,
disableNotNull: !!collection?.versions?.drafts,
disableUnique: false,
fields: collection.flattenedFields,
locales,
tableName,
timestamps: collection.timestamps,
versions: false,
})

if (collection.versions) {
const versionsTableName = this.tableNameMap.get(
`_${toSnakeCase(collection.slug)}${this.versionsSuffix}`,
)
const versionFields = buildVersionCollectionFields(config, collection, true)

buildTable({
adapter: this,
disableNotNull: !!collection.versions?.drafts,
disableUnique: true,
fields: versionFields,
locales,
tableName: versionsTableName,
timestamps: true,
versions: true,
})
}
buildRawSchema({
adapter,
setColumnID,
})

this.payload.config.globals.forEach((global) => {
const tableName = createTableName({
adapter: this as unknown as DrizzleAdapter,
config: global,
})

buildTable({
adapter: this,
disableNotNull: !!global?.versions?.drafts,
disableUnique: false,
fields: global.flattenedFields,
locales,
tableName,
timestamps: false,
versions: false,
})
await executeSchemaHooks({ type: 'beforeSchemaInit', adapter: this })

if (global.versions) {
const versionsTableName = createTableName({
adapter: this as unknown as DrizzleAdapter,
config: global,
versions: true,
versionsCustomName: true,
})
const config = this.payload.config
const versionFields = buildVersionGlobalFields(config, global, true)
for (const tableName in this.rawTables) {
buildDrizzleTable({ adapter, locales, rawTable: this.rawTables[tableName] })
}

buildTable({
adapter: this,
disableNotNull: !!global.versions?.drafts,
disableUnique: true,
fields: versionFields,
locales,
tableName: versionsTableName,
timestamps: true,
versions: true,
})
}
buildDrizzleRelations({
adapter,
})

await executeSchemaHooks({ type: 'afterSchemaInit', adapter: this })
Expand Down
Loading