rake-db 2.6.0 → 2.7.0

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.
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { quote, getRaw, EnumColumn, columnTypes, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, TimestampTzColumn, referencesArgsToCode, constraintPropsToCode } from 'pqb';
2
- import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, deepCompare, raw, pathToLog, emptyArray, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
1
+ import { columnTypes, quote, getRaw, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, TimestampTzColumn, referencesArgsToCode, constraintPropsToCode } from 'pqb';
2
+ import { singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, nameKey, emptyObject, deepCompare, raw, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
3
3
  import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
5
5
  import prompts from 'prompts';
@@ -35,12 +35,12 @@ const migrationConfigDefaults = {
35
35
  useCodeUpdater: true
36
36
  };
37
37
  const processRakeDbConfig = (config) => {
38
- var _a;
38
+ var _a, _b;
39
39
  const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
40
40
  if (!result.log) {
41
41
  delete result.logger;
42
42
  }
43
- if (!result.basePath) {
43
+ if (!result.basePath || !result.dbScript) {
44
44
  let stack;
45
45
  const original = Error.prepareStackTrace;
46
46
  Error.prepareStackTrace = (_, s) => stack = s;
@@ -63,6 +63,7 @@ const processRakeDbConfig = (config) => {
63
63
  }
64
64
  }
65
65
  result.basePath = path.dirname(file);
66
+ result.dbScript = path.basename(file);
66
67
  break;
67
68
  }
68
69
  }
@@ -78,6 +79,14 @@ const processRakeDbConfig = (config) => {
78
79
  result.migrationsPath
79
80
  );
80
81
  }
82
+ if ("baseTable" in config) {
83
+ const proto = (_b = config.baseTable) == null ? void 0 : _b.prototype;
84
+ result.columnTypes = proto.columnTypes || columnTypes;
85
+ if (proto.snakeCase)
86
+ result.snakeCase = true;
87
+ } else {
88
+ result.columnTypes = "columnTypes" in config && (typeof config.columnTypes === "function" ? config.columnTypes(columnTypes) : config.columnTypes) || columnTypes;
89
+ }
81
90
  return result;
82
91
  };
83
92
  const getDatabaseAndUserFromOptions = (options) => {
@@ -247,13 +256,11 @@ const makePopulateEnumQuery = (item) => {
247
256
  };
248
257
 
249
258
  let currentChanges = [];
250
- const change = (fn) => {
251
- currentChanges.push(fn);
252
- };
253
259
  const clearChanges = () => {
254
260
  currentChanges = [];
255
261
  };
256
262
  const getCurrentChanges = () => currentChanges;
263
+ const pushChange = (fn) => currentChanges.push(fn);
257
264
 
258
265
  var __defProp$5 = Object.defineProperty;
259
266
  var __defProps$4 = Object.defineProperties;
@@ -548,9 +555,12 @@ var __objRest$1 = (source, exclude) => {
548
555
  }
549
556
  return target;
550
557
  };
