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.
@@ -1,6 +1,6 @@
1
1
  import { promptSelect, colors, getSchemaAndTableFromName, getDbTableColumnsChecks, dbColumnToAst, instantiateDbColumn, concatSchemaAndName, encodeColumnDefault, getIndexName, getExcludeName, getConstraintName, tableToAst, getDbStructureTableData, makeDomainsMap, astToMigration, createMigrationInterface, introspectDbSchema, exhaustive, pluralize, makeStructureToAstCtx, makeFileVersion, writeMigrationFile, migrate, structureToAst, saveMigratedVersion, rakeDbCommands } from 'rake-db';
2
2
  export * from 'rake-db';
3
- import { toSnakeCase, deepCompare, emptyArray, toArray, addCode, codeToString, toCamelCase, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
3
+ import { toSnakeCase, deepCompare, emptyArray, toArray, toCamelCase, addCode, codeToString, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
4
4
  import { EnumColumn, ArrayColumn, getColumnBaseType, RawSQL, VirtualColumn, DomainColumn, UnknownColumn, defaultSchemaConfig, columnsShapeToCode, pushTableDataCode, Adapter } from 'pqb';
5
5
  import path from 'node:path';
6
6
  import { pathToFileURL } from 'url';
@@ -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
  };
@@ -2251,7 +2275,8 @@ const report = (ast, config, currentSchema) => {
2251
2275
  }).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(", ")}` : ""}`
2252
2276
  );
2253
2277
  } else if (change.type === "change") {
2254
- const name = change.from.column?.data.name ?? key;
2278
+ let name = change.from.column?.data.name ?? key;
2279
+ if (config.snakeCase) name = toCamelCase(name);
2255
2280
  const changes2 = [];
2256
2281
  inner.push(`${yellow("~ change column")} ${name}:`, changes2);
2257
2282
  changes2.push(`${yellow("from")}: `);
@@ -2266,7 +2291,7 @@ const report = (ast, config, currentSchema) => {
2266
2291
  }
2267
2292
  } else if (change.type === "rename") {
2268
2293
  inner.push(
2269
- `${yellow("~ rename column")} ${key} ${yellow("=>")} ${change.name}`
2294
+ `${yellow("~ rename column")} ${config.snakeCase ? toCamelCase(key) : key} ${yellow("=>")} ${change.name}`
2270
2295
  );
2271
2296
  } else {
2272
2297
  exhaustive(change.type);
@@ -2694,8 +2719,11 @@ const getActualItems = async (db, currentSchema, internal, columnTypes) => {
2694
2719
  name: name2,
2695
2720
  column: column.data.as ?? new UnknownColumn(defaultSchemaConfig)
2696
2721
  });
2697
- } else if (column.dataType === "enum") {
2698
- processEnumColumn(column, currentSchema, codeItems);
2722
+ } else {
2723
+ const en = column.dataType === "enum" ? column : column instanceof ArrayColumn && column.data.item.dataType === "enum" ? column.data.item : void 0;
2724
+ if (en) {
2725
+ processEnumColumn(en, currentSchema, codeItems);
2726
+ }
2699
2727
  }
2700
2728
  }
2701
2729
  }
@@ -2811,7 +2839,7 @@ const getTableInfosAndFKeys = (asts, config) => {
2811
2839
  }
2812
2840
  return { tableInfos, fkeys };
2813
2841
  };
2814
- const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs) => {
2842
+ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExportedAs, currentSchema) => {
2815
2843
  const tableInfo = tableInfos[ast.schema ? `${ast.schema}.${ast.name}` : ast.name];
2816
2844
  const imports = {
2817
2845
  "orchid-orm": "Selectable, Insertable, Updatable",
@@ -2831,7 +2859,10 @@ const appCodeGenTable = (tableInfos, fkeys, ast, baseTablePath, baseTableExporte
2831
2859
  const hasTableData = Boolean(
2832
2860
  ast.primaryKey || ast.indexes?.length || ast.excludes?.length || ast.constraints?.length
2833
2861
  );
2834
- const shapeCode = columnsShapeToCode({ t: "t", table: ast.name }, ast.shape);
2862
+ const shapeCode = columnsShapeToCode(
2863
+ { t: "t", table: ast.name, currentSchema },
2864
+ ast.shape
2865
+ );
2835
2866
  props.push(
2836
2867
  `columns = this.setColumns(${hasTableData ? "\n " : ""}(t) => ({`,
2837
2868
  hasTableData ? [shapeCode] : shapeCode,
@@ -2895,7 +2926,7 @@ function importsToCode(imports) {
2895
2926
  }
2896
2927
 
2897
2928
  const { createSourceFile, ScriptTarget, SyntaxKind } = typescript;
2898
- const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2929
+ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains, currentSchema) => {
2899
2930
  const content = await fs.readFile(dbPath, "utf-8");
2900
2931
  const statements = getTsStatements(content);
2901
2932
  const importName = getOrchidOrmImportName(statements);
@@ -2913,7 +2944,7 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2913
2944
  if (extensions.length) {
2914
2945
  code += `
2915
2946
  extensions: [${extensions.map(
2916
- (ext) => ext.version ? `{ ${quoteObjectKey(ext.name)}: '${ext.version}' }` : singleQuote(ext.name)
2947
+ (ext) => ext.version ? `{ ${quoteObjectKey(ext.name, false)}: '${ext.version}' }` : singleQuote(ext.name)
2917
2948
  ).join(", ")}],`;
2918
2949
  }
2919
2950
  if (domains.length) {
@@ -2921,9 +2952,10 @@ const appCodeGenUpdateDbFile = async (dbPath, tables, extensions, domains) => {
2921
2952
  domains: {
2922
2953
  ${domains.sort((a, b) => a.name > b.name ? 1 : -1).map(
2923
2954
  (ast) => `${quoteObjectKey(
2924
- ast.schema ? `${ast.schema}.${ast.name}` : ast.name
2955
+ ast.schema ? `${ast.schema}.${ast.name}` : ast.name,
2956
+ false
2925
2957
  )}: (t) => ${ast.baseType.toCode(
2926
- { t: "t", table: ast.name },
2958
+ { t: "t", table: ast.name, currentSchema },
2927
2959
  ast.baseType.data.name ?? ""
2928
2960
  )},`
2929
2961
  ).join("\n ")}
@@ -3083,7 +3115,8 @@ const pull = async (options, config) => {
3083
3115
  fkeys,
3084
3116
  ast,
3085
3117
  baseTablePath,
3086
- baseTableExportedAs
3118
+ baseTableExportedAs,
3119
+ currentSchema
3087
3120
  );
3088
3121
  tables[table.key] = table;
3089
3122
  if (!firstTable) firstTable = table;
@@ -3117,7 +3150,8 @@ const pull = async (options, config) => {
3117
3150
  dbPath,
3118
3151
  tables,
3119
3152
  extensions,
3120
- domains
3153
+ domains,
3154
+ currentSchema
3121
3155
  );
3122
3156
  if (content) pendingFileWrites.push([dbPath, content]);
3123
3157
  if (firstTable) {