@h3ravel/database 11.2.7 → 11.3.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.
package/dist/index.cjs CHANGED
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
8
11
  var __copyProps = (to, from, except, desc) => {
9
12
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
13
  key = keys[i];
@@ -23,15 +26,481 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
26
  //#endregion
24
27
  let __h3ravel_shared = require("@h3ravel/shared");
25
28
  __h3ravel_shared = __toESM(__h3ravel_shared);
26
- let __h3ravel_arquebus_migrations = require("@h3ravel/arquebus/migrations");
27
- __h3ravel_arquebus_migrations = __toESM(__h3ravel_arquebus_migrations);
29
+ let node_fs_promises = require("node:fs/promises");
30
+ node_fs_promises = __toESM(node_fs_promises);
28
31
  let __h3ravel_core = require("@h3ravel/core");
29
32
  __h3ravel_core = __toESM(__h3ravel_core);
33
+ let __h3ravel_support = require("@h3ravel/support");
34
+ __h3ravel_support = __toESM(__h3ravel_support);
30
35
  let node_path = require("node:path");
31
36
  node_path = __toESM(node_path);
37
+ let __h3ravel_arquebus_migrations = require("@h3ravel/arquebus/migrations");
38
+ __h3ravel_arquebus_migrations = __toESM(__h3ravel_arquebus_migrations);
32
39
  let __h3ravel_arquebus = require("@h3ravel/arquebus");
33
40
  __h3ravel_arquebus = __toESM(__h3ravel_arquebus);
34
41
 
42
+ //#region src/Utils/TableGuesser.ts
43
+ var TableGuesser = class TableGuesser {
44
+ static CREATE_PATTERNS = [/^create_(\w+)_table$/, /^create_(\w+)$/];
45
+ static CHANGE_PATTERNS = [/.+_(to|from|in)_(\w+)_table$/, /.+_(to|from|in)_(\w+)$/];
46
+ static guess(migration) {
47
+ for (const pattern of TableGuesser.CREATE_PATTERNS) {
48
+ const matches = migration.match(pattern);
49
+ if (matches) return [matches[1], true];
50
+ }
51
+ for (const pattern of TableGuesser.CHANGE_PATTERNS) {
52
+ const matches = migration.match(pattern);
53
+ if (matches) return [matches[2], false];
54
+ }
55
+ return [];
56
+ }
57
+ };
58
+
59
+ //#endregion
60
+ //#region ../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js
61
+ var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js": ((exports, module) => {
62
+ (function(t, e) {
63
+ "object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
64
+ })(exports, (function() {
65
+ var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = {
66
+ name: "en",
67
+ weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
68
+ months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
69
+ ordinal: function(t$1) {
70
+ var e$1 = [
71
+ "th",
72
+ "st",
73
+ "nd",
74
+ "rd"
75
+ ], n$1 = t$1 % 100;
76
+ return "[" + t$1 + (e$1[(n$1 - 20) % 10] || e$1[n$1] || e$1[0]) + "]";
77
+ }
78
+ }, m = function(t$1, e$1, n$1) {
79
+ var r$1 = String(t$1);
80
+ return !r$1 || r$1.length >= e$1 ? t$1 : "" + Array(e$1 + 1 - r$1.length).join(n$1) + t$1;
81
+ }, v = {
82
+ s: m,
83
+ z: function(t$1) {
84
+ var e$1 = -t$1.utcOffset(), n$1 = Math.abs(e$1), r$1 = Math.floor(n$1 / 60), i$1 = n$1 % 60;
85
+ return (e$1 <= 0 ? "+" : "-") + m(r$1, 2, "0") + ":" + m(i$1, 2, "0");
86
+ },
87
+ m: function t$1(e$1, n$1) {
88
+ if (e$1.date() < n$1.date()) return -t$1(n$1, e$1);
89
+ var r$1 = 12 * (n$1.year() - e$1.year()) + (n$1.month() - e$1.month()), i$1 = e$1.clone().add(r$1, c), s$1 = n$1 - i$1 < 0, u$1 = e$1.clone().add(r$1 + (s$1 ? -1 : 1), c);
90
+ return +(-(r$1 + (n$1 - i$1) / (s$1 ? i$1 - u$1 : u$1 - i$1)) || 0);
91
+ },
92
+ a: function(t$1) {
93
+ return t$1 < 0 ? Math.ceil(t$1) || 0 : Math.floor(t$1);
94
+ },
95
+ p: function(t$1) {
96
+ return {
97
+ M: c,
98
+ y: h,
99
+ w: o,
100
+ d: a,
101
+ D: d,
102
+ h: u,
103
+ m: s,
104
+ s: i,
105
+ ms: r,
106
+ Q: f
107
+ }[t$1] || String(t$1 || "").toLowerCase().replace(/s$/, "");
108
+ },
109
+ u: function(t$1) {
110
+ return void 0 === t$1;
111
+ }
112
+ }, g = "en", D = {};
113
+ D[g] = M;
114
+ var p = "$isDayjsObject", S = function(t$1) {
115
+ return t$1 instanceof _ || !(!t$1 || !t$1[p]);
116
+ }, w = function t$1(e$1, n$1, r$1) {
117
+ var i$1;
118
+ if (!e$1) return g;
119
+ if ("string" == typeof e$1) {
120
+ var s$1 = e$1.toLowerCase();
121
+ D[s$1] && (i$1 = s$1), n$1 && (D[s$1] = n$1, i$1 = s$1);
122
+ var u$1 = e$1.split("-");
123
+ if (!i$1 && u$1.length > 1) return t$1(u$1[0]);
124
+ } else {
125
+ var a$1 = e$1.name;
126
+ D[a$1] = e$1, i$1 = a$1;
127
+ }
128
+ return !r$1 && i$1 && (g = i$1), i$1 || !r$1 && g;
129
+ }, O = function(t$1, e$1) {
130
+ if (S(t$1)) return t$1.clone();
131
+ var n$1 = "object" == typeof e$1 ? e$1 : {};
132
+ return n$1.date = t$1, n$1.args = arguments, new _(n$1);
133
+ }, b = v;
134
+ b.l = w, b.i = S, b.w = function(t$1, e$1) {
135
+ return O(t$1, {
136
+ locale: e$1.$L,
137
+ utc: e$1.$u,
138
+ x: e$1.$x,
139
+ $offset: e$1.$offset
140
+ });
141
+ };
142
+ var _ = function() {
143
+ function M$1(t$1) {
144
+ this.$L = w(t$1.locale, null, !0), this.parse(t$1), this.$x = this.$x || t$1.x || {}, this[p] = !0;
145
+ }
146
+ var m$1 = M$1.prototype;
147
+ return m$1.parse = function(t$1) {
148
+ this.$d = function(t$2) {
149
+ var e$1 = t$2.date, n$1 = t$2.utc;
150
+ if (null === e$1) return /* @__PURE__ */ new Date(NaN);
151
+ if (b.u(e$1)) return /* @__PURE__ */ new Date();
152
+ if (e$1 instanceof Date) return new Date(e$1);
153
+ if ("string" == typeof e$1 && !/Z$/i.test(e$1)) {
154
+ var r$1 = e$1.match($);
155
+ if (r$1) {
156
+ var i$1 = r$1[2] - 1 || 0, s$1 = (r$1[7] || "0").substring(0, 3);
157
+ return n$1 ? new Date(Date.UTC(r$1[1], i$1, r$1[3] || 1, r$1[4] || 0, r$1[5] || 0, r$1[6] || 0, s$1)) : new Date(r$1[1], i$1, r$1[3] || 1, r$1[4] || 0, r$1[5] || 0, r$1[6] || 0, s$1);
158
+ }
159
+ }
160
+ return new Date(e$1);
161
+ }(t$1), this.init();
162
+ }, m$1.init = function() {
163
+ var t$1 = this.$d;
164
+ this.$y = t$1.getFullYear(), this.$M = t$1.getMonth(), this.$D = t$1.getDate(), this.$W = t$1.getDay(), this.$H = t$1.getHours(), this.$m = t$1.getMinutes(), this.$s = t$1.getSeconds(), this.$ms = t$1.getMilliseconds();
165
+ }, m$1.$utils = function() {
166
+ return b;
167
+ }, m$1.isValid = function() {
168
+ return !(this.$d.toString() === l);
169
+ }, m$1.isSame = function(t$1, e$1) {
170
+ var n$1 = O(t$1);
171
+ return this.startOf(e$1) <= n$1 && n$1 <= this.endOf(e$1);
172
+ }, m$1.isAfter = function(t$1, e$1) {
173
+ return O(t$1) < this.startOf(e$1);
174
+ }, m$1.isBefore = function(t$1, e$1) {
175
+ return this.endOf(e$1) < O(t$1);
176
+ }, m$1.$g = function(t$1, e$1, n$1) {
177
+ return b.u(t$1) ? this[e$1] : this.set(n$1, t$1);
178
+ }, m$1.unix = function() {
179
+ return Math.floor(this.valueOf() / 1e3);
180
+ }, m$1.valueOf = function() {
181
+ return this.$d.getTime();
182
+ }, m$1.startOf = function(t$1, e$1) {
183
+ var n$1 = this, r$1 = !!b.u(e$1) || e$1, f$1 = b.p(t$1), l$1 = function(t$2, e$2) {
184
+ var i$1 = b.w(n$1.$u ? Date.UTC(n$1.$y, e$2, t$2) : new Date(n$1.$y, e$2, t$2), n$1);
185
+ return r$1 ? i$1 : i$1.endOf(a);
186
+ }, $$1 = function(t$2, e$2) {
187
+ return b.w(n$1.toDate()[t$2].apply(n$1.toDate("s"), (r$1 ? [
188
+ 0,
189
+ 0,
190
+ 0,
191
+ 0
192
+ ] : [
193
+ 23,
194
+ 59,
195
+ 59,
196
+ 999
197
+ ]).slice(e$2)), n$1);
198
+ }, y$1 = this.$W, M$2 = this.$M, m$2 = this.$D, v$1 = "set" + (this.$u ? "UTC" : "");
199
+ switch (f$1) {
200
+ case h: return r$1 ? l$1(1, 0) : l$1(31, 11);
201
+ case c: return r$1 ? l$1(1, M$2) : l$1(0, M$2 + 1);
202
+ case o:
203
+ var g$1 = this.$locale().weekStart || 0, D$1 = (y$1 < g$1 ? y$1 + 7 : y$1) - g$1;
204
+ return l$1(r$1 ? m$2 - D$1 : m$2 + (6 - D$1), M$2);
205
+ case a:
206
+ case d: return $$1(v$1 + "Hours", 0);
207
+ case u: return $$1(v$1 + "Minutes", 1);
208
+ case s: return $$1(v$1 + "Seconds", 2);
209
+ case i: return $$1(v$1 + "Milliseconds", 3);
210
+ default: return this.clone();
211
+ }
212
+ }, m$1.endOf = function(t$1) {
213
+ return this.startOf(t$1, !1);
214
+ }, m$1.$set = function(t$1, e$1) {
215
+ var n$1, o$1 = b.p(t$1), f$1 = "set" + (this.$u ? "UTC" : ""), l$1 = (n$1 = {}, n$1[a] = f$1 + "Date", n$1[d] = f$1 + "Date", n$1[c] = f$1 + "Month", n$1[h] = f$1 + "FullYear", n$1[u] = f$1 + "Hours", n$1[s] = f$1 + "Minutes", n$1[i] = f$1 + "Seconds", n$1[r] = f$1 + "Milliseconds", n$1)[o$1], $$1 = o$1 === a ? this.$D + (e$1 - this.$W) : e$1;
216
+ if (o$1 === c || o$1 === h) {
217
+ var y$1 = this.clone().set(d, 1);
218
+ y$1.$d[l$1]($$1), y$1.init(), this.$d = y$1.set(d, Math.min(this.$D, y$1.daysInMonth())).$d;
219
+ } else l$1 && this.$d[l$1]($$1);
220
+ return this.init(), this;
221
+ }, m$1.set = function(t$1, e$1) {
222
+ return this.clone().$set(t$1, e$1);
223
+ }, m$1.get = function(t$1) {
224
+ return this[b.p(t$1)]();
225
+ }, m$1.add = function(r$1, f$1) {
226
+ var d$1, l$1 = this;
227
+ r$1 = Number(r$1);
228
+ var $$1 = b.p(f$1), y$1 = function(t$1) {
229
+ var e$1 = O(l$1);
230
+ return b.w(e$1.date(e$1.date() + Math.round(t$1 * r$1)), l$1);
231
+ };
232
+ if ($$1 === c) return this.set(c, this.$M + r$1);
233
+ if ($$1 === h) return this.set(h, this.$y + r$1);
234
+ if ($$1 === a) return y$1(1);
235
+ if ($$1 === o) return y$1(7);
236
+ var M$2 = (d$1 = {}, d$1[s] = e, d$1[u] = n, d$1[i] = t, d$1)[$$1] || 1, m$2 = this.$d.getTime() + r$1 * M$2;
237
+ return b.w(m$2, this);
238
+ }, m$1.subtract = function(t$1, e$1) {
239
+ return this.add(-1 * t$1, e$1);
240
+ }, m$1.format = function(t$1) {
241
+ var e$1 = this, n$1 = this.$locale();
242
+ if (!this.isValid()) return n$1.invalidDate || l;
243
+ var r$1 = t$1 || "YYYY-MM-DDTHH:mm:ssZ", i$1 = b.z(this), s$1 = this.$H, u$1 = this.$m, a$1 = this.$M, o$1 = n$1.weekdays, c$1 = n$1.months, f$1 = n$1.meridiem, h$1 = function(t$2, n$2, i$2, s$2) {
244
+ return t$2 && (t$2[n$2] || t$2(e$1, r$1)) || i$2[n$2].slice(0, s$2);
245
+ }, d$1 = function(t$2) {
246
+ return b.s(s$1 % 12 || 12, t$2, "0");
247
+ }, $$1 = f$1 || function(t$2, e$2, n$2) {
248
+ var r$2 = t$2 < 12 ? "AM" : "PM";
249
+ return n$2 ? r$2.toLowerCase() : r$2;
250
+ };
251
+ return r$1.replace(y, (function(t$2, r$2) {
252
+ return r$2 || function(t$3) {
253
+ switch (t$3) {
254
+ case "YY": return String(e$1.$y).slice(-2);
255
+ case "YYYY": return b.s(e$1.$y, 4, "0");
256
+ case "M": return a$1 + 1;
257
+ case "MM": return b.s(a$1 + 1, 2, "0");
258
+ case "MMM": return h$1(n$1.monthsShort, a$1, c$1, 3);
259
+ case "MMMM": return h$1(c$1, a$1);
260
+ case "D": return e$1.$D;
261
+ case "DD": return b.s(e$1.$D, 2, "0");
262
+ case "d": return String(e$1.$W);
263
+ case "dd": return h$1(n$1.weekdaysMin, e$1.$W, o$1, 2);
264
+ case "ddd": return h$1(n$1.weekdaysShort, e$1.$W, o$1, 3);
265
+ case "dddd": return o$1[e$1.$W];
266
+ case "H": return String(s$1);
267
+ case "HH": return b.s(s$1, 2, "0");
268
+ case "h": return d$1(1);
269
+ case "hh": return d$1(2);
270
+ case "a": return $$1(s$1, u$1, !0);
271
+ case "A": return $$1(s$1, u$1, !1);
272
+ case "m": return String(u$1);
273
+ case "mm": return b.s(u$1, 2, "0");
274
+ case "s": return String(e$1.$s);
275
+ case "ss": return b.s(e$1.$s, 2, "0");
276
+ case "SSS": return b.s(e$1.$ms, 3, "0");
277
+ case "Z": return i$1;
278
+ }
279
+ return null;
280
+ }(t$2) || i$1.replace(":", "");
281
+ }));
282
+ }, m$1.utcOffset = function() {
283
+ return 15 * -Math.round(this.$d.getTimezoneOffset() / 15);
284
+ }, m$1.diff = function(r$1, d$1, l$1) {
285
+ var $$1, y$1 = this, M$2 = b.p(d$1), m$2 = O(r$1), v$1 = (m$2.utcOffset() - this.utcOffset()) * e, g$1 = this - m$2, D$1 = function() {
286
+ return b.m(y$1, m$2);
287
+ };
288
+ switch (M$2) {
289
+ case h:
290
+ $$1 = D$1() / 12;
291
+ break;
292
+ case c:
293
+ $$1 = D$1();
294
+ break;
295
+ case f:
296
+ $$1 = D$1() / 3;
297
+ break;
298
+ case o:
299
+ $$1 = (g$1 - v$1) / 6048e5;
300
+ break;
301
+ case a:
302
+ $$1 = (g$1 - v$1) / 864e5;
303
+ break;
304
+ case u:
305
+ $$1 = g$1 / n;
306
+ break;
307
+ case s:
308
+ $$1 = g$1 / e;
309
+ break;
310
+ case i:
311
+ $$1 = g$1 / t;
312
+ break;
313
+ default: $$1 = g$1;
314
+ }
315
+ return l$1 ? $$1 : b.a($$1);
316
+ }, m$1.daysInMonth = function() {
317
+ return this.endOf(c).$D;
318
+ }, m$1.$locale = function() {
319
+ return D[this.$L];
320
+ }, m$1.locale = function(t$1, e$1) {
321
+ if (!t$1) return this.$L;
322
+ var n$1 = this.clone(), r$1 = w(t$1, e$1, !0);
323
+ return r$1 && (n$1.$L = r$1), n$1;
324
+ }, m$1.clone = function() {
325
+ return b.w(this.$d, this);
326
+ }, m$1.toDate = function() {
327
+ return new Date(this.valueOf());
328
+ }, m$1.toJSON = function() {
329
+ return this.isValid() ? this.toISOString() : null;
330
+ }, m$1.toISOString = function() {
331
+ return this.$d.toISOString();
332
+ }, m$1.toString = function() {
333
+ return this.$d.toUTCString();
334
+ }, M$1;
335
+ }(), k = _.prototype;
336
+ return O.prototype = k, [
337
+ ["$ms", r],
338
+ ["$s", i],
339
+ ["$m", s],
340
+ ["$H", u],
341
+ ["$W", a],
342
+ ["$M", c],
343
+ ["$y", h],
344
+ ["$D", d]
345
+ ].forEach((function(t$1) {
346
+ k[t$1[1]] = function(e$1) {
347
+ return this.$g(e$1, t$1[0], t$1[1]);
348
+ };
349
+ })), O.extend = function(t$1, e$1) {
350
+ return t$1.$i || (t$1(e$1, _, O), t$1.$i = !0), O;
351
+ }, O.locale = w, O.isDayjs = S, O.unix = function(t$1) {
352
+ return O(1e3 * t$1);
353
+ }, O.en = D[g], O.Ls = D, O.p = {}, O;
354
+ }));
355
+ }) });
356
+
357
+ //#endregion
358
+ //#region src/Commands/MakeCommand.ts
359
+ var import_dayjs_min = /* @__PURE__ */ __toESM(require_dayjs_min(), 1);
360
+ var MakeCommand = class extends __h3ravel_core.ConsoleCommand {
361
+ /**
362
+ * The name and signature of the console command.
363
+ *
364
+ * @var string
365
+ */
366
+ signature = `#make:
367
+ {migration : Generates a new database migration class.
368
+ | {--t|table : The table to migrate}
369
+ | {--c|create : The table to be created}
370
+ | {--l|type=ts : The file type to generate}
371
+ }
372
+ {factory : Create a new model factory.
373
+ | {--force : Create the factory even if it already exists}
374
+ | {--l|type=ts : The file type to generate}
375
+ }
376
+ {seeder : Create a new seeder class.
377
+ | {--force : Create the seeder even if it already exists}
378
+ | {--l|type=ts : The file type to generate}
379
+ }
380
+ {model : Create a new Eloquent model class.
381
+ | {--api : Indicates if the generated controller should be an API resource controller}
382
+ | {--c|controller : Create a new controller for the model}
383
+ | {--f|factory : Create a new factory for the model}
384
+ | {--m|migration : Create a new migration file for the model}
385
+ | {--r|resource : Indicates if the generated controller should be a resource controller}
386
+ | {--a|all : Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model}
387
+ | {--s|seed : Create a new seeder for the model}
388
+ | {--l|type=ts : The file type to generate}
389
+ | {--force : Create the model even if it already exists}
390
+ }
391
+ {^name : The name of the [name] to generate}
392
+ `;
393
+ /**
394
+ * The console command description.
395
+ *
396
+ * @var string
397
+ */
398
+ description = "Generate component classes";
399
+ async handle() {
400
+ const command = this.dictionary.baseCommand ?? this.dictionary.name;
401
+ if (!this.argument("name")) this.program.error("Please provide a valid name for the " + command);
402
+ const methods = {
403
+ migration: "makeMigration",
404
+ factory: "makeFactory",
405
+ seeder: "makeSeeder",
406
+ model: "makeModel"
407
+ };
408
+ console.log("");
409
+ await this[methods[command]]();
410
+ }
411
+ /**
412
+ * Generate a new database migration class
413
+ */
414
+ async makeMigration() {
415
+ const type = this.option("type", "ts");
416
+ const name = this.argument("name");
417
+ const datePrefix = (0, import_dayjs_min.default)().format("YYYY_MM_DD_HHmmss");
418
+ const path$1 = database_path(`migrations/${datePrefix}_${name}.${type}`);
419
+ const dbPkgPath = __h3ravel_shared.FileSystem.findModulePkg("@h3ravel/database", this.kernel.cwd) ?? "";
420
+ let create = this.option("create", false);
421
+ let table = this.option("table");
422
+ if (!table && typeof create === "string") {
423
+ table = create;
424
+ create = true;
425
+ }
426
+ if (!table) {
427
+ const guessed = TableGuesser.guess(name);
428
+ table = guessed[0];
429
+ create = !!guessed[1];
430
+ }
431
+ const stubPath = node_path.default.join(dbPkgPath, this.getStubName("migration", type, {
432
+ table,
433
+ create
434
+ }));
435
+ let stub = await (0, node_fs_promises.readFile)(stubPath, "utf-8");
436
+ if (table !== null) stub = stub.replace(/DummyTable|{{\s*table\s*}}/g, table);
437
+ __h3ravel_shared.Logger.info("INFO: Creating Migration");
438
+ await this.kernel.ensureDirectoryExists(node_path.default.dirname(path$1));
439
+ await (0, node_fs_promises.writeFile)(path$1, stub);
440
+ __h3ravel_shared.Logger.split("INFO: Migration Created", __h3ravel_shared.Logger.log(node_path.default.basename(path$1), "gray", false));
441
+ }
442
+ /**
443
+ * Create a new model factory
444
+ */
445
+ makeFactory() {
446
+ __h3ravel_shared.Logger.success("Factory support is not yet available");
447
+ }
448
+ /**
449
+ * Create a new seeder class
450
+ */
451
+ async makeSeeder() {
452
+ const type = this.option("type", "ts");
453
+ const name = this.argument("name");
454
+ const force = this.option("force");
455
+ const path$1 = database_path(`seeders/${__h3ravel_support.Str.snake(name)}.${type}`);
456
+ /** Check if the model already exists */
457
+ if (!force && await __h3ravel_shared.FileSystem.fileExists(path$1)) __h3ravel_shared.Logger.error(`ERORR: ${name} already exists`);
458
+ const dbPkgPath = __h3ravel_shared.FileSystem.findModulePkg("@h3ravel/database", this.kernel.cwd) ?? "";
459
+ const stubPath = node_path.default.join(dbPkgPath, this.getStubName("seeder", type));
460
+ let stub = await (0, node_fs_promises.readFile)(stubPath, "utf-8");
461
+ stub = stub.replace(/{{ name }}/g, name);
462
+ await (0, node_fs_promises.writeFile)(path$1, stub);
463
+ __h3ravel_shared.Logger.info(`INFO: Seeder ${__h3ravel_shared.Logger.log(`[${node_path.default.relative(process.cwd(), path$1)}]`, "bold", false)} created successfully.`);
464
+ }
465
+ /**
466
+ * Generate a new Arquebus model class
467
+ */
468
+ async makeModel() {
469
+ const type = this.option("type", "ts");
470
+ const name = this.argument("name");
471
+ const force = this.option("force");
472
+ const path$1 = app_path(`Models/${__h3ravel_support.Str.lower(name)}.${type}`);
473
+ /** The model is scoped to a path make sure to create the associated directories */
474
+ if (name.includes("/")) await (0, node_fs_promises.mkdir)(__h3ravel_support.Str.beforeLast(path$1, "/"), { recursive: true });
475
+ /** Check if the model already exists */
476
+ if (!force && await __h3ravel_shared.FileSystem.fileExists(path$1)) __h3ravel_shared.Logger.error(`ERORR: ${name} model already exists`);
477
+ const dbPkgPath = __h3ravel_shared.FileSystem.findModulePkg("@h3ravel/database", this.kernel.cwd) ?? "";
478
+ const stubPath = node_path.default.join(dbPkgPath, `dist/stubs/model-${type}.stub`);
479
+ let stub = await (0, node_fs_promises.readFile)(stubPath, "utf-8");
480
+ stub = stub.replace(/{{ name }}/g, name);
481
+ await (0, node_fs_promises.writeFile)(path$1, stub);
482
+ __h3ravel_shared.Logger.info(`INFO: Model ${__h3ravel_shared.Logger.log(`[${node_path.default.relative(process.cwd(), path$1)}]`, "bold", false)} created successfully.`);
483
+ }
484
+ /**
485
+ * Ge the database migration file name
486
+ *
487
+ * @param table
488
+ * @param create
489
+ * @param type
490
+ * @returns
491
+ */
492
+ getStubName(type, ext = "ts", { table, create } = {}) {
493
+ let stub;
494
+ if (type === "migration") {
495
+ if (!table) stub = `migration-${ext}.stub`;
496
+ else if (create) stub = `migration.create-${ext}.stub`;
497
+ else stub = `migration.update-${ext}.stub`;
498
+ return "dist/stubs/" + stub;
499
+ } else return `dist/stubs/${type}-${ext}.stub`;
500
+ }
501
+ };
502
+
503
+ //#endregion
35
504
  //#region src/Commands/MigrateCommand.ts
