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.
@@ -7,6 +7,7 @@ var path = require('node:path');
7
7
  var url = require('url');
8
8
  var fs = require('fs/promises');
9
9
  var typescript = require('typescript');
10
+ var postgresJs = require('rake-db/postgres-js');
10
11
 
11
12
  const compareSqlExpressions = async (tableExpressions, adapter) => {
12
13
  if (tableExpressions.length) {
@@ -17,10 +18,10 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
17
18
  const values = [];
18
19
  let result;
19
20
  try {
20
- const results = await adapter.query({
21
+ const results = await adapter.query(
21
22
  // It is important to run `CREATE TEMPORARY VIEW` and `DROP VIEW` on the same db connection,
22
23
  // that's why SQLs are combined into a single query.
23
- text: [
24
+ [
24
25
  `CREATE TEMPORARY VIEW ${viewName} AS (SELECT ${compare.map(
25
26
  ({ inDb, inCode }, i) => `${inDb} AS "*inDb-${i}*", ${inCode.map(
26
27
  (s, j) => `(${typeof s === "string" ? s : s.toSQL({ values })}) "*inCode-${i}-${j}*"`
@@ -30,7 +31,7 @@ const compareSqlExpressions = async (tableExpressions, adapter) => {
30
31
  `DROP VIEW ${viewName}`
31
32
  ].join("; "),
32
33
  values
33
- });
34
+ );
34
35
  result = results[1];
35
36
  } catch {
36
37
  }
@@ -89,20 +90,20 @@ const promptCreateOrRename = (kind, name, drop, verifying) => {
89
90
  }
90
91
  const renameMessage = `rename ${kind}`;
91
92
  return rakeDb.promptSelect({
92
- message: `Create or rename ${rakeDb.colors.blueBold(
93
+ message: `Create or rename ${orchidCore.colors.blueBold(
93
94
  name
94
95
  )} ${kind} from another ${kind}?`,
95
96
  options: [
96
- `${rakeDb.colors.greenBold("+")} ${name} ${rakeDb.colors.pale(
97
+ `${orchidCore.colors.greenBold("+")} ${name} ${orchidCore.colors.pale(
97
98
  `create ${kind}`.padStart(
98
99
  hintPos + renameMessage.length - name.length - 4,
99
100
  " "
100
101
  )
101
102
  )}`,
102
103
  ...drop.map(
103
- (d) => `${rakeDb.colors.yellowBold("~")} ${d} ${rakeDb.colors.yellowBold(
104
+ (d) => `${orchidCore.colors.yellowBold("~")} ${d} ${orchidCore.colors.yellowBold(
104
105
  "=>"
105
- )} ${name} ${rakeDb.colors.pale(
106
+ )} ${name} ${orchidCore.colors.pale(
106
107
  renameMessage.padStart(
107
108
  hintPos + renameMessage.length - d.length - name.length - 8,
108
109
  " "
@@ -460,9 +461,9 @@ const compareColumns = async (adapter, domainsMap, ast, currentSchema, compareSq
460
461
  const abort = await rakeDb.promptSelect({
461
462
  message: `Cannot cast type of ${tableName}'s column ${key} from ${dbType} to ${codeType}`,
462
463
  options: [
463
- `${rakeDb.colors.yellowBold(
464
+ `${orchidCore.colors.yellowBold(
464
465
  `-/+`
465
- )} recreate the column, existing data will be ${rakeDb.colors.red(
466
+ )} recreate the column, existing data will be ${orchidCore.colors.red(
466
467
  "lost"
467
468
  )}`,
468
469
  `write migration manually`
@@ -2006,10 +2007,10 @@ const applyCompareSql = async (compareSql, adapter) => {
2006
2007
  if (compareSql.expressions.length) {
2007
2008
  const {
2008
2009
  rows: [results]
2009
- } = await adapter.arrays({
2010
- text: "SELECT " + compareSql.expressions.map((x) => `${x.inDb} = (${x.inCode})`).join(", "),
2011
- values: compareSql.values
2012
- });
2010
+ } = await adapter.arrays(
2011
+ "SELECT " + compareSql.expressions.map((x) => `${x.inDb} = (${x.inCode})`).join(", "),
2012
+ compareSql.values
2013
+ );
2013
2014
  for (let i = 0; i < results.length; i++) {
2014
2015
  if (!results[i]) {
2015
2016
  compareSql.expressions[i].change();
@@ -2148,29 +2149,25 @@ const composeMigration = async (adapter, config, ast, dbStructure, params) => {
2148
2149
  return rakeDb.astToMigration(currentSchema, config, ast);
2149
2150
  };
2150
2151
 
2152
+ const rollbackErr = new Error("Rollback");
2151
2153
  const verifyMigration = async (adapter, config, migrationCode, generateMigrationParams) => {
2152
2154
  const migrationFn = new Function("change", migrationCode);
2153
- return adapter.transaction(
2154
- { text: "BEGIN" },
2155
- async (trx) => {
2155
+ let code;
2156
+ try {
2157
+ await adapter.transaction(void 0, async (trx) => {
2156
2158
  const changeFns = [];
2157
2159
  migrationFn((changeCb) => {
2158
2160
  changeFns.push(changeCb);
2159
2161
  });
2160
2162
  const { log } = config;
2161
2163
  config.log = false;
2162
- const db = rakeDb.createMigrationInterface(
2163
- trx,
2164
- true,
2165
- config
2166
- );
2164
+ const db = rakeDb.createMigrationInterface(trx, true, config);
2167
2165
  config.log = log;
2168
2166
  for (const changeFn of changeFns) {
2169
2167
  await changeFn(db, true);
2170
2168
  }
2171
2169
  const dbStructure = await rakeDb.introspectDbSchema(trx);
2172
2170
  generateMigrationParams.verifying = true;
2173
- let code;
2174
2171
  try {
2175
2172
  code = await composeMigration(
2176
2173
  trx,
@@ -2181,14 +2178,19 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
2181
2178
  );
2182
2179
  } catch (err) {
2183
2180
  if (err instanceof AbortSignal) {
2184
- return false;
2181
+ code = false;
2182
+ throw rollbackErr;
2185
2183
  }
2186
2184
  throw err;
2187
2185
  }
2188
- return code;
2189
- },
2190
- { text: "ROLLBACK" }
2191
- );
2186
+ throw rollbackErr;
2187
+ });
2188
+ } catch (err) {
2189
+ if (err !== rollbackErr) {
2190
+ throw err;
2191
+ }
2192
+ }
2193
+ return code;
2192
2194
  };
2193
2195
 
2194
2196
  const report = (ast, config, currentSchema) => {
@@ -2198,7 +2200,7 @@ const report = (ast, config, currentSchema) => {
2198
2200
  if (typeof config.log === "object" && config.log.colors === false) {
2199
2201
  green = red = yellow = pale = (s) => s;
2200
2202
  } else {
2201
- ({ green, red, yellow, pale } = rakeDb.colors);
2203
+ ({ green, red, yellow, pale } = orchidCore.colors);
2202
2204
  }
2203
2205
  for (const a of ast) {
2204
2206
  switch (a.type) {
@@ -2241,7 +2243,7 @@ const report = (ast, config, currentSchema) => {
2241
2243
  const value = counters[key];
2242
2244
  if (value || key === "column") {
2243
2245
  summary.push(
2244
- `${value} ${rakeDb.pluralize(key, value, key === "index" ? "es" : "s")}`
2246
+ `${value} ${orchidCore.pluralize(key, value, key === "index" ? "es" : "s")}`
2245
2247
  );
2246
2248
  }
2247
2249
  }
@@ -2295,7 +2297,7 @@ const report = (ast, config, currentSchema) => {
2295
2297
  `${yellow("~ rename column")} ${config.snakeCase ? orchidCore.toCamelCase(key) : key} ${yellow("=>")} ${change.name}`
2296
2298
  );
2297
2299
  } else {
2298
- rakeDb.exhaustive(change.type);
2300
+ orchidCore.exhaustive(change.type);
2299
2301
  }
2300
2302
  }
2301
2303
  }
@@ -2482,7 +2484,7 @@ const report = (ast, config, currentSchema) => {
2482
2484
  break;
2483
2485
  }
2484
2486
  default:
2485
- rakeDb.exhaustive(a);
2487
+ orchidCore.exhaustive(a);
2486
2488
  }
2487
2489
  }
2488
2490
  const result = orchidCore.codeToString(code, "", " ");
@@ -2494,10 +2496,10 @@ const dbItemName = ({ schema, name }, currentSchema) => {
2494
2496
 
2495
2497
  class AbortSignal extends Error {
2496
2498
  }
2497
- const generate = async (options, config, args, afterPull) => {
2499
+ const generate = async (adapters, config, args, afterPull) => {
2498
2500
  let { dbPath } = config;
2499
2501
  if (!dbPath || !config.baseTable) throw invalidConfig(config);
2500
- if (!options.length) throw new Error(`Database options must not be empty`);
2502
+ if (!adapters.length) throw new Error(`Database options must not be empty`);
2501
2503
  if (!dbPath.endsWith(".ts")) dbPath += ".ts";
2502
2504
  let migrationName = args[0] ?? "generated";
2503
2505
  let up;
@@ -2507,8 +2509,11 @@ const generate = async (options, config, args, afterPull) => {
2507
2509
  } else {
2508
2510
  up = args[1] === "up";
2509
2511
  }
2510
- const { dbStructure, adapters } = await migrateAndPullStructures(
2511
- options,
2512
+ if (afterPull) {
2513
+ adapters = [afterPull.adapter];
2514
+ }
2515
+ const { dbStructure } = await migrateAndPullStructures(
2516
+ adapters,
2512
2517
  config,
2513
2518
  afterPull
2514
2519
  );
@@ -2587,7 +2592,9 @@ ${msg}`);
2587
2592
  }
2588
2593
  }
2589
2594
  if (up) {
2590
- await rakeDb.migrate({}, options, config, void 0, adapters);
2595
+ for (const adapter2 of adapters) {
2596
+ await rakeDb.migrateAndClose({ adapter: adapter2, config });
2597
+ }
2591
2598
  } else if (!afterPull) {
2592
2599
  await closeAdapters(adapters);
2593
2600
  }
@@ -2607,7 +2614,7 @@ const getDbFromConfig = async (config, dbPath) => {
2607
2614
  }
2608
2615
  return db;
2609
2616
  };
2610
- const migrateAndPullStructures = async (options, config, afterPull) => {
2617
+ const migrateAndPullStructures = async (adapters, config, afterPull) => {
2611
2618
  if (afterPull) {
2612
2619
  return {
2613
2620
  dbStructure: {
@@ -2622,18 +2629,12 @@ const migrateAndPullStructures = async (options, config, afterPull) => {
2622
2629
  enums: [],
2623
2630
  domains: [],
2624
2631
  collations: []
2625
- },
2626
- adapters: [afterPull.adapter]
2632
+ }
2627
2633
  };
2628
2634
  }
2629
- const adapters = await rakeDb.migrate(
2630
- {},
2631
- options,
2632
- config,
2633
- void 0,
2634
- void 0,
2635
- true
2636
- );
2635
+ for (const adapter of adapters) {
2636
+ await rakeDb.migrate({ adapter, config });
2637
+ }
2637
2638
  const dbStructures = await Promise.all(
2638
2639
  adapters.map((adapter) => rakeDb.introspectDbSchema(adapter))
2639
2640
  );
@@ -2641,7 +2642,7 @@ const migrateAndPullStructures = async (options, config, afterPull) => {
2641
2642
  for (let i = 1; i < dbStructures.length; i++) {
2642
2643
  compareDbStructures(dbStructure, dbStructures[i], i);
2643
2644
  }
2644
- return { dbStructure, adapters };
2645
+ return { dbStructure };
2645
2646
  };
2646
2647
  const compareDbStructures = (a, b, i, path2) => {
2647
2648
  let err;
@@ -3088,7 +3089,7 @@ const applyChanges = (content, changes) => {
3088
3089
  ).join("") : content;
3089
3090
  };
3090
3091
 
3091
- const pull = async (options, config) => {
3092
+ const pull = async (adapters, config) => {
3092
3093
  if (!config.dbPath || !config.baseTable) {
3093
3094
  throw new Error(
3094
3095
  `\`${config.dbPath ? "baseTable" : "dbPath"}\` setting must be set in the migrations config for pull command`
@@ -3096,7 +3097,6 @@ const pull = async (options, config) => {
3096
3097
  }
3097
3098
  const baseTablePath = config.baseTable.getFilePath();
3098
3099
  const baseTableExportedAs = config.baseTable.exportAs;
3099
- const adapters = options.map((opts) => new pqb.Adapter(opts));
3100
3100
  const [adapter] = adapters;
3101
3101
  const currentSchema = adapter.schema || "public";
3102
3102
  const ctx = rakeDb.makeStructureToAstCtx(config, currentSchema);
@@ -3160,13 +3160,13 @@ const pull = async (options, config) => {
3160
3160
  }
3161
3161
  await Promise.all(
3162
3162
  pendingFileWrites.map(
3163
- ([path2, content2, options2]) => fs.writeFile(path2, content2, options2).then(() => {
3163
+ ([path2, content2, options]) => fs.writeFile(path2, content2, options).then(() => {
3164
3164
  config.logger?.log(`Created ${orchidCore.pathToLog(path2)}`);
3165
3165
  })
3166
3166
  )
3167
3167
  );
3168
3168
  const version = await rakeDb.makeFileVersion({}, config);
3169
- await generate(options, config, ["pull"], { adapter, version });
3169
+ await generate(adapters, config, ["pull"], { adapter, version });
3170
3170
  await Promise.all(
3171
3171
  adapters.map(async (adapter2) => {
3172
3172
  const silentAdapter = adapter2;
@@ -3191,10 +3191,10 @@ rakeDb.rakeDbCommands.g = rakeDb.rakeDbCommands.generate = {
3191
3191
  rakeDb.rakeDbCommands.pull.run = pull;
3192
3192
  rakeDb.rakeDbCommands.pull.help = "generate ORM tables and a migration for an existing database";
3193
3193
 
3194
- Object.keys(rakeDb).forEach(function (k) {
3194
+ Object.keys(postgresJs).forEach(function (k) {
3195
3195
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
3196
3196
  enumerable: true,
3197
- get: function () { return rakeDb[k]; }
3197
+ get: function () { return postgresJs[k]; }
3198
3198
  });
3199
3199
  });
3200
- //# sourceMappingURL=migrations.js.map
3200
+ //# sourceMappingURL=postgres-js.js.map