@sqb/migrator 4.5.5 → 4.6.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.
@@ -2,15 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DbMigrator = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const fs_1 = tslib_1.__importDefault(require("fs"));
6
- const path_1 = tslib_1.__importDefault(require("path"));
5
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
6
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
7
7
  const postgresql_client_1 = require("postgresql-client");
8
8
  const load_task_files_js_1 = require("./load-task-files.js");
9
9
  const ignoreEventListener = () => 0;
10
10
  class DbMigrator {
11
11
  async execute(options) {
12
12
  const { connection, schema } = options;
13
- const migrationPackage = DbMigrator.loadMigrationPackage(options.migrationPackage);
13
+ const migrationPackage = await DbMigrator.loadMigrationPackage(options.migrationPackage);
14
14
  const targetVersion = Math.min(options.targetVersion || Number.MAX_SAFE_INTEGER, migrationPackage.maxVersion);
15
15
  if (targetVersion && targetVersion < migrationPackage.minVersion)
16
16
  throw new Error(`Version mismatch. Target schema version (${targetVersion}) is lower than ` +
@@ -91,12 +91,12 @@ class DbMigrator {
91
91
  emit('finish');
92
92
  return true;
93
93
  }
94
- static loadMigrationPackage(pkg) {
95
- const migarr = typeof pkg.migrations === 'function' ? pkg.migrations() : pkg.migrations;
94
+ static async loadMigrationPackage(pkg) {
95
+ const migarr = typeof pkg.migrations === 'function' ? await pkg.migrations() : pkg.migrations;
96
96
  const migrations = [];
97
97
  for (const x of migarr) {
98
98
  if (typeof x === 'string')
99
- locateMigrations(migrations, x);
99
+ await locateMigrations(migrations, x);
100
100
  else
101
101
  migrations.push(x);
102
102
  }
@@ -190,26 +190,26 @@ insert into ${schema}.${infTable} (status) values ('init');
190
190
  }
191
191
  }
192
192
  exports.DbMigrator = DbMigrator;
193
- function locateMigrations(trg, dir) {
193
+ async function locateMigrations(trg, dir) {
194
194
  for (const f of ['migration.ts', 'migration.js', 'migration.json']) {
195
- const filename = path_1.default.join(dir, f);
196
- if (fs_1.default.existsSync(filename)) {
197
- const x = require(filename);
198
- const fileDir = path_1.default.dirname(filename);
195
+ const filename = node_path_1.default.join(dir, f);
196
+ if (node_fs_1.default.existsSync(filename)) {
197
+ const x = await Promise.resolve(`${filename}`).then(s => tslib_1.__importStar(require(s)));
198
+ const fileDir = node_path_1.default.dirname(filename);
199
199
  const obj = x.default || x;
200
200
  if (obj.version && obj.tasks) {
201
201
  const migration = { ...obj };
202
- migration.tasks = obj.tasks.map(k => (typeof k === 'string' ? path_1.default.resolve(fileDir, k) : k));
202
+ migration.tasks = obj.tasks.map(k => (typeof k === 'string' ? node_path_1.default.resolve(fileDir, k) : k));
203
203
  trg.push(migration);
204
204
  }
205
205
  return;
206
206
  }
207
207
  }
208
- const files = fs_1.default.readdirSync(dir);
208
+ const files = node_fs_1.default.readdirSync(dir);
209
209
  for (const f of files) {
210
- const dirname = path_1.default.join(dir, f);
211
- if (fs_1.default.statSync(dirname).isDirectory()) {
212
- locateMigrations(trg, dirname);
210
+ const dirname = node_path_1.default.join(dir, f);
211
+ if (node_fs_1.default.statSync(dirname).isDirectory()) {
212
+ await locateMigrations(trg, dirname);
213
213
  }
214
214
  }
215
215
  }
@@ -16,7 +16,7 @@ interface LoadedMigrationPackage {
16
16
  }
17
17
  export declare class DbMigrator {
18
18
  execute(options: MigrationExecuteOptions): Promise<boolean>;
19
- static loadMigrationPackage(pkg: MigrationPackage): LoadedMigrationPackage;
19
+ static loadMigrationPackage(pkg: MigrationPackage): Promise<LoadedMigrationPackage>;
20
20
  private _lockSchema;
21
21
  private _unlockSchema;
22
22
  private _backup;
@@ -1,12 +1,12 @@
1
- import fs from 'fs';
2
- import path from 'path';
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
3
  import { stringifyValueForSQL } from 'postgresql-client';
4
4
  import { loadTaskFiles } from './load-task-files.js';
5
5
  const ignoreEventListener = () => 0;
6
6
  export class DbMigrator {
7
7
  async execute(options) {
8
8
  const { connection, schema } = options;
9
- const migrationPackage = DbMigrator.loadMigrationPackage(options.migrationPackage);
9
+ const migrationPackage = await DbMigrator.loadMigrationPackage(options.migrationPackage);
10
10
  const targetVersion = Math.min(options.targetVersion || Number.MAX_SAFE_INTEGER, migrationPackage.maxVersion);
11
11
  if (targetVersion && targetVersion < migrationPackage.minVersion)
12
12
  throw new Error(`Version mismatch. Target schema version (${targetVersion}) is lower than ` +
@@ -87,12 +87,12 @@ export class DbMigrator {
87
87
  emit('finish');
88
88
  return true;
89
89
  }
90
- static loadMigrationPackage(pkg) {
91
- const migarr = typeof pkg.migrations === 'function' ? pkg.migrations() : pkg.migrations;
90
+ static async loadMigrationPackage(pkg) {
91
+ const migarr = typeof pkg.migrations === 'function' ? await pkg.migrations() : pkg.migrations;
92
92
  const migrations = [];
93
93
  for (const x of migarr) {
94
94
  if (typeof x === 'string')
95
- locateMigrations(migrations, x);
95
+ await locateMigrations(migrations, x);
96
96
  else
97
97
  migrations.push(x);
98
98
  }
@@ -185,11 +185,11 @@ insert into ${schema}.${infTable} (status) values ('init');
185
185
  return sql;
186
186
  }
187
187
  }
188
- function locateMigrations(trg, dir) {
188
+ async function locateMigrations(trg, dir) {
189
189
  for (const f of ['migration.ts', 'migration.js', 'migration.json']) {
190
190
  const filename = path.join(dir, f);
191
191
  if (fs.existsSync(filename)) {
192
- const x = require(filename);
192
+ const x = await import(filename);
193
193
  const fileDir = path.dirname(filename);
194
194
  const obj = x.default || x;
195
195
  if (obj.version && obj.tasks) {
@@ -204,7 +204,7 @@ function locateMigrations(trg, dir) {
204
204
  for (const f of files) {
205
205
  const dirname = path.join(dir, f);
206
206
  if (fs.statSync(dirname).isDirectory()) {
207
- locateMigrations(trg, dirname);
207
+ await locateMigrations(trg, dirname);
208
208
  }
209
209
  }
210
210
  }
package/esm/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare type MigrationsThunk = () => (string | Migration)[];
1
+ export type MigrationsThunk = (string | Migration)[] | (() => (string | Migration)[]) | (() => Promise<(string | Migration)[]>);
2
2
  export interface MigrationPackage {
3
3
  description: string;
4
4
  informationTableName?: string;
@@ -16,8 +16,8 @@ export interface Migration {
16
16
  version: number;
17
17
  tasks: string[] | MigrationTask[];
18
18
  }
19
- export declare type MigrationTaskFunction = (connection: any) => void | Promise<void>;
20
- export declare type MigrationTask = SqlScriptMigrationTask | CustomMigrationTask | InsertDataMigrationTask;
19
+ export type MigrationTaskFunction = (connection: any) => void | Promise<void>;
20
+ export type MigrationTask = SqlScriptMigrationTask | CustomMigrationTask | InsertDataMigrationTask;
21
21
  export interface BaseMigrationTask {
22
22
  title: string;
23
23
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/migrator",
3
3
  "description": "Database migrator for SQB",
4
- "version": "4.5.5",
4
+ "version": "4.6.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>",
@@ -29,8 +29,8 @@
29
29
  "clean:cover": "rimraf ../../coverage/migrator"
30
30
  },
31
31
  "peerDependencies": {
32
- "@sqb/builder": "^4.5.5",
33
- "@sqb/connect": "^4.5.5"
32
+ "@sqb/builder": "^4.6.0",
33
+ "@sqb/connect": "^4.6.0"
34
34
  },
35
35
  "type": "module",
36
36
  "types": "esm/index.d.ts",