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