orchid-orm 1.61.2 → 1.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
- import { ColumnSchemaConfig, DefaultColumnTypes, DefaultSchemaConfig } from 'pqb';
1
+ import { ColumnSchemaConfig } from 'pqb';
2
2
  export * from 'rake-db';
3
3
 
4
4
  declare module 'rake-db' {
5
- interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> {
5
+ interface RakeDbConfig {
6
6
  dbPath?: string;
7
7
  dbExportedAs?: string;
8
8
  generateTableTo?(tableName: string): string;
9
9
  }
10
- interface InputRakeDbConfigBase<SchemaConfig extends ColumnSchemaConfig, CT> {
10
+ interface RakeDbCliConfigInputBase<SchemaConfig extends ColumnSchemaConfig, CT> {
11
11
  dbPath?: string;
12
12
  dbExportedAs?: string;
13
13
  generateTableTo?(tableName: string): string;
@@ -125,7 +125,8 @@ const checkForColumnAddOrDrop = (shape, key) => {
125
125
  const processSchemas = async (ast, dbStructure, {
126
126
  codeItems: { schemas },
127
127
  verifying,
128
- internal: { generatorIgnore }
128
+ internal: { generatorIgnore },
129
+ currentSchema
129
130
  }) => {
130
131
  const createSchemas = [];
131
132
  const dropSchemas = [];
@@ -135,7 +136,7 @@ const processSchemas = async (ast, dbStructure, {
135
136
  }
136
137
  }
137
138
  for (const schema of dbStructure.schemas) {
138
- if (!schemas.has(schema) && schema !== "public" && !generatorIgnore?.schemas?.includes(schema)) {
139
+ if (!schemas.has(schema) && schema !== "public" && schema !== currentSchema && !generatorIgnore?.schemas?.includes(schema)) {
139
140
  dropSchemas.push(schema);
140
141
  }
141
142
  }
@@ -196,12 +197,12 @@ const renameSchemaInStructures = (items, from, to) => {
196
197
  }
197
198
  };
198
199
 
199
- const processExtensions = (ast, dbStructure, {
200
+ const processExtensions = (config, ast, dbStructure, {
200
201
  currentSchema,
201
202
  internal: { extensions, generatorIgnore }
202
203
  }) => {
203
204
  const codeExtensions = extensions?.map((ext) => {
204
- const [schema, name] = rakeDb.getSchemaAndTableFromName(ext.name);
205
+ const [schema, name] = rakeDb.getSchemaAndTableFromName(config, ext.name);
205
206
  return { schema, name, version: ext.version };
206
207
  });
207
208
  for (const dbExt of dbStructure.extensions) {
@@ -399,6 +400,7 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
399
400
  dbColumnStructure
400
401
  );
401
402
  const action = await compareColumns(
403
+ config,
402
404
  adapter,
403
405
  domainsMap,
404
406
  ast,
@@ -436,13 +438,13 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
436
438
  }
437
439
  }
438
440
  };
439
- const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
441
+ const compareColumns = async (config, adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
440
442
  if (dbColumn instanceof pqb.ArrayColumn && codeColumn instanceof pqb.ArrayColumn) {
441
443
  dbColumn = dbColumn.data.item;
442
444
  codeColumn = codeColumn.data.item;
443
445
  }
444
- const dbType = getColumnDbType(dbColumn, currentSchema);
445
- const codeType = getColumnDbType(codeColumn, currentSchema);
446
+ const dbType = getColumnDbType(config, dbColumn, currentSchema);
447
+ const codeType = getColumnDbType(config, codeColumn, currentSchema);
446
448
  if (dbType !== codeType) {
447
449
  const typeCasts = await getTypeCasts(adapter, typeCastsCache);
448
450
  const dbBaseType = pqb.getColumnBaseType(dbColumn, domainsMap, dbType);
@@ -599,9 +601,10 @@ const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
599
601
  to: { column: simpleCodeColumn }
600
602
  };
601
603
  };
602
- const getColumnDbType = (column, currentSchema) => {
604
+ const getColumnDbType = (config, column, currentSchema) => {
603
605
  if (column instanceof pqb.EnumColumn) {
604
606
  const [schema = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
607
+ config,
605
608
  column.enumName
606
609
  );
607
610
  return `${schema}.${name}`;
@@ -629,7 +632,7 @@ const renameColumn = (columns, from, to) => {
629
632
  }
630
633
  };
631
634
 
632
- const processDomains = async (ast, adapter, domainsMap, dbStructure, {
635
+ const processDomains = async (config, ast, adapter, domainsMap, dbStructure, {
633
636
  codeItems: { domains },
634
637
  structureToAstCtx,
635
638
  currentSchema,
@@ -639,7 +642,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
639
642
  if (domains) {
640
643
  for (const { schemaName, name, column } of domains) {
641
644
  codeDomains.push(
642
- makeComparableDomain(currentSchema, schemaName, name, column)
645
+ makeComparableDomain(config, currentSchema, schemaName, name, column)
643
646
  );
644
647
  }
645
648
  }
@@ -675,6 +678,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
675
678
  }));
676
679
  }
677
680
  const dbDomain = makeComparableDomain(
681
+ config,
678
682
  currentSchema,
679
683
  domain.schemaName,
680
684
  domain.name,
@@ -691,6 +695,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
691
695
  pushCompareDefault(compare, domain, found);
692
696
  pushCompareChecks(compare, domain, found);
693
697
  const source = `(VALUES (NULL::${getColumnDbType(
698
+ config,
694
699
  dbColumn,
695
700
  currentSchema
696
701
  )})) t(value)`;
@@ -744,7 +749,7 @@ const processDomains = async (ast, adapter, domainsMap, dbStructure, {
744
749
  }
745
750
  }
746
751
  };
747
- const makeComparableDomain = (currentSchema, schemaName, name, column) => {
752
+ const makeComparableDomain = (config, currentSchema, schemaName, name, column) => {
748
753
  let arrayDims = 0;
749
754
  const isNullable = column.data.isNullable ?? false;
750
755
  let inner = column;
@@ -752,8 +757,11 @@ const makeComparableDomain = (currentSchema, schemaName, name, column) => {
752
757
  inner = inner.data.item;
753
758
  arrayDims++;
754
759
  }
755
- const fullType = getColumnDbType(inner, currentSchema);
756
- const [typeSchema = "pg_catalog", type] = rakeDb.getSchemaAndTableFromName(fullType);
760
+ const fullType = getColumnDbType(config, inner, currentSchema);
761
+ const [typeSchema = "pg_catalog", type] = rakeDb.getSchemaAndTableFromName(
762
+ config,
763
+ fullType
764
+ );
757
765
  return {
758
766
  schemaName,
759
767
  name,
@@ -1384,6 +1392,7 @@ const findMatchingItemWithoutSql = (dbItem, codeComparableItems, codeItems, skip
1384
1392
  const {
1385
1393
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
1386
1394
  columnKeys,
1395
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1387
1396
  includeKeys,
1388
1397
  ...codeItemWithoutKeys
1389
1398
  } = codeItem;
@@ -1658,7 +1667,7 @@ const fnOrTableToString = (fnOrTable) => {
1658
1667
  };
1659
1668
  const parseForeignKey = (config, codeConstraint, references, currentSchema) => {
1660
1669
  const { fnOrTable, columns, foreignColumns, options } = references;
1661
- const [schema, table] = rakeDb.getSchemaAndTableFromName(fnOrTable);
1670
+ const [schema, table] = rakeDb.getSchemaAndTableFromName(config, fnOrTable);
1662
1671
  return {
1663
1672
  references: {
1664
1673
  foreignSchema: schema ?? currentSchema,
@@ -1905,7 +1914,10 @@ const collectChangeAndDropTables = (config, tables, dbStructure, currentSchema,
1905
1914
  const dropTables = [];
1906
1915
  const tableShapes = {};
1907
1916
  const ignoreTables = generatorIgnore?.tables?.map((name) => {
1908
- const [schema = currentSchema, table] = rakeDb.getSchemaAndTableFromName(name);
1917
+ const [schema = currentSchema, table] = rakeDb.getSchemaAndTableFromName(
1918
+ config,
1919
+ name
1920
+ );
1909
1921
  return { schema, table };
1910
1922
  });
1911
1923
  const { schema: migrationsSchema = "public", table: migrationsTable } = rakeDb.getMigrationsSchemaAndTable(config);
@@ -2163,9 +2175,10 @@ const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2163
2175
  const { structureToAstCtx, currentSchema } = params;
2164
2176
  const domainsMap = rakeDb.makeDomainsMap(structureToAstCtx, dbStructure);
2165
2177
  await processSchemas(ast, dbStructure, params);
2166
- processExtensions(ast, dbStructure, params);
2178
+ processExtensions(config, ast, dbStructure, params);
2167
2179
  const pendingDbTypes = new PendingDbTypes();
2168
2180
  await processDomains(
2181
+ config,
2169
2182
  ast,
2170
2183
  adapter,
2171
2184
  domainsMap,
@@ -2308,7 +2321,7 @@ const report = (ast, config, currentSchema) => {
2308
2321
  const column = change.item;
2309
2322
  const { primaryKey, indexes, excludes, foreignKeys, checks } = column.data;
2310
2323
  inner.push(
2311
- `${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
2324
+ `${change.type === "add" ? green("+ add column") : red("- drop column")} ${key} ${column.data.alias ?? getColumnDbType(config, column, currentSchema)}${column.data.isNullable ? " nullable" : ""}${primaryKey ? " primary key" : ""}${foreignKeys ? ` references ${foreignKeys.map((fk) => {
2312
2325
  return `${fnOrTableToString(
2313
2326
  fk.fnOrTable
2314
2327
  )}(${fk.foreignColumns.join(", ")})`;
@@ -2320,14 +2333,18 @@ const report = (ast, config, currentSchema) => {
2320
2333
  const changes2 = [];
2321
2334
  inner.push(`${yellow("~ change column")} ${name}:`, changes2);
2322
2335
  changes2.push(`${yellow("from")}: `);
2323
- const fromCode = change.from.column.toCode(toCodeCtx, key);
2324
- for (const code2 of fromCode) {
2325
- pqb.addCode(changes2, code2);
2336
+ const fromCode = change.from.column?.toCode(toCodeCtx, key);
2337
+ if (fromCode) {
2338
+ for (const code2 of fromCode) {
2339
+ pqb.addCode(changes2, code2);
2340
+ }
2326
2341
  }
2327
2342
  changes2.push(` ${yellow("to")}: `);
2328
- const toCode = change.to.column.toCode(toCodeCtx, key);
2329
- for (const code2 of toCode) {
2330
- pqb.addCode(changes2, code2);
2343
+ const toCode = change.to.column?.toCode(toCodeCtx, key);
2344
+ if (toCode) {
2345
+ for (const code2 of toCode) {
2346
+ pqb.addCode(changes2, code2);
2347
+ }
2331
2348
  }
2332
2349
  } else if (change.type === "rename") {
2333
2350
  inner.push(
@@ -2365,6 +2382,7 @@ const report = (ast, config, currentSchema) => {
2365
2382
  for (const { references } of a.drop.constraints) {
2366
2383
  if (!references) continue;
2367
2384
  const [schema, name] = rakeDb.getSchemaAndTableFromName(
2385
+ config,
2368
2386
  references.fnOrTable
2369
2387
  );
2370
2388
  inner.push(
@@ -2555,10 +2573,11 @@ const generate = async (adapters, config, args, afterPull) => {
2555
2573
  afterPull
2556
2574
  );
2557
2575
  const [adapter] = adapters;
2558
- const currentSchema = config.schema ?? "public";
2576
+ const currentSchema = (typeof config.schema === "function" ? config.schema() : config.schema) ?? "public";
2559
2577
  const db = await getDbFromConfig(config, dbPath);
2560
2578
  const { columnTypes, internal } = db.$qb;
2561
2579
  const codeItems = await getActualItems(
2580
+ config,
2562
2581
  db,
2563
2582
  currentSchema,
2564
2583
  internal,
@@ -2630,7 +2649,7 @@ ${msg}`);
2630
2649
  }
2631
2650
  if (up) {
2632
2651
  for (const adapter2 of adapters) {
2633
- await rakeDb.migrateAndClose({ adapter: adapter2, config });
2652
+ await rakeDb.migrateAndClose(adapter2, config);
2634
2653
  }
2635
2654
  } else if (!afterPull) {
2636
2655
  await closeAdapters(adapters);
@@ -2670,7 +2689,7 @@ const migrateAndPullStructures = async (adapters, config, afterPull) => {
2670
2689
  };
2671
2690
  }
2672
2691
  for (const adapter of adapters) {
2673
- await rakeDb.migrate({ adapter, config });
2692
+ await rakeDb.migrate(adapter, config);
2674
2693
  }
2675
2694
  const dbStructures = await Promise.all(
2676
2695
  adapters.map((adapter) => rakeDb.introspectDbSchema(adapter))
@@ -2715,7 +2734,7 @@ const compareDbStructures = (a, b, i, path2) => {
2715
2734
  throw new Error(`${path2} in the db 0 does not match db ${i}`);
2716
2735
  }
2717
2736
  };
2718
- const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2737
+ const getActualItems = async (config, db, currentSchema, internal, columnTypes) => {
2719
2738
  const tableNames = /* @__PURE__ */ new Set();
2720
2739
  const habtmTables = /* @__PURE__ */ new Map();
2721
2740
  const codeItems = {
@@ -2758,6 +2777,7 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2758
2777
  delete table.shape[key2];
2759
2778
  } else if (column instanceof pqb.DomainColumn) {
2760
2779
  const [schemaName = currentSchema, name2] = rakeDb.getSchemaAndTableFromName(
2780
+ config,
2761
2781
  column.dataType
2762
2782
  );
2763
2783
  domains.set(column.dataType, {
@@ -2768,20 +2788,23 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2768
2788
  } else {
2769
2789
  const en = column.dataType === "enum" ? column : column instanceof pqb.ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
2770
2790
  if (en) {
2771
- processEnumColumn(en, currentSchema, codeItems);
2791
+ processEnumColumn(config, en, currentSchema, codeItems);
2772
2792
  }
2773
2793
  }
2774
2794
  }
2775
2795
  }
2776
2796
  if (internal.extensions) {
2777
2797
  for (const extension of internal.extensions) {
2778
- const [schema] = rakeDb.getSchemaAndTableFromName(extension.name);
2798
+ const [schema] = rakeDb.getSchemaAndTableFromName(config, extension.name);
2779
2799
  if (schema) codeItems.schemas.add(schema);
2780
2800
  }
2781
2801
  }
2782
2802
  if (internal.domains) {
2783
2803
  for (const key in internal.domains) {
2784
- const [schemaName = currentSchema, name] = rakeDb.getSchemaAndTableFromName(key);
2804
+ const [schemaName = currentSchema, name] = rakeDb.getSchemaAndTableFromName(
2805
+ config,
2806
+ key
2807
+ );
2785
2808
  const column = internal.domains[key](columnTypes);
2786
2809
  domains.set(key, {
2787
2810
  schemaName,
@@ -2796,9 +2819,9 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2796
2819
  }
2797
2820
  return codeItems;
2798
2821
  };
2799
- const processEnumColumn = (column, currentSchema, codeItems) => {
2822
+ const processEnumColumn = (config, column, currentSchema, codeItems) => {
2800
2823
  const { enumName, options } = column;
2801
- const [schema, name] = rakeDb.getSchemaAndTableFromName(enumName);
2824
+ const [schema, name] = rakeDb.getSchemaAndTableFromName(config, enumName);
2802
2825
  const enumSchema = schema ?? currentSchema;
2803
2826
  codeItems.enums.set(`${enumSchema}.${name}`, {
2804
2827
  schema: enumSchema,
@@ -3142,7 +3165,7 @@ const pull = async (adapters, config) => {
3142
3165
  const baseTablePath = config.baseTable.getFilePath();
3143
3166
  const baseTableExportedAs = config.baseTable.exportAs;
3144
3167
  const [adapter] = adapters;
3145
- const currentSchema = config.schema || "public";
3168
+ const currentSchema = (typeof config.schema === "function" ? config.schema() : config.schema) ?? "public";
3146
3169
  const ctx = rakeDb.makeStructureToAstCtx(config, currentSchema);
3147
3170
  const asts = await rakeDb.structureToAst(ctx, adapter, config);
3148
3171
  const { tableInfos, fkeys } = getTableInfosAndFKeys(asts, config);