@noego/proper 0.0.3 → 0.0.5
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/bin/cli.d.mts +1 -0
- package/bin/cli.d.ts +1 -0
- package/bin/cli.js +426 -39
- package/bin/cli.js.map +1 -1
- package/bin/cli.mjs +433 -39
- package/bin/cli.mjs.map +1 -1
- package/bin/index.d.mts +179 -0
- package/bin/index.d.ts +179 -0
- package/bin/index.js +430 -37
- package/bin/index.js.map +1 -1
- package/bin/index.mjs +428 -36
- package/bin/index.mjs.map +1 -1
- package/lib/runner.d.mts +51 -0
- package/lib/runner.d.ts +51 -0
- package/lib/runner.js +257 -0
- package/lib/runner.js.map +1 -0
- package/lib/runner.mjs +230 -0
- package/lib/runner.mjs.map +1 -0
- package/package.json +12 -1
- package/readme.md +295 -39
package/bin/index.mjs
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1
20
|
var __async = (__this, __arguments, generator) => {
|
|
2
21
|
return new Promise((resolve, reject) => {
|
|
3
22
|
var fulfilled = (value) => {
|
|
@@ -306,26 +325,50 @@ var SqlMigrationBuilder = class {
|
|
|
306
325
|
|
|
307
326
|
// framework/MigrationDirectoryReader.ts
|
|
308
327
|
var MigrationDirectoryReader = class {
|
|
309
|
-
constructor(directory, read_strategy, sqlrunner) {
|
|
328
|
+
constructor(directory, read_strategy, sqlrunner, dialect = "sql") {
|
|
310
329
|
this.directory = directory;
|
|
311
330
|
this.read_strategy = read_strategy;
|
|
312
331
|
this.sqlrunner = sqlrunner;
|
|
332
|
+
this.dialect = dialect;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Resolves the appropriate file for a migration based on dialect.
|
|
336
|
+
* Priority: dialect-specific file > generic file
|
|
337
|
+
*/
|
|
338
|
+
resolveFile(baseName, direction) {
|
|
339
|
+
const dialectExt = this.dialect === "sql" ? "mysql" : "sqlite";
|
|
340
|
+
const dialectFile = path.join(this.directory, `${baseName}.${dialectExt}.${direction}.sql`);
|
|
341
|
+
if (fs.existsSync(dialectFile)) return dialectFile;
|
|
342
|
+
const genericFile = path.join(this.directory, `${baseName}.${direction}.sql`);
|
|
343
|
+
if (fs.existsSync(genericFile)) return genericFile;
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Checks if a file path is dialect-specific (contains .mysql. or .sqlite. in the name)
|
|
348
|
+
*/
|
|
349
|
+
isDialectSpecific(filePath) {
|
|
350
|
+
return /\.(mysql|sqlite)\.(up|down)\.sql$/i.test(filePath);
|
|
313
351
|
}
|
|
314
352
|
loadMigrations(table, connection) {
|
|
315
353
|
fs.existsSync(this.directory) || fs.mkdirSync(this.directory);
|
|
316
354
|
const dir_content = fs.readdirSync(this.directory, { withFileTypes: true }).filter((file) => file.isFile()).map((file) => file.name);
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
file
|
|
323
|
-
};
|
|
324
|
-
}).sort();
|
|
355
|
+
const uniqueKeys = /* @__PURE__ */ new Set();
|
|
356
|
+
dir_content.forEach((file) => {
|
|
357
|
+
const key = file.replace(/(?:\.(mysql|sqlite))?\.(up|down)\.(sql|js)/i, "").toLowerCase();
|
|
358
|
+
uniqueKeys.add(key);
|
|
359
|
+
});
|
|
325
360
|
const migration_sorter = {};
|
|
326
|
-
|
|
327
|
-
const builder =
|
|
328
|
-
this.
|
|
361
|
+
uniqueKeys.forEach((migration_key) => {
|
|
362
|
+
const builder = new SqlMigrationBuilder(migration_key);
|
|
363
|
+
const upFile = this.resolveFile(migration_key, "up");
|
|
364
|
+
const downFile = this.resolveFile(migration_key, "down");
|
|
365
|
+
if (upFile) {
|
|
366
|
+
this.loadMigration(builder, upFile);
|
|
367
|
+
}
|
|
368
|
+
if (downFile) {
|
|
369
|
+
this.loadMigration(builder, downFile);
|
|
370
|
+
}
|
|
371
|
+
migration_sorter[migration_key] = builder;
|
|
329
372
|
});
|
|
330
373
|
const keys = Object.keys(migration_sorter);
|
|
331
374
|
keys.sort();
|
|
@@ -352,12 +395,16 @@ var MigrationDirectoryReader = class {
|
|
|
352
395
|
}
|
|
353
396
|
sql_up(file) {
|
|
354
397
|
let content = fs.readFileSync(file).toString();
|
|
355
|
-
|
|
398
|
+
if (!this.isDialectSpecific(file)) {
|
|
399
|
+
content = this.read_strategy(content);
|
|
400
|
+
}
|
|
356
401
|
return content.trim();
|
|
357
402
|
}
|
|
358
403
|
sql_down(file) {
|
|
359
404
|
let content = fs.readFileSync(file).toString();
|
|
360
|
-
|
|
405
|
+
if (!this.isDialectSpecific(file)) {
|
|
406
|
+
content = this.read_strategy(content);
|
|
407
|
+
}
|
|
361
408
|
return content.trim();
|
|
362
409
|
}
|
|
363
410
|
};
|
|
@@ -489,6 +536,9 @@ var BaseSQLRunner = class {
|
|
|
489
536
|
});
|
|
490
537
|
}
|
|
491
538
|
};
|
|
539
|
+
function isPromiseLike(value) {
|
|
540
|
+
return !!value && typeof value.then === "function";
|
|
541
|
+
}
|
|
492
542
|
var SQLRunner = class extends BaseSQLRunner {
|
|
493
543
|
constructor(connection) {
|
|
494
544
|
super();
|
|
@@ -518,14 +568,85 @@ var SQLiteRunner = class extends BaseSQLRunner {
|
|
|
518
568
|
super();
|
|
519
569
|
this.connection = connection;
|
|
520
570
|
}
|
|
571
|
+
prepareStatement(sql) {
|
|
572
|
+
return __async(this, null, function* () {
|
|
573
|
+
if (typeof this.connection.prepare !== "function") {
|
|
574
|
+
throw new Error("SQLite connection does not support prepare()");
|
|
575
|
+
}
|
|
576
|
+
const stmt = this.connection.prepare(sql);
|
|
577
|
+
return isPromiseLike(stmt) ? yield stmt : stmt;
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
finalizeStatement(stmt) {
|
|
581
|
+
return __async(this, null, function* () {
|
|
582
|
+
if (!stmt || typeof stmt.finalize !== "function") return;
|
|
583
|
+
const result = stmt.finalize();
|
|
584
|
+
if (isPromiseLike(result)) {
|
|
585
|
+
yield result;
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
statementAll(stmt, params) {
|
|
590
|
+
return __async(this, null, function* () {
|
|
591
|
+
if (typeof stmt.all !== "function") {
|
|
592
|
+
throw new Error("SQLite statement does not support all()");
|
|
593
|
+
}
|
|
594
|
+
if (stmt.all.length >= 2) {
|
|
595
|
+
return yield new Promise((resolve, reject) => {
|
|
596
|
+
const callback = (err, rows) => {
|
|
597
|
+
if (err) return reject(err);
|
|
598
|
+
resolve(rows || []);
|
|
599
|
+
};
|
|
600
|
+
try {
|
|
601
|
+
if (params.length > 0) {
|
|
602
|
+
stmt.all(params, callback);
|
|
603
|
+
} else {
|
|
604
|
+
stmt.all(callback);
|
|
605
|
+
}
|
|
606
|
+
} catch (error) {
|
|
607
|
+
reject(error);
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
const result = stmt.all(...params);
|
|
612
|
+
return isPromiseLike(result) ? yield result : result;
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
statementRun(stmt, params) {
|
|
616
|
+
return __async(this, null, function* () {
|
|
617
|
+
if (typeof stmt.run !== "function") {
|
|
618
|
+
throw new Error("SQLite statement does not support run()");
|
|
619
|
+
}
|
|
620
|
+
if (stmt.run.length >= 2) {
|
|
621
|
+
return yield new Promise((resolve, reject) => {
|
|
622
|
+
const callback = function(err) {
|
|
623
|
+
var _a;
|
|
624
|
+
if (err) return reject(err);
|
|
625
|
+
resolve({ changes: (_a = this == null ? void 0 : this.changes) != null ? _a : 0, lastID: this == null ? void 0 : this.lastID });
|
|
626
|
+
};
|
|
627
|
+
try {
|
|
628
|
+
if (params.length > 0) {
|
|
629
|
+
stmt.run(params, callback);
|
|
630
|
+
} else {
|
|
631
|
+
stmt.run(callback);
|
|
632
|
+
}
|
|
633
|
+
} catch (error) {
|
|
634
|
+
reject(error);
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
const result = stmt.run(...params);
|
|
639
|
+
return isPromiseLike(result) ? yield result : result;
|
|
640
|
+
});
|
|
641
|
+
}
|
|
521
642
|
_query(_0) {
|
|
522
643
|
return __async(this, arguments, function* (sql, params = []) {
|
|
523
|
-
const stmt = yield this.
|
|
644
|
+
const stmt = yield this.prepareStatement(sql);
|
|
524
645
|
try {
|
|
525
|
-
const rows = yield
|
|
646
|
+
const rows = yield this.statementAll(stmt, params);
|
|
526
647
|
return rows;
|
|
527
648
|
} finally {
|
|
528
|
-
yield
|
|
649
|
+
yield this.finalizeStatement(stmt);
|
|
529
650
|
}
|
|
530
651
|
});
|
|
531
652
|
}
|
|
@@ -539,12 +660,12 @@ ${sql}
|
|
|
539
660
|
throw err;
|
|
540
661
|
});
|
|
541
662
|
}
|
|
542
|
-
const stmt = yield this.
|
|
663
|
+
const stmt = yield this.prepareStatement(sql);
|
|
543
664
|
try {
|
|
544
|
-
const info = yield
|
|
665
|
+
const info = yield this.statementRun(stmt, params);
|
|
545
666
|
return info;
|
|
546
667
|
} finally {
|
|
547
|
-
yield
|
|
668
|
+
yield this.finalizeStatement(stmt);
|
|
548
669
|
}
|
|
549
670
|
});
|
|
550
671
|
}
|
|
@@ -560,13 +681,13 @@ ${sql}
|
|
|
560
681
|
).filter((s) => s.trim() !== "");
|
|
561
682
|
const infos = yield statements.reduce((prev, statement) => __async(this, null, function* () {
|
|
562
683
|
const infos2 = yield prev;
|
|
563
|
-
const stmt = yield this.
|
|
684
|
+
const stmt = yield this.prepareStatement(`${statement};`);
|
|
564
685
|
try {
|
|
565
|
-
const info = yield
|
|
686
|
+
const info = yield this.statementRun(stmt, params);
|
|
566
687
|
infos2.push(info);
|
|
567
688
|
return infos2;
|
|
568
689
|
} finally {
|
|
569
|
-
yield
|
|
690
|
+
yield this.finalizeStatement(stmt);
|
|
570
691
|
}
|
|
571
692
|
}), Promise.resolve([null])).then((infos2) => {
|
|
572
693
|
return infos2.filter((info) => info !== null);
|
|
@@ -599,7 +720,14 @@ function toError(error) {
|
|
|
599
720
|
if (error instanceof Error) return error;
|
|
600
721
|
return new Error(String(error));
|
|
601
722
|
}
|
|
723
|
+
function loadMigrationConfig(configFile) {
|
|
724
|
+
const reader = new FileMigrationConfigReader(configFile);
|
|
725
|
+
return reader.loadFile();
|
|
726
|
+
}
|
|
602
727
|
var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
728
|
+
static isSQLRunner(conn) {
|
|
729
|
+
return !!conn && typeof conn.query === "function" && typeof conn.execute === "function" && typeof conn.end === "function";
|
|
730
|
+
}
|
|
603
731
|
static create(configFile, conn) {
|
|
604
732
|
return __async(this, null, function* () {
|
|
605
733
|
const configReader = new FileMigrationConfigReader(configFile);
|
|
@@ -655,21 +783,27 @@ var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
|
655
783
|
create(config, conn) {
|
|
656
784
|
return __async(this, null, function* () {
|
|
657
785
|
let sqlrunner;
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
786
|
+
let driverConnection = conn;
|
|
787
|
+
if (_MigrationRunnerFactory.isSQLRunner(conn)) {
|
|
788
|
+
sqlrunner = conn;
|
|
789
|
+
driverConnection = null;
|
|
790
|
+
} else {
|
|
791
|
+
switch (config.database) {
|
|
792
|
+
case "sql":
|
|
793
|
+
sqlrunner = new SQLRunner(conn);
|
|
794
|
+
break;
|
|
795
|
+
case "sqlite":
|
|
796
|
+
sqlrunner = new SQLiteRunner(conn);
|
|
797
|
+
break;
|
|
798
|
+
default:
|
|
799
|
+
throw ConfigurationError.unknownDatabaseType(config.database);
|
|
800
|
+
}
|
|
667
801
|
}
|
|
668
802
|
const setup = new MigrationSetup(sqlrunner, config);
|
|
669
803
|
const read_strategy = this.getReadStategy(config);
|
|
670
|
-
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner);
|
|
804
|
+
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner, config.database);
|
|
671
805
|
yield setup.setup();
|
|
672
|
-
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner,
|
|
806
|
+
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, driverConnection);
|
|
673
807
|
});
|
|
674
808
|
}
|
|
675
809
|
createEmpty(config) {
|
|
@@ -688,7 +822,7 @@ var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
|
688
822
|
}
|
|
689
823
|
const setup = new MigrationSetup(sqlrunner, config);
|
|
690
824
|
const read_strategy = this.getReadStategy(config);
|
|
691
|
-
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner);
|
|
825
|
+
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner, config.database);
|
|
692
826
|
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, conn);
|
|
693
827
|
});
|
|
694
828
|
}
|
|
@@ -833,6 +967,11 @@ var MySQLMigrationRunner = class {
|
|
|
833
967
|
}
|
|
834
968
|
});
|
|
835
969
|
}
|
|
970
|
+
query(sql, params) {
|
|
971
|
+
return __async(this, null, function* () {
|
|
972
|
+
return yield this.sqlrunner.query(sql, params);
|
|
973
|
+
});
|
|
974
|
+
}
|
|
836
975
|
init(config_file) {
|
|
837
976
|
return __async(this, null, function* () {
|
|
838
977
|
try {
|
|
@@ -928,8 +1067,261 @@ var MigrationCreator = class {
|
|
|
928
1067
|
}
|
|
929
1068
|
}
|
|
930
1069
|
};
|
|
1070
|
+
|
|
1071
|
+
// framework/SeedRunner.ts
|
|
1072
|
+
import fs4 from "fs";
|
|
1073
|
+
import path2 from "path";
|
|
1074
|
+
import Ajv from "ajv";
|
|
1075
|
+
import addFormats from "ajv-formats";
|
|
1076
|
+
function resolveAlias(name, aliasMap) {
|
|
1077
|
+
var _a;
|
|
1078
|
+
if (!aliasMap) return name;
|
|
1079
|
+
return (_a = aliasMap[name]) != null ? _a : name;
|
|
1080
|
+
}
|
|
1081
|
+
function walkForFile(rootDir, fileName) {
|
|
1082
|
+
if (!fs4.existsSync(rootDir)) return null;
|
|
1083
|
+
const entries = fs4.readdirSync(rootDir, { withFileTypes: true });
|
|
1084
|
+
for (const entry of entries) {
|
|
1085
|
+
const full = path2.join(rootDir, entry.name);
|
|
1086
|
+
if (entry.isDirectory()) {
|
|
1087
|
+
const found = walkForFile(full, fileName);
|
|
1088
|
+
if (found) return found;
|
|
1089
|
+
} else if (entry.isFile() && entry.name === fileName) {
|
|
1090
|
+
return full;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
return null;
|
|
1094
|
+
}
|
|
1095
|
+
function resolveMigrationsDir(migrationConfig, options) {
|
|
1096
|
+
var _a;
|
|
1097
|
+
const fromOptions = options.migrationsDir;
|
|
1098
|
+
const fromConfig = (_a = migrationConfig.seeds) == null ? void 0 : _a.migrationsDir;
|
|
1099
|
+
const dir = fromOptions != null ? fromOptions : fromConfig;
|
|
1100
|
+
if (!dir) {
|
|
1101
|
+
throw new Error("Seed migrationsDir not configured. Set seeds.migrationsDir in proper.json or pass it explicitly.");
|
|
1102
|
+
}
|
|
1103
|
+
return dir;
|
|
1104
|
+
}
|
|
1105
|
+
function mergeSeedConfig(migrationConfig, options) {
|
|
1106
|
+
var _a, _b, _c, _d;
|
|
1107
|
+
const names = options.names && options.names.length ? options.names : ((_a = migrationConfig.seeds) == null ? void 0 : _a.list) && migrationConfig.seeds.list.length ? migrationConfig.seeds.list : [];
|
|
1108
|
+
if (!names.length) {
|
|
1109
|
+
throw new Error("No seed names provided and no seeds.list defined in proper config");
|
|
1110
|
+
}
|
|
1111
|
+
const migrationsDir = resolveMigrationsDir(migrationConfig, options);
|
|
1112
|
+
const dataDir = (_c = options.dataDir) != null ? _c : (_b = migrationConfig.seeds) == null ? void 0 : _b.dataDir;
|
|
1113
|
+
const validate = options.validate !== void 0 ? options.validate : true;
|
|
1114
|
+
const transactional = (_d = options.transactional) != null ? _d : "none";
|
|
1115
|
+
const finalOptions = __spreadProps(__spreadValues({}, options), {
|
|
1116
|
+
migrationsDir,
|
|
1117
|
+
dataDir,
|
|
1118
|
+
validate,
|
|
1119
|
+
transactional
|
|
1120
|
+
});
|
|
1121
|
+
return { names, finalOptions };
|
|
1122
|
+
}
|
|
1123
|
+
function resolveSqlPair(name, migrationsDir) {
|
|
1124
|
+
const up = walkForFile(migrationsDir, `${name}.up.sql`);
|
|
1125
|
+
const down = walkForFile(migrationsDir, `${name}.down.sql`);
|
|
1126
|
+
if (up && down) {
|
|
1127
|
+
return { upPath: up, downPath: down };
|
|
1128
|
+
}
|
|
1129
|
+
return null;
|
|
1130
|
+
}
|
|
1131
|
+
function resolveModule(name, migrationsDir) {
|
|
1132
|
+
const ts = walkForFile(migrationsDir, `${name}.ts`);
|
|
1133
|
+
if (ts) return { kind: "ts", modulePath: ts };
|
|
1134
|
+
const js = walkForFile(migrationsDir, `${name}.js`);
|
|
1135
|
+
if (js) return { kind: "js", modulePath: js };
|
|
1136
|
+
return null;
|
|
1137
|
+
}
|
|
1138
|
+
function resolveSeed(name, migrationConfig, options) {
|
|
1139
|
+
return __async(this, null, function* () {
|
|
1140
|
+
var _a, _b;
|
|
1141
|
+
const migrationsDir = resolveMigrationsDir(migrationConfig, options);
|
|
1142
|
+
const alias = resolveAlias(name, options.aliasMap);
|
|
1143
|
+
const sqlPair = resolveSqlPair(name, migrationsDir);
|
|
1144
|
+
if (sqlPair) {
|
|
1145
|
+
return {
|
|
1146
|
+
kind: "sql",
|
|
1147
|
+
name,
|
|
1148
|
+
alias,
|
|
1149
|
+
upPath: sqlPair.upPath,
|
|
1150
|
+
downPath: sqlPair.downPath,
|
|
1151
|
+
dataPath: null,
|
|
1152
|
+
schemaPath: null
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
const module = resolveModule(name, migrationsDir);
|
|
1156
|
+
if (!module) {
|
|
1157
|
+
throw new Error(`Seed implementation not found for "${name}" under ${migrationsDir}`);
|
|
1158
|
+
}
|
|
1159
|
+
const dataDir = (_b = options.dataDir) != null ? _b : (_a = migrationConfig.seeds) == null ? void 0 : _a.dataDir;
|
|
1160
|
+
let dataPath = null;
|
|
1161
|
+
let schemaPath = null;
|
|
1162
|
+
if (dataDir) {
|
|
1163
|
+
dataPath = walkForFile(dataDir, `${alias}.json`);
|
|
1164
|
+
schemaPath = walkForFile(dataDir, `${alias}.schema.json`);
|
|
1165
|
+
}
|
|
1166
|
+
return {
|
|
1167
|
+
kind: module.kind,
|
|
1168
|
+
name,
|
|
1169
|
+
alias,
|
|
1170
|
+
upPath: module.modulePath,
|
|
1171
|
+
downPath: module.modulePath,
|
|
1172
|
+
dataPath,
|
|
1173
|
+
schemaPath
|
|
1174
|
+
};
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
function loadJson(filePath) {
|
|
1178
|
+
return __async(this, null, function* () {
|
|
1179
|
+
const content = yield fs4.promises.readFile(filePath, "utf8");
|
|
1180
|
+
return JSON.parse(content);
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
function createValidator() {
|
|
1184
|
+
const ajv = new Ajv({ allErrors: true, strict: false });
|
|
1185
|
+
addFormats(ajv);
|
|
1186
|
+
return ajv;
|
|
1187
|
+
}
|
|
1188
|
+
function validateData(schemaPath, data, validate, log) {
|
|
1189
|
+
return __async(this, null, function* () {
|
|
1190
|
+
if (!validate || !schemaPath) return;
|
|
1191
|
+
const content = yield fs4.promises.readFile(schemaPath, "utf8");
|
|
1192
|
+
const schema = JSON.parse(content);
|
|
1193
|
+
const ajv = createValidator();
|
|
1194
|
+
const validateFn = ajv.compile(schema);
|
|
1195
|
+
const ok = validateFn(data);
|
|
1196
|
+
if (!ok) {
|
|
1197
|
+
log == null ? void 0 : log(`Validation failed for seed data (${schemaPath})`);
|
|
1198
|
+
throw new Error(`Seed data validation failed: ${ajv.errorsText(validateFn.errors || [])}`);
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
function runSqlSeed(runner, resolved, direction) {
|
|
1203
|
+
return __async(this, null, function* () {
|
|
1204
|
+
const sqlPath = direction === "up" ? resolved.upPath : resolved.downPath;
|
|
1205
|
+
const sql = yield fs4.promises.readFile(sqlPath, "utf8");
|
|
1206
|
+
yield runner.query(sql);
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
function runModuleSeed(runner, resolved, migrationConfig, options, direction) {
|
|
1210
|
+
return __async(this, null, function* () {
|
|
1211
|
+
const { log } = options;
|
|
1212
|
+
const module = yield import(path2.resolve(resolved.upPath));
|
|
1213
|
+
const handler = module[direction];
|
|
1214
|
+
if (typeof handler !== "function") {
|
|
1215
|
+
throw new Error(`Seed module "${resolved.name}" does not export ${direction}()`);
|
|
1216
|
+
}
|
|
1217
|
+
let data = null;
|
|
1218
|
+
if (options.preloadedData && Object.prototype.hasOwnProperty.call(options.preloadedData, resolved.name)) {
|
|
1219
|
+
data = options.preloadedData[resolved.name];
|
|
1220
|
+
} else if (resolved.dataPath) {
|
|
1221
|
+
data = yield loadJson(resolved.dataPath);
|
|
1222
|
+
}
|
|
1223
|
+
yield validateData(resolved.schemaPath, data, options.validate, log);
|
|
1224
|
+
const ctx = {
|
|
1225
|
+
data,
|
|
1226
|
+
log,
|
|
1227
|
+
dialect: migrationConfig.database || "sql"
|
|
1228
|
+
};
|
|
1229
|
+
yield handler(runner, ctx);
|
|
1230
|
+
});
|
|
1231
|
+
}
|
|
1232
|
+
function runSingleSeed(runner, migrationConfig, name, options, direction) {
|
|
1233
|
+
return __async(this, null, function* () {
|
|
1234
|
+
const resolved = yield resolveSeed(name, migrationConfig, options);
|
|
1235
|
+
if (resolved.kind === "sql") {
|
|
1236
|
+
yield runSqlSeed(runner, resolved, direction);
|
|
1237
|
+
} else {
|
|
1238
|
+
yield runModuleSeed(runner, resolved, migrationConfig, options, direction);
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
}
|
|
1242
|
+
function withTransactionalMode(runner, migrationConfig, names, options, direction) {
|
|
1243
|
+
return __async(this, null, function* () {
|
|
1244
|
+
const mode = options.transactional;
|
|
1245
|
+
if (mode === "runner") {
|
|
1246
|
+
yield runner.query("BEGIN");
|
|
1247
|
+
try {
|
|
1248
|
+
for (const name of names) {
|
|
1249
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1250
|
+
}
|
|
1251
|
+
yield runner.query("COMMIT");
|
|
1252
|
+
} catch (err) {
|
|
1253
|
+
try {
|
|
1254
|
+
yield runner.query("ROLLBACK");
|
|
1255
|
+
} catch (e) {
|
|
1256
|
+
}
|
|
1257
|
+
throw err;
|
|
1258
|
+
}
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
if (mode === "seed") {
|
|
1262
|
+
for (const name of names) {
|
|
1263
|
+
yield runner.query("BEGIN");
|
|
1264
|
+
try {
|
|
1265
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1266
|
+
yield runner.query("COMMIT");
|
|
1267
|
+
} catch (err) {
|
|
1268
|
+
try {
|
|
1269
|
+
yield runner.query("ROLLBACK");
|
|
1270
|
+
} catch (e) {
|
|
1271
|
+
}
|
|
1272
|
+
throw err;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
for (const name of names) {
|
|
1278
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1279
|
+
}
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
function runSeedsWithRunner(runner, migrationConfig, direction, options) {
|
|
1283
|
+
return __async(this, null, function* () {
|
|
1284
|
+
const { names, finalOptions } = mergeSeedConfig(migrationConfig, options);
|
|
1285
|
+
yield withTransactionalMode(runner, migrationConfig, names, finalOptions, direction);
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
function createSeedFactory(options) {
|
|
1289
|
+
var _a;
|
|
1290
|
+
const configFile = (_a = options.configFile) != null ? _a : "proper.json";
|
|
1291
|
+
return {
|
|
1292
|
+
up(names) {
|
|
1293
|
+
return __async(this, null, function* () {
|
|
1294
|
+
const runner = yield MigrationRunnerFactory.create(configFile);
|
|
1295
|
+
const migrationConfig = loadMigrationConfig(configFile);
|
|
1296
|
+
try {
|
|
1297
|
+
yield runSeedsWithRunner(runner, migrationConfig, "up", __spreadProps(__spreadValues({}, options), {
|
|
1298
|
+
names: names && names.length ? names : options.names
|
|
1299
|
+
}));
|
|
1300
|
+
} finally {
|
|
1301
|
+
yield runner.close();
|
|
1302
|
+
}
|
|
1303
|
+
});
|
|
1304
|
+
},
|
|
1305
|
+
down(names) {
|
|
1306
|
+
return __async(this, null, function* () {
|
|
1307
|
+
const runner = yield MigrationRunnerFactory.create(configFile);
|
|
1308
|
+
const migrationConfig = loadMigrationConfig(configFile);
|
|
1309
|
+
try {
|
|
1310
|
+
yield runSeedsWithRunner(runner, migrationConfig, "down", __spreadProps(__spreadValues({}, options), {
|
|
1311
|
+
names: names && names.length ? names : options.names
|
|
1312
|
+
}));
|
|
1313
|
+
} finally {
|
|
1314
|
+
yield runner.close();
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
931
1320
|
export {
|
|
932
1321
|
MySQLMigrationRunner as MigrationRunner,
|
|
933
|
-
MigrationRunnerFactory
|
|
1322
|
+
MigrationRunnerFactory,
|
|
1323
|
+
createSeedFactory,
|
|
1324
|
+
loadMigrationConfig,
|
|
1325
|
+
runSeedsWithRunner
|
|
934
1326
|
};
|
|
935
1327
|
//# sourceMappingURL=index.mjs.map
|