@sqb/migrator 4.15.0 → 4.16.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.
@@ -75,7 +75,7 @@ var MigrationPackage;
75
75
  }
76
76
  else if (['.json', '.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
77
77
  try {
78
- let json = ext === '.json' ? JSON.parse(await promises_1.default.readFile(filename, 'utf-8')) : await import(filename);
78
+ let json = ext === '.json' ? JSON.parse(await promises_1.default.readFile(filename, 'utf-8')) : await Promise.resolve(`${filename}`).then(s => tslib_1.__importStar(require(s)));
79
79
  if (typeof json !== 'object')
80
80
  continue;
81
81
  if (json.__esModule)
@@ -121,7 +121,7 @@ async function loadMigrations(baseDir, pattern) {
121
121
  continue;
122
122
  let json;
123
123
  if (['.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
124
- json = await import(filename);
124
+ json = await Promise.resolve(`${filename}`).then(s => tslib_1.__importStar(require(s)));
125
125
  if (json.__esModule)
126
126
  json = json.default;
127
127
  }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -1,20 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PgMigrationAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- const postgres_1 = require("@sqb/postgres");
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
- const postgrejs_1 = require("postgrejs");
8
- const migration_adapter_js_1 = require("../migration-adapter.js");
9
- const migration_package_js_1 = require("../migration-package.js");
10
- const types_js_1 = require("../types.js");
11
- const pgAdapter = new postgres_1.PgAdapter();
12
- class PgMigrationAdapter extends migration_adapter_js_1.MigrationAdapter {
1
+ import { PgAdapter } from '@sqb/postgres';
2
+ import path from 'path';
3
+ import { stringifyValueForSQL } from 'postgrejs';
4
+ import { MigrationAdapter } from '../migration-adapter.js';
5
+ import { isCustomMigrationTask, isInsertDataMigrationTask, isSqlScriptMigrationTask, } from '../migration-package.js';
6
+ import { MigrationStatus } from '../types.js';
7
+ const pgAdapter = new PgAdapter();
8
+ export class PgMigrationAdapter extends MigrationAdapter {
13
9
  constructor() {
14
10
  super(...arguments);
15
11
  this._infoSchema = 'public';
16
12
  this._version = 0;
17
- this._status = types_js_1.MigrationStatus.idle;
13
+ this._status = MigrationStatus.idle;
18
14
  this.defaultVariables = {
19
15
  tablespace: 'pg_default',
20
16
  schema: 'public',
@@ -89,7 +85,7 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
89
85
  });
90
86
  if (!(r && r.rows?.length)) {
91
87
  await connection.query(`insert into ${adapter.summaryTableFull} (package_name, status) values ($1, $2)`, {
92
- params: [adapter.packageName, types_js_1.MigrationStatus.idle],
88
+ params: [adapter.packageName, MigrationStatus.idle],
93
89
  });
94
90
  }
95
91
  await adapter.refresh();
@@ -152,7 +148,7 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
152
148
  ...this.defaultVariables,
153
149
  ...variables,
154
150
  };
155
- if ((0, migration_package_js_1.isSqlScriptMigrationTask)(task)) {
151
+ if (isSqlScriptMigrationTask(task)) {
156
152
  try {
157
153
  let script;
158
154
  if (typeof task.script === 'function') {
@@ -168,7 +164,7 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
168
164
  catch (e) {
169
165
  let msg = `Error in task "${task.title}"`;
170
166
  if (task.filename)
171
- msg += '\n at ' + path_1.default.relative(migrationPackage.baseDir, task.filename);
167
+ msg += '\n at ' + path.relative(migrationPackage.baseDir, task.filename);
172
168
  if (e.lineNr) {
173
169
  if (!task.filename)
174
170
  e.message += '\n at';
@@ -181,11 +177,11 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
181
177
  }
182
178
  return;
183
179
  }
184
- if ((0, migration_package_js_1.isCustomMigrationTask)(task)) {
180
+ if (isCustomMigrationTask(task)) {
185
181
  await task.fn(this._connection, this);
186
182
  return;
187
183
  }
188
- if ((0, migration_package_js_1.isInsertDataMigrationTask)(task)) {
184
+ if (isInsertDataMigrationTask(task)) {
189
185
  const tableName = this.replaceVariables(task.tableName, variables);
190
186
  const script = task.rows.map(row => this.rowToSql(tableName, row)).join('\n');
191
187
  await this._connection.execute(script);
@@ -208,10 +204,9 @@ CREATE TABLE IF NOT EXISTS ${adapter.eventTableFull}
208
204
  const keys = Object.keys(row);
209
205
  sql += `insert into ${tableName} (${keys}) values (`;
210
206
  for (let i = 0; i < keys.length; i++) {
211
- sql += (i ? ', ' : '') + (0, postgrejs_1.stringifyValueForSQL)(row[keys[i]]);
207
+ sql += (i ? ', ' : '') + stringifyValueForSQL(row[keys[i]]);
212
208
  }
213
209
  sql += ');\n';
214
210
  return sql;
215
211
  }
216
212
  }
217
- exports.PgMigrationAdapter = PgMigrationAdapter;
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DbMigrator = void 0;
4
- const strict_typed_events_1 = require("strict-typed-events");
5
- const pg_migration_adapter_js_1 = require("./adapters/pg-migration-adapter.js");
6
- const migration_adapter_js_1 = require("./migration-adapter.js");
7
- const migration_package_js_1 = require("./migration-package.js");
8
- const types_js_1 = require("./types.js");
9
- class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
1
+ import { AsyncEventEmitter } from 'strict-typed-events';
2
+ import { PgMigrationAdapter } from './adapters/pg-migration-adapter.js';
3
+ import { MigrationAdapter } from './migration-adapter.js';
4
+ import { MigrationPackage } from './migration-package.js';
5
+ import { MigrationStatus } from './types.js';
6
+ export class DbMigrator extends AsyncEventEmitter {
10
7
  async execute(options) {
11
8
  if (!options.connection.dialect)
12
9
  throw new TypeError(`You must provide connection.dialect`);
13
- const migrationPackage = await migration_package_js_1.MigrationPackage.load(options.migrationPackage);
10
+ const migrationPackage = await MigrationPackage.load(options.migrationPackage);
14
11
  let minVersion = migrationPackage.migrations.reduce((a, m) => Math.min(a, m.version), Number.MAX_SAFE_INTEGER);
15
12
  if (minVersion === Number.MAX_SAFE_INTEGER)
16
13
  minVersion = 0;
@@ -24,7 +21,7 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
24
21
  let migrationAdapter;
25
22
  switch (options.connection.dialect) {
26
23
  case 'postgres': {
27
- migrationAdapter = await pg_migration_adapter_js_1.PgMigrationAdapter.create({ ...options, migrationPackage });
24
+ migrationAdapter = await PgMigrationAdapter.create({ ...options, migrationPackage });
28
25
  break;
29
26
  }
30
27
  default:
@@ -61,9 +58,9 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
61
58
  for (let index = 0; index < migration.tasks.length; index++) {
62
59
  task = migration.tasks[index];
63
60
  await this.emitAsync('task-start', { migration, task, total, index });
64
- await migrationAdapter.update({ status: types_js_1.MigrationStatus.busy });
61
+ await migrationAdapter.update({ status: MigrationStatus.busy });
65
62
  await migrationAdapter.writeEvent({
66
- event: migration_adapter_js_1.MigrationAdapter.EventKind.started,
63
+ event: MigrationAdapter.EventKind.started,
67
64
  version: migration.version,
68
65
  title: task.title,
69
66
  filename: task.filename,
@@ -75,7 +72,7 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
75
72
  ...options.scriptVariables,
76
73
  });
77
74
  await migrationAdapter.writeEvent({
78
- event: migration_adapter_js_1.MigrationAdapter.EventKind.success,
75
+ event: MigrationAdapter.EventKind.success,
79
76
  version: migration.version,
80
77
  title: task.title,
81
78
  filename: task.filename,
@@ -84,7 +81,7 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
84
81
  }
85
82
  catch (e) {
86
83
  await migrationAdapter.writeEvent({
87
- event: migration_adapter_js_1.MigrationAdapter.EventKind.error,
84
+ event: MigrationAdapter.EventKind.error,
88
85
  version: migration.version,
89
86
  title: task.title,
90
87
  filename: task.filename,
@@ -101,7 +98,7 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
101
98
  }
102
99
  await this.emitAsync('task-finish', { migration, task, total, index });
103
100
  }
104
- await migrationAdapter.update({ version: migration.version, status: types_js_1.MigrationStatus.idle });
101
+ await migrationAdapter.update({ version: migration.version, status: MigrationStatus.idle });
105
102
  await this.emitAsync('migration-finish', {
106
103
  migration,
107
104
  total: migrations.length,
@@ -128,4 +125,3 @@ class DbMigrator extends strict_typed_events_1.AsyncEventEmitter {
128
125
  return true;
129
126
  }
130
127
  }
131
- exports.DbMigrator = DbMigrator;
package/esm/index.js CHANGED
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./db-migrator.js"), exports);
5
- tslib_1.__exportStar(require("./migration-adapter.js"), exports);
6
- tslib_1.__exportStar(require("./migration-package.js"), exports);
7
- tslib_1.__exportStar(require("./types.js"), exports);
1
+ export * from './db-migrator.js';
2
+ export * from './migration-adapter.js';
3
+ export * from './migration-package.js';
4
+ export * from './types.js';
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MigrationAdapter = void 0;
4
- class MigrationAdapter {
1
+ export class MigrationAdapter {
5
2
  replaceVariables(text, variables) {
6
3
  return text.replace(/(\$\((\w+)\))/g, (s, ...args) => variables[args[1]] || s);
7
4
  }
8
5
  }
9
- exports.MigrationAdapter = MigrationAdapter;
10
6
  (function (MigrationAdapter) {
11
7
  let EventKind;
12
8
  (function (EventKind) {
@@ -14,4 +10,4 @@ exports.MigrationAdapter = MigrationAdapter;
14
10
  EventKind["success"] = "success";
15
11
  EventKind["error"] = "error";
16
12
  })(EventKind = MigrationAdapter.EventKind || (MigrationAdapter.EventKind = {}));
17
- })(MigrationAdapter || (exports.MigrationAdapter = MigrationAdapter = {}));
13
+ })(MigrationAdapter || (MigrationAdapter = {}));
@@ -1,27 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MigrationPackage = void 0;
4
- exports.isSqlScriptMigrationTask = isSqlScriptMigrationTask;
5
- exports.isInsertDataMigrationTask = isInsertDataMigrationTask;
6
- exports.isCustomMigrationTask = isCustomMigrationTask;
7
- const tslib_1 = require("tslib");
8
- const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
9
- const promises_1 = tslib_1.__importDefault(require("fs/promises"));
10
- const path_1 = tslib_1.__importDefault(require("path"));
11
- const get_calling_filename_js_1 = require("./utils/get-calling-filename.js");
12
- function isSqlScriptMigrationTask(x) {
1
+ import glob from 'fast-glob';
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ import { getCallingFilename } from './utils/get-calling-filename.js';
5
+ export function isSqlScriptMigrationTask(x) {
13
6
  return typeof x === 'object' && (typeof x.script === 'string' || typeof x.script === 'function');
14
7
  }
15
- function isInsertDataMigrationTask(x) {
8
+ export function isInsertDataMigrationTask(x) {
16
9
  return typeof x === 'object' && typeof x.tableName === 'string' && Array.isArray(x.rows);
17
10
  }
18
- function isCustomMigrationTask(x) {
11
+ export function isCustomMigrationTask(x) {
19
12
  return typeof x === 'object' && typeof x.fn === 'function';
20
13
  }
21
- var MigrationPackage;
14
+ export var MigrationPackage;
22
15
  (function (MigrationPackage) {
23
16
  async function load(asyncConfig) {
24
- const baseDir = asyncConfig.baseDir || path_1.default.dirname((0, get_calling_filename_js_1.getCallingFilename)(1));
17
+ const baseDir = asyncConfig.baseDir || path.dirname(getCallingFilename(1));
25
18
  const out = {
26
19
  ...asyncConfig,
27
20
  baseDir,
@@ -55,27 +48,27 @@ var MigrationPackage;
55
48
  }
56
49
  else if (typeof t === 'string') {
57
50
  let pattern = t.replace(/\\/g, '/');
58
- pattern = path_1.default.resolve(path_1.default.join(baseDir, trgMigration.baseDir, pattern));
59
- const files = await (0, fast_glob_1.default)(pattern, {
51
+ pattern = path.resolve(path.join(baseDir, trgMigration.baseDir, pattern));
52
+ const files = await glob(pattern, {
60
53
  absolute: true,
61
54
  onlyFiles: true,
62
55
  });
63
56
  files.sort();
64
57
  for (const filename of files) {
65
- const ext = path_1.default.extname(filename).toLowerCase();
66
- if (!path_1.default.basename(filename, ext).endsWith('.task'))
58
+ const ext = path.extname(filename).toLowerCase();
59
+ if (!path.basename(filename, ext).endsWith('.task'))
67
60
  continue;
68
61
  if (ext === '.sql') {
69
- const script = await promises_1.default.readFile(filename, 'utf-8');
62
+ const script = await fs.readFile(filename, 'utf-8');
70
63
  trgMigration.tasks.push({
71
- title: path_1.default.basename(filename, ext),
64
+ title: path.basename(filename, ext),
72
65
  filename,
73
66
  script,
74
67
  });
75
68
  }
76
69
  else if (['.json', '.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
77
70
  try {
78
- let json = ext === '.json' ? JSON.parse(await promises_1.default.readFile(filename, 'utf-8')) : await import(filename);
71
+ let json = ext === '.json' ? JSON.parse(await fs.readFile(filename, 'utf-8')) : await import(filename);
79
72
  if (typeof json !== 'object')
80
73
  continue;
81
74
  if (json.__esModule)
@@ -111,13 +104,13 @@ var MigrationPackage;
111
104
  return out;
112
105
  }
113
106
  MigrationPackage.load = load;
114
- })(MigrationPackage || (exports.MigrationPackage = MigrationPackage = {}));
107
+ })(MigrationPackage || (MigrationPackage = {}));
115
108
  async function loadMigrations(baseDir, pattern) {
116
109
  const out = [];
117
- const files = await (0, fast_glob_1.default)(path_1.default.join(baseDir, pattern), { absolute: true, onlyFiles: true });
110
+ const files = await glob(path.join(baseDir, pattern), { absolute: true, onlyFiles: true });
118
111
  for (const filename of files) {
119
- const ext = path_1.default.extname(filename).toLowerCase();
120
- if (path_1.default.basename(filename, ext) !== 'migration')
112
+ const ext = path.extname(filename).toLowerCase();
113
+ if (path.basename(filename, ext) !== 'migration')
121
114
  continue;
122
115
  let json;
123
116
  if (['.js', '.ts', '.cjs', '.mjs'].includes(ext)) {
@@ -127,7 +120,7 @@ async function loadMigrations(baseDir, pattern) {
127
120
  }
128
121
  else if (ext === '.json') {
129
122
  try {
130
- json = JSON.parse(await promises_1.default.readFile(filename, 'utf-8'));
123
+ json = JSON.parse(await fs.readFile(filename, 'utf-8'));
131
124
  }
132
125
  catch (e) {
133
126
  e.message = `Error in ${filename}\n` + e.message;
@@ -135,7 +128,7 @@ async function loadMigrations(baseDir, pattern) {
135
128
  }
136
129
  }
137
130
  if (json && typeof json === 'object' && json.version && Array.isArray(json.tasks)) {
138
- json.baseDir = path_1.default.relative(baseDir, path_1.default.dirname(filename));
131
+ json.baseDir = path.relative(baseDir, path.dirname(filename));
139
132
  out.push(json);
140
133
  }
141
134
  }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/esm/types.js CHANGED
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MigrationStatus = void 0;
4
- var MigrationStatus;
1
+ export var MigrationStatus;
5
2
  (function (MigrationStatus) {
6
3
  MigrationStatus["idle"] = "idle";
7
4
  MigrationStatus["busy"] = "busy";
8
- })(MigrationStatus || (exports.MigrationStatus = MigrationStatus = {}));
5
+ })(MigrationStatus || (MigrationStatus = {}));
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCallingFilename = getCallingFilename;
4
1
  const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/;
5
- function getCallingFilename(position = 0) {
2
+ export function getCallingFilename(position = 0) {
6
3
  position++;
7
4
  if (position >= Error.stackTraceLimit)
8
5
  return '';
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@sqb/migrator",
3
3
  "description": "Database migrator for SQB",
4
- "version": "4.15.0",
4
+ "version": "4.16.1",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "scripts": {
8
8
  "compile": "tsc",
9
9
  "prebuild": "npm run lint && npm run clean",
10
10
  "build": "npm run build:cjs && npm run build:esm",
11
- "build:cjs": "tsc -b tsconfig-build-cjs.json",
12
- "build:esm": "tsc -b tsconfig-build-esm.json",
11
+ "build:cjs": "tsc -b tsconfig-build-cjs.json && cp ../../support/package.cjs.json ../../build/migrator/cjs/package.json",
12
+ "build:esm": "tsc -b tsconfig-build-esm.json && cp ../../support/package.esm.json ../../build/migrator/esm/package.json",
13
13
  "postbuild": "cp README.md package.json ../../LICENSE ../../build/migrator",
14
14
  "lint": "eslint . --max-warnings=0",
15
15
  "lint:fix": "eslint . --max-warnings=0 --fix",
@@ -23,27 +23,36 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "fast-glob": "^3.3.2",
26
- "strict-typed-events": "^2.5.0",
27
- "ts-gems": "^3.5.0"
26
+ "strict-typed-events": "^2.7.2",
27
+ "ts-gems": "^3.5.0",
28
+ "tslib": "^2.6.3"
28
29
  },
29
30
  "peerDependencies": {
30
- "@sqb/builder": "^4.15.0",
31
- "@sqb/connect": "^4.15.0",
32
- "@sqb/postgres": "^4.15.0"
31
+ "@sqb/builder": "^4.16.1",
32
+ "@sqb/connect": "^4.16.1",
33
+ "@sqb/postgres": "^4.16.1"
33
34
  },
34
35
  "devDependencies": {
35
- "postgrejs": "^2.16.0"
36
+ "postgrejs": "^2.18.1"
36
37
  },
37
- "main": "./cjs/index.js",
38
- "module": "./esm/index.js",
39
- "types": "./types/index.d.ts",
38
+ "type": "module",
40
39
  "exports": {
41
40
  ".": {
42
- "require": "./cjs/index.js",
43
- "import": "./esm/index.js",
44
- "types": "./types/index.d.ts"
45
- }
41
+ "import": {
42
+ "types": "./types/index.d.ts",
43
+ "default": "./esm/index.js"
44
+ },
45
+ "require": {
46
+ "types": "./types/index.d.ts",
47
+ "default": "./cjs/index.js"
48
+ },
49
+ "default": "./esm/index.js"
50
+ },
51
+ "./package.json": "./package.json"
46
52
  },
53
+ "main": "./cjs/index.js",
54
+ "module": "./esm/index.js",
55
+ "types": "./types/index.d.ts",
47
56
  "contributors": [
48
57
  "Eray Hanoglu <e.hanoglu@panates.com>",
49
58
  "Ilker Gurelli <i.gurelli@panates.com>"