### What?
Improves querying performance of the Local API, reduces copying overhead
### How?
Adds `flattenedFields` property for sanitized collection/global config
which contains fields in database schema structure.
For example, It removes rows / collapsible / unnamed tabs and merges
them to the parent `fields`, ignores UI fields, named tabs are added as
`type: 'tab'`.
This simplifies code in places like Drizzle `transform/traverseFields`.
Also, now we can avoid calling `flattenTopLevelFields` which adds some
overhead and use `collection.flattenedFields` / `field.flattenedFields`.
By refactoring `configToJSONSchema.ts` with `flattenedFields` it also
1. Fixes https://github.com/payloadcms/payload/issues/9467
2. Fixes types for UI fields were generated for `select`
Removes this deep copying for each `where` query constraint
58ac784425/packages/payload/src/database/queryValidation/validateQueryPaths.ts (L69-L73)
which potentially can add overhead if you have a large collection/global
config
UPD:
The overhead is even much more than in the benchmark below if you have
Lexical with blocks.
Benchmark in `relationships/int.spec.ts`:
```ts
const now = Date.now()
for (let i = 0; i < 10; i++) {
const now = Date.now()
for (let i = 0; i < 300; i++) {
const query = await payload.find({
collection: 'chained',
where: {
'relation.relation.name': {
equals: 'third',
},
and: [
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
{
'relation.relation.name': {
equals: 'third',
},
},
],
},
})
}
payload.logger.info(`#${i + 1} ${Date.now() - now}`)
}
payload.logger.info(`Total ${Date.now() - now}`)
```
Before:
```
[02:11:48] INFO: #1 3682
[02:11:50] INFO: #2 2199
[02:11:54] INFO: #3 3483
[02:11:56] INFO: #4 2516
[02:11:59] INFO: #5 2467
[02:12:01] INFO: #6 1987
[02:12:03] INFO: #7 1986
[02:12:05] INFO: #8 2375
[02:12:07] INFO: #9 2040
[02:12:09] INFO: #10 1920
[PASS] Relationships > Querying > Nested Querying > should allow querying two levels deep (24667ms)
[02:12:09] INFO: Total 24657
```
After:
```
[02:12:36] INFO: #1 2113
[02:12:38] INFO: #2 1854
[02:12:40] INFO: #3 1700
[02:12:42] INFO: #4 1797
[02:12:44] INFO: #5 2121
[02:12:46] INFO: #6 1774
[02:12:47] INFO: #7 1670
[02:12:49] INFO: #8 1610
[02:12:50] INFO: #9 1596
[02:12:52] INFO: #10 1576
[PASS] Relationships > Querying > Nested Querying > should allow querying two levels deep (17828ms)
[02:12:52] INFO: Total 17818
```
140 lines
4.1 KiB
TypeScript
140 lines
4.1 KiB
TypeScript
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
|
|
import type { Init, SanitizedCollectionConfig } 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 type { BaseExtraConfig } from './schema/build.js'
|
|
import type { SQLiteAdapter } from './types.js'
|
|
|
|
import { buildTable } from './schema/build.js'
|
|
|
|
export const init: Init = async function init(this: SQLiteAdapter) {
|
|
let locales: [string, ...string[]] | undefined
|
|
await executeSchemaHooks({ type: 'beforeSchemaInit', adapter: this })
|
|
|
|
if (this.payload.config.localization) {
|
|
locales = this.payload.config.localization.locales.map(({ code }) => code) as [
|
|
string,
|
|
...string[],
|
|
]
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
})
|
|
|
|
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,
|
|
})
|
|
|
|
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)
|
|
|
|
buildTable({
|
|
adapter: this,
|
|
disableNotNull: !!global.versions?.drafts,
|
|
disableUnique: true,
|
|
fields: versionFields,
|
|
locales,
|
|
tableName: versionsTableName,
|
|
timestamps: true,
|
|
versions: true,
|
|
})
|
|
}
|
|
})
|
|
|
|
await executeSchemaHooks({ type: 'afterSchemaInit', adapter: this })
|
|
}
|