551
- const types = Object.assign(Object.create(columnTypes), tableMethods);
552
558
  const createTable$1 = async (migration, up, tableName, options, fn) => {
553
559
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
560
+ const types = Object.assign(
561
+ Object.create(migration.columnTypes),
562
+ tableMethods
563
+ );
554
564
  types[snakeCaseKey] = snakeCase;
555
565
  const shape = getColumnTypes(types, fn);
556
566
  const tableData = getTableData();
@@ -874,7 +884,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
874
884
  var _a;
875
885
  resetTableData();
876
886
  resetChangeTableData();
877
- const tableChanger = Object.create(columnTypes);
887
+ const tableChanger = Object.create(migration.columnTypes);
878
888
  Object.assign(tableChanger, tableChangeMethods);
879
889
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
880
890
  tableChanger[snakeCaseKey] = snakeCase;
@@ -1264,10 +1274,10 @@ var __spreadValues$2 = (a, b) => {
1264
1274
  return a;
1265
1275
  };
1266
1276
  var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1267
- const createMigrationInterface = (tx, up, options) => {
1277
+ const createMigrationInterface = (tx, up, config) => {
1268
1278
  const adapter = new TransactionAdapter(tx, tx.client, tx.types);
1269
1279
  const { query, arrays } = adapter;
1270
- const log = logParamToLogObject(options.logger || console, options.log);
1280
+ const log = logParamToLogObject(config.logger || console, config.log);
1271
1281
  adapter.query = (q, types) => {
1272
1282
  return wrapWithLog(log, q, () => query.call(adapter, q, types));
1273
1283
  };
@@ -1275,7 +1285,10 @@ const createMigrationInterface = (tx, up, options) => {
1275
1285
  return wrapWithLog(log, q, () => arrays.call(adapter, q, types));
1276
1286
  };
1277
1287
  Object.assign(adapter, { silentQuery: query, silentArrays: arrays });
1278
- const db = createDb$1({ adapter });
1288
+ const db = createDb$1({
1289
+ adapter,
1290
+ columnTypes: config.columnTypes
1291
+ });
1279
1292
  const { prototype: proto } = MigrationBase;
1280
1293
  for (const key of Object.getOwnPropertyNames(proto)) {
1281
1294
  db[key] = proto[key];
@@ -1285,7 +1298,7 @@ const createMigrationInterface = (tx, up, options) => {
1285
1298
  adapter,
1286
1299
  log,
1287
1300
  up,
1288
- options
1301
+ options: config
1289
1302
  });
1290
1303
  };
1291
1304
  class MigrationBase {
@@ -1532,7 +1545,7 @@ const createDomain$1 = async (migration, up, name, fn, options) => {
1532
1545
  action: up ? "create" : "drop",
1533
1546
  schema,
1534
1547
  name: domainName,
1535
- baseType: fn(columnTypes)
1548
+ baseType: fn(migration.columnTypes)
1536
1549
  }, options);
1537
1550
  let query;
1538
1551
  const values = [];
@@ -1669,7 +1682,6 @@ const begin = {
1669
1682
  const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
1670
1683
  var _a;
1671
1684
  const asts = await db.transaction(begin, async (tx) => {
1672
- const db2 = createMigrationInterface(tx, up, config);
1673
1685
  clearChanges();
1674
1686
  let changes = changeCache[file.path];
1675
1687
  if (!changes) {
@@ -1683,6 +1695,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
1683
1695
  changes = getCurrentChanges();
1684
1696
  changeCache[file.path] = changes;
1685
1697
  }
1698
+ const db2 = createMigrationInterface(tx, up, config);
1686
1699
  for (const fn of up ? changes : changes.reverse()) {
1687
1700
  await fn(db2, up);
1688
1701
  }
@@ -1829,14 +1842,18 @@ const writeMigrationFile = async (config, version, name, content) => {
1829
1842
  var _a;
1830
1843
  await mkdir(config.migrationsPath, { recursive: true });
1831
1844
  const filePath = path.resolve(config.migrationsPath, `${version}_${name}.ts`);
1832
- await writeFile(filePath, content);
1845
+ const importPath = getImportPath(
1846
+ filePath,
1847
+ path.join(config.basePath, config.dbScript)
1848
+ );
1849
+ await writeFile(filePath, content(importPath, name));
1833
1850
  (_a = config.logger) == null ? void 0 : _a.log(`Created ${pathToLog(filePath)}`);
1834
1851
  };
1835
1852
  const generate = async (config, [name]) => {
1836
1853
  if (!name)
1837
1854
  throw new Error("Migration name is missing");
1838
1855
  const version = makeFileTimeStamp();
1839
- await writeMigrationFile(config, version, name, makeContent(name));
1856
+ await writeMigrationFile(config, version, name, makeContent);
1840
1857
  };
1841
1858
  const makeFileTimeStamp = () => {
1842
1859
  const now = /* @__PURE__ */ new Date();
@@ -1849,8 +1866,8 @@ const makeFileTimeStamp = () => {
1849
1866
  now.getUTCSeconds()
1850
1867
  ].map((value) => value < 10 ? `0${value}` : value).join("");
1851
1868
  };
1852
- const makeContent = (name) => {
1853
- let content = `import { change } from 'rake-db';
1869
+ const makeContent = (importPath, name) => {
1870
+ let content = `import { change } from '${importPath}';
1854
1871
 
1855
1872
  change(async (db) => {`;
1856
1873
  const [first, rest] = getFirstWordAndRest(name);
@@ -2774,8 +2791,7 @@ const astToMigration = (config, ast) => {
2774
2791
  }
2775
2792
  if (!first.length && !tablesAndViews.length && !constraints.length)
2776
2793
  return;
2777
- let code = `import { change } from 'rake-db';
2778
- `;
2794
+ let code = "";
2779
2795
  if (first.length) {
2780
2796
  code += `
2781
2797
  change(async (db) => {
@@ -2799,7 +2815,8 @@ ${codeToString(constraints, " ", " ")}
2799
2815
  });
2800
2816
  `;
2801
2817
  }
2802
- return code;
2818
+ return (importPath) => `import { change } from '${importPath}';
2819
+ ${code}`;
2803
2820
  };
2804
2821
  const createSchema = (ast) => {
2805
2822
  return `await db.createSchema(${singleQuote(ast.name)});`;
@@ -2989,9 +3006,20 @@ Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count >
2989
3006
  adapter.close();
2990
3007
  };
2991
3008
 
2992
- const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2)) => {
2993
- var _a, _b, _c;
3009
+ const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
2994
3010
  const config = processRakeDbConfig(partialConfig);
3011
+ const promise = runCommand(options, config, args);
3012
+ return Object.assign(
3013
+ (fn) => {
3014
+ pushChange(fn);
3015
+ },
3016
+ {
3017
+ promise
3018
+ }
3019
+ );
3020
+ };
3021
+ const runCommand = async (options, config, args = process.argv.slice(2)) => {
3022
+ var _a, _b, _c;
2995
3023
  const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
2996
3024
  try {
2997
3025
  if (command === "create") {
@@ -3052,5 +3080,5 @@ Migrate and rollback common arguments:
3052
3080
  --code false do not run code updater
3053
3081
  `;
3054
3082
 
3055
- export { MigrationBase, change, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
3083
+ export { MigrationBase, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
3056
3084
  //# sourceMappingURL=index.mjs.map