orchid-orm 1.50.2 → 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.
@@ -442,43 +442,14 @@ const changeColumns = async (adapter, config, structureToAstCtx, dbStructure, do
442
442
  }
443
443
  };
444
444
  const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSql, changeTableData, typeCastsCache, verifying, key, dbName, dbColumn, codeColumn) => {
445
- const dbType = getColumnDbType(dbColumn, currentSchema);
446
- const codeType = getColumnDbType(codeColumn, currentSchema);
447
445
  if (dbColumn instanceof ArrayColumn && codeColumn instanceof ArrayColumn) {
448
446
  dbColumn = dbColumn.data.item;
449
447
  codeColumn = codeColumn.data.item;
450
448
  }
449
+ const dbType = getColumnDbType(dbColumn, currentSchema);
450
+ const codeType = getColumnDbType(codeColumn, currentSchema);
451
451
  if (dbType !== codeType) {
452
- let typeCasts = typeCastsCache.value;
453
- if (!typeCasts) {
454
- const { rows } = await adapter.arrays(`SELECT s.typname, t.typname
455
- FROM pg_cast
456
- JOIN pg_type AS s ON s.oid = castsource
457
- JOIN pg_type AS t ON t.oid = casttarget`);
458
- const directTypeCasts = /* @__PURE__ */ new Map();
459
- for (const [source, target] of rows) {
460
- const set = directTypeCasts.get(source);
461
- if (set) {
462
- set.add(target);
463
- } else {
464
- directTypeCasts.set(source, /* @__PURE__ */ new Set([target]));
465
- }
466
- }
467
- typeCasts = /* @__PURE__ */ new Map();
468
- for (const [type, directSet] of directTypeCasts.entries()) {
469
- const set = new Set(directSet);
470
- typeCasts.set(type, set);
471
- for (const subtype of directSet) {
472
- const subset = directTypeCasts.get(subtype);
473
- if (subset) {
474
- for (const type2 of subset) {
475
- set.add(type2);
476
- }
477
- }
478
- }
479
- }
480
- typeCastsCache.value = typeCasts;
481
- }
452
+ const typeCasts = await getTypeCasts(adapter, typeCastsCache);
482
453
  const dbBaseType = getColumnBaseType(dbColumn, domainsMap, dbType);
483
454
  const codeBaseType = getColumnBaseType(codeColumn, domainsMap, codeType);
484
455
  if (!typeCasts.get(dbBaseType)?.has(codeBaseType)) {
@@ -524,6 +495,15 @@ JOIN pg_type AS t ON t.oid = casttarget`);
524
495
  return "change";
525
496
  }
526
497
  }
498
+ if (dbColumn.data.isOfCustomType) {
499
+ const { typmod } = dbColumn.data;
500
+ if (typmod !== void 0 && typmod !== -1) {
501
+ const i = codeColumn.dataType.indexOf("(");
502
+ if (i === -1 || codeColumn.dataType.slice(i + 1, -1) !== `${typmod}`) {
503
+ return "change";
504
+ }
505
+ }
506
+ }
527
507
  if (!deepCompare(
528
508
  dbData.identity,
529
509
  codeData.identity ? {
@@ -570,6 +550,39 @@ JOIN pg_type AS t ON t.oid = casttarget`);
570
550
  }
571
551
  return;
572
552
  };
553
+ const getTypeCasts = async (adapter, typeCastsCache) => {
554
+ let typeCasts = typeCastsCache.value;
555
+ if (!typeCasts) {
556
+ const { rows } = await adapter.arrays(`SELECT s.typname, t.typname
557
+ FROM pg_cast
558
+ JOIN pg_type AS s ON s.oid = castsource
559
+ JOIN pg_type AS t ON t.oid = casttarget`);
560
+ const directTypeCasts = /* @__PURE__ */ new Map();
561
+ for (const [source, target] of rows) {
562
+ const set = directTypeCasts.get(source);
563
+ if (set) {
564
+ set.add(target);
565
+ } else {
566
+ directTypeCasts.set(source, /* @__PURE__ */ new Set([target]));
567
+ }
568
+ }
569
+ typeCasts = /* @__PURE__ */ new Map();
570
+ for (const [type, directSet] of directTypeCasts.entries()) {
571
+ const set = new Set(directSet);
572
+ typeCasts.set(type, set);
573
+ for (const subtype of directSet) {
574
+ const subset = directTypeCasts.get(subtype);
575
+ if (subset) {
576
+ for (const type2 of subset) {
577
+ set.add(type2);
578
+ }
579
+ }
580
+ }
581
+ }
582
+ typeCastsCache.value = typeCasts;
583
+ }
584
+ return typeCasts;
585
+ };
573
586
  const changeColumn = (changeTableData, key, dbName, dbColumn, codeColumn) => {
574
587
  dbColumn.data.as = codeColumn.data.as = void 0;
575
588
  const simpleCodeColumn = Object.create(codeColumn);
@@ -598,7 +611,17 @@ const getColumnDbType = (column, currentSchema) => {
598
611
  );
599
612
  return column.enumName = `${schema}.${name}`;
600
613
  } else if (column instanceof ArrayColumn) {
601
- return column.data.item.dataType + "[]".repeat(column.data.arrayDims);
614
+ const { item } = column.data;
615
+ let type = item instanceof EnumColumn ? item.enumName : item.dataType;
616
+ type = type.startsWith(currentSchema + ".") ? type.slice(currentSchema.length + 1) : type;
617
+ return type + "[]".repeat(column.data.arrayDims);
618
+ } else if (column.data.isOfCustomType) {
619
+ let type = column.dataType;
620
+ const i = type.indexOf("(");
621
+ if (i !== -1) {
622
+ type = type.slice(0, i);
623
+ }
624
+ return type.includes(".") ? type : currentSchema + "." + type;
602
625
  } else {
603
626
  return column.dataType;
604
627
  }
