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