Skip to content

Commit 7c4ea5b

Browse files
authored
refactor: optimize database schema generation bin script (#10086)
* Avoids additional file system writes (1 for `await writeFile` and then `npx prettier --write`) instead prettier now formats the javascript string directly. Went from 650 MS to 250 MS for the prettify block. * Disables database connection, since the `db.generateSchema` doesn't need connection, this also disables Drizzle schema push. * Properly exits the bin script process.
1 parent 7292220 commit 7c4ea5b

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

packages/drizzle/src/utilities/createSchemaGenerator.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import type { GenerateSchema } from 'payload'
22

3-
import { exec } from 'child_process'
43
import { existsSync } from 'fs'
54
import { writeFile } from 'fs/promises'
65
import path from 'path'
7-
import { promisify } from 'util'
86

97
import type { ColumnToCodeConverter, DrizzleAdapter } from '../types.js'
108

11-
const execAsync = promisify(exec)
12-
139
/**
1410
* @example
1511
* console.log(sanitizeObjectKey("oneTwo")); // oneTwo
@@ -271,7 +267,7 @@ declare module '${this.packageName}/types' {
271267
*/
272268
`
273269

274-
const code = [
270+
let code = [
275271
warning,
276272
...importDeclarationsSanitized,
277273
schemaDeclaration,
@@ -295,15 +291,18 @@ declare module '${this.packageName}/types' {
295291
}
296292
}
297293

298-
await writeFile(outputFile, code, 'utf-8')
299-
300294
if (prettify) {
301295
try {
302-
await execAsync(`npx prettier ${outputFile} --write`)
296+
const prettier = await import('prettier')
297+
const configPath = await prettier.resolveConfigFile()
298+
const config = configPath ? await prettier.resolveConfig(configPath) : {}
299+
code = await prettier.format(code, { ...config, parser: 'typescript' })
303300
// eslint-disable-next-line no-empty
304301
} catch {}
305302
}
306303

304+
await writeFile(outputFile, code, 'utf-8')
305+
307306
if (log) {
308307
this.payload.logger.info(`Written ${outputFile}`)
309308
}

packages/payload/src/bin/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from 'path'
66
import type { BinScript } from '../config/types.js'
77

88
import { findConfig } from '../config/find.js'
9-
import { getPayload } from '../index.js'
9+
import payload, { getPayload } from '../index.js'
1010
import { generateImportMap } from './generateImportMap/index.js'
1111
import { generateTypes } from './generateTypes.js'
1212
import { info } from './info.js'
@@ -110,7 +110,12 @@ export const bin = async () => {
110110
}
111111

112112
if (script === 'generate:db-schema') {
113-
const payload = await getPayload({ config })
113+
// Barebones instance to access database adapter, without connecting to the DB
114+
await payload.init({
115+
config,
116+
disableDBConnect: true,
117+
disableOnInit: true,
118+
})
114119

115120
if (typeof payload.db.generateSchema !== 'function') {
116121
payload.logger.error({
@@ -124,6 +129,8 @@ export const bin = async () => {
124129
log: args.log === 'false' ? false : true,
125130
prettify: args.prettify === 'false' ? false : true,
126131
})
132+
133+
process.exit(0)
127134
}
128135

129136
console.error(`Unknown script: "${script}".`)

test/relationships/payload-generated-schema.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,22 +422,44 @@ export const polymorphic_relationships = pgTable(
422422
}),
423423
)
424424

425+
export const polymorphic_relationships_locales = pgTable(
426+
'polymorphic_relationships_locales',
427+
{
428+
id: serial('id').primaryKey(),
429+
_locale: enum__locales('_locale').notNull(),
430+
_parentID: integer('_parent_id').notNull(),
431+
},
432+
(columns) => ({
433+
_localeParent: uniqueIndex('polymorphic_relationships_locales_locale_parent_id_unique').on(
434+
columns._locale,
435+
columns._parentID,
436+
),
437+
_parentIdFk: foreignKey({
438+
columns: [columns['_parentID']],
439+
foreignColumns: [polymorphic_relationships.id],
440+
name: 'polymorphic_relationships_locales_parent_id_fk',
441+
}).onDelete('cascade'),
442+
}),
443+
)
444+
425445
export const polymorphic_relationships_rels = pgTable(
426446
'polymorphic_relationships_rels',
427447
{
428448
id: serial('id').primaryKey(),
429449
order: integer('order'),
430450
parent: integer('parent_id').notNull(),
431451
path: varchar('path').notNull(),
452+
locale: enum__locales('locale'),
432453
moviesID: integer('movies_id'),
433454
},
434455
(columns) => ({
435456
order: index('polymorphic_relationships_rels_order_idx').on(columns.order),
436457
parentIdx: index('polymorphic_relationships_rels_parent_idx').on(columns.parent),
437458
pathIdx: index('polymorphic_relationships_rels_path_idx').on(columns.path),
459+
localeIdx: index('polymorphic_relationships_rels_locale_idx').on(columns.locale),
438460
polymorphic_relationships_rels_movies_id_idx: index(
439461
'polymorphic_relationships_rels_movies_id_idx',
440-
).on(columns.moviesID),
462+
).on(columns.moviesID, columns.locale),
441463
parentFk: foreignKey({
442464
columns: [columns['parent']],
443465
foreignColumns: [polymorphic_relationships.id],
@@ -1093,6 +1115,16 @@ export const relations_movie_reviews = relations(movie_reviews, ({ one, many })
10931115
relationName: '_rels',
10941116
}),
10951117
}))
1118+
export const relations_polymorphic_relationships_locales = relations(
1119+
polymorphic_relationships_locales,
1120+
({ one }) => ({
1121+
_parentID: one(polymorphic_relationships, {
1122+
fields: [polymorphic_relationships_locales._parentID],
1123+
references: [polymorphic_relationships.id],
1124+
relationName: '_locales',
1125+
}),
1126+
}),
1127+
)
10961128
export const relations_polymorphic_relationships_rels = relations(
10971129
polymorphic_relationships_rels,
10981130
({ one }) => ({
@@ -1111,6 +1143,9 @@ export const relations_polymorphic_relationships_rels = relations(
11111143
export const relations_polymorphic_relationships = relations(
11121144
polymorphic_relationships,
11131145
({ many }) => ({
1146+
_locales: many(polymorphic_relationships_locales, {
1147+
relationName: '_locales',
1148+
}),
11141149
_rels: many(polymorphic_relationships_rels, {
11151150
relationName: '_rels',
11161151
}),
@@ -1347,6 +1382,7 @@ type DatabaseSchema = {
13471382
movie_reviews: typeof movie_reviews
13481383
movie_reviews_rels: typeof movie_reviews_rels
13491384
polymorphic_relationships: typeof polymorphic_relationships
1385+
polymorphic_relationships_locales: typeof polymorphic_relationships_locales
13501386
polymorphic_relationships_rels: typeof polymorphic_relationships_rels
13511387
tree: typeof tree
13521388
pages_menu: typeof pages_menu
@@ -1377,6 +1413,7 @@ type DatabaseSchema = {
13771413
relations_directors: typeof relations_directors
13781414
relations_movie_reviews_rels: typeof relations_movie_reviews_rels
13791415
relations_movie_reviews: typeof relations_movie_reviews
1416+
relations_polymorphic_relationships_locales: typeof relations_polymorphic_relationships_locales
13801417
relations_polymorphic_relationships_rels: typeof relations_polymorphic_relationships_rels
13811418
relations_polymorphic_relationships: typeof relations_polymorphic_relationships
13821419
relations_tree: typeof relations_tree

0 commit comments

Comments
 (0)