orchid-orm 1.50.3 → 1.50.5

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.
@@ -443,43 +443,14 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
443
443
  }
444
444
  };
445
445
  const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
446
- const dbType = getColumnDbType(dbColumn, currentSchema);
447
- const codeType = getColumnDbType(codeColumn, currentSchema);
448
446
  if (dbColumn instanceof pqb.ArrayColumn && codeColumn instanceof pqb.ArrayColumn) {
449
447
  dbColumn = dbColumn.data.item;
450
448
  codeColumn = codeColumn.data.item;
451
449
  }
450
+ const dbType = getColumnDbType(dbColumn, currentSchema);
451
+ const codeType = getColumnDbType(codeColumn, currentSchema);
452
452
  if (dbType !== codeType) {
453
- let typeCasts = typeCastsCache.value;
454
- if (!typeCasts) {
455
- const { rows } = await adapter.arrays(`SELECT s.typname, t.typname
456
- FROM pg_cast
457
- JOIN pg_type AS s ON s.oid = castsource
458
- JOIN pg_type AS t ON t.oid = casttarget`);
459
- const directTypeCasts = /* @__PURE__ */ new Map();
460
- for (const [source, target] of rows) {
461
- const set = directTypeCasts.get(source);
462
- if (set) {
463
- set.add(target);
464
- } else {
465
- directTypeCasts.set(source, /* @__PURE__ */ new Set([target]));
466
- }
467
- }
468
- typeCasts = /* @__PURE__ */ new Map();
469
- for (const [type, directSet] of directTypeCasts.entries()) {
470
- const set = new Set(directSet);
471
- typeCasts.set(type, set);
472
- for (const subtype of directSet) {
473
- const subset = directTypeCasts.get(subtype);
474
- if (subset) {
475
- for (const type2 of subset) {
476
- set.add(type2);
477
- }
478
- }
479
- }
480
- }
481
- typeCastsCache.value = typeCasts;
482
- }
453
+ const typeCasts = await getTypeCasts(adapter, typeCastsCache);
483
454
  const dbBaseType = pqb.getColumnBaseType(dbColumn, domainsMap, dbType);
484
455
  const codeBaseType = pqb.getColumnBaseType(codeColumn, domainsMap, codeType);
485
456
  if (!typeCasts.get(dbBaseType)?.has(codeBaseType)) {
@@ -525,6 +496,15 @@ JOIN pg_type AS t ON t.oid = casttarget`);
525
496
  return "change";
526
497
  }
527
498
  }
499
+ if (dbColumn.data.isOfCustomType) {
500
+ const { typmod } = dbColumn.data;
501
+ if (typmod !== void 0 && typmod !== -1) {
502
+ const i = codeColumn.dataType.indexOf("(");
503
+ if (i === -1 || codeColumn.dataType.slice(i + 1, -1) !== `${typmod}`) {
504
+ return "change";
505
+ }
506
+ }
507
+ }
528
508
  if (!orchidCore.deepCompare(
529
509
  dbData.identity,
530
510
  codeData.identity ? {
@@ -571,6 +551,39 @@ JOIN pg_type AS t ON t.oid = casttarget`);
571
551
  }
572
552
  return;
573
553
  };
554
+ const getTypeCasts = async (adapter, typeCastsCache) => {
555
+ let typeCasts = typeCastsCache.value;
556
+ if (!typeCasts) {
557
+ const { rows } = await adapter.arrays(`SELECT s.typname, t.typname
558
+ FROM pg_cast
559
+ JOIN pg_type AS s ON s.oid = castsource
560
+ JOIN pg_type AS t ON t.oid = casttarget`);
561
+ const directTypeCasts = /* @__PURE__ */ new Map();
562
+ for (const [source, target] of rows) {
563
+ const set = directTypeCasts.get(source);
564
+ if (set) {
565
+ set.add(target);
566
+ } else {
567
+ directTypeCasts.set(source, /* @__PURE__ */ new Set([target]));
568
+ }
569
+ }
570
+ typeCasts = /* @__PURE__ */ new Map();
571
+ for (const [type, directSet] of directTypeCasts.entries()) {
572
+ const set = new Set(directSet);
573
+ typeCasts.set(type, set);
574
+ for (const subtype of directSet) {
575
+ const subset = directTypeCasts.get(subtype);
576
+ if (subset) {
577
+ for (const type2 of subset) {
578
+ set.add(type2);
579
+ }
580
+ }
581
+ }
582
+ }
583
+ typeCastsCache.value = typeCasts;
584
+ }
585
+ return typeCasts;
586
+ };
574
587
  const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
