@h3ravel/arquebus 1.29.0-alpha.15 → 3.0.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/bin/index.cjs +773 -715
- package/bin/index.js +764 -706
- package/dist/browser/index.cjs +117 -148
- package/dist/browser/index.d.ts +967 -901
- package/dist/browser/index.js +102 -136
- package/dist/concerns/index.cjs +277 -324
- package/dist/concerns/index.d.ts +1013 -941
- package/dist/concerns/index.js +264 -312
- package/dist/index.cjs +436 -517
- package/dist/index.d.ts +1015 -942
- package/dist/index.js +373 -455
- package/dist/inspector/index.cjs +335 -402
- package/dist/inspector/index.d.ts +0 -1
- package/dist/inspector/index.js +293 -361
- package/dist/migrations/index.cjs +391 -456
- package/dist/migrations/index.d.ts +992 -922
- package/dist/migrations/index.js +326 -392
- package/dist/migrations/{chunk-Bop6jNiL.js → rolldown-runtime-D7D4PA-g.js} +4 -6
- package/dist/relations/index.cjs +277 -322
- package/dist/relations/index.d.ts +1086 -1014
- package/dist/relations/index.js +264 -310
- package/dist/seeders/index.cjs +15 -19
- package/dist/seeders/index.d.ts +990 -918
- package/dist/seeders/index.js +11 -16
- package/dist/{migrations/stubs → stubs}/migration-js.stub +1 -0
- package/dist/{migrations/stubs → stubs}/migration-ts.stub +1 -0
- package/dist/{migrations/stubs → stubs}/migration.create-js.stub +1 -0
- package/dist/{migrations/stubs → stubs}/migration.create-ts.stub +1 -0
- package/dist/{migrations/stubs → stubs}/migration.update-js.stub +1 -0
- package/dist/{migrations/stubs → stubs}/migration.update-ts.stub +1 -0
- package/dist/stubs/model-ts.stub +5 -1
- package/package.json +32 -32
- package/types/builder.ts +1 -1
- package/types/database.ts +69 -0
- package/types/index.ts +3 -0
- package/types/model-builder.ts +132 -0
- package/types/query-builder.ts +0 -1
- package/types/query-methods.ts +0 -1
- package/types/schema.ts +92 -0
- package/types/utils.ts +1 -40
- package/bin/.gitkeep +0 -0
- package/bin/seeders-D-v59HCz.cjs +0 -3
- package/bin/seeders-DIkcRliK.js +0 -3
package/bin/index.cjs
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//#region
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var
|
|
9
|
+
var __exportAll = (all, no_symbols) => {
|
|
10
10
|
let target = {};
|
|
11
11
|
for (var name in all) __defProp(target, name, {
|
|
12
12
|
get: all[name],
|
|
13
13
|
enumerable: true
|
|
14
14
|
});
|
|
15
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
15
16
|
return target;
|
|
16
17
|
};
|
|
17
18
|
var __copyProps = (to, from, except, desc) => {
|
|
@@ -28,67 +29,193 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
29
|
value: mod,
|
|
29
30
|
enumerable: true
|
|
30
31
|
}) : target, mod));
|
|
31
|
-
|
|
32
32
|
//#endregion
|
|
33
|
-
let
|
|
34
|
-
let
|
|
35
|
-
let
|
|
36
|
-
path = __toESM(path);
|
|
37
|
-
let resolve_from = require("resolve-from");
|
|
38
|
-
resolve_from = __toESM(resolve_from);
|
|
33
|
+
let _h3ravel_shared = require("@h3ravel/shared");
|
|
34
|
+
let _h3ravel_musket = require("@h3ravel/musket");
|
|
35
|
+
let _h3ravel_support = require("@h3ravel/support");
|
|
39
36
|
let node_fs_promises = require("node:fs/promises");
|
|
40
|
-
node_fs_promises = __toESM(node_fs_promises);
|
|
37
|
+
node_fs_promises = __toESM(node_fs_promises, 1);
|
|
38
|
+
let path = require("path");
|
|
39
|
+
path = __toESM(path, 1);
|
|
41
40
|
let node_path = require("node:path");
|
|
41
|
+
node_path = __toESM(node_path, 1);
|
|
42
|
+
let node_fs = require("node:fs");
|
|
42
43
|
let node_url = require("node:url");
|
|
43
|
-
let radashi = require("radashi");
|
|
44
|
-
let dayjs_plugin_advancedFormat_js = require("dayjs/plugin/advancedFormat.js");
|
|
45
|
-
dayjs_plugin_advancedFormat_js = __toESM(dayjs_plugin_advancedFormat_js);
|
|
46
44
|
let dayjs = require("dayjs");
|
|
47
|
-
dayjs = __toESM(dayjs);
|
|
48
|
-
let
|
|
49
|
-
|
|
45
|
+
dayjs = __toESM(dayjs, 1);
|
|
46
|
+
let dayjs_plugin_advancedFormat_js = require("dayjs/plugin/advancedFormat.js");
|
|
47
|
+
dayjs_plugin_advancedFormat_js = __toESM(dayjs_plugin_advancedFormat_js, 1);
|
|
48
|
+
let _h3ravel_collect_js = require("@h3ravel/collect.js");
|
|
50
49
|
let knex = require("knex");
|
|
51
|
-
knex = __toESM(knex);
|
|
50
|
+
knex = __toESM(knex, 1);
|
|
52
51
|
let fs = require("fs");
|
|
53
52
|
let pluralize = require("pluralize");
|
|
54
|
-
pluralize = __toESM(pluralize);
|
|
55
|
-
let
|
|
53
|
+
pluralize = __toESM(pluralize, 1);
|
|
54
|
+
let resolve_from = require("resolve-from");
|
|
55
|
+
resolve_from = __toESM(resolve_from, 1);
|
|
56
56
|
let dotenv = require("dotenv");
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
packageName += parts.shift();
|
|
66
|
-
const packageJson = path.default.join(packageName, "package.json");
|
|
67
|
-
const resolved = resolve_from.default.silent(cwd ?? process.cwd(), packageJson);
|
|
68
|
-
if (!resolved) return;
|
|
69
|
-
return path.default.join(path.default.dirname(resolved), parts.join("/"));
|
|
57
|
+
//#region src/cli/commands.ts
|
|
58
|
+
var ArquebusCommand = class extends _h3ravel_musket.Command {
|
|
59
|
+
pathOption(signature) {
|
|
60
|
+
return signature.option("path", {
|
|
61
|
+
short: "p",
|
|
62
|
+
optionalValue: true,
|
|
63
|
+
description: "The path used by the command."
|
|
64
|
+
});
|
|
70
65
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
66
|
+
};
|
|
67
|
+
var InitCommand = class extends ArquebusCommand {
|
|
68
|
+
buildSignature(signature) {
|
|
69
|
+
return signature.command("init").describe("Create a fresh Arquebus config.").argument("type", {
|
|
70
|
+
required: false,
|
|
71
|
+
default: "js",
|
|
72
|
+
choices: ["js", "ts"],
|
|
73
|
+
description: "Type of config to generate."
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async handle() {
|
|
77
|
+
await this.app.initialize(this.argument("type", "js"));
|
|
74
78
|
}
|
|
75
79
|
};
|
|
76
|
-
var
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
80
|
+
var MakeMigrationCommand = class extends ArquebusCommand {
|
|
81
|
+
buildSignature(signature) {
|
|
82
|
+
return this.pathOption(signature.command("make:migration").describe("Create a new migration file.").argument("name", { description: "The migration name." }).option("type", {
|
|
83
|
+
short: "l",
|
|
84
|
+
default: "js",
|
|
85
|
+
choices: ["js", "ts"],
|
|
86
|
+
description: "Type of migration file to generate."
|
|
87
|
+
}).option("table", {
|
|
88
|
+
short: "t",
|
|
89
|
+
optionalValue: true,
|
|
90
|
+
description: "The table to migrate."
|
|
91
|
+
}).option("create", {
|
|
92
|
+
short: "c",
|
|
93
|
+
optionalValue: true,
|
|
94
|
+
description: "The table to create."
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
async handle() {
|
|
98
|
+
await this.app.makeMigration(this.argument("name"), this.options());
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var PublishMigrationsCommand = class extends ArquebusCommand {
|
|
102
|
+
buildSignature(signature) {
|
|
103
|
+
return this.pathOption(signature.command("migrate:publish").describe("Publish migration files from a package.").argument("package", { description: "The package to publish from." }));
|
|
104
|
+
}
|
|
105
|
+
async handle() {
|
|
106
|
+
await this.app.publishMigrations(this.argument("package"), this.options());
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var MigrationPathCommand = class extends ArquebusCommand {
|
|
110
|
+
buildSignature(signature) {
|
|
111
|
+
return this.pathOption(signature.command(this.commandName).describe(this.commandDescription));
|
|
112
|
+
}
|
|
113
|
+
async handle() {
|
|
114
|
+
await this.execute(this.options());
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var MigrateCommand = class extends ArquebusCommand {
|
|
118
|
+
buildSignature(signature) {
|
|
119
|
+
return this.pathOption(signature.command("migrate").describe("Run all pending migrations.").option("step", {
|
|
120
|
+
short: "s",
|
|
121
|
+
optionalValue: true,
|
|
122
|
+
description: "Run migrations in individual batches."
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
async handle() {
|
|
126
|
+
await this.app.migrate(this.options());
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var RollbackCommand = class extends ArquebusCommand {
|
|
130
|
+
buildSignature(signature) {
|
|
131
|
+
return this.pathOption(signature.command("migrate:rollback").describe("Rollback the last database migration.").option("step", {
|
|
132
|
+
short: "s",
|
|
133
|
+
optionalValue: true,
|
|
134
|
+
description: "The number of migrations to revert."
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
async handle() {
|
|
138
|
+
await this.app.rollback(this.options());
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var ResetCommand = class extends MigrationPathCommand {
|
|
142
|
+
commandName = "migrate:reset";
|
|
143
|
+
commandDescription = "Rollback all database migrations.";
|
|
144
|
+
execute(options) {
|
|
145
|
+
return this.app.reset(options);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var RefreshCommand = class extends MigrationPathCommand {
|
|
149
|
+
commandName = "migrate:refresh";
|
|
150
|
+
commandDescription = "Reset and re-run all migrations.";
|
|
151
|
+
execute(options) {
|
|
152
|
+
return this.app.refresh(options);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var FreshCommand = class extends MigrationPathCommand {
|
|
156
|
+
commandName = "migrate:fresh";
|
|
157
|
+
commandDescription = "Drop all tables and re-run all migrations.";
|
|
158
|
+
execute(options) {
|
|
159
|
+
return this.app.fresh(options);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
var MigrationStatusCommand = class extends MigrationPathCommand {
|
|
163
|
+
commandName = "migrate:status";
|
|
164
|
+
commandDescription = "Show the status of each migration.";
|
|
165
|
+
execute(options) {
|
|
166
|
+
return this.app.status(options);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var SeedCommand = class extends MigrationPathCommand {
|
|
170
|
+
commandName = "db:seed";
|
|
171
|
+
commandDescription = "Run database seeders.";
|
|
172
|
+
execute(options) {
|
|
173
|
+
return this.app.seed(options);
|
|
89
174
|
}
|
|
90
175
|
};
|
|
91
|
-
|
|
176
|
+
var MakeFileCommand = class extends ArquebusCommand {
|
|
177
|
+
buildSignature(signature) {
|
|
178
|
+
return this.pathOption(signature.command(this.commandName).describe(this.commandDescription).argument("name", { description: `The ${this.fileDescription} name.` }).option("type", {
|
|
179
|
+
short: "l",
|
|
180
|
+
default: "js",
|
|
181
|
+
choices: ["js", "ts"],
|
|
182
|
+
description: `Type of ${this.fileDescription} file to generate.`
|
|
183
|
+
}).option("force", { description: `Overwrite an existing ${this.fileDescription} file.` }));
|
|
184
|
+
}
|
|
185
|
+
async handle() {
|
|
186
|
+
await this.execute(this.argument("name"), this.options());
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var MakeSeederCommand = class extends MakeFileCommand {
|
|
190
|
+
commandName = "make:seeder";
|
|
191
|
+
commandDescription = "Create a new seeder file.";
|
|
192
|
+
fileDescription = "seeder";
|
|
193
|
+
execute(name, options) {
|
|
194
|
+
return this.app.makeSeeder(name, options);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
var MakeModelCommand = class extends MakeFileCommand {
|
|
198
|
+
commandName = "make:model";
|
|
199
|
+
commandDescription = "Create a new model file.";
|
|
200
|
+
fileDescription = "model";
|
|
201
|
+
execute(name, options) {
|
|
202
|
+
return this.app.makeModel(name, options);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const ArquebusCommands = [
|
|
206
|
+
InitCommand,
|
|
207
|
+
MakeMigrationCommand,
|
|
208
|
+
PublishMigrationsCommand,
|
|
209
|
+
MigrateCommand,
|
|
210
|
+
RollbackCommand,
|
|
211
|
+
ResetCommand,
|
|
212
|
+
RefreshCommand,
|
|
213
|
+
FreshCommand,
|
|
214
|
+
MigrationStatusCommand,
|
|
215
|
+
SeedCommand,
|
|
216
|
+
MakeSeederCommand,
|
|
217
|
+
MakeModelCommand
|
|
218
|
+
];
|
|
92
219
|
//#endregion
|
|
93
220
|
//#region src/migrations/migration-repository.ts
|
|
94
221
|
var MigrationRepository = class {
|
|
@@ -157,7 +284,6 @@ var MigrationRepository = class {
|
|
|
157
284
|
this.connection = name;
|
|
158
285
|
}
|
|
159
286
|
};
|
|
160
|
-
|
|
161
287
|
//#endregion
|
|
162
288
|
//#region src/inspector/utils/strip-quotes.ts
|
|
163
289
|
/**
|
|
@@ -169,7 +295,6 @@ function stripQuotes(value) {
|
|
|
169
295
|
if (trimmed.startsWith("'") && trimmed.endsWith("'") || trimmed.startsWith("\"") && trimmed.endsWith("\"")) return trimmed.slice(1, -1);
|
|
170
296
|
return value;
|
|
171
297
|
}
|
|
172
|
-
|
|
173
298
|
//#endregion
|
|
174
299
|
//#region src/inspector/dialects/cockroachdb.ts
|
|
175
300
|
/**
|
|
@@ -186,9 +311,9 @@ var CockroachDB = class {
|
|
|
186
311
|
knex;
|
|
187
312
|
schema;
|
|
188
313
|
explodedSchema;
|
|
189
|
-
constructor(knex
|
|
190
|
-
this.knex = knex
|
|
191
|
-
const config = knex
|
|
314
|
+
constructor(knex) {
|
|
315
|
+
this.knex = knex;
|
|
316
|
+
const config = knex.client.config;
|
|
192
317
|
if (!config.searchPath) {
|
|
193
318
|
this.schema = "public";
|
|
194
319
|
this.explodedSchema = [this.schema];
|
|
@@ -237,8 +362,7 @@ var CockroachDB = class {
|
|
|
237
362
|
*/
|
|
238
363
|
async hasTable(table) {
|
|
239
364
|
const subquery = this.knex.select().from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhere({ table_name: table });
|
|
240
|
-
|
|
241
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
365
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
242
366
|
}
|
|
243
367
|
/**
|
|
244
368
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -252,12 +376,12 @@ var CockroachDB = class {
|
|
|
252
376
|
}));
|
|
253
377
|
}
|
|
254
378
|
async columnInfo(table, column) {
|
|
255
|
-
const { knex
|
|
379
|
+
const { knex } = this;
|
|
256
380
|
const bindings = [];
|
|
257
381
|
if (table) bindings.push(table);
|
|
258
382
|
if (column) bindings.push(column);
|
|
259
383
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
260
|
-
const [columns, constraints] = await Promise.all([knex
|
|
384
|
+
const [columns, constraints] = await Promise.all([knex.raw(`
|
|
261
385
|
SELECT *, CASE WHEN res.is_generated THEN (
|
|
262
386
|
SELECT
|
|
263
387
|
generation_expression
|
|
@@ -317,7 +441,7 @@ var CockroachDB = class {
|
|
|
317
441
|
AND att.attnum > 0
|
|
318
442
|
AND NOT att.attisdropped
|
|
319
443
|
ORDER BY rel.relname, att.attnum) res;
|
|
320
|
-
`, bindings), knex
|
|
444
|
+
`, bindings), knex.raw(`
|
|
321
445
|
SELECT
|
|
322
446
|
con.contype AS type,
|
|
323
447
|
rel.relname AS table,
|
|
@@ -338,18 +462,17 @@ var CockroachDB = class {
|
|
|
338
462
|
${column ? "AND att.attname = ?" : ""}
|
|
339
463
|
`, bindings)]);
|
|
340
464
|
const parsedColumms = columns.rows.map((col) => {
|
|
341
|
-
var _col$default_value;
|
|
342
465
|
const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
|
|
343
466
|
const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
|
|
344
467
|
return {
|
|
345
468
|
...col,
|
|
346
469
|
is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
|
|
347
470
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
348
|
-
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (
|
|
471
|
+
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (col.default_value?.startsWith("nextval(") ?? false),
|
|
349
472
|
default_value: parseDefaultValue$5(col.default_value),
|
|
350
|
-
foreign_key_schema:
|
|
351
|
-
foreign_key_table:
|
|
352
|
-
foreign_key_column:
|
|
473
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
474
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
475
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
353
476
|
};
|
|
354
477
|
});
|
|
355
478
|
if (table && column) return parsedColumms[0];
|
|
@@ -363,8 +486,7 @@ var CockroachDB = class {
|
|
|
363
486
|
table_name: table,
|
|
364
487
|
column_name: column
|
|
365
488
|
});
|
|
366
|
-
|
|
367
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
489
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
368
490
|
}
|
|
369
491
|
/**
|
|
370
492
|
* Get the primary key column for the given table
|
|
@@ -470,7 +592,6 @@ var CockroachDB = class {
|
|
|
470
592
|
}
|
|
471
593
|
}
|
|
472
594
|
};
|
|
473
|
-
|
|
474
595
|
//#endregion
|
|
475
596
|
//#region src/inspector/dialects/mssql.ts
|
|
476
597
|
function rawColumnToColumn$2(rawColumn) {
|
|
@@ -487,14 +608,14 @@ function rawColumnToColumn$2(rawColumn) {
|
|
|
487
608
|
numeric_scale: rawColumn.numeric_scale || null,
|
|
488
609
|
max_length: parseMaxLength(rawColumn)
|
|
489
610
|
};
|
|
490
|
-
function parseMaxLength(rawColumn
|
|
491
|
-
const max_length = Number(rawColumn
|
|
492
|
-
if (Number.isNaN(max_length) || rawColumn
|
|
611
|
+
function parseMaxLength(rawColumn) {
|
|
612
|
+
const max_length = Number(rawColumn.max_length);
|
|
613
|
+
if (Number.isNaN(max_length) || rawColumn.max_length === null || rawColumn.max_length === void 0) return null;
|
|
493
614
|
if ([
|
|
494
615
|
"nvarchar",
|
|
495
616
|
"nchar",
|
|
496
617
|
"ntext"
|
|
497
|
-
].includes(rawColumn
|
|
618
|
+
].includes(rawColumn.data_type)) return max_length === -1 ? max_length : max_length / 2;
|
|
498
619
|
return max_length;
|
|
499
620
|
}
|
|
500
621
|
}
|
|
@@ -507,8 +628,8 @@ function parseDefaultValue$4(value) {
|
|
|
507
628
|
var MSSQL = class {
|
|
508
629
|
knex;
|
|
509
630
|
_schema;
|
|
510
|
-
constructor(knex
|
|
511
|
-
this.knex = knex
|
|
631
|
+
constructor(knex) {
|
|
632
|
+
this.knex = knex;
|
|
512
633
|
}
|
|
513
634
|
/**
|
|
514
635
|
* Set the schema to be used in other methods
|
|
@@ -677,15 +798,13 @@ var MSSQL = class {
|
|
|
677
798
|
WHERE
|
|
678
799
|
OBJECT_SCHEMA_NAME (f.parent_object_id) = ?;
|
|
679
800
|
`, [this.schema]);
|
|
680
|
-
if (table) return result
|
|
801
|
+
if (table) return result?.filter((row) => row.table === table);
|
|
681
802
|
return result;
|
|
682
803
|
}
|
|
683
804
|
};
|
|
684
|
-
|
|
685
805
|
//#endregion
|
|
686
806
|
//#region src/inspector/dialects/mysql.ts
|
|
687
807
|
function rawColumnToColumn$1(rawColumn) {
|
|
688
|
-
var _rawColumn$EXTRA;
|
|
689
808
|
let dataType = rawColumn.COLUMN_TYPE.replace(/\(.*?\)/, "");
|
|
690
809
|
if (rawColumn.COLUMN_TYPE.startsWith("tinyint(1)")) dataType = "boolean";
|
|
691
810
|
return {
|
|
@@ -698,7 +817,7 @@ function rawColumnToColumn$1(rawColumn) {
|
|
|
698
817
|
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
|
|
699
818
|
numeric_precision: rawColumn.NUMERIC_PRECISION,
|
|
700
819
|
numeric_scale: rawColumn.NUMERIC_SCALE,
|
|
701
|
-
is_generated: !!
|
|
820
|
+
is_generated: !!rawColumn.EXTRA?.endsWith("GENERATED"),
|
|
702
821
|
is_nullable: rawColumn.IS_NULLABLE === "YES",
|
|
703
822
|
is_unique: rawColumn.COLUMN_KEY === "UNI",
|
|
704
823
|
is_primary_key: rawColumn.CONSTRAINT_NAME === "PRIMARY" || rawColumn.COLUMN_KEY === "PRI",
|
|
@@ -714,8 +833,8 @@ function parseDefaultValue$3(value) {
|
|
|
714
833
|
}
|
|
715
834
|
var MySQL = class {
|
|
716
835
|
knex;
|
|
717
|
-
constructor(knex
|
|
718
|
-
this.knex = knex
|
|
836
|
+
constructor(knex) {
|
|
837
|
+
this.knex = knex;
|
|
719
838
|
}
|
|
720
839
|
/**
|
|
721
840
|
* List all existing tables in the current schema/database
|
|
@@ -780,9 +899,9 @@ var MySQL = class {
|
|
|
780
899
|
}).where({ "c.TABLE_SCHEMA": this.knex.client.database() });
|
|
781
900
|
if (table) query.andWhere({ "c.TABLE_NAME": table });
|
|
782
901
|
if (column) return rawColumnToColumn$1(await query.andWhere({ "c.column_name": column }).first());
|
|
783
|
-
return (await query).map(rawColumnToColumn$1).sort((column
|
|
902
|
+
return (await query).map(rawColumnToColumn$1).sort((column) => +!column.foreign_key_column).filter((column, index, records) => {
|
|
784
903
|
return records.findIndex((_column) => {
|
|
785
|
-
return column
|
|
904
|
+
return column.name === _column.name && column.table === _column.table;
|
|
786
905
|
}) === index;
|
|
787
906
|
});
|
|
788
907
|
}
|
|
@@ -816,10 +935,10 @@ var MySQL = class {
|
|
|
816
935
|
return await query;
|
|
817
936
|
}
|
|
818
937
|
async uniqueConstraints(table) {
|
|
819
|
-
const { knex
|
|
820
|
-
const query = knex
|
|
938
|
+
const { knex } = this;
|
|
939
|
+
const query = knex.select("stat.table_name AS table_name", "stat.index_name AS constraint_name", knex.raw("group_concat(stat.column_name ORDER BY stat.seq_in_index separator ', ') AS columns")).from("information_schema.statistics stat").join("information_schema.table_constraints tco", function() {
|
|
821
940
|
this.on("stat.table_schema", "=", "tco.table_schema").andOn("stat.table_name", "=", "tco.table_name").andOn("stat.index_name", "=", "tco.constraint_name");
|
|
822
|
-
}).where("stat.non_unique", "=", 0).andWhere("tco.constraint_type", "=", "UNIQUE").andWhere("stat.table_schema", knex
|
|
941
|
+
}).where("stat.non_unique", "=", 0).andWhere("tco.constraint_type", "=", "UNIQUE").andWhere("stat.table_schema", knex.client.database()).groupBy([
|
|
823
942
|
"stat.table_name",
|
|
824
943
|
"stat.index_name",
|
|
825
944
|
"tco.constraint_type"
|
|
@@ -832,7 +951,6 @@ var MySQL = class {
|
|
|
832
951
|
}));
|
|
833
952
|
}
|
|
834
953
|
};
|
|
835
|
-
|
|
836
954
|
//#endregion
|
|
837
955
|
//#region src/inspector/dialects/oracledb.ts
|
|
838
956
|
/**
|
|
@@ -868,8 +986,8 @@ function parseDefaultValue$2(value) {
|
|
|
868
986
|
}
|
|
869
987
|
var oracleDB = class {
|
|
870
988
|
knex;
|
|
871
|
-
constructor(knex
|
|
872
|
-
this.knex = knex
|
|
989
|
+
constructor(knex) {
|
|
990
|
+
this.knex = knex;
|
|
873
991
|
}
|
|
874
992
|
/**
|
|
875
993
|
* List all existing tables in the current schema/database
|
|
@@ -892,11 +1010,10 @@ var oracleDB = class {
|
|
|
892
1010
|
* Check if a table exists in the current schema/database
|
|
893
1011
|
*/
|
|
894
1012
|
async hasTable(table) {
|
|
895
|
-
|
|
1013
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
896
1014
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
|
|
897
1015
|
COUNT(*) "count"
|
|
898
|
-
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first();
|
|
899
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
1016
|
+
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first())?.count;
|
|
900
1017
|
}
|
|
901
1018
|
/**
|
|
902
1019
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -972,15 +1089,14 @@ var oracleDB = class {
|
|
|
972
1089
|
* Check if a table exists in the current schema/database
|
|
973
1090
|
*/
|
|
974
1091
|
async hasColumn(table, column) {
|
|
975
|
-
|
|
1092
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
976
1093
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
|
|
977
1094
|
COUNT(*) "count"
|
|
978
1095
|
`)).from("USER_TAB_COLS").where({
|
|
979
1096
|
TABLE_NAME: table,
|
|
980
1097
|
COLUMN_NAME: column,
|
|
981
1098
|
HIDDEN_COLUMN: "NO"
|
|
982
|
-
}).first();
|
|
983
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
1099
|
+
}).first())?.count;
|
|
984
1100
|
}
|
|
985
1101
|
/**
|
|
986
1102
|
* Get the primary key column for the given table
|
|
@@ -1030,7 +1146,6 @@ var oracleDB = class {
|
|
|
1030
1146
|
return await query;
|
|
1031
1147
|
}
|
|
1032
1148
|
};
|
|
1033
|
-
|
|
1034
1149
|
//#endregion
|
|
1035
1150
|
//#region src/inspector/dialects/postgres.ts
|
|
1036
1151
|
/**
|
|
@@ -1048,9 +1163,9 @@ var Postgres = class {
|
|
|
1048
1163
|
knex;
|
|
1049
1164
|
schema;
|
|
1050
1165
|
explodedSchema;
|
|
1051
|
-
constructor(knex
|
|
1052
|
-
this.knex = knex
|
|
1053
|
-
const config = knex
|
|
1166
|
+
constructor(knex) {
|
|
1167
|
+
this.knex = knex;
|
|
1168
|
+
const config = knex.client.config;
|
|
1054
1169
|
if (!config.searchPath) {
|
|
1055
1170
|
this.schema = "public";
|
|
1056
1171
|
this.explodedSchema = [this.schema];
|
|
@@ -1147,13 +1262,12 @@ var Postgres = class {
|
|
|
1147
1262
|
`, bindings)).rows;
|
|
1148
1263
|
}
|
|
1149
1264
|
async columnInfo(table, column) {
|
|
1150
|
-
|
|
1151
|
-
const { knex: knex$1 } = this;
|
|
1265
|
+
const { knex } = this;
|
|
1152
1266
|
const bindings = [];
|
|
1153
1267
|
if (table) bindings.push(table);
|
|
1154
1268
|
if (column) bindings.push(column);
|
|
1155
1269
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
1156
|
-
const majorVersion = (
|
|
1270
|
+
const majorVersion = (await this.knex.raw("SHOW server_version")).rows?.[0]?.server_version?.split(".")?.[0] ?? 10;
|
|
1157
1271
|
let generationSelect = `
|
|
1158
1272
|
NULL AS generation_expression,
|
|
1159
1273
|
pg_get_expr(ad.adbin, ad.adrelid) AS default_value,
|
|
@@ -1164,7 +1278,7 @@ var Postgres = class {
|
|
|
1164
1278
|
CASE WHEN att.attgenerated = '' THEN pg_get_expr(ad.adbin, ad.adrelid) ELSE null END AS default_value,
|
|
1165
1279
|
att.attgenerated = 's' AS is_generated,
|
|
1166
1280
|
`;
|
|
1167
|
-
const [columns, constraints] = await Promise.all([knex
|
|
1281
|
+
const [columns, constraints] = await Promise.all([knex.raw(`
|
|
1168
1282
|
SELECT
|
|
1169
1283
|
att.attname AS name,
|
|
1170
1284
|
rel.relname AS table,
|
|
@@ -1212,7 +1326,7 @@ var Postgres = class {
|
|
|
1212
1326
|
AND att.attnum > 0
|
|
1213
1327
|
AND NOT att.attisdropped
|
|
1214
1328
|
ORDER BY rel.relname, att.attnum;
|
|
1215
|
-
`, bindings), knex
|
|
1329
|
+
`, bindings), knex.raw(`
|
|
1216
1330
|
SELECT
|
|
1217
1331
|
con.contype AS type,
|
|
1218
1332
|
rel.relname AS table,
|
|
@@ -1245,9 +1359,9 @@ var Postgres = class {
|
|
|
1245
1359
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
1246
1360
|
has_auto_increment: constraintsForColumn.some((constraint) => constraint.has_auto_increment),
|
|
1247
1361
|
default_value: parseDefaultValue$1(col.default_value),
|
|
1248
|
-
foreign_key_schema:
|
|
1249
|
-
foreign_key_table:
|
|
1250
|
-
foreign_key_column:
|
|
1362
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
1363
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
1364
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
1251
1365
|
};
|
|
1252
1366
|
});
|
|
1253
1367
|
if (table && column) return parsedColumms[0];
|
|
@@ -1346,11 +1460,11 @@ var Postgres = class {
|
|
|
1346
1460
|
`, bindings)).rows;
|
|
1347
1461
|
}
|
|
1348
1462
|
async uniqueConstraints(table) {
|
|
1349
|
-
const { knex
|
|
1463
|
+
const { knex } = this;
|
|
1350
1464
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
1351
1465
|
const bindings = [];
|
|
1352
1466
|
if (table) bindings.push(table);
|
|
1353
|
-
return (await knex
|
|
1467
|
+
return (await knex.raw(`
|
|
1354
1468
|
SELECT
|
|
1355
1469
|
con.conrelid::regclass AS table_name,
|
|
1356
1470
|
con.conname AS constraint_name,
|
|
@@ -1374,7 +1488,6 @@ var Postgres = class {
|
|
|
1374
1488
|
});
|
|
1375
1489
|
}
|
|
1376
1490
|
};
|
|
1377
|
-
|
|
1378
1491
|
//#endregion
|
|
1379
1492
|
//#region src/inspector/utils/extract-max-length.ts
|
|
1380
1493
|
/**
|
|
@@ -1386,7 +1499,6 @@ function extractMaxLength(type) {
|
|
|
1386
1499
|
if (matches && matches.length > 0 && matches[1]) return Number(matches[1]);
|
|
1387
1500
|
return null;
|
|
1388
1501
|
}
|
|
1389
|
-
|
|
1390
1502
|
//#endregion
|
|
1391
1503
|
//#region src/inspector/utils/extract-type.ts
|
|
1392
1504
|
/**
|
|
@@ -1396,7 +1508,6 @@ function extractMaxLength(type) {
|
|
|
1396
1508
|
function extractType(type) {
|
|
1397
1509
|
return type.replace(/[^a-zA-Z]/g, "").toLowerCase();
|
|
1398
1510
|
}
|
|
1399
|
-
|
|
1400
1511
|
//#endregion
|
|
1401
1512
|
//#region src/seeders/runner.ts
|
|
1402
1513
|
async function glob$1(folderPath) {
|
|
@@ -1422,16 +1533,14 @@ var SeederRunner = class {
|
|
|
1422
1533
|
this.resolver = resolver;
|
|
1423
1534
|
}
|
|
1424
1535
|
path(p) {
|
|
1425
|
-
this.paths = Array.from(new Set([...this.paths, p]));
|
|
1536
|
+
this.paths = Array.from(/* @__PURE__ */ new Set([...this.paths, p]));
|
|
1426
1537
|
}
|
|
1427
1538
|
getPaths() {
|
|
1428
1539
|
return this.paths;
|
|
1429
1540
|
}
|
|
1430
1541
|
resolveConnection(connection) {
|
|
1431
|
-
var _getInstance, _ref, _instance$connections;
|
|
1432
1542
|
const name = connection || this.connection || "default";
|
|
1433
|
-
|
|
1434
|
-
if (!!!(instance === null || instance === void 0 || (_instance$connections = instance.connections) === null || _instance$connections === void 0 ? void 0 : _instance$connections[name])) this.resolver.autoLoad().catch(() => {
|
|
1543
|
+
if (!!!(this.resolver.getInstance?.() ?? null)?.connections?.[name]) this.resolver.autoLoad().catch(() => {
|
|
1435
1544
|
/** noop */
|
|
1436
1545
|
});
|
|
1437
1546
|
return this.resolver.fire(name);
|
|
@@ -1468,8 +1577,6 @@ var SeederRunner = class {
|
|
|
1468
1577
|
}
|
|
1469
1578
|
}
|
|
1470
1579
|
};
|
|
1471
|
-
var runner_default = SeederRunner;
|
|
1472
|
-
|
|
1473
1580
|
//#endregion
|
|
1474
1581
|
//#region src/casts/attribute.ts
|
|
1475
1582
|
var Attribute = class Attribute {
|
|
@@ -1477,21 +1584,21 @@ var Attribute = class Attribute {
|
|
|
1477
1584
|
set;
|
|
1478
1585
|
withCaching = false;
|
|
1479
1586
|
withObjectCaching = true;
|
|
1480
|
-
constructor({ get
|
|
1481
|
-
this.get = get
|
|
1482
|
-
this.set = set
|
|
1587
|
+
constructor({ get = null, set = null }) {
|
|
1588
|
+
this.get = get;
|
|
1589
|
+
this.set = set;
|
|
1483
1590
|
}
|
|
1484
|
-
static make({ get
|
|
1591
|
+
static make({ get = null, set = null }) {
|
|
1485
1592
|
return new Attribute({
|
|
1486
|
-
get
|
|
1487
|
-
set
|
|
1593
|
+
get,
|
|
1594
|
+
set
|
|
1488
1595
|
});
|
|
1489
1596
|
}
|
|
1490
|
-
static get(get
|
|
1491
|
-
return new Attribute({ get
|
|
1597
|
+
static get(get) {
|
|
1598
|
+
return new Attribute({ get });
|
|
1492
1599
|
}
|
|
1493
|
-
static set(set
|
|
1494
|
-
return new Attribute({ set
|
|
1600
|
+
static set(set) {
|
|
1601
|
+
return new Attribute({ set });
|
|
1495
1602
|
}
|
|
1496
1603
|
withoutObjectCaching() {
|
|
1497
1604
|
this.withObjectCaching = false;
|
|
@@ -1502,8 +1609,6 @@ var Attribute = class Attribute {
|
|
|
1502
1609
|
return this;
|
|
1503
1610
|
}
|
|
1504
1611
|
};
|
|
1505
|
-
var attribute_default = Attribute;
|
|
1506
|
-
|
|
1507
1612
|
//#endregion
|
|
1508
1613
|
//#region src/errors.ts
|
|
1509
1614
|
var BaseError = class extends Error {
|
|
@@ -1522,7 +1627,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
1522
1627
|
}
|
|
1523
1628
|
setModel(model, ids = []) {
|
|
1524
1629
|
this.model = model;
|
|
1525
|
-
this.ids =
|
|
1630
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
1526
1631
|
this.message = `No query results for model [${model}]`;
|
|
1527
1632
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
1528
1633
|
else this.message += ".";
|
|
@@ -1537,10 +1642,9 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
1537
1642
|
};
|
|
1538
1643
|
var RelationNotFoundError = class extends BaseError {};
|
|
1539
1644
|
var InvalidArgumentError = class extends BaseError {};
|
|
1540
|
-
|
|
1541
1645
|
//#endregion
|
|
1542
1646
|
//#region src/mixin.ts
|
|
1543
|
-
var mixin_exports = /* @__PURE__ */
|
|
1647
|
+
var mixin_exports = /* @__PURE__ */ __exportAll({ compose: () => compose$1 });
|
|
1544
1648
|
/**
|
|
1545
1649
|
* Compose function that merges multiple classes and mixins
|
|
1546
1650
|
*
|
|
@@ -1613,27 +1717,30 @@ function compose$1(Base, ...mixins) {
|
|
|
1613
1717
|
return acc;
|
|
1614
1718
|
}, Base);
|
|
1615
1719
|
}
|
|
1616
|
-
|
|
1720
|
+
//#endregion
|
|
1721
|
+
//#region src/dayjs.ts
|
|
1722
|
+
const dayjs$1 = dayjs.default ?? dayjs;
|
|
1723
|
+
const advancedFormat = dayjs_plugin_advancedFormat_js.default ?? dayjs_plugin_advancedFormat_js;
|
|
1724
|
+
dayjs$1.extend(advancedFormat);
|
|
1617
1725
|
//#endregion
|
|
1618
1726
|
//#region src/utils.ts
|
|
1619
|
-
dayjs.default.extend(dayjs_plugin_advancedFormat_js.default);
|
|
1620
1727
|
const getRelationName = (relationMethod) => {
|
|
1621
|
-
return
|
|
1728
|
+
return _h3ravel_support.Str.snake(relationMethod.substring(8));
|
|
1622
1729
|
};
|
|
1623
1730
|
const getRelationMethod = (relation) => {
|
|
1624
|
-
return
|
|
1731
|
+
return _h3ravel_support.Str.camel(`relation_${relation}`);
|
|
1625
1732
|
};
|
|
1626
1733
|
const getScopeMethod = (scope) => {
|
|
1627
|
-
return
|
|
1734
|
+
return _h3ravel_support.Str.camel(`scope_${scope}`);
|
|
1628
1735
|
};
|
|
1629
1736
|
const getAttrMethod = (attr) => {
|
|
1630
|
-
return
|
|
1737
|
+
return _h3ravel_support.Str.camel(`attribute_${attr}`);
|
|
1631
1738
|
};
|
|
1632
1739
|
const getGetterMethod = (attr) => {
|
|
1633
|
-
return
|
|
1740
|
+
return _h3ravel_support.Str.camel(`get_${attr}_attribute`);
|
|
1634
1741
|
};
|
|
1635
1742
|
const getSetterMethod = (attr) => {
|
|
1636
|
-
return
|
|
1743
|
+
return _h3ravel_support.Str.camel(`set_${attr}_attribute`);
|
|
1637
1744
|
};
|
|
1638
1745
|
/**
|
|
1639
1746
|
* Tap into a model a collection instance
|
|
@@ -1649,8 +1756,7 @@ const tap = (instance, callback) => {
|
|
|
1649
1756
|
const { compose } = mixin_exports;
|
|
1650
1757
|
const flatten = (arr) => arr.flat();
|
|
1651
1758
|
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
1652
|
-
const snakeCase = (str) =>
|
|
1653
|
-
|
|
1759
|
+
const snakeCase = (str) => _h3ravel_support.Str.trim(_h3ravel_support.Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
|
|
1654
1760
|
//#endregion
|
|
1655
1761
|
//#region src/relations/relation.ts
|
|
1656
1762
|
var Relation = class {
|
|
@@ -1737,16 +1843,13 @@ var Relation = class {
|
|
|
1737
1843
|
return this.parent.getQualifiedKeyName();
|
|
1738
1844
|
}
|
|
1739
1845
|
getExistenceCompareKey() {
|
|
1740
|
-
|
|
1741
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
1846
|
+
return this.getQualifiedForeignKeyName?.();
|
|
1742
1847
|
}
|
|
1743
1848
|
};
|
|
1744
|
-
var relation_default = Relation;
|
|
1745
|
-
|
|
1746
1849
|
//#endregion
|
|
1747
1850
|
//#region src/relations/concerns/supports-default-models.ts
|
|
1748
|
-
const SupportsDefaultModels = (Relation
|
|
1749
|
-
return class extends Relation
|
|
1851
|
+
const SupportsDefaultModels = (Relation) => {
|
|
1852
|
+
return class extends Relation {
|
|
1750
1853
|
_withDefault;
|
|
1751
1854
|
withDefault(callback = true) {
|
|
1752
1855
|
this._withDefault = callback;
|
|
@@ -1761,11 +1864,9 @@ const SupportsDefaultModels = (Relation$1) => {
|
|
|
1761
1864
|
}
|
|
1762
1865
|
};
|
|
1763
1866
|
};
|
|
1764
|
-
var supports_default_models_default = SupportsDefaultModels;
|
|
1765
|
-
|
|
1766
1867
|
//#endregion
|
|
1767
1868
|
//#region src/relations/belongs-to.ts
|
|
1768
|
-
var BelongsTo = class extends compose(
|
|
1869
|
+
var BelongsTo = class extends compose(Relation, SupportsDefaultModels) {
|
|
1769
1870
|
foreignKey;
|
|
1770
1871
|
ownerKey;
|
|
1771
1872
|
child;
|
|
@@ -1850,8 +1951,6 @@ var BelongsTo = class extends compose(relation_default, supports_default_models_
|
|
|
1850
1951
|
return this.related.newInstance();
|
|
1851
1952
|
}
|
|
1852
1953
|
};
|
|
1853
|
-
var belongs_to_default = BelongsTo;
|
|
1854
|
-
|
|
1855
1954
|
//#endregion
|
|
1856
1955
|
//#region src/casts-attributes.ts
|
|
1857
1956
|
var CastsAttributes = class CastsAttributes {
|
|
@@ -1865,12 +1964,10 @@ var CastsAttributes = class CastsAttributes {
|
|
|
1865
1964
|
throw new Error("set not implemented");
|
|
1866
1965
|
}
|
|
1867
1966
|
};
|
|
1868
|
-
var casts_attributes_default = CastsAttributes;
|
|
1869
|
-
|
|
1870
1967
|
//#endregion
|
|
1871
1968
|
//#region src/concerns/has-attributes.ts
|
|
1872
|
-
const HasAttributes = (Model
|
|
1873
|
-
return class extends Model
|
|
1969
|
+
const HasAttributes = (Model) => {
|
|
1970
|
+
return class extends Model {
|
|
1874
1971
|
static castTypeCache = {};
|
|
1875
1972
|
attributes = {};
|
|
1876
1973
|
original = {};
|
|
@@ -1887,8 +1984,7 @@ const HasAttributes = (Model$1) => {
|
|
|
1887
1984
|
return this;
|
|
1888
1985
|
}
|
|
1889
1986
|
normalizeCastClassResponse(key, value) {
|
|
1890
|
-
|
|
1891
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
1987
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
1892
1988
|
}
|
|
1893
1989
|
syncOriginal() {
|
|
1894
1990
|
this.original = this.getAttributes();
|
|
@@ -1947,8 +2043,8 @@ const HasAttributes = (Model$1) => {
|
|
|
1947
2043
|
}
|
|
1948
2044
|
const attrMethod = getAttrMethod(key);
|
|
1949
2045
|
if (typeof this[attrMethod] === "function") {
|
|
1950
|
-
const callback = this[attrMethod]().set || ((value
|
|
1951
|
-
this.attributes[key] = value
|
|
2046
|
+
const callback = this[attrMethod]().set || ((value) => {
|
|
2047
|
+
this.attributes[key] = value;
|
|
1952
2048
|
});
|
|
1953
2049
|
this.attributes = {
|
|
1954
2050
|
...this.attributes,
|
|
@@ -1998,9 +2094,9 @@ const HasAttributes = (Model$1) => {
|
|
|
1998
2094
|
return null;
|
|
1999
2095
|
}
|
|
2000
2096
|
case "collection": try {
|
|
2001
|
-
return (0,
|
|
2097
|
+
return (0, _h3ravel_collect_js.collect)(JSON.parse(value));
|
|
2002
2098
|
} catch {
|
|
2003
|
-
return (0,
|
|
2099
|
+
return (0, _h3ravel_collect_js.collect)([]);
|
|
2004
2100
|
}
|
|
2005
2101
|
case "date": return this.asDate(value);
|
|
2006
2102
|
case "datetime":
|
|
@@ -2013,8 +2109,8 @@ const HasAttributes = (Model$1) => {
|
|
|
2013
2109
|
attributesToData() {
|
|
2014
2110
|
let attributes = { ...this.attributes };
|
|
2015
2111
|
for (const key in attributes) {
|
|
2016
|
-
if (this.hidden.includes(key)) attributes =
|
|
2017
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
2112
|
+
if (this.hidden.includes(key)) attributes = _h3ravel_support.Arr.except(attributes, [key]);
|
|
2113
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = _h3ravel_support.Arr.except(attributes, [key]);
|
|
2018
2114
|
}
|
|
2019
2115
|
for (const key of this.getDates()) {
|
|
2020
2116
|
if (attributes[key] === void 0) continue;
|
|
@@ -2026,7 +2122,7 @@ const HasAttributes = (Model$1) => {
|
|
|
2026
2122
|
if (key in attributes === false) continue;
|
|
2027
2123
|
attributes[key] = this.castAttribute(key, attributes[key]);
|
|
2028
2124
|
if (key in attributes && ["date", "datetime"].includes(String(value))) attributes[key] = this.serializeDate(attributes[key]);
|
|
2029
|
-
if (key in attributes && this.isCustomDateTimeCast(value)) attributes[key] =
|
|
2125
|
+
if (key in attributes && this.isCustomDateTimeCast(value)) attributes[key] = dayjs$1(attributes[key]).format(String(value).split(":")[1]);
|
|
2030
2126
|
}
|
|
2031
2127
|
for (const key of this.appends) attributes[key] = this.mutateAttribute(key, null);
|
|
2032
2128
|
return attributes;
|
|
@@ -2042,7 +2138,7 @@ const HasAttributes = (Model$1) => {
|
|
|
2042
2138
|
return this.getDates().includes(key) || this.isDateCastable(key);
|
|
2043
2139
|
}
|
|
2044
2140
|
serializeDate(date) {
|
|
2045
|
-
return date ?
|
|
2141
|
+
return date ? dayjs$1(date).toISOString() : null;
|
|
2046
2142
|
}
|
|
2047
2143
|
getDates() {
|
|
2048
2144
|
return this.usesTimestamps() ? [this.getCreatedAtColumn(), this.getUpdatedAtColumn()] : [];
|
|
@@ -2058,7 +2154,7 @@ const HasAttributes = (Model$1) => {
|
|
|
2058
2154
|
const castType = this.getCasts()[key];
|
|
2059
2155
|
let castTypeCacheKey;
|
|
2060
2156
|
if (typeof castType === "string") castTypeCacheKey = castType;
|
|
2061
|
-
else if (new castType() instanceof
|
|
2157
|
+
else if (new castType() instanceof CastsAttributes) castTypeCacheKey = castType.name;
|
|
2062
2158
|
if (castTypeCacheKey && this.getConstructor().castTypeCache[castTypeCacheKey] !== void 0) return this.getConstructor().castTypeCache[castTypeCacheKey];
|
|
2063
2159
|
let convertedCastType;
|
|
2064
2160
|
if (this.isCustomDateTimeCast(castType)) convertedCastType = "custom_datetime";
|
|
@@ -2069,16 +2165,16 @@ const HasAttributes = (Model$1) => {
|
|
|
2069
2165
|
}
|
|
2070
2166
|
hasCast(key, types = []) {
|
|
2071
2167
|
if (key in this.casts) {
|
|
2072
|
-
types = (
|
|
2168
|
+
types = flatten(types);
|
|
2073
2169
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
2074
2170
|
}
|
|
2075
2171
|
return false;
|
|
2076
2172
|
}
|
|
2077
2173
|
withDayjs(date) {
|
|
2078
|
-
return
|
|
2174
|
+
return dayjs$1(date);
|
|
2079
2175
|
}
|
|
2080
2176
|
isCustomCast(cast) {
|
|
2081
|
-
return typeof cast === "function" && new cast() instanceof
|
|
2177
|
+
return typeof cast === "function" && new cast() instanceof CastsAttributes;
|
|
2082
2178
|
}
|
|
2083
2179
|
isCustomDateTimeCast(cast) {
|
|
2084
2180
|
if (typeof cast !== "string") return false;
|
|
@@ -2092,7 +2188,7 @@ const HasAttributes = (Model$1) => {
|
|
|
2092
2188
|
return this.hasCast(key, ["date", "datetime"]);
|
|
2093
2189
|
}
|
|
2094
2190
|
fromDateTime(value) {
|
|
2095
|
-
return
|
|
2191
|
+
return dayjs$1(this.asDateTime(value)).format(this.getDateFormat());
|
|
2096
2192
|
}
|
|
2097
2193
|
getDateFormat() {
|
|
2098
2194
|
return this.dateFormat || "YYYY-MM-DD HH:mm:ss";
|
|
@@ -2107,12 +2203,10 @@ const HasAttributes = (Model$1) => {
|
|
|
2107
2203
|
return new Date(value);
|
|
2108
2204
|
}
|
|
2109
2205
|
asDate(value) {
|
|
2110
|
-
return
|
|
2206
|
+
return dayjs$1(this.asDateTime(value)).startOf("day").toDate();
|
|
2111
2207
|
}
|
|
2112
2208
|
};
|
|
2113
2209
|
};
|
|
2114
|
-
var has_attributes_default = HasAttributes;
|
|
2115
|
-
|
|
2116
2210
|
//#endregion
|
|
2117
2211
|
//#region src/scope.ts
|
|
2118
2212
|
var Scope = class Scope {
|
|
@@ -2123,19 +2217,21 @@ var Scope = class Scope {
|
|
|
2123
2217
|
throw new Error("apply not implemented");
|
|
2124
2218
|
}
|
|
2125
2219
|
};
|
|
2126
|
-
var scope_default = Scope;
|
|
2127
|
-
|
|
2128
2220
|
//#endregion
|
|
2129
2221
|
//#region src/concerns/has-global-scopes.ts
|
|
2130
|
-
const HasGlobalScopes = (Model
|
|
2131
|
-
return class extends Model
|
|
2222
|
+
const HasGlobalScopes = (Model) => {
|
|
2223
|
+
return class extends Model {
|
|
2132
2224
|
static globalScopes;
|
|
2133
2225
|
static addGlobalScope(scope, implementation = null) {
|
|
2134
|
-
if (typeof scope === "string" && implementation instanceof
|
|
2135
|
-
|
|
2226
|
+
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
2227
|
+
const scopes = this.globalScopes ?? {};
|
|
2228
|
+
(0, _h3ravel_support.data_set)(scopes, this.name + "." + scope, implementation);
|
|
2229
|
+
this.globalScopes = scopes;
|
|
2136
2230
|
return implementation;
|
|
2137
|
-
} else if (scope instanceof
|
|
2138
|
-
|
|
2231
|
+
} else if (scope instanceof Scope) {
|
|
2232
|
+
const scopes = this.globalScopes ?? {};
|
|
2233
|
+
(0, _h3ravel_support.data_set)(scopes, this.name + "." + scope.constructor.name, scope);
|
|
2234
|
+
this.globalScopes = scopes;
|
|
2139
2235
|
return scope;
|
|
2140
2236
|
}
|
|
2141
2237
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -2144,8 +2240,8 @@ const HasGlobalScopes = (Model$1) => {
|
|
|
2144
2240
|
return this.getGlobalScope(scope) !== null;
|
|
2145
2241
|
}
|
|
2146
2242
|
static getGlobalScope(scope) {
|
|
2147
|
-
if (typeof scope === "string") return (0,
|
|
2148
|
-
return (0,
|
|
2243
|
+
if (typeof scope === "string") return (0, _h3ravel_support.data_get)(this.globalScopes ?? {}, this.name + "." + scope);
|
|
2244
|
+
return (0, _h3ravel_support.data_get)(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
2149
2245
|
}
|
|
2150
2246
|
static getAllGlobalScopes() {
|
|
2151
2247
|
return this.globalScopes;
|
|
@@ -2154,12 +2250,10 @@ const HasGlobalScopes = (Model$1) => {
|
|
|
2154
2250
|
this.globalScopes = scopes;
|
|
2155
2251
|
}
|
|
2156
2252
|
getGlobalScopes() {
|
|
2157
|
-
return (0,
|
|
2253
|
+
return (0, _h3ravel_support.data_get)(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
2158
2254
|
}
|
|
2159
2255
|
};
|
|
2160
2256
|
};
|
|
2161
|
-
var has_global_scopes_default = HasGlobalScopes;
|
|
2162
|
-
|
|
2163
2257
|
//#endregion
|
|
2164
2258
|
//#region src/hooks.ts
|
|
2165
2259
|
var Hooks = class {
|
|
@@ -2187,15 +2281,13 @@ var Hooks = class {
|
|
|
2187
2281
|
return true;
|
|
2188
2282
|
}
|
|
2189
2283
|
};
|
|
2190
|
-
var hooks_default = Hooks;
|
|
2191
|
-
|
|
2192
2284
|
//#endregion
|
|
2193
2285
|
//#region src/concerns/has-hooks.ts
|
|
2194
|
-
const HasHooks = (Model
|
|
2195
|
-
return class extends Model
|
|
2286
|
+
const HasHooks = (Model) => {
|
|
2287
|
+
return class extends Model {
|
|
2196
2288
|
static hooks = null;
|
|
2197
2289
|
static addHook(hook, callback) {
|
|
2198
|
-
if (this.hooks instanceof
|
|
2290
|
+
if (this.hooks instanceof Hooks === false) this.hooks = new Hooks();
|
|
2199
2291
|
this.hooks.add(hook, callback);
|
|
2200
2292
|
}
|
|
2201
2293
|
static creating(callback) {
|
|
@@ -2235,20 +2327,18 @@ const HasHooks = (Model$1) => {
|
|
|
2235
2327
|
this.addHook("forceDeleted", callback);
|
|
2236
2328
|
}
|
|
2237
2329
|
async execHooks(hook, options) {
|
|
2238
|
-
if (this.constructor.hooks instanceof
|
|
2330
|
+
if (this.constructor.hooks instanceof Hooks === false) return;
|
|
2239
2331
|
return await this.constructor.hooks.exec(hook, [this, options]);
|
|
2240
2332
|
}
|
|
2241
2333
|
};
|
|
2242
2334
|
};
|
|
2243
|
-
var has_hooks_default = HasHooks;
|
|
2244
|
-
|
|
2245
2335
|
//#endregion
|
|
2246
2336
|
//#region src/relations/has-one-or-many.ts
|
|
2247
|
-
const HasOneOrMany = (Relation
|
|
2248
|
-
return class extends Relation
|
|
2337
|
+
const HasOneOrMany = (Relation) => {
|
|
2338
|
+
return class extends Relation {
|
|
2249
2339
|
getRelationValue(dictionary, key, type) {
|
|
2250
2340
|
const value = dictionary[key];
|
|
2251
|
-
return type === "one" ? value[0] : new
|
|
2341
|
+
return type === "one" ? value[0] : new Collection(value);
|
|
2252
2342
|
}
|
|
2253
2343
|
matchOneOrMany(models, results, relation, type) {
|
|
2254
2344
|
const dictionary = this.buildDictionary(results);
|
|
@@ -2260,7 +2350,7 @@ const HasOneOrMany = (Relation$1) => {
|
|
|
2260
2350
|
}
|
|
2261
2351
|
buildDictionary(results) {
|
|
2262
2352
|
const foreign = this.getForeignKeyName();
|
|
2263
|
-
return (0,
|
|
2353
|
+
return (0, _h3ravel_collect_js.collect)(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
2264
2354
|
}
|
|
2265
2355
|
async save(model) {
|
|
2266
2356
|
this.setForeignAttributesForCreate(model);
|
|
@@ -2270,7 +2360,7 @@ const HasOneOrMany = (Relation$1) => {
|
|
|
2270
2360
|
await Promise.all(models.map(async (model) => {
|
|
2271
2361
|
await this.save(model);
|
|
2272
2362
|
}));
|
|
2273
|
-
return models instanceof
|
|
2363
|
+
return models instanceof Collection ? models : new Collection(models);
|
|
2274
2364
|
}
|
|
2275
2365
|
async create(attributes = {}) {
|
|
2276
2366
|
return await tap(this.related.constructor.init(attributes), async (instance) => {
|
|
@@ -2282,7 +2372,7 @@ const HasOneOrMany = (Relation$1) => {
|
|
|
2282
2372
|
const instances = await Promise.all(records.map(async (record) => {
|
|
2283
2373
|
return await this.create(record);
|
|
2284
2374
|
}));
|
|
2285
|
-
return instances instanceof
|
|
2375
|
+
return instances instanceof Collection ? instances : new Collection(instances);
|
|
2286
2376
|
}
|
|
2287
2377
|
setForeignAttributesForCreate(model) {
|
|
2288
2378
|
model[this.getForeignKeyName()] = this.getParentKey();
|
|
@@ -2309,11 +2399,9 @@ const HasOneOrMany = (Relation$1) => {
|
|
|
2309
2399
|
}
|
|
2310
2400
|
};
|
|
2311
2401
|
};
|
|
2312
|
-
var has_one_or_many_default = HasOneOrMany;
|
|
2313
|
-
|
|
2314
2402
|
//#endregion
|
|
2315
2403
|
//#region src/relations/has-many.ts
|
|
2316
|
-
var HasMany = class extends compose(
|
|
2404
|
+
var HasMany = class extends compose(Relation, HasOneOrMany) {
|
|
2317
2405
|
foreignKey;
|
|
2318
2406
|
localKey;
|
|
2319
2407
|
constructor(query, parent, foreignKey, localKey) {
|
|
@@ -2325,21 +2413,19 @@ var HasMany = class extends compose(relation_default, has_one_or_many_default) {
|
|
|
2325
2413
|
}
|
|
2326
2414
|
initRelation(models, relation) {
|
|
2327
2415
|
models.map((model) => {
|
|
2328
|
-
model.setRelation(relation, new
|
|
2416
|
+
model.setRelation(relation, new Collection([]));
|
|
2329
2417
|
});
|
|
2330
2418
|
return models;
|
|
2331
2419
|
}
|
|
2332
2420
|
async getResults() {
|
|
2333
|
-
return this.getParentKey() !== null ? await this.query.get() : new
|
|
2421
|
+
return this.getParentKey() !== null ? await this.query.get() : new Collection([]);
|
|
2334
2422
|
}
|
|
2335
2423
|
getForeignKeyName() {
|
|
2336
|
-
|
|
2337
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2338
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2424
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2339
2425
|
}
|
|
2340
2426
|
buildDictionary(results) {
|
|
2341
2427
|
const foreign = this.getForeignKeyName();
|
|
2342
|
-
return (0,
|
|
2428
|
+
return (0, _h3ravel_collect_js.collect)(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
2343
2429
|
}
|
|
2344
2430
|
match(models, results, relation) {
|
|
2345
2431
|
return this.matchOneOrMany(models, results, relation, "many");
|
|
@@ -2348,11 +2434,9 @@ var HasMany = class extends compose(relation_default, has_one_or_many_default) {
|
|
|
2348
2434
|
this.query.whereIn(this.foreignKey, this.getKeys(models, this.localKey));
|
|
2349
2435
|
}
|
|
2350
2436
|
};
|
|
2351
|
-
var has_many_default = HasMany;
|
|
2352
|
-
|
|
2353
2437
|
//#endregion
|
|
2354
2438
|
//#region src/relations/has-one.ts
|
|
2355
|
-
var HasOne = class extends compose(
|
|
2439
|
+
var HasOne = class extends compose(Relation, HasOneOrMany, SupportsDefaultModels) {
|
|
2356
2440
|
foreignKey;
|
|
2357
2441
|
localKey;
|
|
2358
2442
|
constructor(query, parent, foreignKey, localKey) {
|
|
@@ -2372,9 +2456,7 @@ var HasOne = class extends compose(relation_default, has_one_or_many_default, su
|
|
|
2372
2456
|
return this.matchOneOrMany(models, results, relation, "one");
|
|
2373
2457
|
}
|
|
2374
2458
|
getForeignKeyName() {
|
|
2375
|
-
|
|
2376
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2377
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2459
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2378
2460
|
}
|
|
2379
2461
|
async getResults() {
|
|
2380
2462
|
if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
|
|
@@ -2390,11 +2472,9 @@ var HasOne = class extends compose(relation_default, has_one_or_many_default, su
|
|
|
2390
2472
|
return this.related.newInstance().setAttribute(this.getForeignKeyName(), parent[this.localKey]);
|
|
2391
2473
|
}
|
|
2392
2474
|
};
|
|
2393
|
-
var has_one_default = HasOne;
|
|
2394
|
-
|
|
2395
2475
|
//#endregion
|
|
2396
2476
|
//#region src/relations/has-many-through.ts
|
|
2397
|
-
var HasManyThrough = class extends
|
|
2477
|
+
var HasManyThrough = class extends Relation {
|
|
2398
2478
|
throughParent;
|
|
2399
2479
|
farParent;
|
|
2400
2480
|
firstKey;
|
|
@@ -2420,8 +2500,8 @@ var HasManyThrough = class extends relation_default {
|
|
|
2420
2500
|
query = query || this.query;
|
|
2421
2501
|
const farKey = this.getQualifiedFarKeyName();
|
|
2422
2502
|
query.join(this.throughParent.getTable(), this.getQualifiedParentKeyName(), "=", farKey);
|
|
2423
|
-
if (this.throughParentSoftDeletes()) query.withGlobalScope("SoftDeletableHasManyThrough", (query
|
|
2424
|
-
query
|
|
2503
|
+
if (this.throughParentSoftDeletes()) query.withGlobalScope("SoftDeletableHasManyThrough", (query) => {
|
|
2504
|
+
query.whereNull(this.throughParent.getQualifiedDeletedAtColumn());
|
|
2425
2505
|
});
|
|
2426
2506
|
}
|
|
2427
2507
|
getQualifiedParentKeyName() {
|
|
@@ -2485,10 +2565,10 @@ var HasManyThrough = class extends relation_default {
|
|
|
2485
2565
|
}
|
|
2486
2566
|
const model = await this.first(columns);
|
|
2487
2567
|
if (model) return model;
|
|
2488
|
-
return callback
|
|
2568
|
+
return callback?.();
|
|
2489
2569
|
}
|
|
2490
2570
|
async find(id, columns = ["*"]) {
|
|
2491
|
-
if (
|
|
2571
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
2492
2572
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
2493
2573
|
}
|
|
2494
2574
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -2516,7 +2596,7 @@ var HasManyThrough = class extends relation_default {
|
|
|
2516
2596
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
2517
2597
|
}
|
|
2518
2598
|
shouldSelect(columns = ["*"]) {
|
|
2519
|
-
if (
|
|
2599
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
2520
2600
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
2521
2601
|
}
|
|
2522
2602
|
async chunk(count, callback) {
|
|
@@ -2572,11 +2652,9 @@ var HasManyThrough = class extends relation_default {
|
|
|
2572
2652
|
return this.secondLocalKey;
|
|
2573
2653
|
}
|
|
2574
2654
|
};
|
|
2575
|
-
var has_many_through_default = HasManyThrough;
|
|
2576
|
-
|
|
2577
2655
|
//#endregion
|
|
2578
2656
|
//#region src/relations/has-one-through.ts
|
|
2579
|
-
var HasOneThrough = class extends compose(
|
|
2657
|
+
var HasOneThrough = class extends compose(HasManyThrough, SupportsDefaultModels) {
|
|
2580
2658
|
async getResults() {
|
|
2581
2659
|
return await this.first() || this.getDefaultFor(this.farParent);
|
|
2582
2660
|
}
|
|
@@ -2599,12 +2677,10 @@ var HasOneThrough = class extends compose(has_many_through_default, supports_def
|
|
|
2599
2677
|
return this.related.newInstance();
|
|
2600
2678
|
}
|
|
2601
2679
|
};
|
|
2602
|
-
var has_one_through_default = HasOneThrough;
|
|
2603
|
-
|
|
2604
2680
|
//#endregion
|
|
2605
2681
|
//#region src/concerns/has-relations.ts
|
|
2606
|
-
const HasRelations = (Model
|
|
2607
|
-
return class extends Model
|
|
2682
|
+
const HasRelations = (Model) => {
|
|
2683
|
+
return class extends Model {
|
|
2608
2684
|
relations = {};
|
|
2609
2685
|
getRelation(relation) {
|
|
2610
2686
|
return this.relations[relation];
|
|
@@ -2614,7 +2690,7 @@ const HasRelations = (Model$1) => {
|
|
|
2614
2690
|
return this;
|
|
2615
2691
|
}
|
|
2616
2692
|
unsetRelation(relation) {
|
|
2617
|
-
this.relations =
|
|
2693
|
+
this.relations = _h3ravel_support.Arr.except(this.relations, [relation]);
|
|
2618
2694
|
return this;
|
|
2619
2695
|
}
|
|
2620
2696
|
relationLoaded(relation) {
|
|
@@ -2637,8 +2713,7 @@ const HasRelations = (Model$1) => {
|
|
|
2637
2713
|
return data;
|
|
2638
2714
|
}
|
|
2639
2715
|
guessBelongsToRelation() {
|
|
2640
|
-
|
|
2641
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
2716
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
2642
2717
|
return getRelationName(functionName);
|
|
2643
2718
|
}
|
|
2644
2719
|
joiningTable(related, instance = null) {
|
|
@@ -2652,14 +2727,14 @@ const HasRelations = (Model$1) => {
|
|
|
2652
2727
|
const instance = new related();
|
|
2653
2728
|
foreignKey = foreignKey || this.getForeignKey();
|
|
2654
2729
|
localKey = localKey || this.getKeyName();
|
|
2655
|
-
return new
|
|
2730
|
+
return new HasOne(query, this, instance.getTable() + "." + foreignKey, localKey);
|
|
2656
2731
|
}
|
|
2657
2732
|
hasMany(related, foreignKey, localKey) {
|
|
2658
2733
|
const query = related.query();
|
|
2659
2734
|
const instance = new related();
|
|
2660
2735
|
foreignKey = foreignKey || this.getForeignKey();
|
|
2661
2736
|
localKey = localKey || this.getKeyName();
|
|
2662
|
-
return new
|
|
2737
|
+
return new HasMany(query, this, instance.getTable() + "." + foreignKey, localKey);
|
|
2663
2738
|
}
|
|
2664
2739
|
belongsTo(related, foreignKey, ownerKey, relation) {
|
|
2665
2740
|
const query = related.query();
|
|
@@ -2667,7 +2742,7 @@ const HasRelations = (Model$1) => {
|
|
|
2667
2742
|
foreignKey = foreignKey || instance.getForeignKey();
|
|
2668
2743
|
ownerKey = ownerKey || instance.getKeyName();
|
|
2669
2744
|
relation = relation || this.guessBelongsToRelation();
|
|
2670
|
-
return new
|
|
2745
|
+
return new BelongsTo(query, this, foreignKey, ownerKey, relation);
|
|
2671
2746
|
}
|
|
2672
2747
|
belongsToMany(related, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
|
|
2673
2748
|
const query = related.query();
|
|
@@ -2677,30 +2752,28 @@ const HasRelations = (Model$1) => {
|
|
|
2677
2752
|
relatedPivotKey = relatedPivotKey || instance.getForeignKey();
|
|
2678
2753
|
parentKey = parentKey || this.getKeyName();
|
|
2679
2754
|
relatedKey = relatedKey || instance.getKeyName();
|
|
2680
|
-
return new
|
|
2755
|
+
return new BelongsToMany(query, this, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey);
|
|
2681
2756
|
}
|
|
2682
2757
|
hasOneThrough(related, through, firstKey, secondKey, localKey, secondLocalKey) {
|
|
2683
2758
|
through = new through();
|
|
2684
2759
|
const query = related.query();
|
|
2685
2760
|
firstKey = firstKey || this.getForeignKey();
|
|
2686
2761
|
secondKey = secondKey || through.getForeignKey();
|
|
2687
|
-
return new
|
|
2762
|
+
return new HasOneThrough(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
|
|
2688
2763
|
}
|
|
2689
2764
|
hasManyThrough(related, through, firstKey, secondKey, localKey, secondLocalKey) {
|
|
2690
2765
|
through = new through();
|
|
2691
2766
|
const query = related.query();
|
|
2692
2767
|
firstKey = firstKey || this.getForeignKey();
|
|
2693
2768
|
secondKey = secondKey || through.getForeignKey();
|
|
2694
|
-
return new
|
|
2769
|
+
return new HasManyThrough(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
|
|
2695
2770
|
}
|
|
2696
2771
|
};
|
|
2697
2772
|
};
|
|
2698
|
-
var has_relations_default = HasRelations;
|
|
2699
|
-
|
|
2700
2773
|
//#endregion
|
|
2701
2774
|
//#region src/concerns/has-timestamps.ts
|
|
2702
|
-
const HasTimestamps = (Model
|
|
2703
|
-
return class extends Model
|
|
2775
|
+
const HasTimestamps = (Model) => {
|
|
2776
|
+
return class extends Model {
|
|
2704
2777
|
static CREATED_AT = "created_at";
|
|
2705
2778
|
static UPDATED_AT = "updated_at";
|
|
2706
2779
|
static DELETED_AT = "deleted_at";
|
|
@@ -2741,18 +2814,16 @@ const HasTimestamps = (Model$1) => {
|
|
|
2741
2814
|
}
|
|
2742
2815
|
};
|
|
2743
2816
|
};
|
|
2744
|
-
var has_timestamps_default = HasTimestamps;
|
|
2745
|
-
|
|
2746
2817
|
//#endregion
|
|
2747
2818
|
//#region src/concerns/hides-attributes.ts
|
|
2748
|
-
const HidesAttributes = (Model
|
|
2749
|
-
return class extends Model
|
|
2819
|
+
const HidesAttributes = (Model) => {
|
|
2820
|
+
return class extends Model {
|
|
2750
2821
|
hidden = [];
|
|
2751
2822
|
visible = [];
|
|
2752
2823
|
makeVisible(...keys) {
|
|
2753
2824
|
const visible = flattenDeep(keys);
|
|
2754
2825
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
2755
|
-
this.hidden = (0,
|
|
2826
|
+
this.hidden = (0, _h3ravel_collect_js.collect)(this.hidden).diff(visible).all();
|
|
2756
2827
|
return this;
|
|
2757
2828
|
}
|
|
2758
2829
|
makeHidden(key, ...keys) {
|
|
@@ -2776,12 +2847,10 @@ const HidesAttributes = (Model$1) => {
|
|
|
2776
2847
|
}
|
|
2777
2848
|
};
|
|
2778
2849
|
};
|
|
2779
|
-
var hides_attributes_default = HidesAttributes;
|
|
2780
|
-
|
|
2781
2850
|
//#endregion
|
|
2782
2851
|
//#region src/concerns/unique-ids.ts
|
|
2783
|
-
const UniqueIds = (Model
|
|
2784
|
-
return class extends Model
|
|
2852
|
+
const UniqueIds = (Model) => {
|
|
2853
|
+
return class extends Model {
|
|
2785
2854
|
useUniqueIds = false;
|
|
2786
2855
|
usesUniqueIds() {
|
|
2787
2856
|
return this.useUniqueIds;
|
|
@@ -2795,8 +2864,6 @@ const UniqueIds = (Model$1) => {
|
|
|
2795
2864
|
}
|
|
2796
2865
|
};
|
|
2797
2866
|
};
|
|
2798
|
-
var unique_ids_default = UniqueIds;
|
|
2799
|
-
|
|
2800
2867
|
//#endregion
|
|
2801
2868
|
//#region src/paginator.ts
|
|
2802
2869
|
var Paginator = class {
|
|
@@ -2815,8 +2882,11 @@ var Paginator = class {
|
|
|
2815
2882
|
}
|
|
2816
2883
|
constructor(items, total, perPage, currentPage = 1, options = {}) {
|
|
2817
2884
|
this.options = options;
|
|
2818
|
-
for (const key in options)
|
|
2819
|
-
|
|
2885
|
+
for (const key in options) {
|
|
2886
|
+
const value = options[key];
|
|
2887
|
+
this[key] = value;
|
|
2888
|
+
}
|
|
2889
|
+
this._items = new Collection([]);
|
|
2820
2890
|
this._total = total;
|
|
2821
2891
|
this._perPage = parseInt(String(perPage));
|
|
2822
2892
|
this._lastPage = Math.max(Math.ceil(total / perPage), 1);
|
|
@@ -2824,7 +2894,7 @@ var Paginator = class {
|
|
|
2824
2894
|
this.setItems(items);
|
|
2825
2895
|
}
|
|
2826
2896
|
setItems(items) {
|
|
2827
|
-
this._items = items instanceof
|
|
2897
|
+
this._items = items instanceof Collection ? items : new Collection(items);
|
|
2828
2898
|
this.hasMore = this._items.count() > this._perPage;
|
|
2829
2899
|
this._items = this._items.slice(0, this._perPage);
|
|
2830
2900
|
}
|
|
@@ -2882,8 +2952,6 @@ var Paginator = class {
|
|
|
2882
2952
|
return JSON.stringify(this.toData(), ...args);
|
|
2883
2953
|
}
|
|
2884
2954
|
};
|
|
2885
|
-
var paginator_default = Paginator;
|
|
2886
|
-
|
|
2887
2955
|
//#endregion
|
|
2888
2956
|
//#region src/query-builder.ts
|
|
2889
2957
|
const Inference$1 = class {};
|
|
@@ -2899,10 +2967,9 @@ var QueryBuilder = class QueryBuilder extends Inference$1 {
|
|
|
2899
2967
|
asProxy() {
|
|
2900
2968
|
return new Proxy(this, {
|
|
2901
2969
|
get: function(target, prop) {
|
|
2902
|
-
var _target$connector$cli;
|
|
2903
2970
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2904
2971
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2905
|
-
const skipReturning = !!
|
|
2972
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2906
2973
|
if ([
|
|
2907
2974
|
"select",
|
|
2908
2975
|
"from",
|
|
@@ -3031,7 +3098,7 @@ var QueryBuilder = class QueryBuilder extends Inference$1 {
|
|
|
3031
3098
|
this.take(perPage).skip(skip);
|
|
3032
3099
|
results = await this.get();
|
|
3033
3100
|
} else results = [];
|
|
3034
|
-
return new
|
|
3101
|
+
return new Paginator(results, parseInt(total), perPage, page);
|
|
3035
3102
|
}
|
|
3036
3103
|
forPage(page = 1, perPage = 15) {
|
|
3037
3104
|
return this.offset((page - 1) * perPage).limit(perPage);
|
|
@@ -3085,8 +3152,6 @@ var QueryBuilder = class QueryBuilder extends Inference$1 {
|
|
|
3085
3152
|
return this.connector.from;
|
|
3086
3153
|
}
|
|
3087
3154
|
};
|
|
3088
|
-
var query_builder_default = QueryBuilder;
|
|
3089
|
-
|
|
3090
3155
|
//#endregion
|
|
3091
3156
|
//#region src/arquebus.ts
|
|
3092
3157
|
var arquebus = class arquebus {
|
|
@@ -3107,6 +3172,9 @@ var arquebus = class arquebus {
|
|
|
3107
3172
|
if (this.instance === null) this.instance = new arquebus();
|
|
3108
3173
|
return this.instance;
|
|
3109
3174
|
}
|
|
3175
|
+
static withSchema() {
|
|
3176
|
+
return this;
|
|
3177
|
+
}
|
|
3110
3178
|
/**
|
|
3111
3179
|
* Initialize a new database connection
|
|
3112
3180
|
*
|
|
@@ -3162,7 +3230,7 @@ var arquebus = class arquebus {
|
|
|
3162
3230
|
name = name || "default";
|
|
3163
3231
|
const resolvedName = this.connections[name] ? name : "default";
|
|
3164
3232
|
if (this.manager[resolvedName] === void 0) {
|
|
3165
|
-
const queryBuilder = new
|
|
3233
|
+
const queryBuilder = new QueryBuilder(this.connections[resolvedName], arquebus.getConnectorFactory());
|
|
3166
3234
|
this.manager[resolvedName] = queryBuilder;
|
|
3167
3235
|
}
|
|
3168
3236
|
return this.manager[resolvedName];
|
|
@@ -3196,12 +3264,12 @@ var arquebus = class arquebus {
|
|
|
3196
3264
|
const tsPath = path.default.resolve("arquebus.config.ts");
|
|
3197
3265
|
const instance = this.getInstance();
|
|
3198
3266
|
if ((0, fs.existsSync)(jsPath)) {
|
|
3199
|
-
config = (await
|
|
3267
|
+
config = (await (0, _h3ravel_shared.importFile)(jsPath)).default;
|
|
3200
3268
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3201
3269
|
return config;
|
|
3202
3270
|
}
|
|
3203
3271
|
if ((0, fs.existsSync)(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
3204
|
-
config = (await
|
|
3272
|
+
config = (await (0, _h3ravel_shared.importFile)(tsPath)).default;
|
|
3205
3273
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3206
3274
|
return config;
|
|
3207
3275
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -3211,13 +3279,13 @@ var arquebus = class arquebus {
|
|
|
3211
3279
|
path.default.join(process.cwd(), "test")
|
|
3212
3280
|
];
|
|
3213
3281
|
for (const dir of candidateDirs) {
|
|
3214
|
-
const found =
|
|
3282
|
+
const found = _h3ravel_shared.FileSystem.resolveFileUp("arquebus.config", [
|
|
3215
3283
|
"js",
|
|
3216
3284
|
"ts",
|
|
3217
3285
|
"cjs"
|
|
3218
3286
|
], dir);
|
|
3219
3287
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
3220
|
-
config = (await
|
|
3288
|
+
config = (await (0, _h3ravel_shared.importFile)(found)).default;
|
|
3221
3289
|
if (addConnection) instance.addConnection(config, config.client);
|
|
3222
3290
|
return config;
|
|
3223
3291
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -3238,50 +3306,44 @@ var arquebus = class arquebus {
|
|
|
3238
3306
|
}
|
|
3239
3307
|
async destroyAll() {
|
|
3240
3308
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
3241
|
-
return connection
|
|
3309
|
+
return connection?.destroy();
|
|
3242
3310
|
}));
|
|
3243
3311
|
}
|
|
3244
3312
|
createModel(name, options = {}) {
|
|
3245
|
-
let BaseModel
|
|
3246
|
-
if ("plugins" in options) BaseModel
|
|
3313
|
+
let BaseModel = Model;
|
|
3314
|
+
if ("plugins" in options) BaseModel = compose(BaseModel, ...options.plugins ?? []);
|
|
3247
3315
|
this.models = {
|
|
3248
3316
|
...this.models,
|
|
3249
|
-
[name]: class extends BaseModel
|
|
3250
|
-
table =
|
|
3251
|
-
connection =
|
|
3252
|
-
timestamps =
|
|
3253
|
-
primaryKey =
|
|
3254
|
-
keyType =
|
|
3255
|
-
incrementing =
|
|
3256
|
-
with =
|
|
3257
|
-
casts =
|
|
3258
|
-
static CREATED_AT =
|
|
3259
|
-
static UPDATED_AT =
|
|
3260
|
-
static DELETED_AT =
|
|
3317
|
+
[name]: class extends BaseModel {
|
|
3318
|
+
table = options?.table ?? null;
|
|
3319
|
+
connection = options?.connection ?? null;
|
|
3320
|
+
timestamps = options?.timestamps ?? true;
|
|
3321
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
3322
|
+
keyType = options?.keyType ?? "int";
|
|
3323
|
+
incrementing = options?.incrementing ?? true;
|
|
3324
|
+
with = options?.with ?? [];
|
|
3325
|
+
casts = options?.casts ?? {};
|
|
3326
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
3327
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
3328
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
3261
3329
|
}
|
|
3262
3330
|
};
|
|
3263
3331
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
3264
|
-
if (options.attributes[attribute] instanceof
|
|
3265
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
3266
|
-
var _options$attributes;
|
|
3267
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
3268
|
-
};
|
|
3332
|
+
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
3333
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
3269
3334
|
}
|
|
3270
3335
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
3271
|
-
|
|
3272
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
3336
|
+
return options.relations?.[relation](this);
|
|
3273
3337
|
};
|
|
3274
3338
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
3275
3339
|
this.models[name].setConnectionResolver(this);
|
|
3276
3340
|
return this.models[name];
|
|
3277
3341
|
}
|
|
3278
3342
|
};
|
|
3279
|
-
var arquebus_default = arquebus;
|
|
3280
|
-
|
|
3281
3343
|
//#endregion
|
|
3282
3344
|
//#region src/model.ts
|
|
3283
3345
|
const ModelClass = class {};
|
|
3284
|
-
const BaseModel = compose(ModelClass,
|
|
3346
|
+
const BaseModel = compose(ModelClass, HasAttributes, HidesAttributes, HasRelations, HasTimestamps, HasHooks, HasGlobalScopes, UniqueIds);
|
|
3285
3347
|
var Model = class Model extends BaseModel {
|
|
3286
3348
|
builder = null;
|
|
3287
3349
|
table = null;
|
|
@@ -3309,6 +3371,9 @@ var Model = class Model extends BaseModel {
|
|
|
3309
3371
|
this.buildRelationships(attributes);
|
|
3310
3372
|
return this.asProxy();
|
|
3311
3373
|
}
|
|
3374
|
+
static define() {
|
|
3375
|
+
return this;
|
|
3376
|
+
}
|
|
3312
3377
|
static query(trx = null) {
|
|
3313
3378
|
return new this().newQuery(trx);
|
|
3314
3379
|
}
|
|
@@ -3333,8 +3398,8 @@ var Model = class Model extends BaseModel {
|
|
|
3333
3398
|
else {
|
|
3334
3399
|
const relation = this[getRelationMethod(attribute)]();
|
|
3335
3400
|
const related = relation.getRelated().constructor;
|
|
3336
|
-
if (relation instanceof
|
|
3337
|
-
else if ((relation instanceof
|
|
3401
|
+
if (relation instanceof HasOne || relation instanceof BelongsTo) this.setRelation(attribute, related.make(attributes[attribute]));
|
|
3402
|
+
else if ((relation instanceof HasMany || relation instanceof BelongsToMany) && Array.isArray(attributes[attribute])) this.setRelation(attribute, new Collection(attributes[attribute].map((item) => related.make(item))));
|
|
3338
3403
|
}
|
|
3339
3404
|
}
|
|
3340
3405
|
getConstructor() {
|
|
@@ -3411,7 +3476,7 @@ var Model = class Model extends BaseModel {
|
|
|
3411
3476
|
}
|
|
3412
3477
|
getConnection() {
|
|
3413
3478
|
if (this.constructor.resolver) return this.constructor.resolver.getConnection(this.connection);
|
|
3414
|
-
return
|
|
3479
|
+
return arquebus.fire(this.connection);
|
|
3415
3480
|
}
|
|
3416
3481
|
setConnection(connection) {
|
|
3417
3482
|
this.connection = connection;
|
|
@@ -3427,7 +3492,7 @@ var Model = class Model extends BaseModel {
|
|
|
3427
3492
|
return this.newModelQuery(trx).with(this.with).withCount(this.withCount);
|
|
3428
3493
|
}
|
|
3429
3494
|
newModelQuery(trx = null) {
|
|
3430
|
-
return new
|
|
3495
|
+
return new Builder(trx || this.getConnection()).setModel(this);
|
|
3431
3496
|
}
|
|
3432
3497
|
addGlobalScopes(builder) {
|
|
3433
3498
|
const globalScopes = this.getGlobalScopes();
|
|
@@ -3450,14 +3515,14 @@ var Model = class Model extends BaseModel {
|
|
|
3450
3515
|
return this;
|
|
3451
3516
|
}
|
|
3452
3517
|
newCollection(models = []) {
|
|
3453
|
-
return new
|
|
3518
|
+
return new Collection(models);
|
|
3454
3519
|
}
|
|
3455
3520
|
async load(...relations) {
|
|
3456
3521
|
await this.constructor.query().with(...relations).eagerLoadRelations([this]);
|
|
3457
3522
|
return this;
|
|
3458
3523
|
}
|
|
3459
3524
|
async loadAggregate(relations, column, callback = null) {
|
|
3460
|
-
await new
|
|
3525
|
+
await new Collection([this]).loadAggregate(relations, column, callback);
|
|
3461
3526
|
return this;
|
|
3462
3527
|
}
|
|
3463
3528
|
async loadCount(...relations) {
|
|
@@ -3492,7 +3557,7 @@ var Model = class Model extends BaseModel {
|
|
|
3492
3557
|
});
|
|
3493
3558
|
}
|
|
3494
3559
|
toData() {
|
|
3495
|
-
return
|
|
3560
|
+
return _h3ravel_support.Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
3496
3561
|
}
|
|
3497
3562
|
toJSON() {
|
|
3498
3563
|
return this.toData();
|
|
@@ -3503,6 +3568,15 @@ var Model = class Model extends BaseModel {
|
|
|
3503
3568
|
toString() {
|
|
3504
3569
|
return this.toJson();
|
|
3505
3570
|
}
|
|
3571
|
+
getAttributes() {
|
|
3572
|
+
return super.getAttributes();
|
|
3573
|
+
}
|
|
3574
|
+
getAttribute(key) {
|
|
3575
|
+
return super.getAttribute(key);
|
|
3576
|
+
}
|
|
3577
|
+
setAttribute(key, value) {
|
|
3578
|
+
return super.setAttribute(key, value);
|
|
3579
|
+
}
|
|
3506
3580
|
fill(attributes) {
|
|
3507
3581
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
3508
3582
|
return this;
|
|
@@ -3543,10 +3617,9 @@ var Model = class Model extends BaseModel {
|
|
|
3543
3617
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
3544
3618
|
const attributes = this.getAttributes();
|
|
3545
3619
|
if (this.getIncrementing()) {
|
|
3546
|
-
var _data$;
|
|
3547
3620
|
const keyName = this.getKeyName();
|
|
3548
3621
|
const data = await query.insert([attributes], [keyName]);
|
|
3549
|
-
this.setAttribute(keyName,
|
|
3622
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
3550
3623
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
3551
3624
|
this.exists = true;
|
|
3552
3625
|
await this.execHooks("created", options);
|
|
@@ -3586,8 +3659,9 @@ var Model = class Model extends BaseModel {
|
|
|
3586
3659
|
}
|
|
3587
3660
|
async refresh() {
|
|
3588
3661
|
if (!this.exists) return Promise.resolve(void 0);
|
|
3589
|
-
|
|
3590
|
-
|
|
3662
|
+
const model = await this.constructor.query().where(this.getKeyName(), this.getKey()).first();
|
|
3663
|
+
this.attributes = { ...model.attributes };
|
|
3664
|
+
await this.load((0, _h3ravel_collect_js.collect)(this.relations).reject((relation) => {
|
|
3591
3665
|
return relation instanceof Pivot;
|
|
3592
3666
|
}).keys().all());
|
|
3593
3667
|
this.syncOriginal();
|
|
@@ -3607,7 +3681,7 @@ var Model = class Model extends BaseModel {
|
|
|
3607
3681
|
if (!await this.save(options)) return false;
|
|
3608
3682
|
for (const relation in this.relations) {
|
|
3609
3683
|
let models = this.relations[relation];
|
|
3610
|
-
models = models instanceof
|
|
3684
|
+
models = models instanceof Collection ? models.all() : [models];
|
|
3611
3685
|
for (const model of models) if (!await model.push(options)) return false;
|
|
3612
3686
|
}
|
|
3613
3687
|
return true;
|
|
@@ -3649,11 +3723,9 @@ var Pivot = class extends Model {
|
|
|
3649
3723
|
return (attributes || this.attributes)[this.constructor.CREATED_AT] !== void 0;
|
|
3650
3724
|
}
|
|
3651
3725
|
};
|
|
3652
|
-
var model_default = Model;
|
|
3653
|
-
|
|
3654
3726
|
//#endregion
|
|
3655
3727
|
//#region src/collection.ts
|
|
3656
|
-
var Collection = class
|
|
3728
|
+
var Collection = class extends _h3ravel_collect_js.Collection {
|
|
3657
3729
|
newConstructor(...args) {
|
|
3658
3730
|
return new (this.getConstructor())(...args);
|
|
3659
3731
|
}
|
|
@@ -3662,17 +3734,18 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3662
3734
|
}
|
|
3663
3735
|
async load(...relations) {
|
|
3664
3736
|
if (this.isNotEmpty()) {
|
|
3665
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
3737
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
3666
3738
|
return this.newConstructor(items);
|
|
3667
3739
|
}
|
|
3668
3740
|
return this;
|
|
3669
3741
|
}
|
|
3670
3742
|
async loadAggregate(relations, column, action = null) {
|
|
3671
3743
|
if (this.isEmpty()) return this;
|
|
3672
|
-
const
|
|
3673
|
-
const
|
|
3744
|
+
const first = this.first();
|
|
3745
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
3746
|
+
const attributes = (0, _h3ravel_collect_js.collect)(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
3674
3747
|
this.each((model) => {
|
|
3675
|
-
const extraAttributes =
|
|
3748
|
+
const extraAttributes = _h3ravel_support.Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
3676
3749
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
3677
3750
|
});
|
|
3678
3751
|
return this;
|
|
@@ -3700,7 +3773,7 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3700
3773
|
}
|
|
3701
3774
|
contains(key, operator, value) {
|
|
3702
3775
|
if (arguments.length > 1) return super.contains(key, value ?? operator);
|
|
3703
|
-
if (key instanceof
|
|
3776
|
+
if (key instanceof Model) return super.contains((model) => {
|
|
3704
3777
|
return model.is(key);
|
|
3705
3778
|
});
|
|
3706
3779
|
return super.contains((model) => {
|
|
@@ -3710,20 +3783,21 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3710
3783
|
diff(items) {
|
|
3711
3784
|
const diff = new this.constructor();
|
|
3712
3785
|
const dictionary = this.getDictionary(items);
|
|
3713
|
-
this.
|
|
3786
|
+
this.all().map((item) => {
|
|
3714
3787
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
3715
3788
|
});
|
|
3716
3789
|
return diff;
|
|
3717
3790
|
}
|
|
3718
|
-
except(keys) {
|
|
3719
|
-
const
|
|
3791
|
+
except(...keys) {
|
|
3792
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3793
|
+
const dictionary = _h3ravel_support.Arr.except(this.getDictionary(), values);
|
|
3720
3794
|
return new this.constructor(Object.values(dictionary));
|
|
3721
3795
|
}
|
|
3722
3796
|
intersect(items) {
|
|
3723
3797
|
const intersect = new this.constructor();
|
|
3724
|
-
if (
|
|
3798
|
+
if (_h3ravel_support.Arr.isEmpty(items)) return intersect;
|
|
3725
3799
|
const dictionary = this.getDictionary(items);
|
|
3726
|
-
for (const item of this.
|
|
3800
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
3727
3801
|
return intersect;
|
|
3728
3802
|
}
|
|
3729
3803
|
unique(key, _strict = false) {
|
|
@@ -3731,26 +3805,27 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3731
3805
|
return new this.constructor(Object.values(this.getDictionary()));
|
|
3732
3806
|
}
|
|
3733
3807
|
find(key, defaultValue = null) {
|
|
3734
|
-
if (key instanceof
|
|
3735
|
-
if (
|
|
3808
|
+
if (key instanceof Model) key = key.getKey();
|
|
3809
|
+
if (Array.isArray(key)) {
|
|
3736
3810
|
if (this.isEmpty()) return new this.constructor();
|
|
3737
3811
|
return this.whereIn(this.first().getKeyName(), key);
|
|
3738
3812
|
}
|
|
3739
|
-
(0,
|
|
3813
|
+
(0, _h3ravel_collect_js.collect)(this.all()).first((model) => {
|
|
3740
3814
|
return model.getKey() == key;
|
|
3741
3815
|
});
|
|
3742
|
-
return this.
|
|
3816
|
+
return this.all().filter((model) => {
|
|
3743
3817
|
return model.getKey() == key;
|
|
3744
3818
|
})[0] || defaultValue;
|
|
3745
3819
|
}
|
|
3746
3820
|
async fresh(...args) {
|
|
3747
3821
|
if (this.isEmpty()) return new this.constructor();
|
|
3748
3822
|
const model = this.first();
|
|
3823
|
+
if (!model) return new this.constructor();
|
|
3749
3824
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
3750
|
-
return this.filter((model
|
|
3751
|
-
return model
|
|
3752
|
-
}).map((model
|
|
3753
|
-
return freshModels[model
|
|
3825
|
+
return this.filter((model) => {
|
|
3826
|
+
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
3827
|
+
}).map((model) => {
|
|
3828
|
+
return freshModels[model.getKey()];
|
|
3754
3829
|
});
|
|
3755
3830
|
}
|
|
3756
3831
|
makeVisible(attributes) {
|
|
@@ -3768,15 +3843,15 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3768
3843
|
item.append(attributes);
|
|
3769
3844
|
});
|
|
3770
3845
|
}
|
|
3771
|
-
only(keys) {
|
|
3772
|
-
|
|
3773
|
-
const dictionary =
|
|
3846
|
+
only(...keys) {
|
|
3847
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
3848
|
+
const dictionary = _h3ravel_support.Arr.select(this.getDictionary(), values);
|
|
3774
3849
|
return new this.constructor(Object.values(dictionary));
|
|
3775
3850
|
}
|
|
3776
3851
|
getDictionary(items) {
|
|
3777
|
-
|
|
3852
|
+
const values = !items ? this.all() : items instanceof _h3ravel_collect_js.Collection ? items.all() : items;
|
|
3778
3853
|
const dictionary = {};
|
|
3779
|
-
|
|
3854
|
+
values.map((value) => {
|
|
3780
3855
|
dictionary[value.getKey()] = value;
|
|
3781
3856
|
});
|
|
3782
3857
|
return dictionary;
|
|
@@ -3785,8 +3860,8 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3785
3860
|
const model = this.first();
|
|
3786
3861
|
if (!model) throw new Error("Unable to create query for empty collection.");
|
|
3787
3862
|
const modelName = model.constructor.name;
|
|
3788
|
-
if (this.filter((model
|
|
3789
|
-
return !(model
|
|
3863
|
+
if (this.filter((model) => {
|
|
3864
|
+
return !(model instanceof modelName);
|
|
3790
3865
|
}).isNotEmpty()) throw new Error("Unable to create query for collection with mixed types.");
|
|
3791
3866
|
return model.newModelQuery().whereKey(this.modelKeys());
|
|
3792
3867
|
}
|
|
@@ -3800,8 +3875,8 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3800
3875
|
return JSON.stringify(this.toData(), ...args);
|
|
3801
3876
|
}
|
|
3802
3877
|
[Symbol.iterator] = () => {
|
|
3803
|
-
const items = this.
|
|
3804
|
-
const length =
|
|
3878
|
+
const items = this.all();
|
|
3879
|
+
const length = items.length;
|
|
3805
3880
|
let n = 0;
|
|
3806
3881
|
return { next() {
|
|
3807
3882
|
return n < length ? {
|
|
@@ -3811,12 +3886,10 @@ var Collection = class Collection extends collect_js.Collection {
|
|
|
3811
3886
|
} };
|
|
3812
3887
|
};
|
|
3813
3888
|
};
|
|
3814
|
-
var collection_default = Collection;
|
|
3815
|
-
|
|
3816
3889
|
//#endregion
|
|
3817
3890
|
//#region src/relations/concerns/interacts-with-pivot-table.ts
|
|
3818
|
-
const InteractsWithPivotTable = (Relation
|
|
3819
|
-
return class extends Relation
|
|
3891
|
+
const InteractsWithPivotTable = (Relation) => {
|
|
3892
|
+
return class extends Relation {
|
|
3820
3893
|
newExistingPivot(attributes = []) {
|
|
3821
3894
|
return this.newPivot(attributes, true);
|
|
3822
3895
|
}
|
|
@@ -3850,24 +3923,24 @@ const InteractsWithPivotTable = (Relation$1) => {
|
|
|
3850
3923
|
let records;
|
|
3851
3924
|
const results = await this.getCurrentlyAttachedPivots();
|
|
3852
3925
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
3853
|
-
const detach = (0,
|
|
3926
|
+
const detach = (0, _h3ravel_collect_js.collect)(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
3854
3927
|
if (detaching && detach.length > 0) {
|
|
3855
3928
|
await this.detach(detach);
|
|
3856
3929
|
changes.detached = this.castKeys(detach);
|
|
3857
3930
|
}
|
|
3858
|
-
changes =
|
|
3931
|
+
changes = _h3ravel_support.Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
3859
3932
|
return changes;
|
|
3860
3933
|
}
|
|
3861
3934
|
syncWithoutDetaching(ids) {
|
|
3862
3935
|
return this.sync(ids, false);
|
|
3863
3936
|
}
|
|
3864
3937
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
3865
|
-
return this.sync((0,
|
|
3866
|
-
return [id, values];
|
|
3938
|
+
return this.sync((0, _h3ravel_collect_js.collect)(this.parseIds(ids)).mapWithKeys((id) => {
|
|
3939
|
+
return [String(id), values];
|
|
3867
3940
|
}), detaching);
|
|
3868
3941
|
}
|
|
3869
3942
|
withPivot(columns) {
|
|
3870
|
-
this.pivotColumns = this.pivotColumns.concat(
|
|
3943
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
3871
3944
|
return this;
|
|
3872
3945
|
}
|
|
3873
3946
|
async attachNew(records, current, touch = true) {
|
|
@@ -3903,9 +3976,9 @@ const InteractsWithPivotTable = (Relation$1) => {
|
|
|
3903
3976
|
return parseInt(updated);
|
|
3904
3977
|
}
|
|
3905
3978
|
formatRecordsList(records) {
|
|
3906
|
-
return (0,
|
|
3907
|
-
if (!
|
|
3908
|
-
return [id, attributes];
|
|
3979
|
+
return (0, _h3ravel_collect_js.collect)(records).mapWithKeys((attributes, id) => {
|
|
3980
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
3981
|
+
return [String(id), attributes];
|
|
3909
3982
|
}).all();
|
|
3910
3983
|
}
|
|
3911
3984
|
async getCurrentlyAttachedPivots() {
|
|
@@ -3975,7 +4048,7 @@ const InteractsWithPivotTable = (Relation$1) => {
|
|
|
3975
4048
|
}
|
|
3976
4049
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
3977
4050
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
3978
|
-
return
|
|
4051
|
+
return _h3ravel_support.Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
3979
4052
|
}
|
|
3980
4053
|
baseAttachRecord(id, timed) {
|
|
3981
4054
|
let record = {};
|
|
@@ -3988,7 +4061,7 @@ const InteractsWithPivotTable = (Relation$1) => {
|
|
|
3988
4061
|
return record;
|
|
3989
4062
|
}
|
|
3990
4063
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
3991
|
-
return
|
|
4064
|
+
return Array.isArray(value) ? [key, {
|
|
3992
4065
|
...value,
|
|
3993
4066
|
...newAttributes
|
|
3994
4067
|
}] : [value, newAttributes];
|
|
@@ -3998,16 +4071,14 @@ const InteractsWithPivotTable = (Relation$1) => {
|
|
|
3998
4071
|
}
|
|
3999
4072
|
parseIds(value) {
|
|
4000
4073
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
4001
|
-
if (value instanceof
|
|
4002
|
-
return
|
|
4074
|
+
if (value instanceof Collection) return value.pluck(this.relatedKey).all();
|
|
4075
|
+
return Array.isArray(value) ? value : [value];
|
|
4003
4076
|
}
|
|
4004
4077
|
};
|
|
4005
4078
|
};
|
|
4006
|
-
var interacts_with_pivot_table_default = InteractsWithPivotTable;
|
|
4007
|
-
|
|
4008
4079
|
//#endregion
|
|
4009
4080
|
//#region src/relations/belongs-to-many.ts
|
|
4010
|
-
var BelongsToMany = class extends compose(
|
|
4081
|
+
var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
4011
4082
|
table;
|
|
4012
4083
|
foreignPivotKey;
|
|
4013
4084
|
relatedPivotKey;
|
|
@@ -4034,7 +4105,7 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4034
4105
|
}
|
|
4035
4106
|
initRelation(models, relation) {
|
|
4036
4107
|
models.map((model) => {
|
|
4037
|
-
model.setRelation(relation, new
|
|
4108
|
+
model.setRelation(relation, new Collection([]));
|
|
4038
4109
|
});
|
|
4039
4110
|
return models;
|
|
4040
4111
|
}
|
|
@@ -4054,20 +4125,19 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4054
4125
|
return this.related.qualifyColumn(this.relatedKey);
|
|
4055
4126
|
}
|
|
4056
4127
|
async getResults() {
|
|
4057
|
-
return this.parent[this.parentKey] !== null ? await this.get() : new
|
|
4128
|
+
return this.parent[this.parentKey] !== null ? await this.get() : new Collection([]);
|
|
4058
4129
|
}
|
|
4059
4130
|
addWhereConstraints() {
|
|
4060
4131
|
this.query.where(this.getQualifiedForeignPivotKeyName(), "=", this.parent[this.parentKey]);
|
|
4061
4132
|
return this;
|
|
4062
4133
|
}
|
|
4063
4134
|
async get(columns) {
|
|
4064
|
-
var _builder$query;
|
|
4065
4135
|
const builder = this.query.applyScopes();
|
|
4066
|
-
columns =
|
|
4136
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
4067
4137
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
4068
4138
|
this.hydratePivotRelation(models);
|
|
4069
4139
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
4070
|
-
return new
|
|
4140
|
+
return new Collection(models);
|
|
4071
4141
|
}
|
|
4072
4142
|
async first(columns = ["*"]) {
|
|
4073
4143
|
const results = await this.take(1).get(columns);
|
|
@@ -4112,7 +4182,7 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4112
4182
|
const value = model.attributes[key];
|
|
4113
4183
|
if (key.startsWith("pivot_")) {
|
|
4114
4184
|
values[key.substring(6)] = value;
|
|
4115
|
-
model.attributes =
|
|
4185
|
+
model.attributes = _h3ravel_support.Arr.except(model.attributes, [key]);
|
|
4116
4186
|
}
|
|
4117
4187
|
}
|
|
4118
4188
|
return values;
|
|
@@ -4123,11 +4193,11 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4123
4193
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
4124
4194
|
}
|
|
4125
4195
|
shouldSelect(columns = ["*"]) {
|
|
4126
|
-
if (
|
|
4196
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
4127
4197
|
return columns.concat(this.aliasedPivotColumns());
|
|
4128
4198
|
}
|
|
4129
4199
|
aliasedPivotColumns() {
|
|
4130
|
-
return (0,
|
|
4200
|
+
return (0, _h3ravel_collect_js.collect)([this.foreignPivotKey, this.relatedPivotKey].concat(this.pivotColumns)).map((column) => {
|
|
4131
4201
|
return this.qualifyPivotColumn(column) + " as pivot_" + column;
|
|
4132
4202
|
}).unique().all();
|
|
4133
4203
|
}
|
|
@@ -4146,7 +4216,7 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4146
4216
|
const dictionary = {};
|
|
4147
4217
|
results.map((result) => {
|
|
4148
4218
|
const value = result[this.accessor][this.foreignPivotKey];
|
|
4149
|
-
if (dictionary[value] === void 0) dictionary[value] = new
|
|
4219
|
+
if (dictionary[value] === void 0) dictionary[value] = new Collection([]);
|
|
4150
4220
|
dictionary[value].push(result);
|
|
4151
4221
|
});
|
|
4152
4222
|
return dictionary;
|
|
@@ -4228,8 +4298,6 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
4228
4298
|
return super.getRelationExistenceQuery(query, parentQuery, columns);
|
|
4229
4299
|
}
|
|
4230
4300
|
};
|
|
4231
|
-
var belongs_to_many_default = BelongsToMany;
|
|
4232
|
-
|
|
4233
4301
|
//#endregion
|
|
4234
4302
|
//#region src/builder.ts
|
|
4235
4303
|
const Inference = class {};
|
|
@@ -4249,9 +4317,8 @@ var Builder = class Builder extends Inference {
|
|
|
4249
4317
|
}
|
|
4250
4318
|
asProxy() {
|
|
4251
4319
|
return new Proxy(this, { get(target, prop) {
|
|
4252
|
-
var _target$query$connect;
|
|
4253
4320
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
4254
|
-
const skipReturning = !!
|
|
4321
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
4255
4322
|
if ([
|
|
4256
4323
|
"select",
|
|
4257
4324
|
"from",
|
|
@@ -4345,7 +4412,7 @@ var Builder = class Builder extends Inference {
|
|
|
4345
4412
|
};
|
|
4346
4413
|
}
|
|
4347
4414
|
if (prop.startsWith("where")) {
|
|
4348
|
-
const column =
|
|
4415
|
+
const column = _h3ravel_support.Str.snake(prop.substring(5));
|
|
4349
4416
|
return (...args) => {
|
|
4350
4417
|
target.query.where(column, ...args);
|
|
4351
4418
|
return target.asProxy();
|
|
@@ -4420,7 +4487,8 @@ var Builder = class Builder extends Inference {
|
|
|
4420
4487
|
}
|
|
4421
4488
|
addUpdatedAtColumn(values) {
|
|
4422
4489
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
4423
|
-
|
|
4490
|
+
const column = this.model.getUpdatedAtColumn();
|
|
4491
|
+
values = _h3ravel_support.Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
4424
4492
|
return values;
|
|
4425
4493
|
}
|
|
4426
4494
|
delete() {
|
|
@@ -4448,9 +4516,8 @@ var Builder = class Builder extends Inference {
|
|
|
4448
4516
|
return this.model;
|
|
4449
4517
|
}
|
|
4450
4518
|
setModel(model) {
|
|
4451
|
-
var _this$query;
|
|
4452
4519
|
this.model = model;
|
|
4453
|
-
if (typeof
|
|
4520
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
4454
4521
|
else this.query = this.query.table(this.model.getTable());
|
|
4455
4522
|
return this;
|
|
4456
4523
|
}
|
|
@@ -4465,7 +4532,7 @@ var Builder = class Builder extends Inference {
|
|
|
4465
4532
|
if (!this.globalScopes) return this;
|
|
4466
4533
|
for (const identifier in this.globalScopes) {
|
|
4467
4534
|
const scope = this.globalScopes[identifier];
|
|
4468
|
-
if (scope instanceof
|
|
4535
|
+
if (scope instanceof Scope) scope.apply(this, this.getModel());
|
|
4469
4536
|
else scope(this);
|
|
4470
4537
|
}
|
|
4471
4538
|
return this;
|
|
@@ -4493,7 +4560,7 @@ var Builder = class Builder extends Inference {
|
|
|
4493
4560
|
}
|
|
4494
4561
|
withoutGlobalScope(scope) {
|
|
4495
4562
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
4496
|
-
this.globalScopes =
|
|
4563
|
+
this.globalScopes = _h3ravel_support.Arr.except(this.globalScopes, [scope]);
|
|
4497
4564
|
return this;
|
|
4498
4565
|
}
|
|
4499
4566
|
macro(name, callback) {
|
|
@@ -4510,7 +4577,7 @@ var Builder = class Builder extends Inference {
|
|
|
4510
4577
|
let eagerLoads = {};
|
|
4511
4578
|
if (typeof args[1] === "function") {
|
|
4512
4579
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
4513
|
-
this.eagerLoad =
|
|
4580
|
+
this.eagerLoad = _h3ravel_support.Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
4514
4581
|
return this;
|
|
4515
4582
|
}
|
|
4516
4583
|
const relations = flattenDeep(args);
|
|
@@ -4519,13 +4586,13 @@ var Builder = class Builder extends Inference {
|
|
|
4519
4586
|
let eagerLoad;
|
|
4520
4587
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4521
4588
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4522
|
-
eagerLoads =
|
|
4589
|
+
eagerLoads = _h3ravel_support.Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4523
4590
|
}
|
|
4524
|
-
this.eagerLoad =
|
|
4591
|
+
this.eagerLoad = _h3ravel_support.Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
4525
4592
|
return this;
|
|
4526
4593
|
}
|
|
4527
4594
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
4528
|
-
if (
|
|
4595
|
+
if (typeof relation === "string") {
|
|
4529
4596
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
4530
4597
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
4531
4598
|
}
|
|
@@ -4602,7 +4669,7 @@ var Builder = class Builder extends Inference {
|
|
|
4602
4669
|
let eagerLoad;
|
|
4603
4670
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
4604
4671
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
4605
|
-
eagerLoads =
|
|
4672
|
+
eagerLoads = _h3ravel_support.Obj.deepMerge(eagerLoads, eagerLoad);
|
|
4606
4673
|
}
|
|
4607
4674
|
relations = eagerLoads;
|
|
4608
4675
|
const db = this.model.getConnection();
|
|
@@ -4621,7 +4688,7 @@ var Builder = class Builder extends Inference {
|
|
|
4621
4688
|
} else expression = column;
|
|
4622
4689
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
4623
4690
|
constraints(query);
|
|
4624
|
-
alias = alias ||
|
|
4691
|
+
alias = alias || _h3ravel_support.Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
4625
4692
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
4626
4693
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
4627
4694
|
}
|
|
@@ -4644,8 +4711,8 @@ var Builder = class Builder extends Inference {
|
|
|
4644
4711
|
return this.parseSub(query);
|
|
4645
4712
|
}
|
|
4646
4713
|
parseSub(query) {
|
|
4647
|
-
if (query instanceof Builder || query instanceof
|
|
4648
|
-
else if (
|
|
4714
|
+
if (query instanceof Builder || query instanceof Relation) return [query.toSql().sql, query.toSql().bindings];
|
|
4715
|
+
else if (typeof query === "string") return [query, []];
|
|
4649
4716
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
4650
4717
|
}
|
|
4651
4718
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -4656,7 +4723,7 @@ var Builder = class Builder extends Inference {
|
|
|
4656
4723
|
return query;
|
|
4657
4724
|
}
|
|
4658
4725
|
getRelationWithoutConstraints(relation) {
|
|
4659
|
-
return
|
|
4726
|
+
return Relation.noConstraints(() => {
|
|
4660
4727
|
return this.getModel()[relation]();
|
|
4661
4728
|
});
|
|
4662
4729
|
}
|
|
@@ -4702,15 +4769,15 @@ var Builder = class Builder extends Inference {
|
|
|
4702
4769
|
if (prefix !== "") prefix += ".";
|
|
4703
4770
|
for (const key in relations) {
|
|
4704
4771
|
const value = relations[key];
|
|
4705
|
-
if (
|
|
4772
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
4706
4773
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
4707
4774
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
4708
|
-
relations =
|
|
4775
|
+
relations = _h3ravel_support.Arr.except(relations, [key]);
|
|
4709
4776
|
}
|
|
4710
4777
|
for (const key in relations) {
|
|
4711
4778
|
const value = relations[key];
|
|
4712
4779
|
let attribute = key, attributeSelectConstraint = value;
|
|
4713
|
-
if (
|
|
4780
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
4714
4781
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
4715
4782
|
}
|
|
4716
4783
|
return preparedRelationships;
|
|
@@ -4730,7 +4797,7 @@ var Builder = class Builder extends Inference {
|
|
|
4730
4797
|
return [name.split(":")[0], (query) => {
|
|
4731
4798
|
query.select(name.split(":")[1].split(",").map((column) => {
|
|
4732
4799
|
if (column.includes(".")) return column;
|
|
4733
|
-
return query instanceof
|
|
4800
|
+
return query instanceof BelongsToMany ? query.related.getTable() + "." + column : column;
|
|
4734
4801
|
}));
|
|
4735
4802
|
}];
|
|
4736
4803
|
}
|
|
@@ -4758,8 +4825,8 @@ var Builder = class Builder extends Inference {
|
|
|
4758
4825
|
}
|
|
4759
4826
|
async findOrFail(...args) {
|
|
4760
4827
|
const data = await this.find(...args);
|
|
4761
|
-
if (
|
|
4762
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, (0,
|
|
4828
|
+
if (Array.isArray(args[0])) {
|
|
4829
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, (0, _h3ravel_collect_js.collect)(args[0]).diff(data.modelKeys()).all());
|
|
4763
4830
|
return data;
|
|
4764
4831
|
}
|
|
4765
4832
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -4773,13 +4840,13 @@ var Builder = class Builder extends Inference {
|
|
|
4773
4840
|
async firstOrNew(attributes = {}, values = {}) {
|
|
4774
4841
|
const instance = await this.where(attributes).first();
|
|
4775
4842
|
if (instance !== null) return instance;
|
|
4776
|
-
return this.newModelInstance(
|
|
4843
|
+
return this.newModelInstance(_h3ravel_support.Obj.deepMerge(attributes, values));
|
|
4777
4844
|
}
|
|
4778
4845
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
4779
4846
|
const instance = await this.where(attributes).first();
|
|
4780
4847
|
if (instance !== null) return instance;
|
|
4781
|
-
return tap(this.newModelInstance(
|
|
4782
|
-
await instance
|
|
4848
|
+
return tap(this.newModelInstance(_h3ravel_support.Obj.deepMerge(attributes, values)), async (instance) => {
|
|
4849
|
+
await instance.save({ client: this.query });
|
|
4783
4850
|
});
|
|
4784
4851
|
}
|
|
4785
4852
|
async updateOrCreate(attributes, values = {}) {
|
|
@@ -4798,22 +4865,21 @@ var Builder = class Builder extends Inference {
|
|
|
4798
4865
|
return this;
|
|
4799
4866
|
}
|
|
4800
4867
|
async find(id, columns) {
|
|
4801
|
-
if (
|
|
4868
|
+
if (Array.isArray(id) || id instanceof Collection) return await this.findMany(id, columns);
|
|
4802
4869
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
4803
4870
|
}
|
|
4804
4871
|
async findMany(ids, columns = ["*"]) {
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
4872
|
+
const values = ids instanceof Collection ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
4873
|
+
if (values.length === 0) return new Collection([]);
|
|
4874
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
4809
4875
|
}
|
|
4810
4876
|
async pluck(column) {
|
|
4811
|
-
return new
|
|
4877
|
+
return new Collection(await this.query.pluck(column));
|
|
4812
4878
|
}
|
|
4813
4879
|
async destroy(ids) {
|
|
4814
|
-
if (ids instanceof
|
|
4815
|
-
if (ids instanceof
|
|
4816
|
-
ids =
|
|
4880
|
+
if (ids instanceof Collection) ids = ids.modelKeys();
|
|
4881
|
+
if (ids instanceof _h3ravel_collect_js.Collection) ids = ids.all();
|
|
4882
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
4817
4883
|
if (ids.length === 0) return 0;
|
|
4818
4884
|
const key = this.model.newInstance().getKeyName();
|
|
4819
4885
|
let count = 0;
|
|
@@ -4825,28 +4891,27 @@ var Builder = class Builder extends Inference {
|
|
|
4825
4891
|
this.applyScopes();
|
|
4826
4892
|
let models = await this.getModels(columns);
|
|
4827
4893
|
if (models.length > 0) models = await this.eagerLoadRelations(models);
|
|
4828
|
-
return new
|
|
4894
|
+
return new Collection(models);
|
|
4829
4895
|
}
|
|
4830
4896
|
async all(columns = ["*"]) {
|
|
4831
4897
|
return await this.model.newModelQuery().get(columns);
|
|
4832
4898
|
}
|
|
4833
4899
|
async paginate(page = 1, perPage = 10) {
|
|
4834
|
-
var _this;
|
|
4835
4900
|
page = page || 1;
|
|
4836
|
-
perPage = perPage ||
|
|
4901
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
4837
4902
|
this.applyScopes();
|
|
4838
4903
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
4839
|
-
let results
|
|
4904
|
+
let results;
|
|
4840
4905
|
if (total > 0) {
|
|
4841
4906
|
const skip = (page - 1) * (perPage ?? 10);
|
|
4842
4907
|
this.take(perPage).skip(skip);
|
|
4843
4908
|
results = await this.getModels();
|
|
4844
4909
|
if (results.length > 0) results = await this.eagerLoadRelations(results);
|
|
4845
4910
|
} else results = [];
|
|
4846
|
-
return new
|
|
4911
|
+
return new Paginator(results, parseInt(total), perPage, page);
|
|
4847
4912
|
}
|
|
4848
4913
|
async getModels(...columns) {
|
|
4849
|
-
columns = (
|
|
4914
|
+
columns = flatten(columns);
|
|
4850
4915
|
if (columns.length > 0) {
|
|
4851
4916
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
4852
4917
|
}
|
|
@@ -4854,7 +4919,7 @@ var Builder = class Builder extends Inference {
|
|
|
4854
4919
|
}
|
|
4855
4920
|
getRelation(name) {
|
|
4856
4921
|
if (typeof this.model[getRelationMethod(name)] !== "function") throw new RelationNotFoundError(`Model [${this.model.constructor.name}]'s relation [${name}] doesn't exist.`);
|
|
4857
|
-
const relation =
|
|
4922
|
+
const relation = Relation.noConstraints(() => this.model.newInstance(this.model.attributes)[getRelationMethod(name)]());
|
|
4858
4923
|
const nested = this.relationsNestedUnder(name);
|
|
4859
4924
|
if (Object.keys(nested).length > 0) relation.query.with(nested);
|
|
4860
4925
|
return relation.asProxy();
|
|
@@ -4884,14 +4949,12 @@ var Builder = class Builder extends Inference {
|
|
|
4884
4949
|
return models;
|
|
4885
4950
|
}
|
|
4886
4951
|
hydrate(items) {
|
|
4887
|
-
return new
|
|
4952
|
+
return new Collection(items.map((item) => {
|
|
4888
4953
|
if (!this.model) return item;
|
|
4889
4954
|
return this.model.newFromBuilder(item);
|
|
4890
4955
|
}));
|
|
4891
4956
|
}
|
|
4892
4957
|
};
|
|
4893
|
-
var builder_default = Builder;
|
|
4894
|
-
|
|
4895
4958
|
//#endregion
|
|
4896
4959
|
//#region src/inspector/dialects/sqlite.ts
|
|
4897
4960
|
function parseDefaultValue(value) {
|
|
@@ -4900,8 +4963,8 @@ function parseDefaultValue(value) {
|
|
|
4900
4963
|
}
|
|
4901
4964
|
var SQLite = class {
|
|
4902
4965
|
knex;
|
|
4903
|
-
constructor(knex
|
|
4904
|
-
this.knex = knex
|
|
4966
|
+
constructor(knex) {
|
|
4967
|
+
this.knex = knex;
|
|
4905
4968
|
}
|
|
4906
4969
|
/**
|
|
4907
4970
|
* List all existing tables in the current schema/database
|
|
@@ -4913,9 +4976,9 @@ var SQLite = class {
|
|
|
4913
4976
|
const query = this.knex.select("name", "sql").from("sqlite_master").where({ type: "table" }).andWhereRaw("name NOT LIKE 'sqlite_%'");
|
|
4914
4977
|
if (table) query.andWhere({ name: table });
|
|
4915
4978
|
let records = await query;
|
|
4916
|
-
records = records.map((table
|
|
4917
|
-
name: table
|
|
4918
|
-
sql: table
|
|
4979
|
+
records = records.map((table) => ({
|
|
4980
|
+
name: table.name,
|
|
4981
|
+
sql: table.sql
|
|
4919
4982
|
}));
|
|
4920
4983
|
if (table) return records[0];
|
|
4921
4984
|
return records;
|
|
@@ -4938,14 +5001,14 @@ var SQLite = class {
|
|
|
4938
5001
|
column: column.name
|
|
4939
5002
|
}));
|
|
4940
5003
|
const tables = await this.tables();
|
|
4941
|
-
return flatten(await Promise.all(tables.map(async (table
|
|
5004
|
+
return flatten(await Promise.all(tables.map(async (table) => await this.columns(table))));
|
|
4942
5005
|
}
|
|
4943
5006
|
async columnInfo(table, column) {
|
|
4944
|
-
const getColumnsForTable = async (table
|
|
5007
|
+
const getColumnsForTable = async (table) => {
|
|
4945
5008
|
const tablesWithAutoIncrementPrimaryKeys = (await this.knex.select("name").from("sqlite_master").whereRaw("sql LIKE '%AUTOINCREMENT%'")).map(({ name }) => name);
|
|
4946
|
-
const columns = await this.knex.raw("PRAGMA table_xinfo(??)", table
|
|
4947
|
-
const foreignKeys = await this.knex.raw("PRAGMA foreign_key_list(??)", table
|
|
4948
|
-
const indexList = await this.knex.raw("PRAGMA index_list(??)", table
|
|
5009
|
+
const columns = await this.knex.raw("PRAGMA table_xinfo(??)", table);
|
|
5010
|
+
const foreignKeys = await this.knex.raw("PRAGMA foreign_key_list(??)", table);
|
|
5011
|
+
const indexList = await this.knex.raw("PRAGMA index_list(??)", table);
|
|
4949
5012
|
const indexInfoList = await Promise.all(indexList.map((index) => this.knex.raw("PRAGMA index_info(??)", index.name)));
|
|
4950
5013
|
return columns.map((raw) => {
|
|
4951
5014
|
const foreignKey = foreignKeys.find((fk) => fk.from === raw.name);
|
|
@@ -4954,26 +5017,27 @@ var SQLite = class {
|
|
|
4954
5017
|
const indexInfo = indexInfoList[indexIndex];
|
|
4955
5018
|
return {
|
|
4956
5019
|
name: raw.name,
|
|
4957
|
-
table
|
|
5020
|
+
table,
|
|
4958
5021
|
data_type: extractType(raw.type),
|
|
4959
5022
|
default_value: parseDefaultValue(raw.dflt_value),
|
|
4960
5023
|
max_length: extractMaxLength(raw.type),
|
|
5024
|
+
/** @NOTE SQLite3 doesn't support precision/scale */
|
|
4961
5025
|
numeric_precision: null,
|
|
4962
5026
|
numeric_scale: null,
|
|
4963
5027
|
is_generated: raw.hidden !== 0,
|
|
4964
5028
|
generation_expression: null,
|
|
4965
5029
|
is_nullable: raw.notnull === 0,
|
|
4966
|
-
is_unique: !!
|
|
5030
|
+
is_unique: !!index?.unique && indexInfo?.length === 1,
|
|
4967
5031
|
is_primary_key: raw.pk === 1,
|
|
4968
|
-
has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table
|
|
4969
|
-
foreign_key_column:
|
|
4970
|
-
foreign_key_table:
|
|
5032
|
+
has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table),
|
|
5033
|
+
foreign_key_column: foreignKey?.to || null,
|
|
5034
|
+
foreign_key_table: foreignKey?.table || null
|
|
4971
5035
|
};
|
|
4972
5036
|
});
|
|
4973
5037
|
};
|
|
4974
5038
|
if (!table) {
|
|
4975
5039
|
const tables = await this.tables();
|
|
4976
|
-
return flatten(await Promise.all(tables.map(async (table
|
|
5040
|
+
return flatten(await Promise.all(tables.map(async (table) => await getColumnsForTable(table))));
|
|
4977
5041
|
}
|
|
4978
5042
|
if (table && !column) return await getColumnsForTable(table);
|
|
4979
5043
|
return (await getColumnsForTable(table)).find((columnInfo) => columnInfo.name === column);
|
|
@@ -5004,7 +5068,7 @@ var SQLite = class {
|
|
|
5004
5068
|
constraint_name: null
|
|
5005
5069
|
}));
|
|
5006
5070
|
const tables = await this.tables();
|
|
5007
|
-
return flatten(await Promise.all(tables.map(async (table
|
|
5071
|
+
return flatten(await Promise.all(tables.map(async (table) => await this.foreignKeys(table))));
|
|
5008
5072
|
}
|
|
5009
5073
|
async uniqueConstraints(table) {
|
|
5010
5074
|
if (table) {
|
|
@@ -5020,16 +5084,15 @@ var SQLite = class {
|
|
|
5020
5084
|
});
|
|
5021
5085
|
}
|
|
5022
5086
|
const tables = await this.tables();
|
|
5023
|
-
return flatten(await Promise.all(tables.map(async (table
|
|
5087
|
+
return flatten(await Promise.all(tables.map(async (table) => await this.uniqueConstraints(table))));
|
|
5024
5088
|
}
|
|
5025
5089
|
};
|
|
5026
|
-
|
|
5027
5090
|
//#endregion
|
|
5028
5091
|
//#region src/inspector/index.ts
|
|
5029
5092
|
var SchemaInspector = class {
|
|
5030
|
-
static inspect(knex
|
|
5093
|
+
static inspect(knex) {
|
|
5031
5094
|
let constructor;
|
|
5032
|
-
switch (knex
|
|
5095
|
+
switch (knex.client.constructor.name) {
|
|
5033
5096
|
case "Client_MySQL":
|
|
5034
5097
|
case "Client_MySQL2":
|
|
5035
5098
|
constructor = MySQL;
|
|
@@ -5051,12 +5114,11 @@ var SchemaInspector = class {
|
|
|
5051
5114
|
case "Client_MSSQL":
|
|
5052
5115
|
constructor = MSSQL;
|
|
5053
5116
|
break;
|
|
5054
|
-
default: throw Error("Unsupported driver used: " + knex
|
|
5117
|
+
default: throw Error("Unsupported driver used: " + knex.client.constructor.name);
|
|
5055
5118
|
}
|
|
5056
|
-
return new constructor(knex
|
|
5119
|
+
return new constructor(knex);
|
|
5057
5120
|
}
|
|
5058
5121
|
};
|
|
5059
|
-
|
|
5060
5122
|
//#endregion
|
|
5061
5123
|
//#region src/migrations/migrator.ts
|
|
5062
5124
|
async function glob(folderPath) {
|
|
@@ -5099,13 +5161,13 @@ var Migrator = class {
|
|
|
5099
5161
|
}
|
|
5100
5162
|
async runPending(migrations, options = {}) {
|
|
5101
5163
|
if (migrations.length === 0 && !options.quiet) {
|
|
5102
|
-
|
|
5164
|
+
_h3ravel_shared.Logger.info("INFO: Nothing to migrate.");
|
|
5103
5165
|
return;
|
|
5104
5166
|
}
|
|
5105
5167
|
let batch = await this.repository.getNextBatchNumber();
|
|
5106
5168
|
const pretend = options.pretend || false;
|
|
5107
5169
|
const step = options.step || false;
|
|
5108
|
-
|
|
5170
|
+
_h3ravel_shared.Logger.info("INFO: Running migrations...");
|
|
5109
5171
|
for (const file of migrations) {
|
|
5110
5172
|
await this.runUp(file, batch, pretend);
|
|
5111
5173
|
if (step) batch++;
|
|
@@ -5114,19 +5176,19 @@ var Migrator = class {
|
|
|
5114
5176
|
async runUp(file, batch, _pretend) {
|
|
5115
5177
|
const migration = await this.resolvePath(file);
|
|
5116
5178
|
const name = this.getMigrationName(file);
|
|
5117
|
-
await
|
|
5179
|
+
await _h3ravel_shared.TaskManager.taskRunner(name, () => this.runMigration(migration, "up"));
|
|
5118
5180
|
await this.repository.log(name, batch);
|
|
5119
5181
|
}
|
|
5120
5182
|
async runDown(file, migration, _pretend) {
|
|
5121
5183
|
const instance = await this.resolvePath(file);
|
|
5122
5184
|
const name = this.getMigrationName(file);
|
|
5123
|
-
await
|
|
5185
|
+
await _h3ravel_shared.TaskManager.taskRunner(name, () => this.runMigration(instance, "down"));
|
|
5124
5186
|
await this.repository.delete(migration);
|
|
5125
5187
|
}
|
|
5126
5188
|
async rollback(paths = [], options = {}) {
|
|
5127
5189
|
const migrations = await this.getMigrationsForRollback(options);
|
|
5128
5190
|
if (migrations.length === 0) {
|
|
5129
|
-
|
|
5191
|
+
_h3ravel_shared.Logger.info("INFO: Nothing to rollback");
|
|
5130
5192
|
return [];
|
|
5131
5193
|
}
|
|
5132
5194
|
return await this.rollbackMigrations(migrations, paths, options);
|
|
@@ -5139,11 +5201,11 @@ var Migrator = class {
|
|
|
5139
5201
|
async rollbackMigrations(migrations, paths, options) {
|
|
5140
5202
|
const rolledBack = [];
|
|
5141
5203
|
const files = await this.getMigrationFiles(paths);
|
|
5142
|
-
|
|
5204
|
+
_h3ravel_shared.Logger.info("INFO: Rolling back migrations...");
|
|
5143
5205
|
for (const migration of migrations) {
|
|
5144
5206
|
const file = files[migration.migration];
|
|
5145
5207
|
if (!file) {
|
|
5146
|
-
|
|
5208
|
+
_h3ravel_shared.Logger.twoColumnDetail(_h3ravel_shared.Logger.parse([[migration.migration, "green"]], "", false), _h3ravel_shared.Logger.parse([["Migration not found", "yellow"]], "", false));
|
|
5147
5209
|
continue;
|
|
5148
5210
|
}
|
|
5149
5211
|
rolledBack.push(file);
|
|
@@ -5154,7 +5216,7 @@ var Migrator = class {
|
|
|
5154
5216
|
async reset(paths = [], options, pretend = false) {
|
|
5155
5217
|
const migrations = await this.repository.getRan().then((r) => r.map((e) => ({ migration: e })).reverse());
|
|
5156
5218
|
if (migrations.length === 0) {
|
|
5157
|
-
if (!options.quiet)
|
|
5219
|
+
if (!options.quiet) _h3ravel_shared.Logger.info("INFO: Nothing to reset.");
|
|
5158
5220
|
return [];
|
|
5159
5221
|
}
|
|
5160
5222
|
return this.resetMigrations(migrations, paths, pretend);
|
|
@@ -5171,7 +5233,7 @@ var Migrator = class {
|
|
|
5171
5233
|
const inspector = SchemaInspector.inspect(connection);
|
|
5172
5234
|
await connection.raw("SET foreign_key_checks = 0");
|
|
5173
5235
|
/** Drop all existing tables */
|
|
5174
|
-
for (const table of await inspector.tables()) await
|
|
5236
|
+
for (const table of await inspector.tables()) await _h3ravel_shared.TaskManager.taskRunner(`Dropping ${_h3ravel_shared.Logger.parse([[table, "grey"]], "", false)} table`, () => connection.schema.dropTableIfExists(table));
|
|
5175
5237
|
await connection.raw("SET foreign_key_checks = 1");
|
|
5176
5238
|
/** Create the migration repository */
|
|
5177
5239
|
await this.repository.createRepository();
|
|
@@ -5191,8 +5253,7 @@ var Migrator = class {
|
|
|
5191
5253
|
else await callback(connection);
|
|
5192
5254
|
}
|
|
5193
5255
|
async runMethod(connection, migration, method) {
|
|
5194
|
-
|
|
5195
|
-
await ((_migration$method = migration[method]) === null || _migration$method === void 0 ? void 0 : _migration$method.call(migration, connection.schema, connection));
|
|
5256
|
+
await migration[method]?.(connection.schema, connection);
|
|
5196
5257
|
}
|
|
5197
5258
|
async resolvePath(filePath) {
|
|
5198
5259
|
try {
|
|
@@ -5221,7 +5282,7 @@ var Migrator = class {
|
|
|
5221
5282
|
return path.default.basename(filePath).replace(".js", "");
|
|
5222
5283
|
}
|
|
5223
5284
|
path(p) {
|
|
5224
|
-
this.paths = Array.from(new Set([...this.paths, p]));
|
|
5285
|
+
this.paths = Array.from(/* @__PURE__ */ new Set([...this.paths, p]));
|
|
5225
5286
|
}
|
|
5226
5287
|
getPaths() {
|
|
5227
5288
|
return this.paths;
|
|
@@ -5253,11 +5314,45 @@ var Migrator = class {
|
|
|
5253
5314
|
if (this.output) console.log(...args);
|
|
5254
5315
|
}
|
|
5255
5316
|
};
|
|
5256
|
-
|
|
5257
|
-
|
|
5317
|
+
//#endregion
|
|
5318
|
+
//#region src/cli/utils.ts
|
|
5319
|
+
const join = path.default.join;
|
|
5320
|
+
var Utils = class {
|
|
5321
|
+
static findModulePkg(moduleId, cwd) {
|
|
5322
|
+
const parts = moduleId.replace(/\\/g, "/").split("/");
|
|
5323
|
+
let packageName = "";
|
|
5324
|
+
if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
|
|
5325
|
+
packageName += parts.shift();
|
|
5326
|
+
const packageJson = path.default.join(packageName, "package.json");
|
|
5327
|
+
const resolved = resolve_from.default.silent(cwd ?? process.cwd(), packageJson);
|
|
5328
|
+
if (!resolved) return;
|
|
5329
|
+
return path.default.join(path.default.dirname(resolved), parts.join("/"));
|
|
5330
|
+
}
|
|
5331
|
+
static async getMigrationPaths(cwd, migrator, defaultPath, path$7) {
|
|
5332
|
+
if (path$7) return [join(cwd, path$7)];
|
|
5333
|
+
return [...migrator.getPaths(), join(cwd, defaultPath)];
|
|
5334
|
+
}
|
|
5335
|
+
};
|
|
5336
|
+
var TableGuesser = class TableGuesser {
|
|
5337
|
+
static CREATE_PATTERNS = [/^create_(\w+)_table$/, /^create_(\w+)$/];
|
|
5338
|
+
static CHANGE_PATTERNS = [/.+_(to|from|in)_(\w+)_table$/, /.+_(to|from|in)_(\w+)$/];
|
|
5339
|
+
static guess(migration) {
|
|
5340
|
+
for (const pattern of TableGuesser.CREATE_PATTERNS) {
|
|
5341
|
+
const matches = migration.match(pattern);
|
|
5342
|
+
if (matches) return [matches[1], true];
|
|
5343
|
+
}
|
|
5344
|
+
for (const pattern of TableGuesser.CHANGE_PATTERNS) {
|
|
5345
|
+
const matches = migration.match(pattern);
|
|
5346
|
+
if (matches) return [matches[2], false];
|
|
5347
|
+
}
|
|
5348
|
+
return [];
|
|
5349
|
+
}
|
|
5350
|
+
};
|
|
5258
5351
|
//#endregion
|
|
5259
5352
|
//#region src/migrate.ts
|
|
5260
5353
|
var Migrate = class {
|
|
5354
|
+
basePath;
|
|
5355
|
+
customStubPath;
|
|
5261
5356
|
callback;
|
|
5262
5357
|
constructor(basePath, customStubPath, callback) {
|
|
5263
5358
|
this.basePath = basePath;
|
|
@@ -5272,14 +5367,14 @@ var Migrate = class {
|
|
|
5272
5367
|
* @param destroyAll
|
|
5273
5368
|
*/
|
|
5274
5369
|
async run(config, options = {}, destroyAll = false) {
|
|
5275
|
-
const { arquebus
|
|
5370
|
+
const { arquebus, migrator } = await this.setupConnection(config);
|
|
5276
5371
|
await this.prepareDatabase(migrator);
|
|
5277
5372
|
const paths = await Utils.getMigrationPaths(this.basePath ?? process.cwd(), migrator, config.migrations.path, options.path);
|
|
5278
5373
|
await migrator.setOutput(true).run(paths, {
|
|
5279
5374
|
step: options.step,
|
|
5280
5375
|
pretend: options.pretend
|
|
5281
5376
|
});
|
|
5282
|
-
if (destroyAll) await arquebus
|
|
5377
|
+
if (destroyAll) await arquebus.destroyAll();
|
|
5283
5378
|
}
|
|
5284
5379
|
/**
|
|
5285
5380
|
* Rollback the last migration
|
|
@@ -5289,14 +5384,14 @@ var Migrate = class {
|
|
|
5289
5384
|
* @param destroyAll
|
|
5290
5385
|
*/
|
|
5291
5386
|
async rollback(config, options = {}, destroyAll = false) {
|
|
5292
|
-
const { arquebus
|
|
5387
|
+
const { arquebus, migrator } = await this.setupConnection(config);
|
|
5293
5388
|
const paths = await Utils.getMigrationPaths(this.basePath ?? process.cwd(), migrator, config.migrations.path, options.path);
|
|
5294
5389
|
await migrator.setOutput(true).rollback(paths, {
|
|
5295
5390
|
step: options.step || 0,
|
|
5296
5391
|
pretend: options.pretend,
|
|
5297
5392
|
batch: options.batch || 0
|
|
5298
5393
|
});
|
|
5299
|
-
if (destroyAll) await arquebus
|
|
5394
|
+
if (destroyAll) await arquebus.destroyAll();
|
|
5300
5395
|
}
|
|
5301
5396
|
/**
|
|
5302
5397
|
* Rollback all database migrations
|
|
@@ -5306,7 +5401,7 @@ var Migrate = class {
|
|
|
5306
5401
|
* @param destroyAll
|
|
5307
5402
|
*/
|
|
5308
5403
|
async reset(config, options = {}, destroyAll = false) {
|
|
5309
|
-
const { arquebus
|
|
5404
|
+
const { arquebus, migrator } = await this.setupConnection(config);
|
|
5310
5405
|
const paths = await Utils.getMigrationPaths(this.basePath ?? process.cwd(), migrator, config.migrations.path, options.path);
|
|
5311
5406
|
await migrator.setOutput(true).reset(paths, {
|
|
5312
5407
|
step: options.step || 0,
|
|
@@ -5314,7 +5409,7 @@ var Migrate = class {
|
|
|
5314
5409
|
batch: options.batch || 0,
|
|
5315
5410
|
quiet: options.quiet || false
|
|
5316
5411
|
});
|
|
5317
|
-
if (destroyAll) await arquebus
|
|
5412
|
+
if (destroyAll) await arquebus.destroyAll();
|
|
5318
5413
|
}
|
|
5319
5414
|
/**
|
|
5320
5415
|
* Reset and re-run all migrations
|
|
@@ -5336,10 +5431,10 @@ var Migrate = class {
|
|
|
5336
5431
|
* @param destroyAll
|
|
5337
5432
|
*/
|
|
5338
5433
|
async fresh(config, options = {}, destroyAll = false) {
|
|
5339
|
-
const { arquebus
|
|
5434
|
+
const { arquebus, migrator } = await this.setupConnection(config);
|
|
5340
5435
|
const paths = await Utils.getMigrationPaths(this.basePath ?? process.cwd(), migrator, config.migrations.path, options.path);
|
|
5341
5436
|
await migrator.setOutput(true).fresh(paths, { pretend: options.pretend });
|
|
5342
|
-
if (destroyAll) await arquebus
|
|
5437
|
+
if (destroyAll) await arquebus.destroyAll();
|
|
5343
5438
|
}
|
|
5344
5439
|
/**
|
|
5345
5440
|
* Prepares the database for migration
|
|
@@ -5363,7 +5458,7 @@ var Migrate = class {
|
|
|
5363
5458
|
* @returns
|
|
5364
5459
|
*/
|
|
5365
5460
|
async status(config, options = {}, destroyAll = false) {
|
|
5366
|
-
const { arquebus
|
|
5461
|
+
const { arquebus, migrator } = await this.setupConnection(config);
|
|
5367
5462
|
const getAllMigrationFiles = async () => {
|
|
5368
5463
|
return await migrator.getMigrationFiles(await Utils.getMigrationPaths(this.basePath ?? process.cwd(), migrator, config.migrations.path, options.path));
|
|
5369
5464
|
};
|
|
@@ -5380,7 +5475,7 @@ var Migrate = class {
|
|
|
5380
5475
|
}
|
|
5381
5476
|
if (!await migrator.repositoryExists()) this.callback("ERROR: Migration table does not exist.", "error");
|
|
5382
5477
|
const migrations = await getStatusFor(await migrator.repository.getRan(), await migrator.getRepository().getMigrationBatches());
|
|
5383
|
-
if (destroyAll) await arquebus
|
|
5478
|
+
if (destroyAll) await arquebus.destroyAll();
|
|
5384
5479
|
return migrations;
|
|
5385
5480
|
}
|
|
5386
5481
|
/**
|
|
@@ -5390,24 +5485,24 @@ var Migrate = class {
|
|
|
5390
5485
|
* @returns
|
|
5391
5486
|
*/
|
|
5392
5487
|
async setupConnection(config) {
|
|
5393
|
-
|
|
5394
|
-
const table = (config === null || config === void 0 || (_config$migrations = config.migrations) === null || _config$migrations === void 0 ? void 0 : _config$migrations.table) || "migrations";
|
|
5488
|
+
const table = config?.migrations?.table || "migrations";
|
|
5395
5489
|
if (config.skipConnection !== true) {
|
|
5396
|
-
|
|
5490
|
+
arquebus.addConnection(config, "default");
|
|
5397
5491
|
Object.entries(config.connections || {}).forEach(([name, connection]) => {
|
|
5398
|
-
|
|
5492
|
+
arquebus.addConnection(connection, name);
|
|
5399
5493
|
});
|
|
5400
5494
|
}
|
|
5401
5495
|
return {
|
|
5402
|
-
arquebus
|
|
5403
|
-
migrator: new
|
|
5496
|
+
arquebus,
|
|
5497
|
+
migrator: new Migrator(new MigrationRepository(arquebus, table), arquebus)
|
|
5404
5498
|
};
|
|
5405
5499
|
}
|
|
5406
5500
|
};
|
|
5407
|
-
|
|
5408
5501
|
//#endregion
|
|
5409
5502
|
//#region src/migrations/migration-creator.ts
|
|
5410
5503
|
var MigrationCreator = class {
|
|
5504
|
+
customStubPath;
|
|
5505
|
+
type;
|
|
5411
5506
|
postCreate = [];
|
|
5412
5507
|
constructor(customStubPath, type = "js") {
|
|
5413
5508
|
this.customStubPath = customStubPath;
|
|
@@ -5450,14 +5545,13 @@ var MigrationCreator = class {
|
|
|
5450
5545
|
let stub;
|
|
5451
5546
|
if (!table) {
|
|
5452
5547
|
const customPath = path.default.join(this.customStubPath ?? "", `migration-${this.type}.stub`);
|
|
5453
|
-
|
|
5454
|
-
stub = await __h3ravel_shared.FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration-${this.type}.stub`);
|
|
5548
|
+
stub = await _h3ravel_shared.FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration-${this.type}.stub`);
|
|
5455
5549
|
} else if (create) {
|
|
5456
5550
|
const customPath = path.default.join(this.customStubPath ?? "", `migration.create-${this.type}.stub`);
|
|
5457
|
-
stub = await
|
|
5551
|
+
stub = await _h3ravel_shared.FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration.create-${this.type}.stub`);
|
|
5458
5552
|
} else {
|
|
5459
5553
|
const customPath = path.default.join(this.customStubPath ?? "", `migration.update-${this.type}.stub`);
|
|
5460
|
-
stub = await
|
|
5554
|
+
stub = await _h3ravel_shared.FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration.update-${this.type}.stub`);
|
|
5461
5555
|
}
|
|
5462
5556
|
return await (0, node_fs_promises.readFile)(stub, "utf-8");
|
|
5463
5557
|
}
|
|
@@ -5469,7 +5563,7 @@ var MigrationCreator = class {
|
|
|
5469
5563
|
return name.replace(/_+([a-z])/g, (match, char) => char.toUpperCase());
|
|
5470
5564
|
}
|
|
5471
5565
|
getPath(name, dir) {
|
|
5472
|
-
const datePrefix =
|
|
5566
|
+
const datePrefix = dayjs$1().format("YYYY_MM_DD_HHmmss");
|
|
5473
5567
|
return path.default.join(dir, `${datePrefix}_${name}.${this.type}`);
|
|
5474
5568
|
}
|
|
5475
5569
|
async firePostCreateHooks(table, filePath) {
|
|
@@ -5482,8 +5576,10 @@ var MigrationCreator = class {
|
|
|
5482
5576
|
await (0, node_fs_promises.mkdir)(dir, { recursive: true });
|
|
5483
5577
|
}
|
|
5484
5578
|
stubPath(stub = "") {
|
|
5485
|
-
const __dirname
|
|
5486
|
-
|
|
5579
|
+
const __dirname = this.getDirname({});
|
|
5580
|
+
const resolved = [path.default.join(__dirname, "stubs", stub), path.default.join(__dirname, "../stubs", stub)].find((candidate) => (0, node_fs.existsSync)(candidate));
|
|
5581
|
+
if (!resolved) throw new Error(`Migration stub not found: ${stub}`);
|
|
5582
|
+
return resolved;
|
|
5487
5583
|
}
|
|
5488
5584
|
getDirname(meta) {
|
|
5489
5585
|
if (typeof __dirname !== "undefined") return __dirname;
|
|
@@ -5491,47 +5587,63 @@ var MigrationCreator = class {
|
|
|
5491
5587
|
throw new Error("Unable to determine dirname");
|
|
5492
5588
|
}
|
|
5493
5589
|
};
|
|
5494
|
-
|
|
5495
5590
|
//#endregion
|
|
5496
5591
|
//#region package.json
|
|
5497
|
-
var version = "0.
|
|
5498
|
-
|
|
5592
|
+
var version = "3.0.0";
|
|
5499
5593
|
//#endregion
|
|
5500
|
-
//#region src/cli/
|
|
5501
|
-
|
|
5594
|
+
//#region src/cli/index.ts
|
|
5595
|
+
(class Cli {
|
|
5502
5596
|
cwd;
|
|
5503
|
-
output =
|
|
5597
|
+
output = _h3ravel_shared.Logger;
|
|
5504
5598
|
config = {};
|
|
5505
5599
|
basePath = "";
|
|
5506
|
-
modulePath;
|
|
5600
|
+
modulePath = "";
|
|
5507
5601
|
configPath;
|
|
5508
|
-
modulePackage;
|
|
5602
|
+
modulePackage = { version: "N/A" };
|
|
5509
5603
|
constructor(basePath) {
|
|
5510
5604
|
this.basePath = basePath ?? (process.env.TEST === "true" ? "test/cli" : "");
|
|
5511
5605
|
}
|
|
5606
|
+
static async init() {
|
|
5607
|
+
(0, dotenv.config)({ quiet: true });
|
|
5608
|
+
const instance = new Cli();
|
|
5609
|
+
await instance.loadPaths();
|
|
5610
|
+
await instance.loadConfig();
|
|
5611
|
+
await instance.run();
|
|
5612
|
+
}
|
|
5512
5613
|
terminateNotFound() {
|
|
5513
|
-
const cmd =
|
|
5614
|
+
const cmd = _h3ravel_shared.Logger.log([["arquebus init", [
|
|
5514
5615
|
"italic",
|
|
5515
5616
|
"black",
|
|
5516
5617
|
"bgGray"
|
|
5517
5618
|
]]], "", false);
|
|
5518
5619
|
this.output.error(`ERROR: Arquebus config not found. Run ${cmd} first.`);
|
|
5519
5620
|
}
|
|
5520
|
-
|
|
5521
|
-
(
|
|
5522
|
-
|
|
5523
|
-
|
|
5621
|
+
ensureConfigured() {
|
|
5622
|
+
if (!this.configPath) this.terminateNotFound();
|
|
5623
|
+
}
|
|
5624
|
+
resolveBasePath(requestedPath) {
|
|
5625
|
+
return requestedPath ? node_path.default.join(this.cwd, requestedPath) : this.cwd;
|
|
5626
|
+
}
|
|
5627
|
+
async resolveStub(name) {
|
|
5628
|
+
const candidates = [node_path.default.join(this.modulePath, "src/stubs", name), node_path.default.join(this.modulePath, "dist/stubs", name)];
|
|
5629
|
+
for (const candidate of candidates) if (await _h3ravel_shared.FileSystem.fileExists(candidate)) return candidate;
|
|
5630
|
+
throw new Error(`Arquebus stub not found: ${name}`);
|
|
5631
|
+
}
|
|
5632
|
+
migrationReporter() {
|
|
5633
|
+
return (message, status) => {
|
|
5634
|
+
if (status && status !== "quiet") this.output[status](message);
|
|
5635
|
+
};
|
|
5524
5636
|
}
|
|
5525
5637
|
async loadPaths() {
|
|
5526
|
-
this.cwd =
|
|
5527
|
-
this.configPath =
|
|
5638
|
+
this.cwd = node_path.default.join(process.cwd(), this.basePath);
|
|
5639
|
+
this.configPath = _h3ravel_shared.FileSystem.resolveFileUp("arquebus.config", [
|
|
5528
5640
|
"js",
|
|
5529
5641
|
"ts",
|
|
5530
5642
|
"cjs"
|
|
5531
5643
|
], this.cwd) ?? void 0;
|
|
5532
5644
|
this.modulePath = Utils.findModulePkg("@h3ravel/arquebus", this.cwd) ?? "";
|
|
5533
5645
|
try {
|
|
5534
|
-
this.modulePackage = await
|
|
5646
|
+
this.modulePackage = JSON.parse(await (0, node_fs_promises.readFile)(node_path.default.join(this.modulePath, "package.json"), "utf8"));
|
|
5535
5647
|
} catch {
|
|
5536
5648
|
this.modulePackage = { version: "N/A" };
|
|
5537
5649
|
}
|
|
@@ -5539,239 +5651,185 @@ var Cli = class Cli {
|
|
|
5539
5651
|
}
|
|
5540
5652
|
async loadConfig() {
|
|
5541
5653
|
try {
|
|
5542
|
-
|
|
5543
|
-
this.config
|
|
5544
|
-
if ((_this$config$migratio = this.config.migrations) === null || _this$config$migratio === void 0 ? void 0 : _this$config$migratio.path) {
|
|
5545
|
-
var _this$config$migratio2;
|
|
5546
|
-
await (0, node_fs_promises.mkdir)(path.default.join(this.cwd, (_this$config$migratio2 = this.config.migrations) === null || _this$config$migratio2 === void 0 ? void 0 : _this$config$migratio2.path), { recursive: true });
|
|
5547
|
-
}
|
|
5654
|
+
this.config = (await (0, _h3ravel_shared.importFile)(this.configPath ?? "----")).default;
|
|
5655
|
+
if (this.config.migrations?.path) await (0, node_fs_promises.mkdir)(node_path.default.join(this.cwd, this.config.migrations.path), { recursive: true });
|
|
5548
5656
|
} catch {
|
|
5549
5657
|
this.config = {};
|
|
5550
5658
|
}
|
|
5551
5659
|
return this;
|
|
5552
5660
|
}
|
|
5553
5661
|
async run() {
|
|
5554
|
-
const
|
|
5555
|
-
|
|
5556
|
-
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5561
|
-
|
|
5562
|
-
|
|
5563
|
-
|
|
5564
|
-
|
|
5565
|
-
|
|
5566
|
-
|
|
5567
|
-
|
|
5568
|
-
}
|
|
5569
|
-
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
|
|
5573
|
-
|
|
5574
|
-
|
|
5575
|
-
|
|
5576
|
-
|
|
5577
|
-
|
|
5578
|
-
|
|
5579
|
-
|
|
5580
|
-
|
|
5581
|
-
|
|
5582
|
-
|
|
5583
|
-
|
|
5584
|
-
|
|
5585
|
-
|
|
5586
|
-
|
|
5587
|
-
|
|
5588
|
-
|
|
5589
|
-
|
|
5590
|
-
|
|
5591
|
-
|
|
5592
|
-
|
|
5593
|
-
/**
|
|
5594
|
-
* Publish migrations from third party vendors
|
|
5595
|
-
*/
|
|
5596
|
-
commander.program.command("migrate:publish <package>").description("Publish any migration files from packages.").option("-p, --path [path]", "The path to the migrations directory.").action(async (pkg, opts) => {
|
|
5597
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5598
|
-
try {
|
|
5599
|
-
var _this$config$migratio4;
|
|
5600
|
-
const packagePath = Utils.findModulePkg(pkg) ?? "";
|
|
5601
|
-
const basePath = path.default.join(this.cwd, opts.path ?? ((_this$config$migratio4 = this.config.migrations) === null || _this$config$migratio4 === void 0 ? void 0 : _this$config$migratio4.path) ?? "./migrations");
|
|
5602
|
-
const pkgJson = await import(path.default.join(packagePath, "package.json"));
|
|
5603
|
-
if (!packagePath) this.output.error(`ERROR: package ${pkg} not found`);
|
|
5604
|
-
const creator = new MigrationCreator(path.default.join(packagePath, pkgJson.migrations ?? "migrations"));
|
|
5605
|
-
const pkgInf = __h3ravel_shared.Logger.log(path.default.basename(pkgJson.name + "@" + pkgJson.version), ["italic", "gray"], false);
|
|
5606
|
-
this.output.info(`INFO: Publishing migrations from ${pkgInf}`);
|
|
5607
|
-
await creator.publish(basePath, (fileName) => {
|
|
5608
|
-
this.output.split("INFO: Migration Published", fileName, "success");
|
|
5609
|
-
});
|
|
5610
|
-
} catch (e) {
|
|
5611
|
-
this.output.error("ERROR: " + e);
|
|
5612
|
-
}
|
|
5613
|
-
});
|
|
5614
|
-
/**
|
|
5615
|
-
* Run all pending migrations
|
|
5616
|
-
*/
|
|
5617
|
-
commander.program.command("migrate").description("Run all pending migrations.").option("-s, --step [number]", "Force the migrations to be run so they can be rolled back individually.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5618
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5619
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5620
|
-
try {
|
|
5621
|
-
const step = typeof opts.step === "string" ? parseInt(opts.step) : opts.step;
|
|
5622
|
-
await new Migrate(basePath).run(this.config, {
|
|
5623
|
-
...opts,
|
|
5624
|
-
step
|
|
5625
|
-
}, true);
|
|
5626
|
-
} catch (e) {
|
|
5627
|
-
this.output.error("ERROR: " + e);
|
|
5628
|
-
}
|
|
5629
|
-
});
|
|
5630
|
-
/**
|
|
5631
|
-
* Rollaback the last migration
|
|
5632
|
-
*/
|
|
5633
|
-
commander.program.command("migrate:rollback").description("Rollback the last database migration.").option("-s, --step [number]", "The number of migrations to be reverted.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5634
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5635
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5636
|
-
try {
|
|
5637
|
-
await new Migrate(basePath, void 0, (msg, sts) => {
|
|
5638
|
-
if (sts) this.output[sts](msg);
|
|
5639
|
-
}).rollback(this.config, {
|
|
5640
|
-
...opts,
|
|
5641
|
-
step: typeof opts.step === "string" ? parseInt(opts.step) : opts.step
|
|
5642
|
-
}, true);
|
|
5643
|
-
} catch (e) {
|
|
5644
|
-
this.output.error("ERROR: " + e);
|
|
5645
|
-
}
|
|
5646
|
-
});
|
|
5647
|
-
/**
|
|
5648
|
-
* Rollback all database migrations
|
|
5649
|
-
*/
|
|
5650
|
-
commander.program.command("migrate:reset").description("Rollback all database migrations.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5651
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5652
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5653
|
-
try {
|
|
5654
|
-
await new Migrate(basePath, void 0, (msg, sts) => {
|
|
5655
|
-
if (sts) this.output[sts](msg);
|
|
5656
|
-
}).reset(this.config, opts, true);
|
|
5657
|
-
} catch (e) {
|
|
5658
|
-
this.output.error("ERROR: " + e);
|
|
5659
|
-
}
|
|
5660
|
-
});
|
|
5661
|
-
/**
|
|
5662
|
-
* Reset and re-run all migrations
|
|
5663
|
-
*/
|
|
5664
|
-
commander.program.command("migrate:refresh").description("Reset and re-run all migrations.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5665
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5666
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5667
|
-
try {
|
|
5668
|
-
await new Migrate(basePath, void 0, (msg, sts) => {
|
|
5669
|
-
if (sts) this.output[sts](msg);
|
|
5670
|
-
}).refresh(this.config, opts, true);
|
|
5671
|
-
} catch (e) {
|
|
5672
|
-
this.output.error("ERROR: " + e);
|
|
5673
|
-
}
|
|
5674
|
-
});
|
|
5675
|
-
/**
|
|
5676
|
-
* Drop all tables and re-run all migrations
|
|
5677
|
-
*/
|
|
5678
|
-
commander.program.command("migrate:fresh").description("Drop all tables and re-run all migrations.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5679
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5680
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5681
|
-
try {
|
|
5682
|
-
await new Migrate(basePath, void 0, (msg, sts) => {
|
|
5683
|
-
if (sts) this.output[sts](msg);
|
|
5684
|
-
}).fresh(this.config, opts, true);
|
|
5685
|
-
} catch (e) {
|
|
5686
|
-
this.output.error("ERROR: " + e);
|
|
5687
|
-
}
|
|
5688
|
-
});
|
|
5689
|
-
/**
|
|
5690
|
-
* Check the migration status
|
|
5691
|
-
*/
|
|
5692
|
-
commander.program.command("migrate:status").description("Show the status of each migration.").option("-p, --path [path]", "The path to the migrations directory.").action(async (opts) => {
|
|
5693
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5694
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5695
|
-
try {
|
|
5696
|
-
const migrations = await new Migrate(basePath, void 0, (msg, sts) => {
|
|
5697
|
-
if (sts) this.output[sts](msg);
|
|
5698
|
-
}).status(this.config, opts, true);
|
|
5699
|
-
if (migrations.length > 0) {
|
|
5700
|
-
__h3ravel_shared.Logger.twoColumnDetail(__h3ravel_shared.Logger.log("Migration name", "gray", false), __h3ravel_shared.Logger.log("Batch / Status", "gray", false));
|
|
5701
|
-
migrations.forEach((migration) => {
|
|
5702
|
-
const status = migration.ran ? `[${migration.batch}] ${__h3ravel_shared.Logger.log("Ran", "green", false)}` : __h3ravel_shared.Logger.log("Pending", "yellow", false);
|
|
5703
|
-
__h3ravel_shared.Logger.twoColumnDetail(migration.name, status);
|
|
5704
|
-
});
|
|
5705
|
-
} else console.log("No migrations found");
|
|
5706
|
-
} catch (e) {
|
|
5707
|
-
this.output.error("ERROR: " + e);
|
|
5708
|
-
}
|
|
5709
|
-
});
|
|
5710
|
-
/**
|
|
5711
|
-
* Run database seeders
|
|
5712
|
-
*/
|
|
5713
|
-
commander.program.command("db:seed").description("Run database seeders.").option("-p, --path [path]", "The path to the seeders directory.").action(async (opts) => {
|
|
5714
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5715
|
-
const basePath = opts.path ? path.default.join(this.cwd, opts.path) : this.cwd;
|
|
5716
|
-
try {
|
|
5717
|
-
var _this$config$seeders;
|
|
5718
|
-
const { arquebus: arquebus$1 } = await new Migrate(basePath).setupConnection({
|
|
5719
|
-
...this.config,
|
|
5720
|
-
skipConnection: false
|
|
5721
|
-
});
|
|
5722
|
-
const { SeederRunner: SeederRunner$1 } = await Promise.resolve().then(() => require("./seeders-D-v59HCz.cjs"));
|
|
5723
|
-
const runner = new SeederRunner$1(arquebus$1);
|
|
5724
|
-
const seederPath = path.default.join(basePath, ((_this$config$seeders = this.config.seeders) === null || _this$config$seeders === void 0 ? void 0 : _this$config$seeders.path) ?? "./seeders");
|
|
5725
|
-
await runner.setConnection(this.config.client).run([seederPath]);
|
|
5726
|
-
this.output.success("Seeders executed successfully.");
|
|
5727
|
-
} catch (e) {
|
|
5728
|
-
this.output.error("ERROR: " + e);
|
|
5662
|
+
const kernel = new _h3ravel_musket.Kernel(this).setCwd(this.cwd).setConfig({
|
|
5663
|
+
baseCommands: ArquebusCommands,
|
|
5664
|
+
name: "arquebus",
|
|
5665
|
+
hideMusketInfo: true,
|
|
5666
|
+
versionSeparator: "\n"
|
|
5667
|
+
}).bootstrap();
|
|
5668
|
+
kernel.modules = [{
|
|
5669
|
+
name: "@h3ravel/arquebus",
|
|
5670
|
+
label: "Arquebus CLI version",
|
|
5671
|
+
version
|
|
5672
|
+
}, {
|
|
5673
|
+
name: "@h3ravel/arquebus",
|
|
5674
|
+
label: "Arquebus Local version",
|
|
5675
|
+
version: this.modulePackage.version || "N/A"
|
|
5676
|
+
}];
|
|
5677
|
+
return await kernel.run();
|
|
5678
|
+
}
|
|
5679
|
+
async initialize(type = "js") {
|
|
5680
|
+
if (!this.modulePath) this.output.error(["ERROR: No local arquebus install found", " Try running: pnpm add @h3ravel/arquebus"]);
|
|
5681
|
+
if (this.configPath) this.output.error(`ERROR: ${this.configPath} already exists`);
|
|
5682
|
+
try {
|
|
5683
|
+
const stubPath = `./arquebus.config.${type}`;
|
|
5684
|
+
const code = await (0, node_fs_promises.readFile)(await this.resolveStub(`arquebus.config-${type}.stub`), "utf8");
|
|
5685
|
+
await (0, node_fs_promises.writeFile)(node_path.default.join(this.cwd, stubPath), code);
|
|
5686
|
+
this.output.success(`Initialized: Arquebus has been initialized as ${stubPath}`);
|
|
5687
|
+
} catch (error) {
|
|
5688
|
+
this.output.error("ERROR: " + error);
|
|
5689
|
+
}
|
|
5690
|
+
}
|
|
5691
|
+
async makeMigration(rawName, options) {
|
|
5692
|
+
this.ensureConfigured();
|
|
5693
|
+
try {
|
|
5694
|
+
const name = _h3ravel_support.Str.snake(rawName);
|
|
5695
|
+
const migrationPath = node_path.default.join(this.cwd, options.path ?? this.config.migrations?.path ?? "./migrations");
|
|
5696
|
+
let table = options.table;
|
|
5697
|
+
let create = Boolean(options.create) && options.create !== "";
|
|
5698
|
+
if (!table && typeof options.create === "string") {
|
|
5699
|
+
table = options.create;
|
|
5700
|
+
create = true;
|
|
5729
5701
|
}
|
|
5730
|
-
|
|
5731
|
-
|
|
5732
|
-
|
|
5733
|
-
|
|
5734
|
-
commander.program.command("make:seeder <name>").description("Create a new Seeder file.").addOption(new commander.Option("-l, --type [string]", "Type of seeder file to generate.").choices(["js", "ts"]).default("js", "generates a js seeder file")).option("--force", "Force creation if seeder already exists.", false).option("-p, --path [path]", "The path to the seeders directory.").action(async (name, opts) => {
|
|
5735
|
-
var _this$config$seeders2;
|
|
5736
|
-
if (!this.configPath) this.terminateNotFound();
|
|
5737
|
-
const seederPath = path.default.join(this.cwd, opts.path ?? ((_this$config$seeders2 = this.config.seeders) === null || _this$config$seeders2 === void 0 ? void 0 : _this$config$seeders2.path) ?? "./seeders", __h3ravel_support.Str.of(name).snake("-") + "." + opts.type);
|
|
5738
|
-
try {
|
|
5739
|
-
if (!opts.force && await __h3ravel_shared.FileSystem.fileExists(seederPath)) this.output.error("ERROR: Seeder already exists.");
|
|
5740
|
-
await (0, node_fs_promises.mkdir)(path.default.dirname(seederPath), { recursive: true });
|
|
5741
|
-
let stub = await (0, node_fs_promises.readFile)(path.default.join(this.modulePath, `src/stubs/seeder-${opts.type}.stub`), "utf-8");
|
|
5742
|
-
stub = stub.replace(/{{ name }}/g, name);
|
|
5743
|
-
await (0, node_fs_promises.writeFile)(seederPath, stub);
|
|
5744
|
-
this.output.split("INFO: Created Seeder", path.default.relative(this.cwd, seederPath));
|
|
5745
|
-
} catch (e) {
|
|
5746
|
-
this.output.error("ERROR: " + e);
|
|
5702
|
+
if (!table) {
|
|
5703
|
+
const guessed = TableGuesser.guess(name);
|
|
5704
|
+
table = guessed[0];
|
|
5705
|
+
create = Boolean(guessed[1]);
|
|
5747
5706
|
}
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
5752
|
-
|
|
5753
|
-
|
|
5754
|
-
|
|
5755
|
-
|
|
5756
|
-
|
|
5757
|
-
|
|
5758
|
-
|
|
5759
|
-
|
|
5760
|
-
|
|
5761
|
-
|
|
5762
|
-
|
|
5763
|
-
|
|
5764
|
-
|
|
5707
|
+
this.output.info("INFO: Creating Migration");
|
|
5708
|
+
const fileName = await new MigrationCreator(void 0, options.type ?? "js").create(name, migrationPath, table, create);
|
|
5709
|
+
this.output.success(`INFO: Migration Created \n ${_h3ravel_shared.Logger.log(node_path.default.basename(fileName), "gray", false)}`, true);
|
|
5710
|
+
} catch (error) {
|
|
5711
|
+
this.output.error("ERROR: " + error);
|
|
5712
|
+
}
|
|
5713
|
+
}
|
|
5714
|
+
async publishMigrations(pkg, options) {
|
|
5715
|
+
this.ensureConfigured();
|
|
5716
|
+
try {
|
|
5717
|
+
const packagePath = Utils.findModulePkg(pkg) ?? "";
|
|
5718
|
+
if (!packagePath) this.output.error(`ERROR: package ${pkg} not found`);
|
|
5719
|
+
const basePath = node_path.default.join(this.cwd, options.path ?? this.config.migrations?.path ?? "./migrations");
|
|
5720
|
+
const pkgJson = JSON.parse(await (0, node_fs_promises.readFile)(node_path.default.join(packagePath, "package.json"), "utf8"));
|
|
5721
|
+
const creator = new MigrationCreator(node_path.default.join(packagePath, pkgJson.migrations ?? "migrations"));
|
|
5722
|
+
const pkgInfo = _h3ravel_shared.Logger.log(node_path.default.basename(pkgJson.name + "@" + pkgJson.version), ["italic", "gray"], false);
|
|
5723
|
+
this.output.info(`INFO: Publishing migrations from ${pkgInfo}`);
|
|
5724
|
+
await creator.publish(basePath, (fileName) => {
|
|
5725
|
+
this.output.split("INFO: Migration Published", fileName, "success");
|
|
5726
|
+
});
|
|
5727
|
+
} catch (error) {
|
|
5728
|
+
this.output.error("ERROR: " + error);
|
|
5729
|
+
}
|
|
5730
|
+
}
|
|
5731
|
+
async migrate(options) {
|
|
5732
|
+
this.ensureConfigured();
|
|
5733
|
+
const basePath = this.resolveBasePath(options.path);
|
|
5734
|
+
try {
|
|
5735
|
+
const step = typeof options.step === "string" ? Number.parseInt(options.step) : options.step;
|
|
5736
|
+
await new Migrate(basePath).run(this.config, {
|
|
5737
|
+
...options,
|
|
5738
|
+
step
|
|
5739
|
+
}, true);
|
|
5740
|
+
} catch (error) {
|
|
5741
|
+
this.output.error("ERROR: " + error);
|
|
5742
|
+
}
|
|
5743
|
+
}
|
|
5744
|
+
async rollback(options) {
|
|
5745
|
+
this.ensureConfigured();
|
|
5746
|
+
const basePath = this.resolveBasePath(options.path);
|
|
5747
|
+
try {
|
|
5748
|
+
const step = typeof options.step === "string" ? Number.parseInt(options.step) : options.step;
|
|
5749
|
+
await new Migrate(basePath, void 0, this.migrationReporter()).rollback(this.config, {
|
|
5750
|
+
...options,
|
|
5751
|
+
step
|
|
5752
|
+
}, true);
|
|
5753
|
+
} catch (error) {
|
|
5754
|
+
this.output.error("ERROR: " + error);
|
|
5755
|
+
}
|
|
5756
|
+
}
|
|
5757
|
+
async reset(options) {
|
|
5758
|
+
await this.runMigrationOperation("reset", options);
|
|
5759
|
+
}
|
|
5760
|
+
async refresh(options) {
|
|
5761
|
+
await this.runMigrationOperation("refresh", options);
|
|
5762
|
+
}
|
|
5763
|
+
async fresh(options) {
|
|
5764
|
+
await this.runMigrationOperation("fresh", options);
|
|
5765
|
+
}
|
|
5766
|
+
async runMigrationOperation(operation, options) {
|
|
5767
|
+
this.ensureConfigured();
|
|
5768
|
+
const basePath = this.resolveBasePath(options.path);
|
|
5769
|
+
try {
|
|
5770
|
+
await new Migrate(basePath, void 0, this.migrationReporter())[operation](this.config, options, true);
|
|
5771
|
+
} catch (error) {
|
|
5772
|
+
this.output.error("ERROR: " + error);
|
|
5773
|
+
}
|
|
5774
|
+
}
|
|
5775
|
+
async status(options) {
|
|
5776
|
+
this.ensureConfigured();
|
|
5777
|
+
const basePath = this.resolveBasePath(options.path);
|
|
5778
|
+
try {
|
|
5779
|
+
const migrations = await new Migrate(basePath, void 0, this.migrationReporter()).status(this.config, options, true);
|
|
5780
|
+
if (migrations.length === 0) {
|
|
5781
|
+
this.output.info("No migrations found");
|
|
5782
|
+
return;
|
|
5765
5783
|
}
|
|
5766
|
-
|
|
5767
|
-
|
|
5768
|
-
|
|
5784
|
+
_h3ravel_shared.Logger.twoColumnDetail(_h3ravel_shared.Logger.log("Migration name", "gray", false), _h3ravel_shared.Logger.log("Batch / Status", "gray", false));
|
|
5785
|
+
migrations.forEach((migration) => {
|
|
5786
|
+
const status = migration.ran ? `[${migration.batch}] ${_h3ravel_shared.Logger.log("Ran", "green", false)}` : _h3ravel_shared.Logger.log("Pending", "yellow", false);
|
|
5787
|
+
_h3ravel_shared.Logger.twoColumnDetail(migration.name, status);
|
|
5788
|
+
});
|
|
5789
|
+
} catch (error) {
|
|
5790
|
+
this.output.error("ERROR: " + error);
|
|
5791
|
+
}
|
|
5769
5792
|
}
|
|
5770
|
-
|
|
5771
|
-
|
|
5772
|
-
|
|
5773
|
-
|
|
5774
|
-
|
|
5775
|
-
|
|
5793
|
+
async seed(options) {
|
|
5794
|
+
this.ensureConfigured();
|
|
5795
|
+
const basePath = this.resolveBasePath(options.path);
|
|
5796
|
+
try {
|
|
5797
|
+
const { arquebus } = await new Migrate(basePath).setupConnection({
|
|
5798
|
+
...this.config,
|
|
5799
|
+
skipConnection: false
|
|
5800
|
+
});
|
|
5801
|
+
const seederPath = node_path.default.join(basePath, this.config.seeders?.path ?? "./seeders");
|
|
5802
|
+
await new SeederRunner(arquebus).setConnection(this.config.client).run([seederPath]);
|
|
5803
|
+
this.output.success("Seeders executed successfully.");
|
|
5804
|
+
} catch (error) {
|
|
5805
|
+
this.output.error("ERROR: " + error);
|
|
5806
|
+
}
|
|
5807
|
+
}
|
|
5808
|
+
async makeSeeder(name, options) {
|
|
5809
|
+
this.ensureConfigured();
|
|
5810
|
+
const type = options.type ?? "js";
|
|
5811
|
+
const seederPath = node_path.default.join(this.cwd, options.path ?? this.config.seeders?.path ?? "./seeders", `${_h3ravel_support.Str.of(name).snake("-")}.${type}`);
|
|
5812
|
+
try {
|
|
5813
|
+
if (!options.force && await _h3ravel_shared.FileSystem.fileExists(seederPath)) this.output.error("ERROR: Seeder already exists.");
|
|
5814
|
+
await (0, node_fs_promises.mkdir)(node_path.default.dirname(seederPath), { recursive: true });
|
|
5815
|
+
await (0, node_fs_promises.writeFile)(seederPath, (await (0, node_fs_promises.readFile)(await this.resolveStub(`seeder-${type}.stub`), "utf8")).replace(/{{ name }}/g, name));
|
|
5816
|
+
this.output.split("INFO: Created Seeder", node_path.default.relative(this.cwd, seederPath));
|
|
5817
|
+
} catch (error) {
|
|
5818
|
+
this.output.error("ERROR: " + error);
|
|
5819
|
+
}
|
|
5820
|
+
}
|
|
5821
|
+
async makeModel(name, options) {
|
|
5822
|
+
this.ensureConfigured();
|
|
5823
|
+
const type = options.type ?? "js";
|
|
5824
|
+
const modelPath = node_path.default.join(this.cwd, options.path ?? this.config.models?.path ?? "./models", `${name.toLowerCase()}.${type}`);
|
|
5825
|
+
try {
|
|
5826
|
+
if (!options.force && await _h3ravel_shared.FileSystem.fileExists(modelPath)) this.output.error("ERROR: Model already exists.");
|
|
5827
|
+
await (0, node_fs_promises.mkdir)(node_path.default.dirname(modelPath), { recursive: true });
|
|
5828
|
+
await (0, node_fs_promises.writeFile)(modelPath, (await (0, node_fs_promises.readFile)(await this.resolveStub(`model-${type}.stub`), "utf8")).replace(/{{ name }}/g, name));
|
|
5829
|
+
this.output.success(`Created Model: ${modelPath}`);
|
|
5830
|
+
} catch (error) {
|
|
5831
|
+
this.output.error("ERROR: " + error);
|
|
5832
|
+
}
|
|
5833
|
+
}
|
|
5834
|
+
}).init();
|
|
5776
5835
|
//#endregion
|
|
5777
|
-
exports.runner_default = runner_default;
|