@noego/proper 0.0.3 → 0.0.4

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/index.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ import mysql from 'mysql2/promise';
2
+
3
+ interface MigrationConfig {
4
+ config_file?: string;
5
+ migration_table: string;
6
+ migration_folder: string;
7
+ database: string;
8
+ sql?: {
9
+ host: string;
10
+ user: string;
11
+ database: string;
12
+ password?: string;
13
+ port?: number;
14
+ };
15
+ sqlite?: {
16
+ database: string;
17
+ };
18
+ seeds?: {
19
+ migrationsDir?: string;
20
+ dataDir?: string;
21
+ list?: string[];
22
+ };
23
+ }
24
+
25
+ interface MigrationStatus {
26
+ completed: boolean;
27
+ }
28
+
29
+ interface ISQLRunner {
30
+ query(sql: string, params?: any[]): Promise<any>;
31
+ execute(sql: string, params?: any[]): Promise<any>;
32
+ end(): Promise<void>;
33
+ }
34
+
35
+ declare abstract class MigrationNode {
36
+ name: string;
37
+ constructor(name?: string);
38
+ abstract get_key(): string;
39
+ abstract status(): Promise<MigrationStatus>;
40
+ abstract up(): Promise<void>;
41
+ abstract down(): Promise<void>;
42
+ abstract up_sql(): string;
43
+ abstract down_sql(): string;
44
+ }
45
+ declare class SqlMigrationNode extends MigrationNode {
46
+ private conn;
47
+ private table;
48
+ private key;
49
+ private up_file;
50
+ private sql_up;
51
+ private down_file;
52
+ private sql_down;
53
+ up_sql(): string;
54
+ down_sql(): string;
55
+ get_key(): string;
56
+ constructor(conn: ISQLRunner, table: string, key: string, up_file: string, sql_up: string, down_file: string, sql_down: string);
57
+ status(): Promise<MigrationStatus>;
58
+ up(): Promise<void>;
59
+ down(): Promise<void>;
60
+ }
61
+
62
+ declare class SqlMigrationBuilder {
63
+ private key;
64
+ private up;
65
+ private up_file;
66
+ private down;
67
+ private down_file;
68
+ constructor(key: string);
69
+ set_up(file: string, up: string): void;
70
+ set_down(file: string, down: string): void;
71
+ build(table: string, conn: ISQLRunner): SqlMigrationNode;
72
+ }
73
+
74
+ declare class MigrationDirectoryReader {
75
+ private directory;
76
+ private read_strategy;
77
+ private sqlrunner;
78
+ constructor(directory: string, read_strategy: any, sqlrunner: ISQLRunner);
79
+ loadMigrations(table: string, connection: any): MigrationNode[];
80
+ loadMigration(builder: SqlMigrationBuilder, file: string): SqlMigrationBuilder;
81
+ sql_up(file: string): string;
82
+ sql_down(file: string): string;
83
+ }
84
+
85
+ declare class MigrationSetup {
86
+ private sqlrunner;
87
+ private config;
88
+ constructor(sqlrunner: ISQLRunner, config: MigrationConfig);
89
+ setup(): Promise<void>;
90
+ teardown(): Promise<void>;
91
+ }
92
+
93
+ declare function loadMigrationConfig(configFile: string): MigrationConfig;
94
+ declare class MigrationRunnerFactory {
95
+ private static isSQLRunner;
96
+ static create(configFile: string, conn?: any): Promise<MySQLMigrationRunner>;
97
+ static createConnection(config: MigrationConfig): Promise<any>;
98
+ static createEmpty(configFile: string): Promise<MySQLMigrationRunner>;
99
+ create(config: MigrationConfig, conn: any): Promise<MySQLMigrationRunner>;
100
+ createEmpty(config: MigrationConfig): Promise<MySQLMigrationRunner>;
101
+ private getReadStategy;
102
+ }
103
+ interface MigrationHistory {
104
+ name: string;
105
+ up: string;
106
+ down: string;
107
+ }
108
+ interface IMigrationRunner {
109
+ setup(): Promise<void>;
110
+ terminate(): Promise<void>;
111
+ getMigrationsHistory(): Promise<MigrationHistory[]>;
112
+ getMigrations(): Promise<MigrationNode[]>;
113
+ getPendingMigrations(): Promise<MigrationNode[]>;
114
+ getCompletedMigrations(): Promise<MigrationNode[]>;
115
+ migrate(migrationNodes: MigrationNode[], forward: boolean): Promise<void>;
116
+ reset(): Promise<void>;
117
+ createMigration(name: string): void;
118
+ close(): Promise<void>;
119
+ init(config_file: string): Promise<void>;
120
+ query(sql: string, params?: any[]): Promise<any>;
121
+ }
122
+ declare class MySQLMigrationRunner implements IMigrationRunner {
123
+ private config;
124
+ private directory;
125
+ private setupRunner;
126
+ private sqlrunner;
127
+ private connection;
128
+ constructor(config: MigrationConfig, directory: MigrationDirectoryReader, setupRunner: MigrationSetup, sqlrunner: ISQLRunner, connection: mysql.Connection);
129
+ setup(): Promise<void>;
130
+ terminate(): Promise<void>;
131
+ getMigrationsHistory(): Promise<MigrationHistory[]>;
132
+ getMigrations(): Promise<MigrationNode[]>;
133
+ getPendingMigrations(): Promise<MigrationNode[]>;
134
+ getCompletedMigrations(): Promise<MigrationNode[]>;
135
+ migrate(migrationNodes: MigrationNode[], forward: boolean): Promise<void>;
136
+ reset(): Promise<void>;
137
+ createMigration(name: string): void;
138
+ close(): Promise<void>;
139
+ query(sql: string, params?: any[]): Promise<any>;
140
+ init(config_file: string): Promise<void>;
141
+ }
142
+
143
+ type SeedTransactionalMode = 'runner' | 'seed' | 'none';
144
+ type SeedOptions = {
145
+ migrationsDir?: string;
146
+ dataDir?: string;
147
+ validate?: boolean;
148
+ transactional?: SeedTransactionalMode;
149
+ aliasMap?: Record<string, string>;
150
+ preloadedData?: Record<string, unknown>;
151
+ log?: (msg: string) => void;
152
+ names?: string[];
153
+ };
154
+ type SeedContext = {
155
+ data: unknown;
156
+ log?: (msg: string) => void;
157
+ dialect: string;
158
+ };
159
+ declare function runSeedsWithRunner(runner: IMigrationRunner, migrationConfig: MigrationConfig, direction: 'up' | 'down', options: SeedOptions): Promise<void>;
160
+ type SeedFactoryOptions = SeedOptions & {
161
+ configFile?: string;
162
+ };
163
+ type SeedFactory = {
164
+ up(names?: string[]): Promise<void>;
165
+ down(names?: string[]): Promise<void>;
166
+ };
167
+ declare function createSeedFactory(options: SeedFactoryOptions): SeedFactory;
168
+
169
+ export { type MigrationConfig, MySQLMigrationRunner as MigrationRunner, MigrationRunnerFactory, type SeedContext, type SeedFactory, type SeedFactoryOptions, createSeedFactory, loadMigrationConfig, runSeedsWithRunner };
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
 