575
588
  dbColumn.data.as = codeColumn.data.as = void 0;
576
589
  const simpleCodeColumn = Object.create(codeColumn);
@@ -599,7 +612,17 @@ const getColumnDbType = (column, currentSchema) => {
599
612
  );
600
613
  return column.enumName = `${schema}.${name}`;
601
614
  } else if (column instanceof pqb.ArrayColumn) {
602
- return column.data.item.dataType + "[]".repeat(column.data.arrayDims);
615
+ const { item } = column.data;
616
+ let type = item instanceof pqb.EnumColumn ? item.enumName : item.dataType;
617
+ type = type.startsWith(currentSchema + ".") ? type.slice(currentSchema.length + 1) : type;
618
+ return type + "[]".repeat(column.data.arrayDims);
619
+ } else if (column.data.isOfCustomType) {
620
+ let type = column.dataType;
621
+ const i = type.indexOf("(");
622
+ if (i !== -1) {
623
+ type = type.slice(0, i);
624
+ }
625
+ return type.includes(".") ? type : currentSchema + "." + type;
603
626
  } else {
604
627
  return column.dataType;
605
628
  }
@@ -2235,6 +2258,7 @@ const report = (ast, config, currentSchema) => {
2235
2258
  const toCodeCtx = {
2236
2259
  t: "t",
2237
2260
  table: a.name,
2261
+ currentSchema,
2238
2262
  migration: true,
2239
2263
  snakeCase: config.snakeCase
2240
2264
  };
@@ -2252,7 +2276,8 @@ const report = (ast, config, currentSchema) => {
2252
2276
  }).join(", ")}` : ""}${indexes?.length ? indexes.length === 1 ? ", has index" : `, has ${indexes.length} indexes` : ""}${excludes?.length ? excludes.length === 1 ? ", has exclude" : `, has ${excludes.length} excludes` : ""}${checks?.length ? `, checks ${checks.map((check) => check.sql.toSQL({ values: [] })).join(", ")}` : ""}`
2253
2277
  );
2254
2278
  } else if (change.type === "change") {
2255
- const name = change.from.column?.data.name ?? key;
2279
+ let name = change.from.column?.data.name ?? key;
2280
+ if (config.snakeCase) name = orchidCore.toCamelCase(name);
2256
2281
  const changes2 = [];
2257
2282
  inner.push(`${yellow("~ change column")} ${name}:`, changes2);
2258
2283
  changes2.push(`${yellow("from")}: `);
@@ -2267,7 +2292,7 @@ const report = (ast, config, currentSchema) => {
2267
2292
  }
2268
2293
  } else if (change.type === "rename") {
2269
2294
  inner.push(
2270
- `${yellow("~ rename column")} ${key} ${yellow("=>")} ${change.name}`
2295
+ `${yellow("~ rename column")} ${config.snakeCase ? orchidCore.toCamelCase(key) : key} ${yellow("=>")} ${change.name}`
2271
2296
  );
2272
2297
  } else {
2273
2298
  rakeDb.exhaustive(change.type);
@@ -2695,8 +2720,11 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2695
2720
  name: name2,
2696
2721
  column: column.data.as ?? new pqb.UnknownColumn(pqb.defaultSchemaConfig)
2697
2722
  });
