Skip to content

Commit a624345

Browse files
committed
feat(drizzle-kit): add --omitSuffixForSchema to control schema suffix in introspected outputs
* add CLI option --omitSuffixForSchema (defaults to 'public') * plumb config through validations and utils * apply to introspection generators: * update paramNameFor() to accept omitSuffixForSchema * update schemaToTypeScript() to accept and pass omitSuffixForSchema *propagate to FK/column helpers for PG and Gel * wire option into introspect for Postgres and Gel flows * default behavior remains unchanged for schema 'public'
1 parent 37d059f commit a624345

File tree

7 files changed

+63
-28
lines changed

7 files changed

+63
-28
lines changed

drizzle-kit/src/cli/commands/introspect.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const introspectPostgres = async (
6161
schemasFilter: string[],
6262
prefix: Prefix,
6363
entities: Entities,
64+
omitSuffixForSchema: string,
6465
) => {
6566
const { preparePostgresDB } = await import('../connections');
6667
const db = await preparePostgresDB(credentials);
@@ -108,7 +109,7 @@ export const introspectPostgres = async (
108109
);
109110

110111
const schema = { id: originUUID, prevId: '', ...res } as PgSchema;
111-
const ts = postgresSchemaToTypeScript(schema, casing);
112+
const ts = postgresSchemaToTypeScript(schema, casing, omitSuffixForSchema);
112113
const relationsTs = relationsToTypeScript(schema, casing);
113114
const { internal, ...schemaWithoutInternals } = schema;
114115

@@ -187,6 +188,7 @@ export const introspectGel = async (
187188
schemasFilter: string[],
188189
prefix: Prefix,
189190
entities: Entities,
191+
omitSuffixForSchema: string,
190192
) => {
191193
const { prepareGelDB } = await import('../connections');
192194
const db = await prepareGelDB(credentials);
@@ -234,7 +236,7 @@ export const introspectGel = async (
234236
);
235237

236238
const schema = { id: originUUID, prevId: '', ...res } as GelSchema;
237-
const ts = gelSchemaToTypeScript(schema, casing);
239+
const ts = gelSchemaToTypeScript(schema, casing, omitSuffixForSchema);
238240
const relationsTs = relationsToTypeScript(schema, casing);
239241
const { internal, ...schemaWithoutInternals } = schema;
240242

drizzle-kit/src/cli/commands/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ export const preparePullConfig = async (
461461
| {
462462
dialect: 'postgresql';
463463
credentials: PostgresCredentials;
464+
omitSuffixForSchema: string;
464465
}
465466
| {
466467
dialect: 'sqlite';
@@ -477,6 +478,7 @@ export const preparePullConfig = async (
477478
| {
478479
dialect: 'gel';
479480
credentials?: GelCredentials;
481+
omitSuffixForSchema: string;
480482
}
481483
) & {
482484
out: string;

drizzle-kit/src/cli/schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const optionDriver = string()
4747

4848
const optionCasing = string().enum('camelCase', 'snake_case').desc('Casing for serialization');
4949

50+
const optionOmitSuffixForSchema = string().desc(
51+
`Schema name to treat as suffixless; when schema equals this value, the In<Schema> suffix is not appended. Defaults to 'public'.`
52+
);
53+
5054
export const generate = command({
5155
name: 'generate',
5256
options: {
@@ -476,6 +480,7 @@ export const pull = command({
476480
out: optionOut,
477481
breakpoints: optionBreakpoints,
478482
casing: string('introspect-casing').enum('camel', 'preserve'),
483+
omitSuffixForSchema: optionOmitSuffixForSchema,
479484
...optionsFilters,
480485
...optionsDatabaseCredentials,
481486
},
@@ -502,6 +507,7 @@ export const pull = command({
502507
'schemaFilters',
503508
'extensionsFilters',
504509
'tlsSecurity',
510+
'omitSuffixForSchema',
505511
],
506512
);
507513
return preparePullConfig(opts, from);
@@ -567,6 +573,7 @@ export const pull = command({
567573
schemasFilter,
568574
prefix,
569575
entities,
576+
config.omitSuffixForSchema
570577
);
571578
} else if (dialect === 'mysql') {
572579
const { introspectMysql } = await import('./commands/introspect');
@@ -619,6 +626,7 @@ export const pull = command({
619626
schemasFilter,
620627
prefix,
621628
entities,
629+
config.omitSuffixForSchema
622630
);
623631
} else {
624632
assertUnreachable(dialect);

drizzle-kit/src/cli/validations/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const introspectParams = object({
128128
introspect: object({
129129
casing,
130130
}).default({ casing: 'camel' }),
131+
omitSuffixForSchema: string().optional().default('public'),
131132
});
132133

133134
export type IntrospectParams = TypeOf<typeof introspectParams>;
@@ -142,6 +143,7 @@ export const configIntrospectCliSchema = object({
142143
introspectCasing: union([literal('camel'), literal('preserve')]).default(
143144
'camel',
144145
),
146+
omitSuffixForSchema: string().optional().default('public')
145147
});
146148

147149
export const configGenerateSchema = object({

drizzle-kit/src/introspect-gel.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ function generateIdentityParams(identity: Column['identity']) {
203203
return `.generatedByDefaultAsIdentity(${paramsObj})`;
204204
}
205205

206-
export const paramNameFor = (name: string, schema?: string) => {
207-
const schemaSuffix = schema && schema !== 'public' ? `In${schema.capitalise()}` : '';
206+
export const paramNameFor = (name: string, schema?: string, omitSuffixForSchema = 'public') => {
207+
const schemaSuffix = schema && schema !== omitSuffixForSchema ? `In${schema.capitalise()}` : '';
208208
return `${name}${schemaSuffix}`;
209209
};
210210

211-
export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) => {
211+
export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing, omitSuffixForSchema: string) => {
212212
// collectFKs
213213
Object.values(schema.tables).forEach((table) => {
214214
Object.values(table.foreignKeys).forEach((fk) => {
@@ -314,7 +314,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
314314
// .map((it) => {
315315
// const enumSchema = schemas[it.schema];
316316
// // const func = schema || schema === "public" ? "gelTable" : schema;
317-
// const paramName = paramNameFor(it.name, enumSchema);
317+
// const paramName = paramNameFor(it.name, enumSchema, omitSuffixForSchema);
318318

319319
// const func = enumSchema ? `${enumSchema}.enum` : 'gelEnum';
320320

@@ -329,7 +329,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
329329
// const sequencesStatements = Object.values(schema.sequences)
330330
// .map((it) => {
331331
// const seqSchema = schemas[it.schema];
332-
// const paramName = paramNameFor(it.name, seqSchema);
332+
// const paramName = paramNameFor(it.name, seqSchema, omitSuffixForSchema);
333333

334334
// const func = seqSchema ? `${seqSchema}.sequence` : 'gelSequence';
335335

@@ -390,7 +390,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
390390

391391
const tableStatements = Object.values(schema.tables).map((table) => {
392392
const tableSchema = schemas[table.schema];
393-
const paramName = paramNameFor(table.name, tableSchema);
393+
const paramName = paramNameFor(table.name, tableSchema, omitSuffixForSchema);
394394

395395
const func = tableSchema ? `${tableSchema}.table` : 'gelTable';
396396
let statement = `export const ${withCasing(paramName, casing)} = ${func}("${table.name}", {\n`;
@@ -403,6 +403,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
403403
schemas,
404404
casing,
405405
schema.internal,
406+
omitSuffixForSchema,
406407
);
407408
statement += '}';
408409

@@ -423,7 +424,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
423424
statement += ', ';
424425
statement += '(table) => [';
425426
statement += createTableIndexes(table.name, Object.values(table.indexes), casing);
426-
statement += createTableFKs(Object.values(table.foreignKeys), schemas, casing);
427+
statement += createTableFKs(Object.values(table.foreignKeys), schemas, casing, omitSuffixForSchema);
427428
statement += createTablePKs(
428429
Object.values(table.compositePrimaryKeys),
429430
casing,
@@ -452,7 +453,7 @@ export const schemaToTypeScript = (schema: GelSchemaInternal, casing: Casing) =>
452453
// .map((it) => {
453454
// const viewSchema = schemas[it.schema];
454455

455-
// const paramName = paramNameFor(it.name, viewSchema);
456+
// const paramName = paramNameFor(it.name, viewSchema, omitSuffixForSchema);
456457

457458
// const func = viewSchema
458459
// ? (it.materialized ? `${viewSchema}.materializedView` : `${viewSchema}.view`)
@@ -704,12 +705,13 @@ const column = (
704705
casing: Casing,
705706
defaultValue?: any,
706707
internals?: GelKitInternals,
708+
omitSuffixForSchema?: string,
707709
) => {
708710
const isExpression = internals?.tables[tableName]?.columns[name]?.isDefaultAnExpression ?? false;
709711
const lowered = type.toLowerCase().replace('[]', '');
710712

711713
if (enumTypes.has(`${typeSchema}.${type.replace('[]', '')}`)) {
712-
let out = `${withCasing(name, casing)}: ${withCasing(paramNameFor(type.replace('[]', ''), typeSchema), casing)}(${
714+
let out = `${withCasing(name, casing)}: ${withCasing(paramNameFor(type.replace('[]', ''), typeSchema, omitSuffixForSchema), casing)}(${
713715
dbColumnName({ name, casing })
714716
})`;
715717
return out;
@@ -839,6 +841,7 @@ const createTableColumns = (
839841
schemas: Record<string, string>,
840842
casing: Casing,
841843
internals: GelKitInternals,
844+
omitSuffixForSchema?: string,
842845
): string => {
843846
let statement = '';
844847

@@ -866,6 +869,7 @@ const createTableColumns = (
866869
casing,
867870
it.default,
868871
internals,
872+
omitSuffixForSchema,
869873
);
870874
statement += '\t';
871875
statement += columnStatement;
@@ -882,7 +886,7 @@ const createTableColumns = (
882886
statement += it.generated ? `.generatedAlwaysAs(sql\`${it.generated.as}\`)` : '';
883887

884888
// const fks = fkByColumnName[it.name];
885-
// Andrii: I switched it off until we will get a custom naem setting in references
889+
// Andrii: I switched it off until we will get a custom name setting in references
886890
// if (fks) {
887891
// const fksStatement = fks
888892
// .map((it) => {
@@ -894,7 +898,7 @@ const createTableColumns = (
894898

895899
// const paramsStr = objToStatement2(params);
896900
// const tableSchema = schemas[it.schemaTo || ''];
897-
// const paramName = paramNameFor(it.tableTo, tableSchema);
901+
// const paramName = paramNameFor(it.tableTo, tableSchema, omitSuffixForSchema);
898902
// if (paramsStr) {
899903
// return `.references(()${typeSuffix} => ${
900904
// withCasing(
@@ -1062,12 +1066,17 @@ const createTableChecks = (
10621066
return statement;
10631067
};
10641068

1065-
const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casing: Casing): string => {
1069+
const createTableFKs = (
1070+
fks: ForeignKey[],
1071+
schemas: Record<string, string>,
1072+
casing: Casing,
1073+
omitSuffixForSchema?: string,
1074+
): string => {
10661075
let statement = '';
10671076

10681077
fks.forEach((it) => {
10691078
const tableSchema = schemas[it.schemaTo || ''];
1070-
const paramName = paramNameFor(it.tableTo, tableSchema);
1079+
const paramName = paramNameFor(it.tableTo, tableSchema, omitSuffixForSchema);
10711080

10721081
const isSelf = it.tableTo === it.tableFrom;
10731082
const tableTo = isSelf ? 'table' : `${withCasing(paramName, casing)}`;

drizzle-kit/src/introspect-pg.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ function generateIdentityParams(identity: Column['identity']) {
301301
return `.generatedByDefaultAsIdentity(${paramsObj})`;
302302
}
303303

304-
export const paramNameFor = (name: string, schema?: string) => {
305-
const schemaSuffix = schema && schema !== 'public' ? `In${schema.capitalise()}` : '';
304+
export const paramNameFor = (name: string, schema?: string, omitSuffixForSchema = 'public') => {
305+
const schemaSuffix = schema && schema !== omitSuffixForSchema ? `In${schema.capitalise()}` : '';
306306
return `${name}${schemaSuffix}`;
307307
};
308308

309-
export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) => {
309+
export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing, omitSuffixForSchema: string) => {
310310
// collectFKs
311311
Object.values(schema.tables).forEach((table) => {
312312
Object.values(table.foreignKeys).forEach((fk) => {
@@ -431,7 +431,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
431431
.map((it) => {
432432
const enumSchema = schemas[it.schema];
433433
// const func = schema || schema === "public" ? "pgTable" : schema;
434-
const paramName = paramNameFor(it.name, enumSchema);
434+
const paramName = paramNameFor(it.name, enumSchema, omitSuffixForSchema);
435435

436436
const func = enumSchema ? `${enumSchema}.enum` : 'pgEnum';
437437

@@ -446,7 +446,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
446446
const sequencesStatements = Object.values(schema.sequences)
447447
.map((it) => {
448448
const seqSchema = schemas[it.schema];
449-
const paramName = paramNameFor(it.name, seqSchema);
449+
const paramName = paramNameFor(it.name, seqSchema, omitSuffixForSchema);
450450

451451
const func = seqSchema ? `${seqSchema}.sequence` : 'pgSequence';
452452

@@ -507,7 +507,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
507507

508508
const tableStatements = Object.values(schema.tables).map((table) => {
509509
const tableSchema = schemas[table.schema];
510-
const paramName = paramNameFor(table.name, tableSchema);
510+
const paramName = paramNameFor(table.name, tableSchema, omitSuffixForSchema);
511511

512512
const func = tableSchema ? `${tableSchema}.table` : 'pgTable';
513513
let statement = `export const ${withCasing(paramName, casing)} = ${func}("${table.name}", {\n`;
@@ -519,6 +519,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
519519
schemas,
520520
casing,
521521
schema.internal,
522+
omitSuffixForSchema,
522523
);
523524
statement += '}';
524525

@@ -568,7 +569,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
568569
.map((it) => {
569570
const viewSchema = schemas[it.schema];
570571

571-
const paramName = paramNameFor(it.name, viewSchema);
572+
const paramName = paramNameFor(it.name, viewSchema, omitSuffixForSchema);
572573

573574
const func = viewSchema
574575
? (it.materialized ? `${viewSchema}.materializedView` : `${viewSchema}.view`)
@@ -590,6 +591,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
590591
schemas,
591592
casing,
592593
schema.internal,
594+
omitSuffixForSchema,
593595
);
594596

595597
let statement = `export const ${withCasing(paramName, casing)} = ${func}("${it.name}", {${columns}})`;
@@ -844,12 +846,13 @@ const column = (
844846
casing: Casing,
845847
defaultValue?: any,
846848
internals?: PgKitInternals,
849+
omitSuffixForSchema?: string,
847850
) => {
848851
const isExpression = internals?.tables[tableName]?.columns[name]?.isDefaultAnExpression ?? false;
849852
const lowered = type.toLowerCase().replace('[]', '');
850853

851854
if (enumTypes.has(`${typeSchema}.${type.replace('[]', '')}`)) {
852-
let out = `${withCasing(name, casing)}: ${withCasing(paramNameFor(type.replace('[]', ''), typeSchema), casing)}(${
855+
let out = `${withCasing(name, casing)}: ${withCasing(paramNameFor(type.replace('[]', ''), typeSchema, omitSuffixForSchema), casing)}(${
853856
dbColumnName({ name, casing })
854857
})`;
855858
return out;
@@ -1117,6 +1120,7 @@ const createTableColumns = (
11171120
schemas: Record<string, string>,
11181121
casing: Casing,
11191122
internals: PgKitInternals,
1123+
omitSuffixForSchema?: string,
11201124
): string => {
11211125
let statement = '';
11221126

@@ -1144,6 +1148,7 @@ const createTableColumns = (
11441148
casing,
11451149
it.default,
11461150
internals,
1151+
omitSuffixForSchema,
11471152
);
11481153
statement += '\t';
11491154
statement += columnStatement;
@@ -1172,7 +1177,7 @@ const createTableColumns = (
11721177

11731178
// const paramsStr = objToStatement2(params);
11741179
// const tableSchema = schemas[it.schemaTo || ''];
1175-
// const paramName = paramNameFor(it.tableTo, tableSchema);
1180+
// const paramName = paramNameFor(it.tableTo, tableSchema, omitSuffixForSchema);
11761181
// if (paramsStr) {
11771182
// return `.references(()${typeSuffix} => ${
11781183
// withCasing(
@@ -1340,12 +1345,17 @@ const createTableChecks = (
13401345
return statement;
13411346
};
13421347

1343-
const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casing: Casing): string => {
1348+
const createTableFKs = (
1349+
fks: ForeignKey[],
1350+
schemas: Record<string, string>,
1351+
casing: Casing,
1352+
omitSuffixForSchema?: string
1353+
): string => {
13441354
let statement = '';
13451355

13461356
fks.forEach((it) => {
13471357
const tableSchema = schemas[it.schemaTo || ''];
1348-
const paramName = paramNameFor(it.tableTo, tableSchema);
1358+
const paramName = paramNameFor(it.tableTo, tableSchema, omitSuffixForSchema);
13491359

13501360
const isSelf = it.tableTo === it.tableFrom;
13511361
const tableTo = isSelf ? 'table' : `${withCasing(paramName, casing)}`;

0 commit comments

Comments
 (0)