@@ -524,6 +544,9 @@ var BaseSQLRunner = class {
524
544
  });
525
545
  }
526
546
  };
547
+ function isPromiseLike(value) {
548
+ return !!value && typeof value.then === "function";
549
+ }
527
550
  var SQLRunner = class extends BaseSQLRunner {
528
551
  constructor(connection) {
529
552
  super();
@@ -553,14 +576,85 @@ var SQLiteRunner = class extends BaseSQLRunner {
553
576
  super();
554
577
  this.connection = connection;
555
578
  }
579
+ prepareStatement(sql) {
580
+ return __async(this, null, function* () {
581
+ if (typeof this.connection.prepare !== "function") {
582
+ throw new Error("SQLite connection does not support prepare()");
583
+ }
584
+ const stmt = this.connection.prepare(sql);
585
+ return isPromiseLike(stmt) ? yield stmt : stmt;
586
+ });
587
+ }
588
+ finalizeStatement(stmt) {
589
+ return __async(this, null, function* () {
590
+ if (!stmt || typeof stmt.finalize !== "function") return;
591
+ const result = stmt.finalize();
592
+ if (isPromiseLike(result)) {
593
+ yield result;
594
+ }
595
+ });
596
+ }
597
+ statementAll(stmt, params) {
598
+ return __async(this, null, function* () {
599
+ if (typeof stmt.all !== "function") {
600
+ throw new Error("SQLite statement does not support all()");
601
+ }
602
+ if (stmt.all.length >= 2) {
603
+ return yield new Promise((resolve, reject) => {
604
+ const callback = (err, rows) => {
605
+ if (err) return reject(err);
606
+ resolve(rows || []);
607
+ };
608
+ try {
609
+ if (params.length > 0) {
610
+ stmt.all(params, callback);
611
+ } else {
612
+ stmt.all(callback);
613
+ }
614
+ } catch (error) {
615
+ reject(error);
616
+ }
617
+ });
618
+ }
619
+ const result = stmt.all(...params);
620
+ return isPromiseLike(result) ? yield result : result;
621
+ });
622
+ }
623
+ statementRun(stmt, params) {
624
+ return __async(this, null, function* () {
625
+ if (typeof stmt.run !== "function") {
626
+ throw new Error("SQLite statement does not support run()");
627
+ }
628
+ if (stmt.run.length >= 2) {
629
+ return yield new Promise((resolve, reject) => {
630
+ const callback = function(err) {
631
+ var _a;
632
+ if (err) return reject(err);
633
+ resolve({ changes: (_a = this == null ? void 0 : this.changes) != null ? _a : 0, lastID: this == null ? void 0 : this.lastID });
634
+ };
635
+ try {
636
+ if (params.length > 0) {
637
+ stmt.run(params, callback);
638
+ } else {
639
+ stmt.run(callback);
640
+ }
641
+ } catch (error) {
642
+ reject(error);
643
+ }
644
+ });
645
+ }
646
+ const result = stmt.run(...params);
647
+ return isPromiseLike(result) ? yield result : result;
648
+ });
649
+ }
556
650
  _query(_0) {
557
651
  return __async(this, arguments, function* (sql, params = []) {
558
- const stmt = yield this.connection.prepare(sql);
652
+ const stmt = yield this.prepareStatement(sql);
559
653
  try {
560
- const rows = yield stmt.all(...params);
654
+ const rows = yield this.statementAll(stmt, params);
561
655
  return rows;
562
656
  } finally {
563
- yield stmt.finalize();
657
+ yield this.finalizeStatement(stmt);
564
658
  }
565
659
  });
566
660
  }
@@ -574,12 +668,12 @@ ${sql}
574
668
  throw err;
575
669
  });