36
505
  var MigrateCommand = class extends __h3ravel_core.ConsoleCommand {
37
506
  /**
@@ -158,10 +627,10 @@ var MigrateCommand = class extends __h3ravel_core.ConsoleCommand {
158
627
  }).status(this.connection, this.options(), true);
159
628
  try {
160
629
  if (migrations.length > 0) {
161
- __h3ravel_shared.Logger.twoColumnLog("Migration name", "Batch / Status");
630
+ __h3ravel_shared.Logger.twoColumnDetail("Migration name", "Batch / Status");
162
631
  migrations.forEach((migration) => {
163
632
  const status = migration.ran ? __h3ravel_shared.Logger.parse([[`[${migration.batch}]`, "white"], ["Ran", "green"]], " ", false) : __h3ravel_shared.Logger.parse([["Pending", "yellow"]], "", false);
164
- __h3ravel_shared.Logger.twoColumnLog(migration.name, status);
633
+ __h3ravel_shared.Logger.twoColumnDetail(migration.name, status);
165
634
  });
166
635
  } else __h3ravel_shared.Logger.info("No migrations found");
167
636
  } catch (e) {
@@ -185,7 +654,7 @@ var MigrateCommand = class extends __h3ravel_core.ConsoleCommand {
185
654
  try {
186
655
  /** Publish any existing migrations */
187
656
  await creator.publish(this.databasePath, (fileName) => {
188
- __h3ravel_shared.Logger.twoColumnLog(fileName, __h3ravel_shared.Logger.parse([["PUBLISHED", "green"]], "", false));
657
+ __h3ravel_shared.Logger.twoColumnDetail(fileName, __h3ravel_shared.Logger.parse([["PUBLISHED", "green"]], "", false));
189
658
  });
190
659
  } catch {
191
660
  __h3ravel_shared.Logger.error([`ERROR: ${name} has no publishable migrations.`]);
@@ -206,6 +675,65 @@ var MigrateCommand = class extends __h3ravel_core.ConsoleCommand {
206
675
  }
207
676
  };
