orchid-orm 1.50.3 → 1.50.4

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
  };
@@ -2695,8 +2719,11 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2695
2719
  name: name2,
2696
2720
  column: column.data.as ?? new pqb.UnknownColumn(pqb.defaultSchemaConfig)
2697
2721
  });
2698
- } else if (column.dataType === "enum") {
2699
- processEnumColumn(column, currentSchema, codeItems);
2722
+ } else {
2723
+ const en = column.dataType === "enum" ? column : column instanceof pqb.ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
2724
+ if (en) {
2725
+ processEnumColumn(en, currentSchema, codeItems);
2726
+ }
2700
2727
  }
2701
2728
  }
2702
2729
  }
@@ -2812,7 +2839,7 @@ const getTableInfosAndFKeys = (asts, config) => {
2812
2839
  }
2813
2840
  return { tableInfos, fkeys };
2814
2841
  };
2815
- const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
2842
+ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs, currentSchema) => {
2816
2843
  const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
2817
2844
  const imports = {
2818
2845
  "orchid-orm": "Selectable, Insertable, Updatable",
@@ -2832,7 +2859,10 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
2832
2859
  const hasTableData = Boolean(
2833
2860
  ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
2834
2861
  );
2835
- const shapeCode = pqb.columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
2862
+ const shapeCode = pqb.columnsShapeToCode(
2863
+ { t: "t", table: ast.name, currentSchema },
2864
+ ast.shape
2865
+ );
2836
2866
  props.push(
2837
2867
  `columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
2838
2868
  hasTableData ? [shapeCode] : shapeCode,
@@ -2896,7 +2926,7 @@ function importsToCode(imports) {
2896
2926
  }
2897
2927
 
2898
2928
  const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
2899
- const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2929
+ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains, currentSchema) => {
2900
2930
  const content = await fs.readFile(dbPath, "utf-8");
2901
2931
  const statements = getTsStatements(content);
2902
2932
  const importName = getOrchidOrmImportName(statements);
@@ -2924,7 +2954,7 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2924
2954
  (ast) => `${orchidCore.quoteObjectKey(
2925
2955
  ast.schema ? `${ast.schema}.${ast.name}` : ast.name
2926
2956
  )}: (t) => ${ast.baseType.toCode(
2927
- { t: "t", table: ast.name },
2957
+ { t: "t", table: ast.name, currentSchema },
2928
2958
  ast.baseType.data.name ?? ""
2929
2959
  )},`
2930
2960
  ).join("\n ")}
@@ -3084,7 +3114,8 @@ const pull = async (options, config) => {
3084
3114
  fkeys,
3085
3115
  ast,
3086
3116
  baseTablePath,
3087
- baseTableExportedAs
3117
+ baseTableExportedAs,
3118
+ currentSchema
3088
3119
  );
3089
3120
  tables[table.key] = table;
3090
3121
  if (!firstTable) firstTable = table;
@@ -3118,7 +3149,8 @@ const pull = async (options, config) => {
3118
3149
  dbPath,
3119
3150
  tables,
3120
3151
  extensions,
3121
- domains
3152
+ domains,
3153
+ currentSchema
3122
3154
  );
3123
3155
  if (content) pendingFileWrites.push([dbPath, content]);
3124
3156
  if (firstTable) {