@h3ravel/arquebus 2.0.0 → 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 +580 -451
- package/bin/index.js +566 -437
- package/dist/browser/index.cjs +54 -51
- package/dist/browser/index.d.ts +804 -737
- package/dist/browser/index.js +48 -44
- package/dist/concerns/index.cjs +152 -143
- package/dist/concerns/index.d.ts +849 -776
- package/dist/concerns/index.js +143 -133
- package/dist/index.cjs +186 -185
- package/dist/index.d.ts +856 -782
- package/dist/index.js +179 -177
- package/dist/inspector/index.cjs +175 -174
- package/dist/inspector/index.d.ts +0 -1
- package/dist/inspector/index.js +161 -159
- package/dist/migrations/index.cjs +183 -182
- package/dist/migrations/index.d.ts +799 -727
- package/dist/migrations/index.js +172 -170
- package/dist/relations/index.cjs +152 -143
- package/dist/relations/index.d.ts +835 -762
- package/dist/relations/index.js +143 -133
- package/dist/seeders/index.cjs +7 -5
- package/dist/seeders/index.d.ts +797 -724
- package/dist/seeders/index.js +8 -6
- package/dist/{migrations/stubs/stubs → stubs}/migration-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration-ts.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.create-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.create-ts.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.update-js.stub +1 -0
- package/dist/{migrations/stubs/stubs → stubs}/migration.update-ts.stub +1 -0
- package/dist/stubs/model-ts.stub +9 -0
- package/package.json +31 -31
- 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/dist/stubs/stubs/model-ts.stub +0 -5
- /package/dist/stubs/{stubs/arquebus.config-js.stub → arquebus.config-js.stub} +0 -0
- /package/dist/stubs/{stubs/arquebus.config-ts.stub → arquebus.config-ts.stub} +0 -0
- /package/dist/stubs/{stubs/model-js.stub → model-js.stub} +0 -0
- /package/dist/stubs/{stubs/seeder-js.stub → seeder-js.stub} +0 -0
- /package/dist/stubs/{stubs/seeder-ts.stub → seeder-ts.stub} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import fs, { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
4
5
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
6
|
+
import { Arr, Obj, Str, data_get, data_set } from "@h3ravel/support";
|
|
7
|
+
import * as dayjsModule from "dayjs";
|
|
8
|
+
import * as advancedFormatModule from "dayjs/plugin/advancedFormat.js";
|
|
9
|
+
import { Collection as Collection$1, collect } from "@h3ravel/collect.js";
|
|
9
10
|
import Knex$1 from "knex";
|
|
10
|
-
import { existsSync } from "fs";
|
|
11
|
-
import { FileSystem, Logger, TaskManager } from "@h3ravel/shared";
|
|
11
|
+
import { existsSync as existsSync$1 } from "fs";
|
|
12
|
+
import { FileSystem, Logger, TaskManager, importFile } from "@h3ravel/shared";
|
|
12
13
|
import pluralize from "pluralize";
|
|
13
14
|
import resolveFrom from "resolve-from";
|
|
14
15
|
//#region \0rolldown/runtime.js
|
|
@@ -50,16 +51,14 @@ var SeederRunner = class {
|
|
|
50
51
|
this.resolver = resolver;
|
|
51
52
|
}
|
|
52
53
|
path(p) {
|
|
53
|
-
this.paths = Array.from(new Set([...this.paths, p]));
|
|
54
|
+
this.paths = Array.from(/* @__PURE__ */ new Set([...this.paths, p]));
|
|
54
55
|
}
|
|
55
56
|
getPaths() {
|
|
56
57
|
return this.paths;
|
|
57
58
|
}
|
|
58
59
|
resolveConnection(connection) {
|
|
59
|
-
var _getInstance, _ref, _instance$connections;
|
|
60
60
|
const name = connection || this.connection || "default";
|
|
61
|
-
|
|
62
|
-
if (!!!(instance === null || instance === void 0 || (_instance$connections = instance.connections) === null || _instance$connections === void 0 ? void 0 : _instance$connections[name])) this.resolver.autoLoad().catch(() => {
|
|
61
|
+
if (!!!(this.resolver.getInstance?.() ?? null)?.connections?.[name]) this.resolver.autoLoad().catch(() => {
|
|
63
62
|
/** noop */
|
|
64
63
|
});
|
|
65
64
|
return this.resolver.fire(name);
|
|
@@ -114,7 +113,10 @@ var SeederCreator = class {
|
|
|
114
113
|
getStubPath(type) {
|
|
115
114
|
if (this.customStubPath) return path.join(this.customStubPath, `seeder-${type}.stub`);
|
|
116
115
|
const __dirname = this.getDirname(import.meta);
|
|
117
|
-
|
|
116
|
+
const name = `seeder-${type}.stub`;
|
|
117
|
+
const resolved = [path.join(__dirname, "stubs", name), path.join(__dirname, "../stubs", name)].find((candidate) => existsSync(candidate));
|
|
118
|
+
if (!resolved) throw new Error(`Seeder stub not found: ${name}`);
|
|
119
|
+
return resolved;
|
|
118
120
|
}
|
|
119
121
|
getDirname(meta) {
|
|
120
122
|
if (typeof __dirname !== "undefined") return __dirname;
|
|
@@ -172,7 +174,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
172
174
|
}
|
|
173
175
|
setModel(model, ids = []) {
|
|
174
176
|
this.model = model;
|
|
175
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
177
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
176
178
|
this.message = `No query results for model [${model}]`;
|
|
177
179
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
178
180
|
else this.message += ".";
|
|
@@ -263,29 +265,33 @@ function compose$1(Base, ...mixins) {
|
|
|
263
265
|
}, Base);
|
|
264
266
|
}
|
|
265
267
|
//#endregion
|
|
266
|
-
//#region src/
|
|
268
|
+
//#region src/dayjs.ts
|
|
269
|
+
const dayjs = dayjsModule.default ?? dayjsModule;
|
|
270
|
+
const advancedFormat = advancedFormatModule.default ?? advancedFormatModule;
|
|
267
271
|
dayjs.extend(advancedFormat);
|
|
272
|
+
//#endregion
|
|
273
|
+
//#region src/utils.ts
|
|
268
274
|
const now = (format = "YYYY-MM-DD HH:mm:ss") => dayjs().format(format);
|
|
269
275
|
const getRelationName = (relationMethod) => {
|
|
270
|
-
return snake(relationMethod.substring(8));
|
|
276
|
+
return Str.snake(relationMethod.substring(8));
|
|
271
277
|
};
|
|
272
278
|
const getScopeName = (scopeMethod) => {
|
|
273
|
-
return snake(scopeMethod.substring(5));
|
|
279
|
+
return Str.snake(scopeMethod.substring(5));
|
|
274
280
|
};
|
|
275
281
|
const getRelationMethod = (relation) => {
|
|
276
|
-
return camel(`relation_${relation}`);
|
|
282
|
+
return Str.camel(`relation_${relation}`);
|
|
277
283
|
};
|
|
278
284
|
const getScopeMethod = (scope) => {
|
|
279
|
-
return camel(`scope_${scope}`);
|
|
285
|
+
return Str.camel(`scope_${scope}`);
|
|
280
286
|
};
|
|
281
287
|
const getAttrMethod = (attr) => {
|
|
282
|
-
return camel(`attribute_${attr}`);
|
|
288
|
+
return Str.camel(`attribute_${attr}`);
|
|
283
289
|
};
|
|
284
290
|
const getGetterMethod = (attr) => {
|
|
285
|
-
return camel(`get_${attr}_attribute`);
|
|
291
|
+
return Str.camel(`get_${attr}_attribute`);
|
|
286
292
|
};
|
|
287
293
|
const getSetterMethod = (attr) => {
|
|
288
|
-
return camel(`set_${attr}_attribute`);
|
|
294
|
+
return Str.camel(`set_${attr}_attribute`);
|
|
289
295
|
};
|
|
290
296
|
const getAttrName = (attrMethod) => {
|
|
291
297
|
return attrMethod.substring(3, attrMethod.length - 9).toLowerCase();
|
|
@@ -304,8 +310,8 @@ const tap = (instance, callback) => {
|
|
|
304
310
|
const { compose } = mixin_exports;
|
|
305
311
|
const flatten = (arr) => arr.flat();
|
|
306
312
|
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
307
|
-
const kebabCase = (str) => trim(
|
|
308
|
-
const snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
313
|
+
const kebabCase = (str) => Str.trim(Str.kebab(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/-+/g, "-");
|
|
314
|
+
const snakeCase = (str) => Str.trim(Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
|
|
309
315
|
const defineConfig = (config) => {
|
|
310
316
|
return config;
|
|
311
317
|
};
|
|
@@ -395,8 +401,7 @@ var Relation$1 = class {
|
|
|
395
401
|
return this.parent.getQualifiedKeyName();
|
|
396
402
|
}
|
|
397
403
|
getExistenceCompareKey() {
|
|
398
|
-
|
|
399
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
404
|
+
return this.getQualifiedForeignKeyName?.();
|
|
400
405
|
}
|
|
401
406
|
};
|
|
402
407
|
//#endregion
|
|
@@ -537,8 +542,7 @@ const HasAttributes = (Model) => {
|
|
|
537
542
|
return this;
|
|
538
543
|
}
|
|
539
544
|
normalizeCastClassResponse(key, value) {
|
|
540
|
-
|
|
541
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
545
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
542
546
|
}
|
|
543
547
|
syncOriginal() {
|
|
544
548
|
this.original = this.getAttributes();
|
|
@@ -648,9 +652,9 @@ const HasAttributes = (Model) => {
|
|
|
648
652
|
return null;
|
|
649
653
|
}
|
|
650
654
|
case "collection": try {
|
|
651
|
-
return collect
|
|
655
|
+
return collect(JSON.parse(value));
|
|
652
656
|
} catch {
|
|
653
|
-
return collect
|
|
657
|
+
return collect([]);
|
|
654
658
|
}
|
|
655
659
|
case "date": return this.asDate(value);
|
|
656
660
|
case "datetime":
|
|
@@ -663,8 +667,8 @@ const HasAttributes = (Model) => {
|
|
|
663
667
|
attributesToData() {
|
|
664
668
|
let attributes = { ...this.attributes };
|
|
665
669
|
for (const key in attributes) {
|
|
666
|
-
if (this.hidden.includes(key)) attributes =
|
|
667
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
670
|
+
if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
|
|
671
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
|
|
668
672
|
}
|
|
669
673
|
for (const key of this.getDates()) {
|
|
670
674
|
if (attributes[key] === void 0) continue;
|
|
@@ -719,7 +723,7 @@ const HasAttributes = (Model) => {
|
|
|
719
723
|
}
|
|
720
724
|
hasCast(key, types = []) {
|
|
721
725
|
if (key in this.casts) {
|
|
722
|
-
types =
|
|
726
|
+
types = flatten(types);
|
|
723
727
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
724
728
|
}
|
|
725
729
|
return false;
|
|
@@ -778,10 +782,14 @@ const HasGlobalScopes = (Model) => {
|
|
|
778
782
|
static globalScopes;
|
|
779
783
|
static addGlobalScope(scope, implementation = null) {
|
|
780
784
|
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
781
|
-
|
|
785
|
+
const scopes = this.globalScopes ?? {};
|
|
786
|
+
data_set(scopes, this.name + "." + scope, implementation);
|
|
787
|
+
this.globalScopes = scopes;
|
|
782
788
|
return implementation;
|
|
783
789
|
} else if (scope instanceof Scope) {
|
|
784
|
-
|
|
790
|
+
const scopes = this.globalScopes ?? {};
|
|
791
|
+
data_set(scopes, this.name + "." + scope.constructor.name, scope);
|
|
792
|
+
this.globalScopes = scopes;
|
|
785
793
|
return scope;
|
|
786
794
|
}
|
|
787
795
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -790,8 +798,8 @@ const HasGlobalScopes = (Model) => {
|
|
|
790
798
|
return this.getGlobalScope(scope) !== null;
|
|
791
799
|
}
|
|
792
800
|
static getGlobalScope(scope) {
|
|
793
|
-
if (typeof scope === "string") return
|
|
794
|
-
return
|
|
801
|
+
if (typeof scope === "string") return data_get(this.globalScopes ?? {}, this.name + "." + scope);
|
|
802
|
+
return data_get(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
795
803
|
}
|
|
796
804
|
static getAllGlobalScopes() {
|
|
797
805
|
return this.globalScopes;
|
|
@@ -800,7 +808,7 @@ const HasGlobalScopes = (Model) => {
|
|
|
800
808
|
this.globalScopes = scopes;
|
|
801
809
|
}
|
|
802
810
|
getGlobalScopes() {
|
|
803
|
-
return
|
|
811
|
+
return data_get(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
804
812
|
}
|
|
805
813
|
};
|
|
806
814
|
};
|
|
@@ -900,7 +908,7 @@ const HasOneOrMany$1 = (Relation) => {
|
|
|
900
908
|
}
|
|
901
909
|
buildDictionary(results) {
|
|
902
910
|
const foreign = this.getForeignKeyName();
|
|
903
|
-
return collect
|
|
911
|
+
return collect(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
904
912
|
}
|
|
905
913
|
async save(model) {
|
|
906
914
|
this.setForeignAttributesForCreate(model);
|
|
@@ -971,9 +979,7 @@ var HasMany$1 = class extends compose(Relation$1, HasOneOrMany$1) {
|
|
|
971
979
|
return this.getParentKey() !== null ? await this.query.get() : new Collection([]);
|
|
972
980
|
}
|
|
973
981
|
getForeignKeyName() {
|
|
974
|
-
|
|
975
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
976
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
982
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
977
983
|
}
|
|
978
984
|
buildDictionary(results) {
|
|
979
985
|
const foreign = this.getForeignKeyName();
|
|
@@ -1008,9 +1014,7 @@ var HasOne$1 = class extends compose(Relation$1, HasOneOrMany$1, SupportsDefault
|
|
|
1008
1014
|
return this.matchOneOrMany(models, results, relation, "one");
|
|
1009
1015
|
}
|
|
1010
1016
|
getForeignKeyName() {
|
|
1011
|
-
|
|
1012
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
1013
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
1017
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
1014
1018
|
}
|
|
1015
1019
|
async getResults() {
|
|
1016
1020
|
if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
|
|
@@ -1119,10 +1123,10 @@ var HasManyThrough$1 = class extends Relation$1 {
|
|
|
1119
1123
|
}
|
|
1120
1124
|
const model = await this.first(columns);
|
|
1121
1125
|
if (model) return model;
|
|
1122
|
-
return callback
|
|
1126
|
+
return callback?.();
|
|
1123
1127
|
}
|
|
1124
1128
|
async find(id, columns = ["*"]) {
|
|
1125
|
-
if (isArray(id)) return await this.findMany(id, columns);
|
|
1129
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
1126
1130
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
1127
1131
|
}
|
|
1128
1132
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -1150,7 +1154,7 @@ var HasManyThrough$1 = class extends Relation$1 {
|
|
|
1150
1154
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
1151
1155
|
}
|
|
1152
1156
|
shouldSelect(columns = ["*"]) {
|
|
1153
|
-
if (
|
|
1157
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
1154
1158
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
1155
1159
|
}
|
|
1156
1160
|
async chunk(count, callback) {
|
|
@@ -1244,7 +1248,7 @@ const HasRelations = (Model) => {
|
|
|
1244
1248
|
return this;
|
|
1245
1249
|
}
|
|
1246
1250
|
unsetRelation(relation) {
|
|
1247
|
-
this.relations =
|
|
1251
|
+
this.relations = Arr.except(this.relations, [relation]);
|
|
1248
1252
|
return this;
|
|
1249
1253
|
}
|
|
1250
1254
|
relationLoaded(relation) {
|
|
@@ -1267,8 +1271,7 @@ const HasRelations = (Model) => {
|
|
|
1267
1271
|
return data;
|
|
1268
1272
|
}
|
|
1269
1273
|
guessBelongsToRelation() {
|
|
1270
|
-
|
|
1271
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
1274
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
1272
1275
|
return getRelationName(functionName);
|
|
1273
1276
|
}
|
|
1274
1277
|
joiningTable(related, instance = null) {
|
|
@@ -1378,7 +1381,7 @@ const HidesAttributes = (Model) => {
|
|
|
1378
1381
|
makeVisible(...keys) {
|
|
1379
1382
|
const visible = flattenDeep(keys);
|
|
1380
1383
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
1381
|
-
this.hidden =
|
|
1384
|
+
this.hidden = collect(this.hidden).diff(visible).all();
|
|
1382
1385
|
return this;
|
|
1383
1386
|
}
|
|
1384
1387
|
makeHidden(key, ...keys) {
|
|
@@ -1522,10 +1525,9 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
|
|
|
1522
1525
|
asProxy() {
|
|
1523
1526
|
return new Proxy(this, {
|
|
1524
1527
|
get: function(target, prop) {
|
|
1525
|
-
var _target$connector$cli;
|
|
1526
1528
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
1527
1529
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
1528
|
-
const skipReturning = !!
|
|
1530
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
1529
1531
|
if ([
|
|
1530
1532
|
"select",
|
|
1531
1533
|
"from",
|
|
@@ -1728,6 +1730,9 @@ var arquebus = class arquebus {
|
|
|
1728
1730
|
if (this.instance === null) this.instance = new arquebus();
|
|
1729
1731
|
return this.instance;
|
|
1730
1732
|
}
|
|
1733
|
+
static withSchema() {
|
|
1734
|
+
return this;
|
|
1735
|
+
}
|
|
1731
1736
|
/**
|
|
1732
1737
|
* Initialize a new database connection
|
|
1733
1738
|
*
|
|
@@ -1816,13 +1821,13 @@ var arquebus = class arquebus {
|
|
|
1816
1821
|
const jsPath = path.resolve("arquebus.config.js");
|
|
1817
1822
|
const tsPath = path.resolve("arquebus.config.ts");
|
|
1818
1823
|
const instance = this.getInstance();
|
|
1819
|
-
if (existsSync(jsPath)) {
|
|
1820
|
-
config = (await
|
|
1824
|
+
if (existsSync$1(jsPath)) {
|
|
1825
|
+
config = (await importFile(jsPath)).default;
|
|
1821
1826
|
if (addConnection) instance.addConnection(config, config.client);
|
|
1822
1827
|
return config;
|
|
1823
1828
|
}
|
|
1824
|
-
if (existsSync(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
1825
|
-
config = (await
|
|
1829
|
+
if (existsSync$1(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
1830
|
+
config = (await importFile(tsPath)).default;
|
|
1826
1831
|
if (addConnection) instance.addConnection(config, config.client);
|
|
1827
1832
|
return config;
|
|
1828
1833
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -1838,7 +1843,7 @@ var arquebus = class arquebus {
|
|
|
1838
1843
|
"cjs"
|
|
1839
1844
|
], dir);
|
|
1840
1845
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
1841
|
-
config = (await
|
|
1846
|
+
config = (await importFile(found)).default;
|
|
1842
1847
|
if (addConnection) instance.addConnection(config, config.client);
|
|
1843
1848
|
return config;
|
|
1844
1849
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -1859,7 +1864,7 @@ var arquebus = class arquebus {
|
|
|
1859
1864
|
}
|
|
1860
1865
|
async destroyAll() {
|
|
1861
1866
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
1862
|
-
return connection
|
|
1867
|
+
return connection?.destroy();
|
|
1863
1868
|
}));
|
|
1864
1869
|
}
|
|
1865
1870
|
createModel(name, options = {}) {
|
|
@@ -1868,29 +1873,25 @@ var arquebus = class arquebus {
|
|
|
1868
1873
|
this.models = {
|
|
1869
1874
|
...this.models,
|
|
1870
1875
|
[name]: class extends BaseModel {
|
|
1871
|
-
table =
|
|
1872
|
-
connection =
|
|
1873
|
-
timestamps =
|
|
1874
|
-
primaryKey =
|
|
1875
|
-
keyType =
|
|
1876
|
-
incrementing =
|
|
1877
|
-
with =
|
|
1878
|
-
casts =
|
|
1879
|
-
static CREATED_AT =
|
|
1880
|
-
static UPDATED_AT =
|
|
1881
|
-
static DELETED_AT =
|
|
1876
|
+
table = options?.table ?? null;
|
|
1877
|
+
connection = options?.connection ?? null;
|
|
1878
|
+
timestamps = options?.timestamps ?? true;
|
|
1879
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
1880
|
+
keyType = options?.keyType ?? "int";
|
|
1881
|
+
incrementing = options?.incrementing ?? true;
|
|
1882
|
+
with = options?.with ?? [];
|
|
1883
|
+
casts = options?.casts ?? {};
|
|
1884
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
1885
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
1886
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
1882
1887
|
}
|
|
1883
1888
|
};
|
|
1884
1889
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
1885
1890
|
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
1886
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
1887
|
-
var _options$attributes;
|
|
1888
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
1889
|
-
};
|
|
1891
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
1890
1892
|
}
|
|
1891
1893
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
1892
|
-
|
|
1893
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
1894
|
+
return options.relations?.[relation](this);
|
|
1894
1895
|
};
|
|
1895
1896
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
1896
1897
|
this.models[name].setConnectionResolver(this);
|
|
@@ -1928,6 +1929,9 @@ var Model = class Model extends BaseModel {
|
|
|
1928
1929
|
this.buildRelationships(attributes);
|
|
1929
1930
|
return this.asProxy();
|
|
1930
1931
|
}
|
|
1932
|
+
static define() {
|
|
1933
|
+
return this;
|
|
1934
|
+
}
|
|
1931
1935
|
static query(trx = null) {
|
|
1932
1936
|
return new this().newQuery(trx);
|
|
1933
1937
|
}
|
|
@@ -2111,7 +2115,7 @@ var Model = class Model extends BaseModel {
|
|
|
2111
2115
|
});
|
|
2112
2116
|
}
|
|
2113
2117
|
toData() {
|
|
2114
|
-
return
|
|
2118
|
+
return Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
2115
2119
|
}
|
|
2116
2120
|
toJSON() {
|
|
2117
2121
|
return this.toData();
|
|
@@ -2122,6 +2126,15 @@ var Model = class Model extends BaseModel {
|
|
|
2122
2126
|
toString() {
|
|
2123
2127
|
return this.toJson();
|
|
2124
2128
|
}
|
|
2129
|
+
getAttributes() {
|
|
2130
|
+
return super.getAttributes();
|
|
2131
|
+
}
|
|
2132
|
+
getAttribute(key) {
|
|
2133
|
+
return super.getAttribute(key);
|
|
2134
|
+
}
|
|
2135
|
+
setAttribute(key, value) {
|
|
2136
|
+
return super.setAttribute(key, value);
|
|
2137
|
+
}
|
|
2125
2138
|
fill(attributes) {
|
|
2126
2139
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
2127
2140
|
return this;
|
|
@@ -2162,10 +2175,9 @@ var Model = class Model extends BaseModel {
|
|
|
2162
2175
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
2163
2176
|
const attributes = this.getAttributes();
|
|
2164
2177
|
if (this.getIncrementing()) {
|
|
2165
|
-
var _data$;
|
|
2166
2178
|
const keyName = this.getKeyName();
|
|
2167
2179
|
const data = await query.insert([attributes], [keyName]);
|
|
2168
|
-
this.setAttribute(keyName,
|
|
2180
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
2169
2181
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
2170
2182
|
this.exists = true;
|
|
2171
2183
|
await this.execHooks("created", options);
|
|
@@ -2207,7 +2219,7 @@ var Model = class Model extends BaseModel {
|
|
|
2207
2219
|
if (!this.exists) return Promise.resolve(void 0);
|
|
2208
2220
|
const model = await this.constructor.query().where(this.getKeyName(), this.getKey()).first();
|
|
2209
2221
|
this.attributes = { ...model.attributes };
|
|
2210
|
-
await this.load(collect
|
|
2222
|
+
await this.load(collect(this.relations).reject((relation) => {
|
|
2211
2223
|
return relation instanceof Pivot;
|
|
2212
2224
|
}).keys().all());
|
|
2213
2225
|
this.syncOriginal();
|
|
@@ -2271,7 +2283,7 @@ var Pivot = class extends Model {
|
|
|
2271
2283
|
};
|
|
2272
2284
|
//#endregion
|
|
2273
2285
|
//#region src/collection.ts
|
|
2274
|
-
var Collection = class
|
|
2286
|
+
var Collection = class extends Collection$1 {
|
|
2275
2287
|
newConstructor(...args) {
|
|
2276
2288
|
return new (this.getConstructor())(...args);
|
|
2277
2289
|
}
|
|
@@ -2280,17 +2292,18 @@ var Collection = class Collection extends Collection$1 {
|
|
|
2280
2292
|
}
|
|
2281
2293
|
async load(...relations) {
|
|
2282
2294
|
if (this.isNotEmpty()) {
|
|
2283
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
2295
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
2284
2296
|
return this.newConstructor(items);
|
|
2285
2297
|
}
|
|
2286
2298
|
return this;
|
|
2287
2299
|
}
|
|
2288
2300
|
async loadAggregate(relations, column, action = null) {
|
|
2289
2301
|
if (this.isEmpty()) return this;
|
|
2290
|
-
const
|
|
2291
|
-
const
|
|
2302
|
+
const first = this.first();
|
|
2303
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
2304
|
+
const attributes = collect(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
2292
2305
|
this.each((model) => {
|
|
2293
|
-
const extraAttributes =
|
|
2306
|
+
const extraAttributes = Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
2294
2307
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
2295
2308
|
});
|
|
2296
2309
|
return this;
|
|
@@ -2328,20 +2341,21 @@ var Collection = class Collection extends Collection$1 {
|
|
|
2328
2341
|
diff(items) {
|
|
2329
2342
|
const diff = new this.constructor();
|
|
2330
2343
|
const dictionary = this.getDictionary(items);
|
|
2331
|
-
this.
|
|
2344
|
+
this.all().map((item) => {
|
|
2332
2345
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
2333
2346
|
});
|
|
2334
2347
|
return diff;
|
|
2335
2348
|
}
|
|
2336
|
-
except(keys) {
|
|
2337
|
-
const
|
|
2349
|
+
except(...keys) {
|
|
2350
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
2351
|
+
const dictionary = Arr.except(this.getDictionary(), values);
|
|
2338
2352
|
return new this.constructor(Object.values(dictionary));
|
|
2339
2353
|
}
|
|
2340
2354
|
intersect(items) {
|
|
2341
2355
|
const intersect = new this.constructor();
|
|
2342
|
-
if (isEmpty(items)) return intersect;
|
|
2356
|
+
if (Arr.isEmpty(items)) return intersect;
|
|
2343
2357
|
const dictionary = this.getDictionary(items);
|
|
2344
|
-
for (const item of this.
|
|
2358
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
2345
2359
|
return intersect;
|
|
2346
2360
|
}
|
|
2347
2361
|
unique(key, _strict = false) {
|
|
@@ -2350,20 +2364,21 @@ var Collection = class Collection extends Collection$1 {
|
|
|
2350
2364
|
}
|
|
2351
2365
|
find(key, defaultValue = null) {
|
|
2352
2366
|
if (key instanceof Model) key = key.getKey();
|
|
2353
|
-
if (isArray(key)) {
|
|
2367
|
+
if (Array.isArray(key)) {
|
|
2354
2368
|
if (this.isEmpty()) return new this.constructor();
|
|
2355
2369
|
return this.whereIn(this.first().getKeyName(), key);
|
|
2356
2370
|
}
|
|
2357
|
-
collect(this.
|
|
2371
|
+
collect(this.all()).first((model) => {
|
|
2358
2372
|
return model.getKey() == key;
|
|
2359
2373
|
});
|
|
2360
|
-
return this.
|
|
2374
|
+
return this.all().filter((model) => {
|
|
2361
2375
|
return model.getKey() == key;
|
|
2362
2376
|
})[0] || defaultValue;
|
|
2363
2377
|
}
|
|
2364
2378
|
async fresh(...args) {
|
|
2365
2379
|
if (this.isEmpty()) return new this.constructor();
|
|
2366
2380
|
const model = this.first();
|
|
2381
|
+
if (!model) return new this.constructor();
|
|
2367
2382
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
2368
2383
|
return this.filter((model) => {
|
|
2369
2384
|
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
@@ -2386,15 +2401,15 @@ var Collection = class Collection extends Collection$1 {
|
|
|
2386
2401
|
item.append(attributes);
|
|
2387
2402
|
});
|
|
2388
2403
|
}
|
|
2389
|
-
only(keys) {
|
|
2390
|
-
|
|
2391
|
-
const dictionary =
|
|
2404
|
+
only(...keys) {
|
|
2405
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
2406
|
+
const dictionary = Arr.select(this.getDictionary(), values);
|
|
2392
2407
|
return new this.constructor(Object.values(dictionary));
|
|
2393
2408
|
}
|
|
2394
2409
|
getDictionary(items) {
|
|
2395
|
-
|
|
2410
|
+
const values = !items ? this.all() : items instanceof Collection$1 ? items.all() : items;
|
|
2396
2411
|
const dictionary = {};
|
|
2397
|
-
|
|
2412
|
+
values.map((value) => {
|
|
2398
2413
|
dictionary[value.getKey()] = value;
|
|
2399
2414
|
});
|
|
2400
2415
|
return dictionary;
|
|
@@ -2418,8 +2433,8 @@ var Collection = class Collection extends Collection$1 {
|
|
|
2418
2433
|
return JSON.stringify(this.toData(), ...args);
|
|
2419
2434
|
}
|
|
2420
2435
|
[Symbol.iterator] = () => {
|
|
2421
|
-
const items = this.
|
|
2422
|
-
const length =
|
|
2436
|
+
const items = this.all();
|
|
2437
|
+
const length = items.length;
|
|
2423
2438
|
let n = 0;
|
|
2424
2439
|
return { next() {
|
|
2425
2440
|
return n < length ? {
|
|
@@ -2466,12 +2481,12 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2466
2481
|
let records;
|
|
2467
2482
|
const results = await this.getCurrentlyAttachedPivots();
|
|
2468
2483
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
2469
|
-
const detach = diff(
|
|
2484
|
+
const detach = collect(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
2470
2485
|
if (detaching && detach.length > 0) {
|
|
2471
2486
|
await this.detach(detach);
|
|
2472
2487
|
changes.detached = this.castKeys(detach);
|
|
2473
2488
|
}
|
|
2474
|
-
changes =
|
|
2489
|
+
changes = Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
2475
2490
|
return changes;
|
|
2476
2491
|
}
|
|
2477
2492
|
syncWithoutDetaching(ids) {
|
|
@@ -2479,11 +2494,11 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2479
2494
|
}
|
|
2480
2495
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
2481
2496
|
return this.sync(collect(this.parseIds(ids)).mapWithKeys((id) => {
|
|
2482
|
-
return [id, values];
|
|
2497
|
+
return [String(id), values];
|
|
2483
2498
|
}), detaching);
|
|
2484
2499
|
}
|
|
2485
2500
|
withPivot(columns) {
|
|
2486
|
-
this.pivotColumns = this.pivotColumns.concat(isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
2501
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
2487
2502
|
return this;
|
|
2488
2503
|
}
|
|
2489
2504
|
async attachNew(records, current, touch = true) {
|
|
@@ -2520,8 +2535,8 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2520
2535
|
}
|
|
2521
2536
|
formatRecordsList(records) {
|
|
2522
2537
|
return collect(records).mapWithKeys((attributes, id) => {
|
|
2523
|
-
if (!isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
2524
|
-
return [id, attributes];
|
|
2538
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
2539
|
+
return [String(id), attributes];
|
|
2525
2540
|
}).all();
|
|
2526
2541
|
}
|
|
2527
2542
|
async getCurrentlyAttachedPivots() {
|
|
@@ -2591,7 +2606,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2591
2606
|
}
|
|
2592
2607
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
2593
2608
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
2594
|
-
return
|
|
2609
|
+
return Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
2595
2610
|
}
|
|
2596
2611
|
baseAttachRecord(id, timed) {
|
|
2597
2612
|
let record = {};
|
|
@@ -2604,7 +2619,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2604
2619
|
return record;
|
|
2605
2620
|
}
|
|
2606
2621
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
2607
|
-
return isArray(value) ? [key, {
|
|
2622
|
+
return Array.isArray(value) ? [key, {
|
|
2608
2623
|
...value,
|
|
2609
2624
|
...newAttributes
|
|
2610
2625
|
}] : [value, newAttributes];
|
|
@@ -2615,7 +2630,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
2615
2630
|
parseIds(value) {
|
|
2616
2631
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
2617
2632
|
if (value instanceof Collection) return value.pluck(this.relatedKey).all();
|
|
2618
|
-
return isArray(value) ? value : [value];
|
|
2633
|
+
return Array.isArray(value) ? value : [value];
|
|
2619
2634
|
}
|
|
2620
2635
|
};
|
|
2621
2636
|
};
|
|
@@ -2675,9 +2690,8 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
2675
2690
|
return this;
|
|
2676
2691
|
}
|
|
2677
2692
|
async get(columns) {
|
|
2678
|
-
var _builder$query;
|
|
2679
2693
|
const builder = this.query.applyScopes();
|
|
2680
|
-
columns =
|
|
2694
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
2681
2695
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
2682
2696
|
this.hydratePivotRelation(models);
|
|
2683
2697
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
@@ -2726,7 +2740,7 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
2726
2740
|
const value = model.attributes[key];
|
|
2727
2741
|
if (key.startsWith("pivot_")) {
|
|
2728
2742
|
values[key.substring(6)] = value;
|
|
2729
|
-
model.attributes =
|
|
2743
|
+
model.attributes = Arr.except(model.attributes, [key]);
|
|
2730
2744
|
}
|
|
2731
2745
|
}
|
|
2732
2746
|
return values;
|
|
@@ -2737,7 +2751,7 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
2737
2751
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
2738
2752
|
}
|
|
2739
2753
|
shouldSelect(columns = ["*"]) {
|
|
2740
|
-
if (
|
|
2754
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
2741
2755
|
return columns.concat(this.aliasedPivotColumns());
|
|
2742
2756
|
}
|
|
2743
2757
|
aliasedPivotColumns() {
|
|
@@ -2861,9 +2875,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
2861
2875
|
}
|
|
2862
2876
|
asProxy() {
|
|
2863
2877
|
return new Proxy(this, { get(target, prop) {
|
|
2864
|
-
var _target$query$connect;
|
|
2865
2878
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2866
|
-
const skipReturning = !!
|
|
2879
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2867
2880
|
if ([
|
|
2868
2881
|
"select",
|
|
2869
2882
|
"from",
|
|
@@ -2957,7 +2970,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
2957
2970
|
};
|
|
2958
2971
|
}
|
|
2959
2972
|
if (prop.startsWith("where")) {
|
|
2960
|
-
const column = snake(prop.substring(5));
|
|
2973
|
+
const column = Str.snake(prop.substring(5));
|
|
2961
2974
|
return (...args) => {
|
|
2962
2975
|
target.query.where(column, ...args);
|
|
2963
2976
|
return target.asProxy();
|
|
@@ -3032,7 +3045,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3032
3045
|
}
|
|
3033
3046
|
addUpdatedAtColumn(values) {
|
|
3034
3047
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
3035
|
-
|
|
3048
|
+
const column = this.model.getUpdatedAtColumn();
|
|
3049
|
+
values = Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
3036
3050
|
return values;
|
|
3037
3051
|
}
|
|
3038
3052
|
delete() {
|
|
@@ -3060,9 +3074,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3060
3074
|
return this.model;
|
|
3061
3075
|
}
|
|
3062
3076
|
setModel(model) {
|
|
3063
|
-
var _this$query;
|
|
3064
3077
|
this.model = model;
|
|
3065
|
-
if (typeof
|
|
3078
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
3066
3079
|
else this.query = this.query.table(this.model.getTable());
|
|
3067
3080
|
return this;
|
|
3068
3081
|
}
|
|
@@ -3105,7 +3118,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3105
3118
|
}
|
|
3106
3119
|
withoutGlobalScope(scope) {
|
|
3107
3120
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
3108
|
-
this.globalScopes =
|
|
3121
|
+
this.globalScopes = Arr.except(this.globalScopes, [scope]);
|
|
3109
3122
|
return this;
|
|
3110
3123
|
}
|
|
3111
3124
|
macro(name, callback) {
|
|
@@ -3122,7 +3135,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3122
3135
|
let eagerLoads = {};
|
|
3123
3136
|
if (typeof args[1] === "function") {
|
|
3124
3137
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
3125
|
-
this.eagerLoad =
|
|
3138
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
3126
3139
|
return this;
|
|
3127
3140
|
}
|
|
3128
3141
|
const relations = flattenDeep(args);
|
|
@@ -3131,13 +3144,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3131
3144
|
let eagerLoad;
|
|
3132
3145
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
3133
3146
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
3134
|
-
eagerLoads =
|
|
3147
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
3135
3148
|
}
|
|
3136
|
-
this.eagerLoad =
|
|
3149
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
3137
3150
|
return this;
|
|
3138
3151
|
}
|
|
3139
3152
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
3140
|
-
if (
|
|
3153
|
+
if (typeof relation === "string") {
|
|
3141
3154
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
3142
3155
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
3143
3156
|
}
|
|
@@ -3214,7 +3227,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3214
3227
|
let eagerLoad;
|
|
3215
3228
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
3216
3229
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
3217
|
-
eagerLoads =
|
|
3230
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
3218
3231
|
}
|
|
3219
3232
|
relations = eagerLoads;
|
|
3220
3233
|
const db = this.model.getConnection();
|
|
@@ -3233,7 +3246,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3233
3246
|
} else expression = column;
|
|
3234
3247
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
3235
3248
|
constraints(query);
|
|
3236
|
-
alias = alias || snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
3249
|
+
alias = alias || Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
3237
3250
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
3238
3251
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
3239
3252
|
}
|
|
@@ -3257,7 +3270,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3257
3270
|
}
|
|
3258
3271
|
parseSub(query) {
|
|
3259
3272
|
if (query instanceof Builder || query instanceof Relation$1) return [query.toSql().sql, query.toSql().bindings];
|
|
3260
|
-
else if (
|
|
3273
|
+
else if (typeof query === "string") return [query, []];
|
|
3261
3274
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
3262
3275
|
}
|
|
3263
3276
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -3314,15 +3327,15 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3314
3327
|
if (prefix !== "") prefix += ".";
|
|
3315
3328
|
for (const key in relations) {
|
|
3316
3329
|
const value = relations[key];
|
|
3317
|
-
if (
|
|
3330
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
3318
3331
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
3319
3332
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
3320
|
-
relations =
|
|
3333
|
+
relations = Arr.except(relations, [key]);
|
|
3321
3334
|
}
|
|
3322
3335
|
for (const key in relations) {
|
|
3323
3336
|
const value = relations[key];
|
|
3324
3337
|
let attribute = key, attributeSelectConstraint = value;
|
|
3325
|
-
if (
|
|
3338
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
3326
3339
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
3327
3340
|
}
|
|
3328
3341
|
return preparedRelationships;
|
|
@@ -3370,8 +3383,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3370
3383
|
}
|
|
3371
3384
|
async findOrFail(...args) {
|
|
3372
3385
|
const data = await this.find(...args);
|
|
3373
|
-
if (isArray(args[0])) {
|
|
3374
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name,
|
|
3386
|
+
if (Array.isArray(args[0])) {
|
|
3387
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, collect(args[0]).diff(data.modelKeys()).all());
|
|
3375
3388
|
return data;
|
|
3376
3389
|
}
|
|
3377
3390
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -3385,12 +3398,12 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3385
3398
|
async firstOrNew(attributes = {}, values = {}) {
|
|
3386
3399
|
const instance = await this.where(attributes).first();
|
|
3387
3400
|
if (instance !== null) return instance;
|
|
3388
|
-
return this.newModelInstance(
|
|
3401
|
+
return this.newModelInstance(Obj.deepMerge(attributes, values));
|
|
3389
3402
|
}
|
|
3390
3403
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
3391
3404
|
const instance = await this.where(attributes).first();
|
|
3392
3405
|
if (instance !== null) return instance;
|
|
3393
|
-
return tap(this.newModelInstance(
|
|
3406
|
+
return tap(this.newModelInstance(Obj.deepMerge(attributes, values)), async (instance) => {
|
|
3394
3407
|
await instance.save({ client: this.query });
|
|
3395
3408
|
});
|
|
3396
3409
|
}
|
|
@@ -3410,14 +3423,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3410
3423
|
return this;
|
|
3411
3424
|
}
|
|
3412
3425
|
async find(id, columns) {
|
|
3413
|
-
if (isArray(id) || id instanceof Collection) return await this.findMany(id, columns);
|
|
3426
|
+
if (Array.isArray(id) || id instanceof Collection) return await this.findMany(id, columns);
|
|
3414
3427
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
3415
3428
|
}
|
|
3416
3429
|
async findMany(ids, columns = ["*"]) {
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
3430
|
+
const values = ids instanceof Collection ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
3431
|
+
if (values.length === 0) return new Collection([]);
|
|
3432
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
3421
3433
|
}
|
|
3422
3434
|
async pluck(column) {
|
|
3423
3435
|
return new Collection(await this.query.pluck(column));
|
|
@@ -3425,7 +3437,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3425
3437
|
async destroy(ids) {
|
|
3426
3438
|
if (ids instanceof Collection) ids = ids.modelKeys();
|
|
3427
3439
|
if (ids instanceof Collection$1) ids = ids.all();
|
|
3428
|
-
ids = isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
3440
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
3429
3441
|
if (ids.length === 0) return 0;
|
|
3430
3442
|
const key = this.model.newInstance().getKeyName();
|
|
3431
3443
|
let count = 0;
|
|
@@ -3443,12 +3455,11 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3443
3455
|
return await this.model.newModelQuery().get(columns);
|
|
3444
3456
|
}
|
|
3445
3457
|
async paginate(page = 1, perPage = 10) {
|
|
3446
|
-
var _this;
|
|
3447
3458
|
page = page || 1;
|
|
3448
|
-
perPage = perPage ||
|
|
3459
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
3449
3460
|
this.applyScopes();
|
|
3450
3461
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
3451
|
-
let results
|
|
3462
|
+
let results;
|
|
3452
3463
|
if (total > 0) {
|
|
3453
3464
|
const skip = (page - 1) * (perPage ?? 10);
|
|
3454
3465
|
this.take(perPage).skip(skip);
|
|
@@ -3458,7 +3469,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
3458
3469
|
return new Paginator(results, parseInt(total), perPage, page);
|
|
3459
3470
|
}
|
|
3460
3471
|
async getModels(...columns) {
|
|
3461
|
-
columns =
|
|
3472
|
+
columns = flatten(columns);
|
|
3462
3473
|
if (columns.length > 0) {
|
|
3463
3474
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
3464
3475
|
}
|
|
@@ -3666,8 +3677,7 @@ var CockroachDB = class {
|
|
|
3666
3677
|
*/
|
|
3667
3678
|
async hasTable(table) {
|
|
3668
3679
|
const subquery = this.knex.select().from("information_schema.tables").whereIn("table_schema", this.explodedSchema).andWhere({ table_name: table });
|
|
3669
|
-
|
|
3670
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
3680
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
3671
3681
|
}
|
|
3672
3682
|
/**
|
|
3673
3683
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -3767,18 +3777,17 @@ var CockroachDB = class {
|
|
|
3767
3777
|
${column ? "AND att.attname = ?" : ""}
|
|
3768
3778
|
`, bindings)]);
|
|
3769
3779
|
const parsedColumms = columns.rows.map((col) => {
|
|
3770
|
-
var _col$default_value;
|
|
3771
3780
|
const constraintsForColumn = constraints.rows.filter((constraint) => constraint.table === col.table && constraint.column === col.name);
|
|
3772
3781
|
const foreignKeyConstraint = constraintsForColumn.find((constraint) => constraint.type === "f");
|
|
3773
3782
|
return {
|
|
3774
3783
|
...col,
|
|
3775
3784
|
is_unique: constraintsForColumn.some((constraint) => ["u", "p"].includes(constraint.type)),
|
|
3776
3785
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
3777
|
-
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (
|
|
3786
|
+
has_auto_increment: ["integer", "bigint"].includes(col.data_type) && (col.default_value?.startsWith("nextval(") ?? false),
|
|
3778
3787
|
default_value: parseDefaultValue$5(col.default_value),
|
|
3779
|
-
foreign_key_schema:
|
|
3780
|
-
foreign_key_table:
|
|
3781
|
-
foreign_key_column:
|
|
3788
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
3789
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
3790
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
3782
3791
|
};
|
|
3783
3792
|
});
|
|
3784
3793
|
if (table && column) return parsedColumms[0];
|
|
@@ -3792,8 +3801,7 @@ var CockroachDB = class {
|
|
|
3792
3801
|
table_name: table,
|
|
3793
3802
|
column_name: column
|
|
3794
3803
|
});
|
|
3795
|
-
|
|
3796
|
-
return (record === null || record === void 0 ? void 0 : record.exists) || false;
|
|
3804
|
+
return (await this.knex.select(this.knex.raw("exists (?)", [subquery])).first())?.exists || false;
|
|
3797
3805
|
}
|
|
3798
3806
|
/**
|
|
3799
3807
|
* Get the primary key column for the given table
|
|
@@ -4105,14 +4113,13 @@ var MSSQL = class {
|
|
|
4105
4113
|
WHERE
|
|
4106
4114
|
OBJECT_SCHEMA_NAME (f.parent_object_id) = ?;
|
|
4107
4115
|
`, [this.schema]);
|
|
4108
|
-
if (table) return result
|
|
4116
|
+
if (table) return result?.filter((row) => row.table === table);
|
|
4109
4117
|
return result;
|
|
4110
4118
|
}
|
|
4111
4119
|
};
|
|
4112
4120
|
//#endregion
|
|
4113
4121
|
//#region src/inspector/dialects/mysql.ts
|
|
4114
4122
|
function rawColumnToColumn$1(rawColumn) {
|
|
4115
|
-
var _rawColumn$EXTRA;
|
|
4116
4123
|
let dataType = rawColumn.COLUMN_TYPE.replace(/\(.*?\)/, "");
|
|
4117
4124
|
if (rawColumn.COLUMN_TYPE.startsWith("tinyint(1)")) dataType = "boolean";
|
|
4118
4125
|
return {
|
|
@@ -4125,7 +4132,7 @@ function rawColumnToColumn$1(rawColumn) {
|
|
|
4125
4132
|
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
|
|
4126
4133
|
numeric_precision: rawColumn.NUMERIC_PRECISION,
|
|
4127
4134
|
numeric_scale: rawColumn.NUMERIC_SCALE,
|
|
4128
|
-
is_generated: !!
|
|
4135
|
+
is_generated: !!rawColumn.EXTRA?.endsWith("GENERATED"),
|
|
4129
4136
|
is_nullable: rawColumn.IS_NULLABLE === "YES",
|
|
4130
4137
|
is_unique: rawColumn.COLUMN_KEY === "UNI",
|
|
4131
4138
|
is_primary_key: rawColumn.CONSTRAINT_NAME === "PRIMARY" || rawColumn.COLUMN_KEY === "PRI",
|
|
@@ -4318,11 +4325,10 @@ var oracleDB = class {
|
|
|
4318
4325
|
* Check if a table exists in the current schema/database
|
|
4319
4326
|
*/
|
|
4320
4327
|
async hasTable(table) {
|
|
4321
|
-
|
|
4328
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
4322
4329
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') */
|
|
4323
4330
|
COUNT(*) "count"
|
|
4324
|
-
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first();
|
|
4325
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
4331
|
+
`)).from("USER_TABLES").where({ TABLE_NAME: table }).first())?.count;
|
|
4326
4332
|
}
|
|
4327
4333
|
/**
|
|
4328
4334
|
* Get all the available columns in the current schema/database. Can be filtered to a specific table
|
|
@@ -4398,15 +4404,14 @@ var oracleDB = class {
|
|
|
4398
4404
|
* Check if a table exists in the current schema/database
|
|
4399
4405
|
*/
|
|
4400
4406
|
async hasColumn(table, column) {
|
|
4401
|
-
|
|
4407
|
+
return !!(await this.knex.select(this.knex.raw(`
|
|
4402
4408
|
/*+ OPTIMIZER_FEATURES_ENABLE('${OPTIMIZER_FEATURES}') NO_QUERY_TRANSFORMATION */
|
|
4403
4409
|
COUNT(*) "count"
|
|
4404
4410
|
`)).from("USER_TAB_COLS").where({
|
|
4405
4411
|
TABLE_NAME: table,
|
|
4406
4412
|
COLUMN_NAME: column,
|
|
4407
4413
|
HIDDEN_COLUMN: "NO"
|
|
4408
|
-
}).first();
|
|
4409
|
-
return !!(result === null || result === void 0 ? void 0 : result.count);
|
|
4414
|
+
}).first())?.count;
|
|
4410
4415
|
}
|
|
4411
4416
|
/**
|
|
4412
4417
|
* Get the primary key column for the given table
|
|
@@ -4572,13 +4577,12 @@ var Postgres = class {
|
|
|
4572
4577
|
`, bindings)).rows;
|
|
4573
4578
|
}
|
|
4574
4579
|
async columnInfo(table, column) {
|
|
4575
|
-
var _versionResponse$rows;
|
|
4576
4580
|
const { knex } = this;
|
|
4577
4581
|
const bindings = [];
|
|
4578
4582
|
if (table) bindings.push(table);
|
|
4579
4583
|
if (column) bindings.push(column);
|
|
4580
4584
|
const schemaIn = this.explodedSchema.map((schemaName) => `${this.knex.raw("?", [schemaName])}::regnamespace`);
|
|
4581
|
-
const majorVersion = (
|
|
4585
|
+
const majorVersion = (await this.knex.raw("SHOW server_version")).rows?.[0]?.server_version?.split(".")?.[0] ?? 10;
|
|
4582
4586
|
let generationSelect = `
|
|
4583
4587
|
NULL AS generation_expression,
|
|
4584
4588
|
pg_get_expr(ad.adbin, ad.adrelid) AS default_value,
|
|
@@ -4670,9 +4674,9 @@ var Postgres = class {
|
|
|
4670
4674
|
is_primary_key: constraintsForColumn.some((constraint) => constraint.type === "p"),
|
|
4671
4675
|
has_auto_increment: constraintsForColumn.some((constraint) => constraint.has_auto_increment),
|
|
4672
4676
|
default_value: parseDefaultValue$1(col.default_value),
|
|
4673
|
-
foreign_key_schema:
|
|
4674
|
-
foreign_key_table:
|
|
4675
|
-
foreign_key_column:
|
|
4677
|
+
foreign_key_schema: foreignKeyConstraint?.foreign_key_schema ?? null,
|
|
4678
|
+
foreign_key_table: foreignKeyConstraint?.foreign_key_table ?? null,
|
|
4679
|
+
foreign_key_column: foreignKeyConstraint?.foreign_key_column ?? null
|
|
4676
4680
|
};
|
|
4677
4681
|
});
|
|
4678
4682
|
if (table && column) return parsedColumms[0];
|
|
@@ -4891,11 +4895,11 @@ var SQLite = class {
|
|
|
4891
4895
|
is_generated: raw.hidden !== 0,
|
|
4892
4896
|
generation_expression: null,
|
|
4893
4897
|
is_nullable: raw.notnull === 0,
|
|
4894
|
-
is_unique: !!
|
|
4898
|
+
is_unique: !!index?.unique && indexInfo?.length === 1,
|
|
4895
4899
|
is_primary_key: raw.pk === 1,
|
|
4896
4900
|
has_auto_increment: raw.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table),
|
|
4897
|
-
foreign_key_column:
|
|
4898
|
-
foreign_key_table:
|
|
4901
|
+
foreign_key_column: foreignKey?.to || null,
|
|
4902
|
+
foreign_key_table: foreignKey?.table || null
|
|
4899
4903
|
};
|
|
4900
4904
|
});
|
|
4901
4905
|
};
|
|
@@ -5117,8 +5121,7 @@ var Migrator = class {
|
|
|
5117
5121
|
else await callback(connection);
|
|
5118
5122
|
}
|
|
5119
5123
|
async runMethod(connection, migration, method) {
|
|
5120
|
-
|
|
5121
|
-
await ((_migration$method = migration[method]) === null || _migration$method === void 0 ? void 0 : _migration$method.call(migration, connection.schema, connection));
|
|
5124
|
+
await migration[method]?.(connection.schema, connection);
|
|
5122
5125
|
}
|
|
5123
5126
|
async resolvePath(filePath) {
|
|
5124
5127
|
try {
|
|
@@ -5147,7 +5150,7 @@ var Migrator = class {
|
|
|
5147
5150
|
return path.basename(filePath).replace(".js", "");
|
|
5148
5151
|
}
|
|
5149
5152
|
path(p) {
|
|
5150
|
-
this.paths = Array.from(new Set([...this.paths, p]));
|
|
5153
|
+
this.paths = Array.from(/* @__PURE__ */ new Set([...this.paths, p]));
|
|
5151
5154
|
}
|
|
5152
5155
|
getPaths() {
|
|
5153
5156
|
return this.paths;
|
|
@@ -5335,8 +5338,7 @@ var Migrate = class {
|
|
|
5335
5338
|
* @returns
|
|
5336
5339
|
*/
|
|
5337
5340
|
async setupConnection(config) {
|
|
5338
|
-
|
|
5339
|
-
const table = (config === null || config === void 0 || (_config$migrations = config.migrations) === null || _config$migrations === void 0 ? void 0 : _config$migrations.table) || "migrations";
|
|
5341
|
+
const table = config?.migrations?.table || "migrations";
|
|
5340
5342
|
if (config.skipConnection !== true) {
|
|
5341
5343
|
arquebus.addConnection(config, "default");
|
|
5342
5344
|
Object.entries(config.connections || {}).forEach(([name, connection]) => {
|
|
@@ -5365,7 +5367,7 @@ var pivot_default = Pivot;
|
|
|
5365
5367
|
//#endregion
|
|
5366
5368
|
//#region src/soft-deleting-scope.ts
|
|
5367
5369
|
const hasJoins = (statements) => {
|
|
5368
|
-
for (const statement of statements) if (
|
|
5370
|
+
for (const statement of statements) if (statement?.grouping === "join") return true;
|
|
5369
5371
|
return false;
|
|
5370
5372
|
};
|
|
5371
5373
|
var SoftDeletingScope = class extends Scope {
|
|
@@ -5492,7 +5494,7 @@ const softDeletes = (Model) => {
|
|
|
5492
5494
|
return this.withoutEvents(() => this.restore());
|
|
5493
5495
|
}
|
|
5494
5496
|
trashed() {
|
|
5495
|
-
return
|
|
5497
|
+
return this[this.getDeletedAtColumn()] != null;
|
|
5496
5498
|
}
|
|
5497
5499
|
static softDeleted(callback) {
|
|
5498
5500
|
this.addHook("trashed", callback);
|
|
@@ -5590,7 +5592,7 @@ function Relationship(target, context, descriptor) {
|
|
|
5590
5592
|
const make = (model, data, options = {}) => {
|
|
5591
5593
|
const { paginated } = options;
|
|
5592
5594
|
if (paginated) return new Paginator(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
|
|
5593
|
-
if (isArray(data)) return new Collection(data.map((item) => model.make(item)));
|
|
5595
|
+
if (Array.isArray(data)) return new Collection(data.map((item) => model.make(item)));
|
|
5594
5596
|
return model.make(data);
|
|
5595
5597
|
};
|
|
5596
5598
|
const makeCollection = (model, data) => new Collection(data.map((item) => model.make(item)));
|