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