orchid-orm 1.55.0 → 1.56.2

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,11 +1,11 @@
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
- export * from 'rake-db';
3
- import { toSnakeCase, deepCompare, emptyArray, toArray, toCamelCase, addCode, codeToString, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
4
- import { EnumColumn, ArrayColumn, getColumnBaseType, RawSQL, VirtualColumn, DomainColumn, UnknownColumn, defaultSchemaConfig, columnsShapeToCode, pushTableDataCode, Adapter } from 'pqb';
1
+ import { promptSelect, getSchemaAndTableFromName, getDbTableColumnsChecks, dbColumnToAst, instantiateDbColumn, concatSchemaAndName, encodeColumnDefault, getIndexName, getExcludeName, getConstraintName, tableToAst, getDbStructureTableData, makeDomainsMap, astToMigration, createMigrationInterface, introspectDbSchema, makeStructureToAstCtx, makeFileVersion, writeMigrationFile, migrateAndClose, migrate, structureToAst, saveMigratedVersion, rakeDbCommands } from 'rake-db';
2
+ import { colors, toSnakeCase, deepCompare, emptyArray, toArray, exhaustive, toCamelCase, addCode, pluralize, codeToString, toPascalCase, getImportPath, singleQuote, quoteObjectKey, pathToLog } from 'orchid-core';
3
+ import { EnumColumn, ArrayColumn, getColumnBaseType, RawSQL, VirtualColumn, DomainColumn, UnknownColumn, defaultSchemaConfig, columnsShapeToCode, pushTableDataCode } from 'pqb';
5
4
  import path from 'node:path';
6
5
  import { pathToFileURL } from 'url';
7
6
  import fs from 'fs/promises';
8
7
  import typescript from 'typescript';
8
+ export * from 'rake-db/postgres-js';
9
9
 
10
10
  const compareSqlExpressions = async (tableExpressions, adapter) => {
11
11
  if (tableExpressions.length) {
@@ -16,10 +16,10 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
16
16
  const values = [];
17
17
  let result;
18
18
  try {
19
- const results = await adapter.query({
19
+ const results = await adapter.query(
20
20
  // It is important to run `CREATE TEMPORARY VIEW` and `DROP VIEW` on the same db connection,
21
21
  // that's why SQLs are combined into a single query.
22
- text: [
22
+ [
23
23
  `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
24
24
  ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
25
25
  (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
@@ -29,7 +29,7 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
29
29
  `DROP VIEW ${viewName}`
30
30
  ].join("; "),
31
31
  values
32
- });
32
+ );
33
33
  result = results[1];
34
34
  } catch {
35
35
  }
@@ -2005,10 +2005,10 @@ const applyCompareSql = async (compareSql, adapter) => {
2005
2005
  if (compareSql.expressions.length) {
2006
2006
  const {
2007
2007
  rows: [results]
2008
- } = await adapter.arrays({
2009
- text: "SELECT " + compareSql.expressions.map((x) => `${x.inDb} = (${x.inCode})`).join(", "),
2010
- values: compareSql.values
2011
- });
2008
+ } = await adapter.arrays(
2009
+ "SELECT " + compareSql.expressions.map((x) => `${x.inDb} = (${x.inCode})`).join(", "),
2010
+ compareSql.values
2011
+ );
2012
2012
  for (let i = 0; i < results.length; i++) {
2013
2013
  if (!results[i]) {
2014
2014
  compareSql.expressions[i].change();
@@ -2147,29 +2147,25 @@ const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2147
2147
  return astToMigration(currentSchema, config, ast);
2148
2148
  };
2149
2149
 
2150
+ const rollbackErr = new Error("Rollback");
2150
2151
  const verifyMigration = async (adapter, config, migrationCode, generateMigrationParams) => {
2151
2152
  const migrationFn = new Function("change", migrationCode);
2152
- return adapter.transaction(
2153
- { text: "BEGIN" },
2154
- async (trx) => {
2153
+ let code;
2154
+ try {
2155
+ await adapter.transaction(void 0, async (trx) => {
2155
2156
  const changeFns = [];
2156
2157
  migrationFn((changeCb) => {
2157
2158
  changeFns.push(changeCb);
2158
2159
  });
2159
2160
  const { log } = config;
2160
2161
  config.log = false;
2161
- const db = createMigrationInterface(
2162
- trx,
2163
- true,
2164
- config
2165
- );
2162
+ const db = createMigrationInterface(trx, true, config);
2166
2163
  config.log = log;
2167
2164
  for (const changeFn of changeFns) {
2168
2165
  await changeFn(db, true);
2169
2166
  }
2170
2167
  const dbStructure = await introspectDbSchema(trx);
2171
2168
  generateMigrationParams.verifying = true;
2172
- let code;
2173
2169
  try {
2174
2170
  code = await composeMigration(
2175
2171
  trx,
@@ -2180,14 +2176,19 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
2180
2176
  );
2181
2177
  } catch (err) {
2182
2178
  if (err instanceof AbortSignal) {
2183
- return false;
2179
+ code = false;
2180
+ throw rollbackErr;
2184
2181
  }
2185
2182
  throw err;
2186
2183
  }
2187
- return code;
2188
- },
2189
- { text: "ROLLBACK" }
2190
- );
2184
+ throw rollbackErr;
2185
+ });
2186
+ } catch (err) {
2187
+ if (err !== rollbackErr) {
2188
+ throw err;
2189
+ }
2190
+ }
2191
+ return code;
2191
2192
  };
2192
2193
 
2193
2194
  const report = (ast, config, currentSchema) => {
@@ -2493,10 +2494,10 @@ const dbItemName = ({ schema, name }, currentSchema) => {
2493
2494
 
2494
2495
  class AbortSignal extends Error {
2495
2496
  }
2496
- const generate = async (options, config, args, afterPull) => {
2497
+ const generate = async (adapters, config, args, afterPull) => {
2497
2498
  let { dbPath } = config;
2498
2499
  if (!dbPath || !config.baseTable) throw invalidConfig(config);
2499
- if (!options.length) throw new Error(`Database options must not be empty`);
2500
+ if (!adapters.length) throw new Error(`Database options must not be empty`);
2500
2501
  if (!dbPath.endsWith(".ts")) dbPath += ".ts";
2501
2502
  let migrationName = args[0] ?? "generated";
2502
2503
  let up;
@@ -2506,8 +2507,11 @@ const generate = async (options, config, args, afterPull) => {
2506
2507
  } else {
2507
2508
  up = args[1] === "up";
2508
2509
  }
2509
- const { dbStructure, adapters } = await migrateAndPullStructures(
2510
- options,
2510
+ if (afterPull) {
2511
+ adapters = [afterPull.adapter];
2512
+ }
2513
+ const { dbStructure } = await migrateAndPullStructures(
2514
+ adapters,
2511
2515
  config,
2512
2516
  afterPull
2513
2517
  );
@@ -2586,7 +2590,9 @@ ${msg}`);
2586
2590
  }
2587
2591
  }
2588
2592
  if (up) {
2589
- await migrate({}, options, config, void 0, adapters);
2593
+ for (const adapter2 of adapters) {
2594
+ await migrateAndClose({ adapter: adapter2, config });
2595
+ }
2590
2596
  } else if (!afterPull) {
2591
2597
  await closeAdapters(adapters);
2592
2598
  }
@@ -2606,7 +2612,7 @@ const getDbFromConfig = async (config, dbPath) => {
2606
2612
  }
2607
2613
  return db;
2608
2614
  };
2609
- const migrateAndPullStructures = async (options, config, afterPull) => {
2615
+ const migrateAndPullStructures = async (adapters, config, afterPull) => {
2610
2616
  if (afterPull) {
2611
2617
  return {
2612
2618
  dbStructure: {
@@ -2621,18 +2627,12 @@ const migrateAndPullStructures = async (options, config, afterPull) => {
2621
2627
  enums: [],
2622
2628
  domains: [],
2623
2629
  collations: []
2624
- },
2625
- adapters: [afterPull.adapter]
2630
+ }
2626
2631
  };
2627
2632
  }
2628
- const adapters = await migrate(
2629
- {},
2630
- options,
2631
- config,
2632
- void 0,
2633
- void 0,
2634
- true
2635
- );
2633
+ for (const adapter of adapters) {
2634
+ await migrate({ adapter, config });
2635
+ }
2636
2636
  const dbStructures = await Promise.all(
2637
2637
  adapters.map((adapter) => introspectDbSchema(adapter))
2638
2638
  );
@@ -2640,7 +2640,7 @@ const migrateAndPullStructures = async (options, config, afterPull) => {
2640
2640
  for (let i = 1; i < dbStructures.length; i++) {
2641
2641
  compareDbStructures(dbStructure, dbStructures[i], i);
2642
2642
  }
2643
- return { dbStructure, adapters };
2643
+ return { dbStructure };
2644
2644
  };
2645
2645
  const compareDbStructures = (a, b, i, path2) => {
2646
2646
  let err;
@@ -3087,7 +3087,7 @@ const applyChanges = (content, changes) => {
3087
3087
  ).join("") : content;
3088
3088
  };
3089
3089
 
3090
- const pull = async (options, config) => {
3090
+ const pull = async (adapters, config) => {
3091
3091
  if (!config.dbPath || !config.baseTable) {
3092
3092
  throw new Error(
3093
3093
  `\`${config.dbPath ? "baseTable" : "dbPath"}\` setting must be set in the migrations config for pull command`
@@ -3095,7 +3095,6 @@ const pull = async (options, config) => {
3095
3095
  }
3096
3096
  const baseTablePath = config.baseTable.getFilePath();
3097
3097
  const baseTableExportedAs = config.baseTable.exportAs;
3098
- const adapters = options.map((opts) => new Adapter(opts));
3099
3098
  const [adapter] = adapters;
3100
3099
  const currentSchema = adapter.schema || "public";
3101
3100
  const ctx = makeStructureToAstCtx(config, currentSchema);
@@ -3159,13 +3158,13 @@ const pull = async (options, config) => {
3159
3158
  }
3160
3159
  await Promise.all(
3161
3160
  pendingFileWrites.map(
3162
- ([path2, content2, options2]) => fs.writeFile(path2, content2, options2).then(() => {
3161
+ ([path2, content2, options]) => fs.writeFile(path2, content2, options).then(() => {
3163
3162
  config.logger?.log(`Created ${pathToLog(path2)}`);
3164
3163
  })
3165
3164
  )
3166
3165
  );
3167
3166
  const version = await makeFileVersion({}, config);
3168
- await generate(options, config, ["pull"], { adapter, version });
3167
+ await generate(adapters, config, ["pull"], { adapter, version });
3169
3168
  await Promise.all(
3170
3169
  adapters.map(async (adapter2) => {
3171
3170
  const silentAdapter = adapter2;
@@ -3189,4 +3188,4 @@ rakeDbCommands.g = rakeDbCommands.generate = {
3189
3188
  };
3190
3189
  rakeDbCommands.pull.run = pull;
3191
3190
  rakeDbCommands.pull.help = "generate ORM tables and a migration for an existing database";
3192
- //# sourceMappingURL=migrations.mjs.map
3191
+ //# sourceMappingURL=postgres-js.mjs.map