@@ -2234,6 +2257,7 @@ const report = (ast, config, currentSchema) => {
2234
2257
  const toCodeCtx = {
2235
2258
  t: "t",
2236
2259
  table: a.name,
2260
+ currentSchema,
2237
2261
  migration: true,
2238
2262
  snakeCase: config.snakeCase
2239
2263
  };
@@ -2694,8 +2718,11 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2694
2718
  name: name2,
2695
2719
  column: column.data.as ?? new UnknownColumn(defaultSchemaConfig)
2696
2720
  });
2697
- } else if (column.dataType === "enum") {
2698
- processEnumColumn(column, currentSchema, codeItems);
2721
+ } else {
2722
+ const en = column.dataType === "enum" ? column : column instanceof ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
2723
+ if (en) {
2724
+ processEnumColumn(en, currentSchema, codeItems);
2725
+ }
2699
2726
  }
2700
2727
  }
2701
2728
  }
@@ -2811,7 +2838,7 @@ const getTableInfosAndFKeys = (asts, config) => {
2811
2838
  }
2812
2839
  return { tableInfos, fkeys };
2813
2840
  };
2814
- const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
2841
+ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs, currentSchema) => {
2815
2842
  const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
2816
2843
  const imports = {
2817
2844
  "orchid-orm": "Selectable, Insertable, Updatable",
@@ -2831,7 +2858,10 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
2831
2858
  const hasTableData = Boolean(
2832
2859
  ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
2833
2860
  );
2834
- const shapeCode = columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
2861
+ const shapeCode = columnsShapeToCode(
2862
+ { t: "t", table: ast.name, currentSchema },
2863
+ ast.shape
2864
+ );
2835
2865
  props.push(
2836
2866
  `columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
2837
2867
  hasTableData ? [shapeCode] : shapeCode,
@@ -2895,7 +2925,7 @@ function importsToCode(imports) {
2895
2925
  }
2896
2926
 
2897
2927
  const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
2898
- const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2928
+ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains, currentSchema) => {
2899
2929
  const content = await fs.readFile(dbPath, "utf-8");
2900
2930
  const statements = getTsStatements(content);
2901
2931
  const importName = getOrchidOrmImportName(statements);
@@ -2923,7 +2953,7 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2923
2953
  (ast) => `${quoteObjectKey(
2924
2954
  ast.schema ? `${ast.schema}.${ast.name}` : ast.name
2925
2955
  )}: (t) => ${ast.baseType.toCode(
2926
- { t: "t", table: ast.name },
2956
+ { t: "t", table: ast.name, currentSchema },
2927
2957
  ast.baseType.data.name ?? ""
2928
2958
  )},`
2929
2959
  ).join("\n ")}
@@ -3083,7 +3113,8 @@ const pull = async (options, config) => {
3083
3113
  fkeys,
3084
3114
  ast,
3085
3115
  baseTablePath,
3086
- baseTableExportedAs
3116
+ baseTableExportedAs,
3117
+ currentSchema
3087
3118
  );
3088
3119
  tables[table.key] = table;
3089
3120
  if (!firstTable) firstTable = table;
@@ -3117,7 +3148,8 @@ const pull = async (options, config) => {
3117
3148
  dbPath,
3118
3149
  tables,
3119
3150
  extensions,
3120
- domains
3151
+ domains,
3152
+ currentSchema
3121
3153
  );
3122
3154
  if (content) pendingFileWrites.push([dbPath, content]);
3123
3155
  if (firstTable) {