2698
- } else if (column.dataType === "enum") {
2699
- processEnumColumn(column, currentSchema, codeItems);
2723
+ } else {
2724
+ const en = column.dataType === "enum" ? column : column instanceof pqb.ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
2725
+ if (en) {
2726
+ processEnumColumn(en, currentSchema, codeItems);
2727
+ }
2700
2728
  }
2701
2729
  }
2702
2730
  }
@@ -2812,7 +2840,7 @@ const getTableInfosAndFKeys = (asts, config) => {
2812
2840
  }
2813
2841
  return { tableInfos, fkeys };
2814
2842
  };
2815
- const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
2843
+ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs, currentSchema) => {
2816
2844
  const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
2817
2845
  const imports = {
2818
2846
  "orchid-orm": "Selectable, Insertable, Updatable",
@@ -2832,7 +2860,10 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
2832
2860
  const hasTableData = Boolean(
2833
2861
  ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
2834
2862
  );
2835
- const shapeCode = pqb.columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
2863
+ const shapeCode = pqb.columnsShapeToCode(
2864
+ { t: "t", table: ast.name, currentSchema },
2865
+ ast.shape
2866
+ );
2836
2867
  props.push(
2837
2868
  `columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
2838
2869
  hasTableData ? [shapeCode] : shapeCode,
@@ -2896,7 +2927,7 @@ function importsToCode(imports) {
2896
2927
  }
2897
2928
 
2898
2929
  const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
2899
- const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2930
+ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains, currentSchema) => {
2900
2931
  const content = await fs.readFile(dbPath, "utf-8");
2901
2932
  const statements = getTsStatements(content);
2902
2933
  const importName = getOrchidOrmImportName(statements);
@@ -2914,7 +2945,7 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2914
2945
  if (extensions.length) {
2915
2946
  code += `
2916
2947
  extensions: [${extensions.map(
2917
- (ext) => ext.version ? `{ ${orchidCore.quoteObjectKey(ext.name)}: '${ext.version}' }` : orchidCore.singleQuote(ext.name)
2948
+ (ext) => ext.version ? `{ ${orchidCore.quoteObjectKey(ext.name, false)}: '${ext.version}' }` : orchidCore.singleQuote(ext.name)
2918
2949
  ).join(", ")}],`;
2919
2950
  }
2920
2951
  if (domains.length) {
@@ -2922,9 +2953,10 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2922
2953
  domains: {
2923
2954
  ${domains.sort((a, b) => a.name > b.name ? 1 : -1).map(
2924
2955
  (ast) => `${orchidCore.quoteObjectKey(
2925
- ast.schema ? `${ast.schema}.${ast.name}` : ast.name
2956
+ ast.schema ? `${ast.schema}.${ast.name}` : ast.name,
2957
+ false
2926
2958
  )}: (t) => ${ast.baseType.toCode(
2927
- { t: "t", table: ast.name },
2959
+ { t: "t", table: ast.name, currentSchema },
2928
2960
  ast.baseType.data.name ?? ""
2929
2961
  )},`
2930
2962
  ).join("\n ")}
@@ -3084,7 +3116,8 @@ const pull = async (options, config) => {
3084
3116
  fkeys,
3085
3117
  ast,
3086
3118
  baseTablePath,
3087
- baseTableExportedAs
3119
+ baseTableExportedAs,
3120
+ currentSchema
3088
3121
  );
3089
3122
  tables[table.key] = table;
3090
3123
  if (!firstTable) firstTable = table;
@@ -3118,7 +3151,8 @@ const pull = async (options, config) => {
3118
3151
  dbPath,
3119
3152
  tables,
3120
3153
  extensions,
3121
- domains
3154
+ domains,
3155
+ currentSchema
3122
3156
  );
3123
3157
  if (content) pendingFileWrites.push([dbPath, content]);
3124
3158
  if (firstTable) {