208
677
 
678
+ //#endregion
679
+ //#region src/Commands/SeedCommand.ts
680
+ var SeedCommand = class extends __h3ravel_core.ConsoleCommand {
681
+ /**
682
+ * The current query builder instance
683
+ */
684
+ queryBuilder;
685
+ /**
686
+ * The current database connection name
687
+ */
688
+ connection;
689
+ /**
690
+ * The name and signature of the console command.
691
+ *
692
+ * @var string
693
+ */
694
+ signature = `db:seed
695
+ {--class=DatabaseSeeder : The file name of the root seeder}
696
+ {--d|database? : The database connection to use}
697
+ {--force : Force the operation to run when in production}
698
+ `;
699
+ /**
700
+ * The console command description.
701
+ *
702
+ * @var string
703
+ */
704
+ description = "Seed the database with records.";
705
+ /**
706
+ * Execute the console command.
707
+ */
708
+ async handle() {
709
+ const file = this.option("class");
710
+ const force = this.option("force");
711
+ const database = this.option("database");
712
+ console.log("");
713
+ if (env("APP_ENV") === "production" && !force) __h3ravel_shared.Logger.error("INFO: Unable to run seeders, your app is currently in production.");
714
+ this.connection = database ?? config("database.default");
715
+ this.queryBuilder = DB.instance(this.connection);
716
+ if (!this.connection) __h3ravel_shared.Logger.error("ERROR: Unknown database connection.");
717
+ let path$1 = node_path.default.join(process.cwd(), file);
718
+ const [f1, f2] = [__h3ravel_shared.FileSystem.resolveFileUp(__h3ravel_support.Str.snake(file), ["js", "ts"], database_path("seeders")), __h3ravel_shared.FileSystem.resolveFileUp(__h3ravel_support.Str.studly(file), ["js", "ts"], database_path("seeders"))];
719
+ if (!f1 && !f2) {
720
+ /**
721
+ * Try to find the path assuming it's relative to cwd
722
+ */
723
+ if (!await __h3ravel_shared.FileSystem.fileExists(path$1)) path$1 = database_path(node_path.default.join("seeders", file));
724
+ /**
725
+ * Now try to find the path knowing it's relative to database_path
726
+ */
727
+ if (!await __h3ravel_shared.FileSystem.fileExists(path$1)) __h3ravel_shared.Logger.error(`ERROR: Seeder ${__h3ravel_shared.Logger.log(`[${file}]`, "bold", false)} not found.`);
728
+ } else path$1 = String(f1 ?? f2);
729
+ const { default: seeder } = await import(path$1);
730
+ if (seeder) {
731
+ __h3ravel_shared.Logger.info("INFO: Seeding database.");
732
+ await new seeder(this.app, this).run(this.queryBuilder);
733
+ }
734
+ }
735
+ };
736
+
209
737
  //#endregion
