rake-db 2.7.2 → 2.8.1

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.d.ts CHANGED
@@ -262,13 +262,18 @@ declare namespace RakeDbAst {
262
262
  }
263
263
 
264
264
  type Db = DbResult<DefaultColumnTypes>;
265
- type InputRakeDbConfig<CT extends ColumnTypesBase> = Partial<Omit<RakeDbConfig<CT>, 'columnTypes'>> & ({
266
- columnTypes?: CT | ((t: DefaultColumnTypes) => CT);
267
- } | {
268
- baseTable?: new () => {
265
+ type BaseTable<CT extends ColumnTypesBase> = {
266
+ name: string;
267
+ filePath: string;
268
+ new (): {
269
269
  columnTypes: CT;
270
270
  snakeCase?: boolean;
271
271
  };
272
+ };
273
+ type InputRakeDbConfig<CT extends ColumnTypesBase> = Partial<Omit<RakeDbConfig<CT>, 'columnTypes'>> & ({
274
+ columnTypes?: CT | ((t: DefaultColumnTypes) => CT);
275
+ } | {
276
+ baseTable?: BaseTable<CT>;
272
277
  });
273
278
  type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
274
279
  columnTypes: CT;
@@ -280,6 +285,7 @@ type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
280
285
  commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig<CT>, args: string[]) => Promise<void>>;
281
286
  import(path: string): Promise<void>;
282
287
  noPrimaryKey?: NoPrimaryKeyOption;
288
+ baseTable?: BaseTable<CT>;
283
289
  appCodeUpdater?: AppCodeUpdater;
284
290
  useCodeUpdater?: boolean;
285
291
  beforeMigrate?(db: Db): Promise<void>;
@@ -287,13 +293,22 @@ type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
287
293
  beforeRollback?(db: Db): Promise<void>;
288
294
  afterRollback?(db: Db): Promise<void>;
289
295
  } & QueryLogOptions;
290
- type AppCodeUpdater = (params: {
291
- ast: RakeDbAst;
296
+ type AppCodeUpdaterParams = {
292
297
  options: AdapterOptions;
293
298
  basePath: string;
294
299
  cache: object;
295
300
  logger: QueryLogOptions['logger'];
296
- }) => Promise<void>;
301
+ baseTable: {
302
+ filePath: string;
303
+ name: string;
304
+ };
305
+ };
306
+ type AppCodeUpdater = {
307
+ process(params: AppCodeUpdaterParams & {
308
+ ast: RakeDbAst;
309
+ }): Promise<void>;
310
+ afterAll(params: AppCodeUpdaterParams): Promise<void>;
311
+ };
297
312
 
298
313
  declare const createDb: <CT extends ColumnTypesBase>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
299
314
  declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
package/dist/index.js CHANGED
@@ -64,43 +64,25 @@ const migrationConfigDefaults = {
64
64
  useCodeUpdater: true
65
65
  };
66
66
  const processRakeDbConfig = (config) => {
67
- var _a, _b;
67
+ var _a;
68
68
  const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
69
+ if (config.appCodeUpdater && (!("baseTable" in config) || !config.baseTable)) {
70
+ throw new Error(
71
+ "`baseTable` option is required in `rakeDb` for `appCodeUpdater`"
72
+ );
73
+ }
69
74
  if (!result.log) {
70
75
  delete result.logger;
71
76
  }
72
77
  if (!result.basePath || !result.dbScript) {
73
- let stack;
74
- const original = Error.prepareStackTrace;
75
- Error.prepareStackTrace = (_, s) => stack = s;
76
- new Error().stack;
77
- Error.prepareStackTrace = original;
78
- if (stack) {
79
- const thisFile = (_a = stack[0]) == null ? void 0 : _a.getFileName();
80
- const thisDir = thisFile && path.dirname(thisFile);
81
- for (const item of stack) {
82
- let file = item.getFileName();
83
- if (!file || path.dirname(file) === thisDir || /\bnode_modules\b/.test(file)) {
84
- continue;
85
- }
86
- if (/file:\/\/\/\w+:\//.test(file)) {
87
- file = decodeURI(file.slice(8));
88
- } else {
89
- try {
90
- file = new URL(file).pathname;
91
- } catch (_) {
92
- }
93
- }
94
- result.basePath = path.dirname(file);
95
- result.dbScript = path.basename(file);
96
- break;
97
- }
98
- }
99
- if (!result.basePath) {
78
+ const filePath = orchidCore.getCallerFilePath();
79
+ if (!filePath) {
100
80
  throw new Error(
101
81
  "Failed to determine path to db script. Please set basePath option of rakeDb"
102
82
  );
103
83
  }
84
+ result.basePath = path.dirname(filePath);
85
+ result.dbScript = path.basename(filePath);
104
86
  }
105
87
  if (!path.isAbsolute(result.migrationsPath)) {
106
88
  result.migrationsPath = path.resolve(
@@ -109,7 +91,7 @@ const processRakeDbConfig = (config) => {
109
91
  );
110
92
  }
111
93
  if ("baseTable" in config) {
112
- const proto = (_b = config.baseTable) == null ? void 0 : _b.prototype;
94
+ const proto = (_a = config.baseTable) == null ? void 0 : _a.prototype;
113
95
  result.columnTypes = proto.columnTypes || pqb.columnTypes;
114
96
  if (proto.snakeCase)
115
97
  result.snakeCase = true;
@@ -1647,7 +1629,7 @@ var __spreadValues$1 = (a, b) => {
1647
1629
  };
1648
1630
  const getDb = (adapter) => pqb.createDb({ adapter });
1649
1631
  const migrateOrRollback = async (options, config, args, up) => {
1650
- var _a, _b, _c, _d, _e;
1632
+ var _a, _b, _c, _d, _e, _f;
1651
1633
  config = __spreadValues$1({}, config);
1652
1634
  const files = await getMigrationFiles(config, up);
1653
1635
  let count = up ? Infinity : 1;
@@ -1698,6 +1680,14 @@ const migrateOrRollback = async (options, config, args, up) => {
1698
1680
  await ((_e = config.afterRollback) == null ? void 0 : _e.call(config, db != null ? db : db = getDb(adapter)));
1699
1681
  }
1700
1682
  } finally {
1683
+ await ((_f = config.appCodeUpdater) == null ? void 0 : _f.afterAll({
1684
+ options: opts,
1685
+ basePath: config.basePath,
1686
+ cache: appCodeUpdaterCache,
1687
+ logger: config.logger,
1688
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1689
+ baseTable: config.baseTable
1690
+ }));
1701
1691
  await adapter.close();
1702
1692
  }
1703
1693
  delete config.appCodeUpdater;
@@ -1736,12 +1726,14 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
1736
1726
  return db2.migratedAsts;
1737
1727
  });
1738
1728
  for (const ast of asts) {
1739
- await ((_a = config.appCodeUpdater) == null ? void 0 : _a.call(config, {
1729
+ await ((_a = config.appCodeUpdater) == null ? void 0 : _a.process({
1740
1730
  ast,
1741
1731
  options,
1742
1732
  basePath: config.basePath,
1743
1733
  cache: appCodeUpdaterCache,
1744
- logger: config.logger
1734
+ logger: config.logger,
1735
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1736
+ baseTable: config.baseTable
1745
1737
  }));
1746
1738
  }
1747
1739
  };
@@ -2987,7 +2979,7 @@ const createView = (ast) => {
2987
2979
  };
2988
2980
 
2989
2981
  const pullDbStructure = async (options, config) => {
2990
- var _a, _b, _c;
2982
+ var _a, _b, _c, _d;
2991
2983
  const adapter = new pqb.Adapter(options);
2992
2984
  const currentSchema = adapter.schema || "public";
2993
2985
  const db = new DbStructure(adapter);
@@ -3010,19 +3002,29 @@ const pullDbStructure = async (options, config) => {
3010
3002
  await saveMigratedVersion(silentQueries, version, config);
3011
3003
  const cache = {};
3012
3004
  for (const item of ast) {
3013
- await ((_a = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _a.call(config, {
3005
+ await ((_a = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _a.process({
3014
3006
  ast: item,
3015
3007
  options,
3016
3008
  basePath: config.basePath,
3017
3009
  cache,
3018
- logger: config.logger
3010
+ logger: config.logger,
3011
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3012
+ baseTable: config.baseTable
3019
3013
  }));
3020
3014
  }
3015
+ await ((_b = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _b.afterAll({
3016
+ options,
3017
+ basePath: config.basePath,
3018
+ cache,
3019
+ logger: config.logger,
3020
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3021
+ baseTable: config.baseTable
3022
+ }));
3021
3023
  const unsupportedEntries = Object.entries(ctx.unsupportedTypes);
3022
3024
  const len = unsupportedEntries.length;
3023
3025
  if (len) {
3024
3026
  let count = 0;
3025
- (_b = config.logger) == null ? void 0 : _b.warn(
3027
+ (_c = config.logger) == null ? void 0 : _c.warn(
3026
3028
  `Found unsupported types:
3027
3029
  ${unsupportedEntries.map(([type, columns]) => {
3028
3030
  count += columns.length;
@@ -3031,7 +3033,7 @@ ${unsupportedEntries.map(([type, columns]) => {
3031
3033
  Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count > 1 ? "s" : ""} to treat ${count > 1 ? "them" : "it"} as other column type`
3032
3034
  );
3033
3035
  }
3034
- (_c = config.logger) == null ? void 0 : _c.log("Database pulled successfully");
3036
+ (_d = config.logger) == null ? void 0 : _d.log("Database pulled successfully");
3035
3037
  adapter.close();
3036
3038
  };
3037
3039