576
670
  }
577
- const stmt = yield this.connection.prepare(sql);
671
+ const stmt = yield this.prepareStatement(sql);
578
672
  try {
579
- const info = yield stmt.run(...params);
673
+ const info = yield this.statementRun(stmt, params);
580
674
  return info;
581
675
  } finally {
582
- yield stmt.finalize();
676
+ yield this.finalizeStatement(stmt);
583
677
  }
584
678
  });
585
679
  }
@@ -595,13 +689,13 @@ ${sql}
595
689
  ).filter((s) => s.trim() !== "");
596
690
  const infos = yield statements.reduce((prev, statement) => __async(this, null, function* () {
597
691
  const infos2 = yield prev;
598
- const stmt = yield this.connection.prepare(`${statement};`);
692
+ const stmt = yield this.prepareStatement(`${statement};`);
599
693
  try {
600
- const info = yield stmt.run(...params);
694
+ const info = yield this.statementRun(stmt, params);
601
695
  infos2.push(info);
602
696
  return infos2;
603
697
  } finally {
604
- yield stmt.finalize();
698
+ yield this.finalizeStatement(stmt);
605
699
  }
606
700
  }), Promise.resolve([null])).then((infos2) => {
607
701
  return infos2.filter((info) => info !== null);
@@ -634,7 +728,14 @@ function toError(error) {
634
728
  if (error instanceof Error) return error;
635
729
  return new Error(String(error));
636
730
  }
731
+ function loadMigrationConfig(configFile) {
732
+ const reader = new FileMigrationConfigReader(configFile);
733
+ return reader.loadFile();
734
+ }
637
735
  var MigrationRunnerFactory = class _MigrationRunnerFactory {
736
+ static isSQLRunner(conn) {
737
+ return !!conn && typeof conn.query === "function" && typeof conn.execute === "function" && typeof conn.end === "function";
738
+ }
638
739
  static create(configFile, conn) {
639
740
  return __async(this, null, function* () {
640
741
  const configReader = new FileMigrationConfigReader(configFile);
@@ -690,21 +791,27 @@ var MigrationRunnerFactory = class _MigrationRunnerFactory {
690
791
  create(config, conn) {
691
792
  return __async(this, null, function* () {
692
793
  let sqlrunner;
693
- switch (config.database) {
694
- case "sql":
695
- sqlrunner = new SQLRunner(conn);
696
- break;
697
- case "sqlite":
698
- sqlrunner = new SQLiteRunner(conn);
699
- break;
700
- default:
701
- throw ConfigurationError.unknownDatabaseType(config.database);
794
+ let driverConnection = conn;
795
+ if (_MigrationRunnerFactory.isSQLRunner(conn)) {
796
+ sqlrunner = conn;
797
+ driverConnection = null;
798
+ } else {
799
+ switch (config.database) {
800
+ case "sql":
801
+ sqlrunner = new SQLRunner(conn);
802
+ break;
803
+ case "sqlite":
804
+ sqlrunner = new SQLiteRunner(conn);
805
+ break;
806
+ default:
807
+ throw ConfigurationError.unknownDatabaseType(config.database);
808
+ }
702
809
  }
703
810
  const setup = new MigrationSetup(sqlrunner, config);
704
811
  const read_strategy = this.getReadStategy(config);
705
812
  const migration_files = new MigrationDirectoryReader(config.migration_folder, read_strategy, sqlrunner);
706
813
  yield setup.setup();
707
- return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, conn);
814
+ return new MySQLMigrationRunner(config, migration_files, setup, sqlrunner, driverConnection);
708
815
  });
709
816
  }
710
817
  createEmpty(config) {
@@ -868,6 +975,11 @@ var MySQLMigrationRunner = class {
868
975
  }
869
976
  });
870
977
  }
978
+ query(sql, params) {
979
+ return __async(this, null, function* () {
980
+ return yield this.sqlrunner.query(sql, params);
981
+ });
982
+ }
871
983
  init(config_file) {
872
984
  return __async(this, null, function* () {
873
985
  try {
@@ -963,9 +1075,262 @@ var MigrationCreator = class {
963
1075
  }
964
1076
  }
965
1077
  };
1078
+
1079
+ // framework/SeedRunner.ts
1080
+ var import_fs4 = __toESM(require("fs"));
1081
+ var import_path2 = __toESM(require("path"));
1082
+ var import_ajv = __toESM(require("ajv"));
1083
+ var import_ajv_formats = __toESM(require("ajv-formats"));
1084
+ function resolveAlias(name, aliasMap) {
1085
+ var _a;
1086
+ if (!aliasMap) return name;
1087
+ return (_a = aliasMap[name]) != null ? _a : name;
1088
+ }
1089
+ function walkForFile(rootDir, fileName) {
1090
+ if (!import_fs4.default.existsSync(rootDir)) return null;
1091
+ const entries = import_fs4.default.readdirSync(rootDir, { withFileTypes: true });
1092
+ for (const entry of entries) {
1093
+ const full = import_path2.default.join(rootDir, entry.name);
1094
+ if (entry.isDirectory()) {
1095
+ const found = walkForFile(full, fileName);
1096
+ if (found) return found;
1097
+ } else if (entry.isFile() && entry.name === fileName) {
1098
+ return full;
1099
+ }
1100
+ }
1101
+ return null;
1102
+ }
1103
+ function resolveMigrationsDir(migrationConfig, options) {
1104
+ var _a;
1105
+ const fromOptions = options.migrationsDir;
1106
+ const fromConfig = (_a = migrationConfig.seeds) == null ? void 0 : _a.migrationsDir;
1107
+ const dir = fromOptions != null ? fromOptions : fromConfig;
1108
+ if (!dir) {
1109
+ throw new Error("Seed migrationsDir not configured. Set seeds.migrationsDir in proper.json or pass it explicitly.");
1110
+ }
1111
+ return dir;
1112
+ }
1113
+ function mergeSeedConfig(migrationConfig, options) {
1114
+ var _a, _b, _c, _d;
1115
+ const names = options.names && options.names.length ? options.names : ((_a = migrationConfig.seeds) == null ? void 0 : _a.list) && migrationConfig.seeds.list.length ? migrationConfig.seeds.list : [];
1116
+ if (!names.length) {
1117
+ throw new Error("No seed names provided and no seeds.list defined in proper config");
1118
+ }
1119
+ const migrationsDir = resolveMigrationsDir(migrationConfig, options);
1120
+ const dataDir = (_c = options.dataDir) != null ? _c : (_b = migrationConfig.seeds) == null ? void 0 : _b.dataDir;
1121
+ const validate = options.validate !== void 0 ? options.validate : true;
1122
+ const transactional = (_d = options.transactional) != null ? _d : "none";
1123
+ const finalOptions = __spreadProps(__spreadValues({}, options), {
1124
+ migrationsDir,
1125
+ dataDir,
1126
+ validate,
1127
+ transactional
1128
+ });
1129
+ return { names, finalOptions };
1130
+ }
1131
+ function resolveSqlPair(name, migrationsDir) {
1132
+ const up = walkForFile(migrationsDir, `${name}.up.sql`);
1133
+ const down = walkForFile(migrationsDir, `${name}.down.sql`);
1134
+ if (up && down) {
1135
+ return { upPath: up, downPath: down };
1136
+ }
1137
+ return null;
1138
+ }
1139
+ function resolveModule(name, migrationsDir) {
1140
+ const ts = walkForFile(migrationsDir, `${name}.ts`);
1141
+ if (ts) return { kind: "ts", modulePath: ts };
1142
+ const js = walkForFile(migrationsDir, `${name}.js`);
1143
+ if (js) return { kind: "js", modulePath: js };
1144
+ return null;
1145
+ }
1146
+ function resolveSeed(name, migrationConfig, options) {
1147
+ return __async(this, null, function* () {
1148
+ var _a, _b;
1149
+ const migrationsDir = resolveMigrationsDir(migrationConfig, options);
1150
+ const alias = resolveAlias(name, options.aliasMap);
1151
+ const sqlPair = resolveSqlPair(name, migrationsDir);
1152
+ if (sqlPair) {
1153
+ return {
1154
+ kind: "sql",
1155
+ name,
1156
+ alias,
1157
+ upPath: sqlPair.upPath,
1158
+ downPath: sqlPair.downPath,
1159
+ dataPath: null,
1160
+ schemaPath: null
1161
+ };
1162
+ }
1163
+ const module2 = resolveModule(name, migrationsDir);
1164
+ if (!module2) {
1165
+ throw new Error(`Seed implementation not found for "${name}" under ${migrationsDir}`);
1166
+ }
1167
+ const dataDir = (_b = options.dataDir) != null ? _b : (_a = migrationConfig.seeds) == null ? void 0 : _a.dataDir;
1168
+ let dataPath = null;
1169
+ let schemaPath = null;
1170
+ if (dataDir) {
1171
+ dataPath = walkForFile(dataDir, `${alias}.json`);
1172
+ schemaPath = walkForFile(dataDir, `${alias}.schema.json`);
1173
+ }
1174
+ return {
1175
+ kind: module2.kind,
1176
+ name,
1177
+ alias,
1178
+ upPath: module2.modulePath,
1179
+ downPath: module2.modulePath,
1180
+ dataPath,
1181
+ schemaPath
1182
+ };
1183
+ });
1184
+ }
1185
+ function loadJson(filePath) {
1186
+ return __async(this, null, function* () {
1187
+ const content = yield import_fs4.default.promises.readFile(filePath, "utf8");
1188
+ return JSON.parse(content);
1189
+ });
1190
+ }
1191
+ function createValidator() {
1192
+ const ajv = new import_ajv.default({ allErrors: true, strict: false });
1193
+ (0, import_ajv_formats.default)(ajv);
1194
+ return ajv;
1195
+ }
1196
+ function validateData(schemaPath, data, validate, log) {
1197
+ return __async(this, null, function* () {
1198
+ if (!validate || !schemaPath) return;
1199
+ const content = yield import_fs4.default.promises.readFile(schemaPath, "utf8");
1200
+ const schema = JSON.parse(content);
1201
+ const ajv = createValidator();
1202
+ const validateFn = ajv.compile(schema);
1203
+ const ok = validateFn(data);
1204
+ if (!ok) {
1205
+ log == null ? void 0 : log(`Validation failed for seed data (${schemaPath})`);
1206
+ throw new Error(`Seed data validation failed: ${ajv.errorsText(validateFn.errors || [])}`);
1207
+ }
1208
+ });
1209
+ }
1210
+ function runSqlSeed(runner, resolved, direction) {
1211
+ return __async(this, null, function* () {
1212
+ const sqlPath = direction === "up" ? resolved.upPath : resolved.downPath;
1213
+ const sql = yield import_fs4.default.promises.readFile(sqlPath, "utf8");
1214
+ yield runner.query(sql);
1215
+ });
1216
+ }
1217
+ function runModuleSeed(runner, resolved, migrationConfig, options, direction) {
1218
+ return __async(this, null, function* () {
1219
+ const { log } = options;
1220
+ const module2 = yield import(import_path2.default.resolve(resolved.upPath));
1221
+ const handler = module2[direction];
1222
+ if (typeof handler !== "function") {
1223
+ throw new Error(`Seed module "${resolved.name}" does not export ${direction}()`);
1224
+ }
1225
+ let data = null;
1226
+ if (options.preloadedData && Object.prototype.hasOwnProperty.call(options.preloadedData, resolved.name)) {
1227
+ data = options.preloadedData[resolved.name];
1228
+ } else if (resolved.dataPath) {
1229
+ data = yield loadJson(resolved.dataPath);
1230
+ }
1231
+ yield validateData(resolved.schemaPath, data, options.validate, log);
1232
+ const ctx = {
1233
+ data,
1234
+ log,
1235
+ dialect: migrationConfig.database || "sql"
1236
+ };
1237
+ yield handler(runner, ctx);
1238
+ });
1239
+ }
1240
+ function runSingleSeed(runner, migrationConfig, name, options, direction) {
1241
+ return __async(this, null, function* () {
1242
+ const resolved = yield resolveSeed(name, migrationConfig, options);
1243
+ if (resolved.kind === "sql") {
1244
+ yield runSqlSeed(runner, resolved, direction);
1245
+ } else {
1246
+ yield runModuleSeed(runner, resolved, migrationConfig, options, direction);
1247
+ }
1248
+ });
1249
+ }
1250
+ function withTransactionalMode(runner, migrationConfig, names, options, direction) {
1251
+ return __async(this, null, function* () {
1252
+ const mode = options.transactional;
1253
+ if (mode === "runner") {
1254
+ yield runner.query("BEGIN");
1255
+ try {
1256
+ for (const name of names) {
1257
+ yield runSingleSeed(runner, migrationConfig, name, options, direction);
1258
+ }
1259
+ yield runner.query("COMMIT");
1260
+ } catch (err) {
1261
+ try {
1262
+ yield runner.query("ROLLBACK");
1263
+ } catch (e) {
1264
+ }
1265
+ throw err;
1266
+ }
1267
+ return;
1268
+ }
1269
+ if (mode === "seed") {
1270
+ for (const name of names) {
1271
+ yield runner.query("BEGIN");
1272
+ try {
1273
+ yield runSingleSeed(runner, migrationConfig, name, options, direction);
1274
+ yield runner.query("COMMIT");
1275
+ } catch (err) {
1276
+ try {
1277
+ yield runner.query("ROLLBACK");
1278
+ } catch (e) {
1279
+ }
1280
+ throw err;
1281
+ }
1282
+ }
1283
+ return;
1284
+ }
1285
+ for (const name of names) {
1286
+ yield runSingleSeed(runner, migrationConfig, name, options, direction);
1287
+ }
1288
+ });
1289
+ }
1290
+ function runSeedsWithRunner(runner, migrationConfig, direction, options) {
1291
+ return __async(this, null, function* () {
1292
+ const { names, finalOptions } = mergeSeedConfig(migrationConfig, options);
1293
+ yield withTransactionalMode(runner, migrationConfig, names, finalOptions, direction);
1294
+ });
1295
+ }
1296
+ function createSeedFactory(options) {
1297
+ var _a;
1298
+ const configFile = (_a = options.configFile) != null ? _a : "proper.json";
1299
+ return {
1300
+ up(names) {
1301
+ return __async(this, null, function* () {
1302
+ const runner = yield MigrationRunnerFactory.create(configFile);
1303
+ const migrationConfig = loadMigrationConfig(configFile);
1304
+ try {
1305
+ yield runSeedsWithRunner(runner, migrationConfig, "up", __spreadProps(__spreadValues({}, options), {
1306
+ names: names && names.length ? names : options.names
1307
+ }));
1308
+ } finally {
1309
+ yield runner.close();
1310
+ }
1311
+ });
1312
+ },
1313
+ down(names) {
1314
+ return __async(this, null, function* () {
1315
+ const runner = yield MigrationRunnerFactory.create(configFile);
1316
+ const migrationConfig = loadMigrationConfig(configFile);
1317
+ try {
1318
+ yield runSeedsWithRunner(runner, migrationConfig, "down", __spreadProps(__spreadValues({}, options), {
1319
+ names: names && names.length ? names : options.names
1320
+ }));
1321
+ } finally {
1322
+ yield runner.close();
1323
+ }
1324
+ });
1325
+ }
1326
+ };
1327
+ }
966
1328
  // Annotate the CommonJS export names for ESM import in node:
967
1329
  0 && (module.exports = {
968
1330
  MigrationRunner,
969
- MigrationRunnerFactory
1331
+ MigrationRunnerFactory,
1332
+ createSeedFactory,
1333
+ loadMigrationConfig,
1334
+ runSeedsWithRunner
970
1335
  });
971
1336
  //# sourceMappingURL=index.js.map