@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.js
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
var __create = Object.create;
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
8
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
9
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
7
24
|
var __export = (target, all) => {
|
|
8
25
|
for (var name in all)
|
|
9
26
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -50,7 +67,10 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
67
|
var index_exports = {};
|
|
51
68
|
__export(index_exports, {
|
|
52
69
|
MigrationRunner: () => MySQLMigrationRunner,
|
|
53
|
-
MigrationRunnerFactory: () => MigrationRunnerFactory
|
|
70
|
+
MigrationRunnerFactory: () => MigrationRunnerFactory,
|
|
71
|
+
createSeedFactory: () => createSeedFactory,
|
|
72
|
+
loadMigrationConfig: () => loadMigrationConfig,
|
|
73
|
+
runSeedsWithRunner: () => runSeedsWithRunner
|
|
54
74
|
});
|
|
55
75
|
module.exports = __toCommonJS(index_exports);
|
|
56
76
|
|
|
@@ -341,26 +361,50 @@ var SqlMigrationBuilder = class {
|
|
|
341
361
|
|
|
342
362
|
// framework/MigrationDirectoryReader.ts
|
|
343
363
|
var MigrationDirectoryReader = class {
|
|
344
|
-
constructor(directory, read_strategy, sqlrunner) {
|
|
364
|
+
constructor(directory, read_strategy, sqlrunner, dialect = "sql") {
|
|
345
365
|
this.directory = directory;
|
|
346
366
|
this.read_strategy = read_strategy;
|
|
347
367
|
this.sqlrunner = sqlrunner;
|
|
368
|
+
this.dialect = dialect;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Resolves the appropriate file for a migration based on dialect.
|
|
372
|
+
* Priority: dialect-specific file > generic file
|
|
373
|
+
*/
|
|
374
|
+
resolveFile(baseName, direction) {
|
|
375
|
+
const dialectExt = this.dialect === "sql" ? "mysql" : "sqlite";
|
|
376
|
+
const dialectFile = import_path.default.join(this.directory, `${baseName}.${dialectExt}.${direction}.sql`);
|
|
377
|
+
if (import_fs.default.existsSync(dialectFile)) return dialectFile;
|
|
378
|
+
const genericFile = import_path.default.join(this.directory, `${baseName}.${direction}.sql`);
|
|
379
|
+
if (import_fs.default.existsSync(genericFile)) return genericFile;
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Checks if a file path is dialect-specific (contains .mysql. or .sqlite. in the name)
|
|
384
|
+
*/
|
|
385
|
+
isDialectSpecific(filePath) {
|
|
386
|
+
return /\.(mysql|sqlite)\.(up|down)\.sql$/i.test(filePath);
|
|
348
387
|
}
|
|
349
388
|
loadMigrations(table, connection) {
|
|
350
389
|
import_fs.default.existsSync(this.directory) || import_fs.default.mkdirSync(this.directory);
|
|
351
390
|
const dir_content = import_fs.default.readdirSync(this.directory, { withFileTypes: true }).filter((file) => file.isFile()).map((file) => file.name);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
file
|
|
358
|
-
};
|
|
359
|
-
}).sort();
|
|
391
|
+
const uniqueKeys = /* @__PURE__ */ new Set();
|
|
392
|
+
dir_content.forEach((file) => {
|
|
393
|
+
const key = file.replace(/(?:\.(mysql|sqlite))?\.(up|down)\.(sql|js)/i, "").toLowerCase();
|
|
394
|
+
uniqueKeys.add(key);
|
|
395
|
+
});
|
|
360
396
|
const migration_sorter = {};
|
|
361
|
-
|
|
362
|
-
const builder =
|
|
363
|
-
this.
|
|
397
|
+
uniqueKeys.forEach((migration_key) => {
|
|
398
|
+
const builder = new SqlMigrationBuilder(migration_key);
|
|
399
|
+
const upFile = this.resolveFile(migration_key, "up");
|
|
400
|
+
const downFile = this.resolveFile(migration_key, "down");
|
|
401
|
+
if (upFile) {
|
|
402
|
+
this.loadMigration(builder, upFile);
|
|
403
|
+
}
|
|
404
|
+
if (downFile) {
|
|
405
|
+
this.loadMigration(builder, downFile);
|
|
406
|
+
}
|
|
407
|
+
migration_sorter[migration_key] = builder;
|
|
364
408
|
});
|
|
365
409
|
const keys = Object.keys(migration_sorter);
|
|
366
410
|
keys.sort();
|
|
@@ -387,12 +431,16 @@ var MigrationDirectoryReader = class {
|
|
|
387
431
|
}
|
|
388
432
|
sql_up(file) {
|
|
389
433
|
let content = import_fs.default.readFileSync(file).toString();
|
|
390
|
-
|
|
434
|
+
if (!this.isDialectSpecific(file)) {
|
|
435
|
+
content = this.read_strategy(content);
|
|
436
|
+
}
|
|
391
437
|
return content.trim();
|
|
392
438
|
}
|
|
393
439
|
sql_down(file) {
|
|
394
440
|
let content = import_fs.default.readFileSync(file).toString();
|
|
395
|
-
|
|
441
|
+
if (!this.isDialectSpecific(file)) {
|
|
442
|
+
content = this.read_strategy(content);
|
|
443
|
+
}
|
|
396
444
|
return content.trim();
|
|
397
445
|
}
|
|
398
446
|
};
|
|
@@ -524,6 +572,9 @@ var BaseSQLRunner = class {
|
|
|
524
572
|
});
|
|
525
573
|
}
|
|
526
574
|
};
|
|
575
|
+
function isPromiseLike(value) {
|
|
576
|
+
return !!value && typeof value.then === "function";
|
|
577
|
+
}
|
|
527
578
|
var SQLRunner = class extends BaseSQLRunner {
|
|
528
579
|
constructor(connection) {
|
|
529
580
|
super();
|
|
@@ -553,14 +604,85 @@ var SQLiteRunner = class extends BaseSQLRunner {
|
|
|
553
604
|
super();
|
|
554
605
|
this.connection = connection;
|
|
555
606
|
}
|
|
607
|
+
prepareStatement(sql) {
|
|
608
|
+
return __async(this, null, function* () {
|
|
609
|
+
if (typeof this.connection.prepare !== "function") {
|
|
610
|
+
throw new Error("SQLite connection does not support prepare()");
|
|
611
|
+
}
|
|
612
|
+
const stmt = this.connection.prepare(sql);
|
|
613
|
+
return isPromiseLike(stmt) ? yield stmt : stmt;
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
finalizeStatement(stmt) {
|
|
617
|
+
return __async(this, null, function* () {
|
|
618
|
+
if (!stmt || typeof stmt.finalize !== "function") return;
|
|
619
|
+
const result = stmt.finalize();
|
|
620
|
+
if (isPromiseLike(result)) {
|
|
621
|
+
yield result;
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
statementAll(stmt, params) {
|
|
626
|
+
return __async(this, null, function* () {
|
|
627
|
+
if (typeof stmt.all !== "function") {
|
|
628
|
+
throw new Error("SQLite statement does not support all()");
|
|
629
|
+
}
|
|
630
|
+
if (stmt.all.length >= 2) {
|
|
631
|
+
return yield new Promise((resolve, reject) => {
|
|
632
|
+
const callback = (err, rows) => {
|
|
633
|
+
if (err) return reject(err);
|
|
634
|
+
resolve(rows || []);
|
|
635
|
+
};
|
|
636
|
+
try {
|
|
637
|
+
if (params.length > 0) {
|
|
638
|
+
stmt.all(params, callback);
|
|
639
|
+
} else {
|
|
640
|
+
stmt.all(callback);
|
|
641
|
+
}
|
|
642
|
+
} catch (error) {
|
|
643
|
+
reject(error);
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
const result = stmt.all(...params);
|
|
648
|
+
return isPromiseLike(result) ? yield result : result;
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
statementRun(stmt, params) {
|
|
652
|
+
return __async(this, null, function* () {
|
|
653
|
+
if (typeof stmt.run !== "function") {
|
|
654
|
+
throw new Error("SQLite statement does not support run()");
|
|
655
|
+
}
|
|
656
|
+
if (stmt.run.length >= 2) {
|
|
657
|
+
return yield new Promise((resolve, reject) => {
|
|
658
|
+
const callback = function(err) {
|
|
659
|
+
var _a;
|
|
660
|
+
if (err) return reject(err);
|
|
661
|
+
resolve({ changes: (_a = this == null ? void 0 : this.changes) != null ? _a : 0, lastID: this == null ? void 0 : this.lastID });
|
|
662
|
+
};
|
|
663
|
+
try {
|
|
664
|
+
if (params.length > 0) {
|
|
665
|
+
stmt.run(params, callback);
|
|
666
|
+
} else {
|
|
667
|
+
stmt.run(callback);
|
|
668
|
+
}
|
|
669
|
+
} catch (error) {
|
|
670
|
+
reject(error);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
const result = stmt.run(...params);
|
|
675
|
+
return isPromiseLike(result) ? yield result : result;
|
|
676
|
+
});
|
|
677
|
+
}
|
|
556
678
|
_query(_0) {
|
|
557
679
|
return __async(this, arguments, function* (sql, params = []) {
|
|
558
|
-
const stmt = yield this.
|
|
680
|
+
const stmt = yield this.prepareStatement(sql);
|
|
559
681
|
try {
|
|
560
|
-
const rows = yield
|
|
682
|
+
const rows = yield this.statementAll(stmt, params);
|
|
561
683
|
return rows;
|
|
562
684
|
} finally {
|
|
563
|
-
yield
|
|
685
|
+
yield this.finalizeStatement(stmt);
|
|
564
686
|
}
|
|
565
687
|
});
|
|
566
688
|
}
|
|
@@ -574,12 +696,12 @@ ${sql}
|
|
|
574
696
|
throw err;
|
|
575
697
|
});
|
|
576
698
|
}
|
|
577
|
-
const stmt = yield this.
|
|
699
|
+
const stmt = yield this.prepareStatement(sql);
|
|
578
700
|
try {
|
|
579
|
-
const info = yield
|
|
701
|
+
const info = yield this.statementRun(stmt, params);
|
|
580
702
|
return info;
|
|
581
703
|
} finally {
|
|
582
|
-
yield
|
|
704
|
+
yield this.finalizeStatement(stmt);
|
|
583
705
|
}
|
|
584
706
|
});
|
|
585
707
|
}
|
|
@@ -595,13 +717,13 @@ ${sql}
|
|
|
595
717
|
).filter((s) => s.trim() !== "");
|
|
596
718
|
const infos = yield statements.reduce((prev, statement) => __async(this, null, function* () {
|
|
597
719
|
const infos2 = yield prev;
|
|
598
|
-
const stmt = yield this.
|
|
720
|
+
const stmt = yield this.prepareStatement(`${statement};`);
|
|
599
721
|
try {
|
|
600
|
-
const info = yield
|
|
722
|
+
const info = yield this.statementRun(stmt, params);
|
|
601
723
|
infos2.push(info);
|
|
602
724
|
return infos2;
|
|
603
725
|
} finally {
|
|
604
|
-
yield
|
|
726
|
+
yield this.finalizeStatement(stmt);
|
|
605
727
|
}
|
|
606
728
|
}), Promise.resolve([null])).then((infos2) => {
|
|
607
729
|
return infos2.filter((info) => info !== null);
|
|
@@ -634,7 +756,14 @@ function toError(error) {
|
|
|
634
756
|
if (error instanceof Error) return error;
|
|
635
757
|
return new Error(String(error));
|
|
636
758
|
}
|
|
759
|
+
function loadMigrationConfig(configFile) {
|
|
760
|
+
const reader = new FileMigrationConfigReader(configFile);
|
|
761
|
+
return reader.loadFile();
|
|
762
|
+
}
|
|
637
763
|
var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
764
|
+
static isSQLRunner(conn) {
|
|
765
|
+
return !!conn && typeof conn.query === "function" && typeof conn.execute === "function" && typeof conn.end === "function";
|
|
766
|
+
}
|
|
638
767
|
static create(configFile, conn) {
|
|
639
768
|
return __async(this, null, function* () {
|
|
640
769
|
const configReader = new FileMigrationConfigReader(configFile);
|
|
@@ -690,21 +819,27 @@ var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
|
690
819
|
create(config, conn) {
|
|
691
820
|
return __async(this, null, function* () {
|
|
692
821
|
let sqlrunner;
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
822
|
+
let driverConnection = conn;
|
|
823
|
+
if (_MigrationRunnerFactory.isSQLRunner(conn)) {
|
|
824
|
+
sqlrunner = conn;
|
|
825
|
+
driverConnection = null;
|
|
826
|
+
} else {
|
|
827
|
+
switch (config.database) {
|
|
828
|
+
case "sql":
|
|
829
|
+
sqlrunner = new SQLRunner(conn);
|
|
830
|
+
break;
|
|
831
|
+
case "sqlite":
|
|
832
|
+
sqlrunner = new SQLiteRunner(conn);
|
|
833
|
+
break;
|
|
834
|
+
default:
|
|
835
|
+
throw ConfigurationError.unknownDatabaseType(config.database);
|
|
836
|
+
}
|
|
702
837
|
}
|
|
703
838
|
const setup = new MigrationSetup(sqlrunner, config);
|
|
704
839
|
const read_strategy = this.getReadStategy(config);
|
|
705
|
-
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner);
|
|
840
|
+
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner, config.database);
|
|
706
841
|
yield setup.setup();
|
|
707
|
-
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner,
|
|
842
|
+
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, driverConnection);
|
|
708
843
|
});
|
|
709
844
|
}
|
|
710
845
|
createEmpty(config) {
|
|
@@ -723,7 +858,7 @@ var MigrationRunnerFactory = class _MigrationRunnerFactory {
|
|
|
723
858
|
}
|
|
724
859
|
const setup = new MigrationSetup(sqlrunner, config);
|
|
725
860
|
const read_strategy = this.getReadStategy(config);
|
|
726
|
-
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner);
|
|
861
|
+
const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner, config.database);
|
|
727
862
|
return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, conn);
|
|
728
863
|
});
|
|
729
864
|
}
|
|
@@ -868,6 +1003,11 @@ var MySQLMigrationRunner = class {
|
|
|
868
1003
|
}
|
|
869
1004
|
});
|
|
870
1005
|
}
|
|
1006
|
+
query(sql, params) {
|
|
1007
|
+
return __async(this, null, function* () {
|
|
1008
|
+
return yield this.sqlrunner.query(sql, params);
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
871
1011
|
init(config_file) {
|
|
872
1012
|
return __async(this, null, function* () {
|
|
873
1013
|
try {
|
|
@@ -963,9 +1103,262 @@ var MigrationCreator = class {
|
|
|
963
1103
|
}
|
|
964
1104
|
}
|
|
965
1105
|
};
|
|
1106
|
+
|
|
1107
|
+
// framework/SeedRunner.ts
|
|
1108
|
+
var import_fs4 = __toESM(require("fs"));
|
|
1109
|
+
var import_path2 = __toESM(require("path"));
|
|
1110
|
+
var import_ajv = __toESM(require("ajv"));
|
|
1111
|
+
var import_ajv_formats = __toESM(require("ajv-formats"));
|
|
1112
|
+
function resolveAlias(name, aliasMap) {
|
|
1113
|
+
var _a;
|
|
1114
|
+
if (!aliasMap) return name;
|
|
1115
|
+
return (_a = aliasMap[name]) != null ? _a : name;
|
|
1116
|
+
}
|
|
1117
|
+
function walkForFile(rootDir, fileName) {
|
|
1118
|
+
if (!import_fs4.default.existsSync(rootDir)) return null;
|
|
1119
|
+
const entries = import_fs4.default.readdirSync(rootDir, { withFileTypes: true });
|
|
1120
|
+
for (const entry of entries) {
|
|
1121
|
+
const full = import_path2.default.join(rootDir, entry.name);
|
|
1122
|
+
if (entry.isDirectory()) {
|
|
1123
|
+
const found = walkForFile(full, fileName);
|
|
1124
|
+
if (found) return found;
|
|
1125
|
+
} else if (entry.isFile() && entry.name === fileName) {
|
|
1126
|
+
return full;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return null;
|
|
1130
|
+
}
|
|
1131
|
+
function resolveMigrationsDir(migrationConfig, options) {
|
|
1132
|
+
var _a;
|
|
1133
|
+
const fromOptions = options.migrationsDir;
|
|
1134
|
+
const fromConfig = (_a = migrationConfig.seeds) == null ? void 0 : _a.migrationsDir;
|
|
1135
|
+
const dir = fromOptions != null ? fromOptions : fromConfig;
|
|
1136
|
+
if (!dir) {
|
|
1137
|
+
throw new Error("Seed migrationsDir not configured. Set seeds.migrationsDir in proper.json or pass it explicitly.");
|
|
1138
|
+
}
|
|
1139
|
+
return dir;
|
|
1140
|
+
}
|
|
1141
|
+
function mergeSeedConfig(migrationConfig, options) {
|
|
1142
|
+
var _a, _b, _c, _d;
|
|
1143
|
+
const names = options.names && options.names.length ? options.names : ((_a = migrationConfig.seeds) == null ? void 0 : _a.list) && migrationConfig.seeds.list.length ? migrationConfig.seeds.list : [];
|
|
1144
|
+
if (!names.length) {
|
|
1145
|
+
throw new Error("No seed names provided and no seeds.list defined in proper config");
|
|
1146
|
+
}
|
|
1147
|
+
const migrationsDir = resolveMigrationsDir(migrationConfig, options);
|
|
1148
|
+
const dataDir = (_c = options.dataDir) != null ? _c : (_b = migrationConfig.seeds) == null ? void 0 : _b.dataDir;
|
|
1149
|
+
const validate = options.validate !== void 0 ? options.validate : true;
|
|
1150
|
+
const transactional = (_d = options.transactional) != null ? _d : "none";
|
|
1151
|
+
const finalOptions = __spreadProps(__spreadValues({}, options), {
|
|
1152
|
+
migrationsDir,
|
|
1153
|
+
dataDir,
|
|
1154
|
+
validate,
|
|
1155
|
+
transactional
|
|
1156
|
+
});
|
|
1157
|
+
return { names, finalOptions };
|
|
1158
|
+
}
|
|
1159
|
+
function resolveSqlPair(name, migrationsDir) {
|
|
1160
|
+
const up = walkForFile(migrationsDir, `${name}.up.sql`);
|
|
1161
|
+
const down = walkForFile(migrationsDir, `${name}.down.sql`);
|
|
1162
|
+
if (up && down) {
|
|
1163
|
+
return { upPath: up, downPath: down };
|
|
1164
|
+
}
|
|
1165
|
+
return null;
|
|
1166
|
+
}
|
|
1167
|
+
function resolveModule(name, migrationsDir) {
|
|
1168
|
+
const ts = walkForFile(migrationsDir, `${name}.ts`);
|
|
1169
|
+
if (ts) return { kind: "ts", modulePath: ts };
|
|
1170
|
+
const js = walkForFile(migrationsDir, `${name}.js`);
|
|
1171
|
+
if (js) return { kind: "js", modulePath: js };
|
|
1172
|
+
return null;
|
|
1173
|
+
}
|
|
1174
|
+
function resolveSeed(name, migrationConfig, options) {
|
|
1175
|
+
return __async(this, null, function* () {
|
|
1176
|
+
var _a, _b;
|
|
1177
|
+
const migrationsDir = resolveMigrationsDir(migrationConfig, options);
|
|
1178
|
+
const alias = resolveAlias(name, options.aliasMap);
|
|
1179
|
+
const sqlPair = resolveSqlPair(name, migrationsDir);
|
|
1180
|
+
if (sqlPair) {
|
|
1181
|
+
return {
|
|
1182
|
+
kind: "sql",
|
|
1183
|
+
name,
|
|
1184
|
+
alias,
|
|
1185
|
+
upPath: sqlPair.upPath,
|
|
1186
|
+
downPath: sqlPair.downPath,
|
|
1187
|
+
dataPath: null,
|
|
1188
|
+
schemaPath: null
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
const module2 = resolveModule(name, migrationsDir);
|
|
1192
|
+
if (!module2) {
|
|
1193
|
+
throw new Error(`Seed implementation not found for "${name}" under ${migrationsDir}`);
|
|
1194
|
+
}
|
|
1195
|
+
const dataDir = (_b = options.dataDir) != null ? _b : (_a = migrationConfig.seeds) == null ? void 0 : _a.dataDir;
|
|
1196
|
+
let dataPath = null;
|
|
1197
|
+
let schemaPath = null;
|
|
1198
|
+
if (dataDir) {
|
|
1199
|
+
dataPath = walkForFile(dataDir, `${alias}.json`);
|
|
1200
|
+
schemaPath = walkForFile(dataDir, `${alias}.schema.json`);
|
|
1201
|
+
}
|
|
1202
|
+
return {
|
|
1203
|
+
kind: module2.kind,
|
|
1204
|
+
name,
|
|
1205
|
+
alias,
|
|
1206
|
+
upPath: module2.modulePath,
|
|
1207
|
+
downPath: module2.modulePath,
|
|
1208
|
+
dataPath,
|
|
1209
|
+
schemaPath
|
|
1210
|
+
};
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
function loadJson(filePath) {
|
|
1214
|
+
return __async(this, null, function* () {
|
|
1215
|
+
const content = yield import_fs4.default.promises.readFile(filePath, "utf8");
|
|
1216
|
+
return JSON.parse(content);
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
function createValidator() {
|
|
1220
|
+
const ajv = new import_ajv.default({ allErrors: true, strict: false });
|
|
1221
|
+
(0, import_ajv_formats.default)(ajv);
|
|
1222
|
+
return ajv;
|
|
1223
|
+
}
|
|
1224
|
+
function validateData(schemaPath, data, validate, log) {
|
|
1225
|
+
return __async(this, null, function* () {
|
|
1226
|
+
if (!validate || !schemaPath) return;
|
|
1227
|
+
const content = yield import_fs4.default.promises.readFile(schemaPath, "utf8");
|
|
1228
|
+
const schema = JSON.parse(content);
|
|
1229
|
+
const ajv = createValidator();
|
|
1230
|
+
const validateFn = ajv.compile(schema);
|
|
1231
|
+
const ok = validateFn(data);
|
|
1232
|
+
if (!ok) {
|
|
1233
|
+
log == null ? void 0 : log(`Validation failed for seed data (${schemaPath})`);
|
|
1234
|
+
throw new Error(`Seed data validation failed: ${ajv.errorsText(validateFn.errors || [])}`);
|
|
1235
|
+
}
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
function runSqlSeed(runner, resolved, direction) {
|
|
1239
|
+
return __async(this, null, function* () {
|
|
1240
|
+
const sqlPath = direction === "up" ? resolved.upPath : resolved.downPath;
|
|
1241
|
+
const sql = yield import_fs4.default.promises.readFile(sqlPath, "utf8");
|
|
1242
|
+
yield runner.query(sql);
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
function runModuleSeed(runner, resolved, migrationConfig, options, direction) {
|
|
1246
|
+
return __async(this, null, function* () {
|
|
1247
|
+
const { log } = options;
|
|
1248
|
+
const module2 = yield import(import_path2.default.resolve(resolved.upPath));
|
|
1249
|
+
const handler = module2[direction];
|
|
1250
|
+
if (typeof handler !== "function") {
|
|
1251
|
+
throw new Error(`Seed module "${resolved.name}" does not export ${direction}()`);
|
|
1252
|
+
}
|
|
1253
|
+
let data = null;
|
|
1254
|
+
if (options.preloadedData && Object.prototype.hasOwnProperty.call(options.preloadedData, resolved.name)) {
|
|
1255
|
+
data = options.preloadedData[resolved.name];
|
|
1256
|
+
} else if (resolved.dataPath) {
|
|
1257
|
+
data = yield loadJson(resolved.dataPath);
|
|
1258
|
+
}
|
|
1259
|
+
yield validateData(resolved.schemaPath, data, options.validate, log);
|
|
1260
|
+
const ctx = {
|
|
1261
|
+
data,
|
|
1262
|
+
log,
|
|
1263
|
+
dialect: migrationConfig.database || "sql"
|
|
1264
|
+
};
|
|
1265
|
+
yield handler(runner, ctx);
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
function runSingleSeed(runner, migrationConfig, name, options, direction) {
|
|
1269
|
+
return __async(this, null, function* () {
|
|
1270
|
+
const resolved = yield resolveSeed(name, migrationConfig, options);
|
|
1271
|
+
if (resolved.kind === "sql") {
|
|
1272
|
+
yield runSqlSeed(runner, resolved, direction);
|
|
1273
|
+
} else {
|
|
1274
|
+
yield runModuleSeed(runner, resolved, migrationConfig, options, direction);
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
function withTransactionalMode(runner, migrationConfig, names, options, direction) {
|
|
1279
|
+
return __async(this, null, function* () {
|
|
1280
|
+
const mode = options.transactional;
|
|
1281
|
+
if (mode === "runner") {
|
|
1282
|
+
yield runner.query("BEGIN");
|
|
1283
|
+
try {
|
|
1284
|
+
for (const name of names) {
|
|
1285
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1286
|
+
}
|
|
1287
|
+
yield runner.query("COMMIT");
|
|
1288
|
+
} catch (err) {
|
|
1289
|
+
try {
|
|
1290
|
+
yield runner.query("ROLLBACK");
|
|
1291
|
+
} catch (e) {
|
|
1292
|
+
}
|
|
1293
|
+
throw err;
|
|
1294
|
+
}
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (mode === "seed") {
|
|
1298
|
+
for (const name of names) {
|
|
1299
|
+
yield runner.query("BEGIN");
|
|
1300
|
+
try {
|
|
1301
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1302
|
+
yield runner.query("COMMIT");
|
|
1303
|
+
} catch (err) {
|
|
1304
|
+
try {
|
|
1305
|
+
yield runner.query("ROLLBACK");
|
|
1306
|
+
} catch (e) {
|
|
1307
|
+
}
|
|
1308
|
+
throw err;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
for (const name of names) {
|
|
1314
|
+
yield runSingleSeed(runner, migrationConfig, name, options, direction);
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
function runSeedsWithRunner(runner, migrationConfig, direction, options) {
|
|
1319
|
+
return __async(this, null, function* () {
|
|
1320
|
+
const { names, finalOptions } = mergeSeedConfig(migrationConfig, options);
|
|
1321
|
+
yield withTransactionalMode(runner, migrationConfig, names, finalOptions, direction);
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
function createSeedFactory(options) {
|
|
1325
|
+
var _a;
|
|
1326
|
+
const configFile = (_a = options.configFile) != null ? _a : "proper.json";
|
|
1327
|
+
return {
|
|
1328
|
+
up(names) {
|
|
1329
|
+
return __async(this, null, function* () {
|
|
1330
|
+
const runner = yield MigrationRunnerFactory.create(configFile);
|
|
1331
|
+
const migrationConfig = loadMigrationConfig(configFile);
|
|
1332
|
+
try {
|
|
1333
|
+
yield runSeedsWithRunner(runner, migrationConfig, "up", __spreadProps(__spreadValues({}, options), {
|
|
1334
|
+
names: names && names.length ? names : options.names
|
|
1335
|
+
}));
|
|
1336
|
+
} finally {
|
|
1337
|
+
yield runner.close();
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
},
|
|
1341
|
+
down(names) {
|
|
1342
|
+
return __async(this, null, function* () {
|
|
1343
|
+
const runner = yield MigrationRunnerFactory.create(configFile);
|
|
1344
|
+
const migrationConfig = loadMigrationConfig(configFile);
|
|
1345
|
+
try {
|
|
1346
|
+
yield runSeedsWithRunner(runner, migrationConfig, "down", __spreadProps(__spreadValues({}, options), {
|
|
1347
|
+
names: names && names.length ? names : options.names
|
|
1348
|
+
}));
|
|
1349
|
+
} finally {
|
|
1350
|
+
yield runner.close();
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
966
1356
|
// Annotate the CommonJS export names for ESM import in node:
|
|
967
1357
|
0 && (module.exports = {
|
|
968
1358
|
MigrationRunner,
|
|
969
|
-
MigrationRunnerFactory
|
|
1359
|
+
MigrationRunnerFactory,
|
|
1360
|
+
createSeedFactory,
|
|
1361
|
+
loadMigrationConfig,
|
|
1362
|
+
runSeedsWithRunner
|
|
970
1363
|
});
|
|
971
1364
|
//# sourceMappingURL=index.js.map
|