rake-db 2.7.2 → 2.8.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.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>;
@@ -293,6 +299,10 @@ type AppCodeUpdater = (params: {
293
299
  basePath: string;
294
300
  cache: object;
295
301
  logger: QueryLogOptions['logger'];
302
+ baseTable: {
303
+ filePath: string;
304
+ name: string;
305
+ };
296
306
  }) => Promise<void>;
297
307
 
298
308
  declare const createDb: <CT extends ColumnTypesBase>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => 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;
@@ -1741,7 +1723,9 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
1741
1723
  options,
1742
1724
  basePath: config.basePath,
1743
1725
  cache: appCodeUpdaterCache,
1744
- logger: config.logger
1726
+ logger: config.logger,
1727
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1728
+ baseTable: config.baseTable
1745
1729
  }));
1746
1730
  }
1747
1731
  };
@@ -3015,7 +2999,9 @@ const pullDbStructure = async (options, config) => {
3015
2999
  options,
3016
3000
  basePath: config.basePath,
3017
3001
  cache,
3018
- logger: config.logger
3002
+ logger: config.logger,
3003
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3004
+ baseTable: config.baseTable
3019
3005
  }));
3020
3006
  }
3021
3007
  const unsupportedEntries = Object.entries(ctx.unsupportedTypes);