210
738
  //#region src/Configuration.ts
211
739
  const arquebusConfig = (config$1) => {
@@ -296,13 +824,133 @@ var DatabaseServiceProvider = class extends __h3ravel_core.ServiceProvider {
296
824
  const connection = Object.entries(arquebusConfig(config$1.get("database"))).find(([client]) => client === config$1.get("database.default"))?.at(1);
297
825
  if (connection) __h3ravel_arquebus.arquebus.addConnection(connection);
298
826
  /** Register Musket Commands */
299
- this.commands([MigrateCommand]);
827
+ this.commands([
828
+ MigrateCommand,
829
+ MakeCommand,
830
+ SeedCommand
831
+ ]);
832
+ }
833
+ };
834
+
835
+ //#endregion
836
+ //#region src/Query/DB.ts
837
+ var DB = class DB {
838
+ connection;
839
+ constructor(connection) {
840
+ if (connection) this.connection = connection;
841
+ }
842
+ /**
843
+ * New instance
844
+ */
845
+ static table(name) {
846
+ return new DB().builder().table(name);
847
+ }
848
+ /**
849
+ * Builder table instance
850
+ */
851
+ static instance(connection) {
852
+ return new DB(connection).builder();
853
+ }
854
+ /**
855
+ * Builder transaction instance
856
+ */
857
+ static transaction(callback) {
858
+ return new DB().builder().transaction(callback);
859
+ }
860
+ builder() {
861
+ return __h3ravel_arquebus.arquebus.getInstance().connection(this.connection);
862
+ }
863
+ };
864
+
865
+ //#endregion
866
+ //#region src/Seeder.ts
867
+ var Seeder = class Seeder extends __h3ravel_arquebus.Seeder {
868
+ /**
869
+ * Seeders that have been called at least one time.
870
+ */
871
+ static called = [];
872
+ constructor(app, command) {
873
+ super();
874
+ this.app = app;
875
+ this.command = command;
876
+ }
877
+ async run(_conn, ..._args) {}
878
+ /**
879
+ * Run the given seeder class.
880
+ *
881
+ * @param className
882
+ * @param silent
883
+ * @param parameters
884
+ *
885
+ * @return this
886
+ */
887
+ async call(className, silent = false, parameters = []) {
888
+ const classes = __h3ravel_support.Arr.wrap(className);
889
+ for (let i = 0; i < classes.length; i++) {
890
+ const instance = this.resolve(classes[i]);
891
+ const name = instance.constructor.name;
892
+ if (silent === false) await __h3ravel_shared.TaskManager.advancedTaskRunner([[name, "RUNNING"], [name, "DONE"]], async () => await instance.run(this.command.queryBuilder, ...parameters));
893
+ else await instance.run(this.command.queryBuilder, ...parameters);
894
+ Seeder.called.push(instance);
895
+ }
896
+ return this;
897
+ }
898
+ /**
899
+ * Resolve an instance of the given seeder class.
900
+ *
901
+ * @param className
902
+ *
903
+ * @return Seeder
904
+ */
905
+ resolve(className) {
906
+ const instance = new className(this.app);
907
+ if (this.command) instance.setCommand(this.command);
908
+ return instance;
909
+ }
910
+ /**
911
+ * Run the given seeder class.
912
+ *
913
+ * @param className
914
+ * @param silent
915
+ * @param parameters
916
+ *
917
+ * @return void
918
+ */
919
+ async callWith(className, parameters) {
920
+ await this.call(className, false, parameters);
921
+ }
922
+ /**
923
+ * Silently run the given seeder class.
924
+ *
925
+ * @param className
926
+ * @param parameters
927
+ *
928
+ * @return void
929
+ */
930
+ async callSilent(className, parameters = []) {
931
+ await this.call(className, true, parameters);
932
+ }
933
+ /**
934
+ * Set the console command instance.
935
+ *
936
+ * @param command
937
+ *
938
+ * @return this
939
+ */
940
+ setCommand(command) {
941
+ this.command = command;
942
+ return this;
300
943
  }
301
944
  };
302
945
 
303
946
  //#endregion
947
+ exports.DB = DB;
304
948
  exports.DatabaseServiceProvider = DatabaseServiceProvider;
949
+ exports.MakeCommand = MakeCommand;
305
950
  exports.MigrateCommand = MigrateCommand;
306
951
  exports.Model = Model;
952
+ exports.SeedCommand = SeedCommand;
953
+ exports.Seeder = Seeder;
954
+ exports.TableGuesser = TableGuesser;
307
955
  exports.arquebusConfig = arquebusConfig;
308
956
  //# sourceMappingURL=index.cjs.map