@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/concerns/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { Arr, Obj, Str, data_get, data_set } from "@h3ravel/support";
|
|
2
|
+
import * as dayjsModule from "dayjs";
|
|
3
|
+
import * as advancedFormatModule from "dayjs/plugin/advancedFormat.js";
|
|
4
|
+
import { Collection, collect } from "@h3ravel/collect.js";
|
|
5
5
|
import Knex$1 from "knex";
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { existsSync } from "fs";
|
|
8
|
-
import { FileSystem } from "@h3ravel/shared";
|
|
8
|
+
import { FileSystem, importFile } from "@h3ravel/shared";
|
|
9
9
|
import pluralize from "pluralize";
|
|
10
10
|
//#region \0rolldown/runtime.js
|
|
11
11
|
var __defProp = Object.defineProperty;
|
|
@@ -94,25 +94,29 @@ function compose$1(Base, ...mixins) {
|
|
|
94
94
|
}, Base);
|
|
95
95
|
}
|
|
96
96
|
//#endregion
|
|
97
|
-
//#region src/
|
|
97
|
+
//#region src/dayjs.ts
|
|
98
|
+
const dayjs = dayjsModule.default ?? dayjsModule;
|
|
99
|
+
const advancedFormat = advancedFormatModule.default ?? advancedFormatModule;
|
|
98
100
|
dayjs.extend(advancedFormat);
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/utils.ts
|
|
99
103
|
const getRelationName = (relationMethod) => {
|
|
100
|
-
return snake(relationMethod.substring(8));
|
|
104
|
+
return Str.snake(relationMethod.substring(8));
|
|
101
105
|
};
|
|
102
106
|
const getRelationMethod = (relation) => {
|
|
103
|
-
return camel(`relation_${relation}`);
|
|
107
|
+
return Str.camel(`relation_${relation}`);
|
|
104
108
|
};
|
|
105
109
|
const getScopeMethod = (scope) => {
|
|
106
|
-
return camel(`scope_${scope}`);
|
|
110
|
+
return Str.camel(`scope_${scope}`);
|
|
107
111
|
};
|
|
108
112
|
const getAttrMethod = (attr) => {
|
|
109
|
-
return camel(`attribute_${attr}`);
|
|
113
|
+
return Str.camel(`attribute_${attr}`);
|
|
110
114
|
};
|
|
111
115
|
const getGetterMethod = (attr) => {
|
|
112
|
-
return camel(`get_${attr}_attribute`);
|
|
116
|
+
return Str.camel(`get_${attr}_attribute`);
|
|
113
117
|
};
|
|
114
118
|
const getSetterMethod = (attr) => {
|
|
115
|
-
return camel(`set_${attr}_attribute`);
|
|
119
|
+
return Str.camel(`set_${attr}_attribute`);
|
|
116
120
|
};
|
|
117
121
|
/**
|
|
118
122
|
* Tap into a model a collection instance
|
|
@@ -126,8 +130,9 @@ const tap = (instance, callback) => {
|
|
|
126
130
|
return result instanceof Promise ? result.then(() => instance) : instance;
|
|
127
131
|
};
|
|
128
132
|
const { compose } = mixin_exports;
|
|
133
|
+
const flatten = (arr) => arr.flat();
|
|
129
134
|
const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
130
|
-
const snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
135
|
+
const snakeCase = (str) => Str.trim(Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
|
|
131
136
|
//#endregion
|
|
132
137
|
//#region src/casts-attributes.ts
|
|
133
138
|
var CastsAttributes = class CastsAttributes {
|
|
@@ -161,8 +166,7 @@ const HasAttributes$1 = (Model) => {
|
|
|
161
166
|
return this;
|
|
162
167
|
}
|
|
163
168
|
normalizeCastClassResponse(key, value) {
|
|
164
|
-
|
|
165
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
169
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
166
170
|
}
|
|
167
171
|
syncOriginal() {
|
|
168
172
|
this.original = this.getAttributes();
|
|
@@ -272,9 +276,9 @@ const HasAttributes$1 = (Model) => {
|
|
|
272
276
|
return null;
|
|
273
277
|
}
|
|
274
278
|
case "collection": try {
|
|
275
|
-
return collect
|
|
279
|
+
return collect(JSON.parse(value));
|
|
276
280
|
} catch {
|
|
277
|
-
return collect
|
|
281
|
+
return collect([]);
|
|
278
282
|
}
|
|
279
283
|
case "date": return this.asDate(value);
|
|
280
284
|
case "datetime":
|
|
@@ -287,8 +291,8 @@ const HasAttributes$1 = (Model) => {
|
|
|
287
291
|
attributesToData() {
|
|
288
292
|
let attributes = { ...this.attributes };
|
|
289
293
|
for (const key in attributes) {
|
|
290
|
-
if (this.hidden.includes(key)) attributes =
|
|
291
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
294
|
+
if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
|
|
295
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
|
|
292
296
|
}
|
|
293
297
|
for (const key of this.getDates()) {
|
|
294
298
|
if (attributes[key] === void 0) continue;
|
|
@@ -343,7 +347,7 @@ const HasAttributes$1 = (Model) => {
|
|
|
343
347
|
}
|
|
344
348
|
hasCast(key, types = []) {
|
|
345
349
|
if (key in this.casts) {
|
|
346
|
-
types =
|
|
350
|
+
types = flatten(types);
|
|
347
351
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
348
352
|
}
|
|
349
353
|
return false;
|
|
@@ -403,7 +407,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
403
407
|
}
|
|
404
408
|
setModel(model, ids = []) {
|
|
405
409
|
this.model = model;
|
|
406
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
410
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
407
411
|
this.message = `No query results for model [${model}]`;
|
|
408
412
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
409
413
|
else this.message += ".";
|
|
@@ -435,10 +439,14 @@ const HasGlobalScopes$1 = (Model) => {
|
|
|
435
439
|
static globalScopes;
|
|
436
440
|
static addGlobalScope(scope, implementation = null) {
|
|
437
441
|
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
438
|
-
|
|
442
|
+
const scopes = this.globalScopes ?? {};
|
|
443
|
+
data_set(scopes, this.name + "." + scope, implementation);
|
|
444
|
+
this.globalScopes = scopes;
|
|
439
445
|
return implementation;
|
|
440
446
|
} else if (scope instanceof Scope) {
|
|
441
|
-
|
|
447
|
+
const scopes = this.globalScopes ?? {};
|
|
448
|
+
data_set(scopes, this.name + "." + scope.constructor.name, scope);
|
|
449
|
+
this.globalScopes = scopes;
|
|
442
450
|
return scope;
|
|
443
451
|
}
|
|
444
452
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -447,8 +455,8 @@ const HasGlobalScopes$1 = (Model) => {
|
|
|
447
455
|
return this.getGlobalScope(scope) !== null;
|
|
448
456
|
}
|
|
449
457
|
static getGlobalScope(scope) {
|
|
450
|
-
if (typeof scope === "string") return
|
|
451
|
-
return
|
|
458
|
+
if (typeof scope === "string") return data_get(this.globalScopes ?? {}, this.name + "." + scope);
|
|
459
|
+
return data_get(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
452
460
|
}
|
|
453
461
|
static getAllGlobalScopes() {
|
|
454
462
|
return this.globalScopes;
|
|
@@ -457,7 +465,7 @@ const HasGlobalScopes$1 = (Model) => {
|
|
|
457
465
|
this.globalScopes = scopes;
|
|
458
466
|
}
|
|
459
467
|
getGlobalScopes() {
|
|
460
|
-
return
|
|
468
|
+
return data_get(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
461
469
|
}
|
|
462
470
|
};
|
|
463
471
|
};
|
|
@@ -541,7 +549,7 @@ const HasHooks$1 = (Model) => {
|
|
|
541
549
|
};
|
|
542
550
|
//#endregion
|
|
543
551
|
//#region src/collection.ts
|
|
544
|
-
var Collection$1 = class
|
|
552
|
+
var Collection$1 = class extends Collection {
|
|
545
553
|
newConstructor(...args) {
|
|
546
554
|
return new (this.getConstructor())(...args);
|
|
547
555
|
}
|
|
@@ -550,17 +558,18 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
550
558
|
}
|
|
551
559
|
async load(...relations) {
|
|
552
560
|
if (this.isNotEmpty()) {
|
|
553
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
561
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
554
562
|
return this.newConstructor(items);
|
|
555
563
|
}
|
|
556
564
|
return this;
|
|
557
565
|
}
|
|
558
566
|
async loadAggregate(relations, column, action = null) {
|
|
559
567
|
if (this.isEmpty()) return this;
|
|
560
|
-
const
|
|
561
|
-
const
|
|
568
|
+
const first = this.first();
|
|
569
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
570
|
+
const attributes = collect(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
562
571
|
this.each((model) => {
|
|
563
|
-
const extraAttributes =
|
|
572
|
+
const extraAttributes = Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
564
573
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
565
574
|
});
|
|
566
575
|
return this;
|
|
@@ -598,20 +607,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
598
607
|
diff(items) {
|
|
599
608
|
const diff = new this.constructor();
|
|
600
609
|
const dictionary = this.getDictionary(items);
|
|
601
|
-
this.
|
|
610
|
+
this.all().map((item) => {
|
|
602
611
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
603
612
|
});
|
|
604
613
|
return diff;
|
|
605
614
|
}
|
|
606
|
-
except(keys) {
|
|
607
|
-
const
|
|
615
|
+
except(...keys) {
|
|
616
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
617
|
+
const dictionary = Arr.except(this.getDictionary(), values);
|
|
608
618
|
return new this.constructor(Object.values(dictionary));
|
|
609
619
|
}
|
|
610
620
|
intersect(items) {
|
|
611
621
|
const intersect = new this.constructor();
|
|
612
|
-
if (isEmpty(items)) return intersect;
|
|
622
|
+
if (Arr.isEmpty(items)) return intersect;
|
|
613
623
|
const dictionary = this.getDictionary(items);
|
|
614
|
-
for (const item of this.
|
|
624
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
615
625
|
return intersect;
|
|
616
626
|
}
|
|
617
627
|
unique(key, _strict = false) {
|
|
@@ -620,20 +630,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
620
630
|
}
|
|
621
631
|
find(key, defaultValue = null) {
|
|
622
632
|
if (key instanceof Model) key = key.getKey();
|
|
623
|
-
if (isArray(key)) {
|
|
633
|
+
if (Array.isArray(key)) {
|
|
624
634
|
if (this.isEmpty()) return new this.constructor();
|
|
625
635
|
return this.whereIn(this.first().getKeyName(), key);
|
|
626
636
|
}
|
|
627
|
-
collect(this.
|
|
637
|
+
collect(this.all()).first((model) => {
|
|
628
638
|
return model.getKey() == key;
|
|
629
639
|
});
|
|
630
|
-
return this.
|
|
640
|
+
return this.all().filter((model) => {
|
|
631
641
|
return model.getKey() == key;
|
|
632
642
|
})[0] || defaultValue;
|
|
633
643
|
}
|
|
634
644
|
async fresh(...args) {
|
|
635
645
|
if (this.isEmpty()) return new this.constructor();
|
|
636
646
|
const model = this.first();
|
|
647
|
+
if (!model) return new this.constructor();
|
|
637
648
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
638
649
|
return this.filter((model) => {
|
|
639
650
|
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
@@ -656,15 +667,15 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
656
667
|
item.append(attributes);
|
|
657
668
|
});
|
|
658
669
|
}
|
|
659
|
-
only(keys) {
|
|
660
|
-
|
|
661
|
-
const dictionary =
|
|
670
|
+
only(...keys) {
|
|
671
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
672
|
+
const dictionary = Arr.select(this.getDictionary(), values);
|
|
662
673
|
return new this.constructor(Object.values(dictionary));
|
|
663
674
|
}
|
|
664
675
|
getDictionary(items) {
|
|
665
|
-
|
|
676
|
+
const values = !items ? this.all() : items instanceof Collection ? items.all() : items;
|
|
666
677
|
const dictionary = {};
|
|
667
|
-
|
|
678
|
+
values.map((value) => {
|
|
668
679
|
dictionary[value.getKey()] = value;
|
|
669
680
|
});
|
|
670
681
|
return dictionary;
|
|
@@ -688,8 +699,8 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
688
699
|
return JSON.stringify(this.toData(), ...args);
|
|
689
700
|
}
|
|
690
701
|
[Symbol.iterator] = () => {
|
|
691
|
-
const items = this.
|
|
692
|
-
const length =
|
|
702
|
+
const items = this.all();
|
|
703
|
+
const length = items.length;
|
|
693
704
|
let n = 0;
|
|
694
705
|
return { next() {
|
|
695
706
|
return n < length ? {
|
|
@@ -736,12 +747,12 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
736
747
|
let records;
|
|
737
748
|
const results = await this.getCurrentlyAttachedPivots();
|
|
738
749
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
739
|
-
const detach = diff(
|
|
750
|
+
const detach = collect(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
740
751
|
if (detaching && detach.length > 0) {
|
|
741
752
|
await this.detach(detach);
|
|
742
753
|
changes.detached = this.castKeys(detach);
|
|
743
754
|
}
|
|
744
|
-
changes =
|
|
755
|
+
changes = Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
745
756
|
return changes;
|
|
746
757
|
}
|
|
747
758
|
syncWithoutDetaching(ids) {
|
|
@@ -749,11 +760,11 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
749
760
|
}
|
|
750
761
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
751
762
|
return this.sync(collect(this.parseIds(ids)).mapWithKeys((id) => {
|
|
752
|
-
return [id, values];
|
|
763
|
+
return [String(id), values];
|
|
753
764
|
}), detaching);
|
|
754
765
|
}
|
|
755
766
|
withPivot(columns) {
|
|
756
|
-
this.pivotColumns = this.pivotColumns.concat(isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
767
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
757
768
|
return this;
|
|
758
769
|
}
|
|
759
770
|
async attachNew(records, current, touch = true) {
|
|
@@ -790,8 +801,8 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
790
801
|
}
|
|
791
802
|
formatRecordsList(records) {
|
|
792
803
|
return collect(records).mapWithKeys((attributes, id) => {
|
|
793
|
-
if (!isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
794
|
-
return [id, attributes];
|
|
804
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
805
|
+
return [String(id), attributes];
|
|
795
806
|
}).all();
|
|
796
807
|
}
|
|
797
808
|
async getCurrentlyAttachedPivots() {
|
|
@@ -861,7 +872,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
861
872
|
}
|
|
862
873
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
863
874
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
864
|
-
return
|
|
875
|
+
return Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
865
876
|
}
|
|
866
877
|
baseAttachRecord(id, timed) {
|
|
867
878
|
let record = {};
|
|
@@ -874,7 +885,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
874
885
|
return record;
|
|
875
886
|
}
|
|
876
887
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
877
|
-
return isArray(value) ? [key, {
|
|
888
|
+
return Array.isArray(value) ? [key, {
|
|
878
889
|
...value,
|
|
879
890
|
...newAttributes
|
|
880
891
|
}] : [value, newAttributes];
|
|
@@ -885,7 +896,7 @@ const InteractsWithPivotTable = (Relation) => {
|
|
|
885
896
|
parseIds(value) {
|
|
886
897
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
887
898
|
if (value instanceof Collection$1) return value.pluck(this.relatedKey).all();
|
|
888
|
-
return isArray(value) ? value : [value];
|
|
899
|
+
return Array.isArray(value) ? value : [value];
|
|
889
900
|
}
|
|
890
901
|
};
|
|
891
902
|
};
|
|
@@ -975,8 +986,7 @@ var Relation = class {
|
|
|
975
986
|
return this.parent.getQualifiedKeyName();
|
|
976
987
|
}
|
|
977
988
|
getExistenceCompareKey() {
|
|
978
|
-
|
|
979
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
989
|
+
return this.getQualifiedForeignKeyName?.();
|
|
980
990
|
}
|
|
981
991
|
};
|
|
982
992
|
//#endregion
|
|
@@ -1035,9 +1045,8 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
1035
1045
|
return this;
|
|
1036
1046
|
}
|
|
1037
1047
|
async get(columns) {
|
|
1038
|
-
var _builder$query;
|
|
1039
1048
|
const builder = this.query.applyScopes();
|
|
1040
|
-
columns =
|
|
1049
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
1041
1050
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
1042
1051
|
this.hydratePivotRelation(models);
|
|
1043
1052
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
@@ -1086,7 +1095,7 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
1086
1095
|
const value = model.attributes[key];
|
|
1087
1096
|
if (key.startsWith("pivot_")) {
|
|
1088
1097
|
values[key.substring(6)] = value;
|
|
1089
|
-
model.attributes =
|
|
1098
|
+
model.attributes = Arr.except(model.attributes, [key]);
|
|
1090
1099
|
}
|
|
1091
1100
|
}
|
|
1092
1101
|
return values;
|
|
@@ -1097,7 +1106,7 @@ var BelongsToMany = class extends compose(Relation, InteractsWithPivotTable) {
|
|
|
1097
1106
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
1098
1107
|
}
|
|
1099
1108
|
shouldSelect(columns = ["*"]) {
|
|
1100
|
-
if (
|
|
1109
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
1101
1110
|
return columns.concat(this.aliasedPivotColumns());
|
|
1102
1111
|
}
|
|
1103
1112
|
aliasedPivotColumns() {
|
|
@@ -1309,9 +1318,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1309
1318
|
}
|
|
1310
1319
|
asProxy() {
|
|
1311
1320
|
return new Proxy(this, { get(target, prop) {
|
|
1312
|
-
var _target$query$connect;
|
|
1313
1321
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
1314
|
-
const skipReturning = !!
|
|
1322
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
1315
1323
|
if ([
|
|
1316
1324
|
"select",
|
|
1317
1325
|
"from",
|
|
@@ -1405,7 +1413,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1405
1413
|
};
|
|
1406
1414
|
}
|
|
1407
1415
|
if (prop.startsWith("where")) {
|
|
1408
|
-
const column = snake(prop.substring(5));
|
|
1416
|
+
const column = Str.snake(prop.substring(5));
|
|
1409
1417
|
return (...args) => {
|
|
1410
1418
|
target.query.where(column, ...args);
|
|
1411
1419
|
return target.asProxy();
|
|
@@ -1480,7 +1488,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1480
1488
|
}
|
|
1481
1489
|
addUpdatedAtColumn(values) {
|
|
1482
1490
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
1483
|
-
|
|
1491
|
+
const column = this.model.getUpdatedAtColumn();
|
|
1492
|
+
values = Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
1484
1493
|
return values;
|
|
1485
1494
|
}
|
|
1486
1495
|
delete() {
|
|
@@ -1508,9 +1517,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1508
1517
|
return this.model;
|
|
1509
1518
|
}
|
|
1510
1519
|
setModel(model) {
|
|
1511
|
-
var _this$query;
|
|
1512
1520
|
this.model = model;
|
|
1513
|
-
if (typeof
|
|
1521
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
1514
1522
|
else this.query = this.query.table(this.model.getTable());
|
|
1515
1523
|
return this;
|
|
1516
1524
|
}
|
|
@@ -1553,7 +1561,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1553
1561
|
}
|
|
1554
1562
|
withoutGlobalScope(scope) {
|
|
1555
1563
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
1556
|
-
this.globalScopes =
|
|
1564
|
+
this.globalScopes = Arr.except(this.globalScopes, [scope]);
|
|
1557
1565
|
return this;
|
|
1558
1566
|
}
|
|
1559
1567
|
macro(name, callback) {
|
|
@@ -1570,7 +1578,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1570
1578
|
let eagerLoads = {};
|
|
1571
1579
|
if (typeof args[1] === "function") {
|
|
1572
1580
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
1573
|
-
this.eagerLoad =
|
|
1581
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
1574
1582
|
return this;
|
|
1575
1583
|
}
|
|
1576
1584
|
const relations = flattenDeep(args);
|
|
@@ -1579,13 +1587,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1579
1587
|
let eagerLoad;
|
|
1580
1588
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
1581
1589
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
1582
|
-
eagerLoads =
|
|
1590
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
1583
1591
|
}
|
|
1584
|
-
this.eagerLoad =
|
|
1592
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
1585
1593
|
return this;
|
|
1586
1594
|
}
|
|
1587
1595
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
1588
|
-
if (
|
|
1596
|
+
if (typeof relation === "string") {
|
|
1589
1597
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
1590
1598
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
1591
1599
|
}
|
|
@@ -1662,7 +1670,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1662
1670
|
let eagerLoad;
|
|
1663
1671
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
1664
1672
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
1665
|
-
eagerLoads =
|
|
1673
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
1666
1674
|
}
|
|
1667
1675
|
relations = eagerLoads;
|
|
1668
1676
|
const db = this.model.getConnection();
|
|
@@ -1681,7 +1689,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1681
1689
|
} else expression = column;
|
|
1682
1690
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
1683
1691
|
constraints(query);
|
|
1684
|
-
alias = alias || snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
1692
|
+
alias = alias || Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
1685
1693
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
1686
1694
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
1687
1695
|
}
|
|
@@ -1705,7 +1713,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1705
1713
|
}
|
|
1706
1714
|
parseSub(query) {
|
|
1707
1715
|
if (query instanceof Builder || query instanceof Relation) return [query.toSql().sql, query.toSql().bindings];
|
|
1708
|
-
else if (
|
|
1716
|
+
else if (typeof query === "string") return [query, []];
|
|
1709
1717
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
1710
1718
|
}
|
|
1711
1719
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -1762,15 +1770,15 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1762
1770
|
if (prefix !== "") prefix += ".";
|
|
1763
1771
|
for (const key in relations) {
|
|
1764
1772
|
const value = relations[key];
|
|
1765
|
-
if (
|
|
1773
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
1766
1774
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
1767
1775
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
1768
|
-
relations =
|
|
1776
|
+
relations = Arr.except(relations, [key]);
|
|
1769
1777
|
}
|
|
1770
1778
|
for (const key in relations) {
|
|
1771
1779
|
const value = relations[key];
|
|
1772
1780
|
let attribute = key, attributeSelectConstraint = value;
|
|
1773
|
-
if (
|
|
1781
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
1774
1782
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
1775
1783
|
}
|
|
1776
1784
|
return preparedRelationships;
|
|
@@ -1818,8 +1826,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1818
1826
|
}
|
|
1819
1827
|
async findOrFail(...args) {
|
|
1820
1828
|
const data = await this.find(...args);
|
|
1821
|
-
if (isArray(args[0])) {
|
|
1822
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name,
|
|
1829
|
+
if (Array.isArray(args[0])) {
|
|
1830
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, collect(args[0]).diff(data.modelKeys()).all());
|
|
1823
1831
|
return data;
|
|
1824
1832
|
}
|
|
1825
1833
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -1833,12 +1841,12 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1833
1841
|
async firstOrNew(attributes = {}, values = {}) {
|
|
1834
1842
|
const instance = await this.where(attributes).first();
|
|
1835
1843
|
if (instance !== null) return instance;
|
|
1836
|
-
return this.newModelInstance(
|
|
1844
|
+
return this.newModelInstance(Obj.deepMerge(attributes, values));
|
|
1837
1845
|
}
|
|
1838
1846
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
1839
1847
|
const instance = await this.where(attributes).first();
|
|
1840
1848
|
if (instance !== null) return instance;
|
|
1841
|
-
return tap(this.newModelInstance(
|
|
1849
|
+
return tap(this.newModelInstance(Obj.deepMerge(attributes, values)), async (instance) => {
|
|
1842
1850
|
await instance.save({ client: this.query });
|
|
1843
1851
|
});
|
|
1844
1852
|
}
|
|
@@ -1858,14 +1866,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1858
1866
|
return this;
|
|
1859
1867
|
}
|
|
1860
1868
|
async find(id, columns) {
|
|
1861
|
-
if (isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
1869
|
+
if (Array.isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
1862
1870
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
1863
1871
|
}
|
|
1864
1872
|
async findMany(ids, columns = ["*"]) {
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
1873
|
+
const values = ids instanceof Collection$1 ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
1874
|
+
if (values.length === 0) return new Collection$1([]);
|
|
1875
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
1869
1876
|
}
|
|
1870
1877
|
async pluck(column) {
|
|
1871
1878
|
return new Collection$1(await this.query.pluck(column));
|
|
@@ -1873,7 +1880,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1873
1880
|
async destroy(ids) {
|
|
1874
1881
|
if (ids instanceof Collection$1) ids = ids.modelKeys();
|
|
1875
1882
|
if (ids instanceof Collection) ids = ids.all();
|
|
1876
|
-
ids = isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
1883
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
1877
1884
|
if (ids.length === 0) return 0;
|
|
1878
1885
|
const key = this.model.newInstance().getKeyName();
|
|
1879
1886
|
let count = 0;
|
|
@@ -1891,12 +1898,11 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1891
1898
|
return await this.model.newModelQuery().get(columns);
|
|
1892
1899
|
}
|
|
1893
1900
|
async paginate(page = 1, perPage = 10) {
|
|
1894
|
-
var _this;
|
|
1895
1901
|
page = page || 1;
|
|
1896
|
-
perPage = perPage ||
|
|
1902
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
1897
1903
|
this.applyScopes();
|
|
1898
1904
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
1899
|
-
let results
|
|
1905
|
+
let results;
|
|
1900
1906
|
if (total > 0) {
|
|
1901
1907
|
const skip = (page - 1) * (perPage ?? 10);
|
|
1902
1908
|
this.take(perPage).skip(skip);
|
|
@@ -1906,7 +1912,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1906
1912
|
return new Paginator(results, parseInt(total), perPage, page);
|
|
1907
1913
|
}
|
|
1908
1914
|
async getModels(...columns) {
|
|
1909
|
-
columns =
|
|
1915
|
+
columns = flatten(columns);
|
|
1910
1916
|
if (columns.length > 0) {
|
|
1911
1917
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
1912
1918
|
}
|
|
@@ -1968,7 +1974,7 @@ const HasOneOrMany = (Relation) => {
|
|
|
1968
1974
|
}
|
|
1969
1975
|
buildDictionary(results) {
|
|
1970
1976
|
const foreign = this.getForeignKeyName();
|
|
1971
|
-
return collect
|
|
1977
|
+
return collect(results).mapToDictionary((result) => [result[foreign], result]).all();
|
|
1972
1978
|
}
|
|
1973
1979
|
async save(model) {
|
|
1974
1980
|
this.setForeignAttributesForCreate(model);
|
|
@@ -2039,9 +2045,7 @@ var HasMany = class extends compose(Relation, HasOneOrMany) {
|
|
|
2039
2045
|
return this.getParentKey() !== null ? await this.query.get() : new Collection$1([]);
|
|
2040
2046
|
}
|
|
2041
2047
|
getForeignKeyName() {
|
|
2042
|
-
|
|
2043
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2044
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2048
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2045
2049
|
}
|
|
2046
2050
|
buildDictionary(results) {
|
|
2047
2051
|
const foreign = this.getForeignKeyName();
|
|
@@ -2094,9 +2098,7 @@ var HasOne = class extends compose(Relation, HasOneOrMany, SupportsDefaultModels
|
|
|
2094
2098
|
return this.matchOneOrMany(models, results, relation, "one");
|
|
2095
2099
|
}
|
|
2096
2100
|
getForeignKeyName() {
|
|
2097
|
-
|
|
2098
|
-
const segments = (_this$foreignKey = this.foreignKey) === null || _this$foreignKey === void 0 ? void 0 : _this$foreignKey.split(".");
|
|
2099
|
-
return segments === null || segments === void 0 ? void 0 : segments.pop();
|
|
2101
|
+
return (this.foreignKey?.split("."))?.pop();
|
|
2100
2102
|
}
|
|
2101
2103
|
async getResults() {
|
|
2102
2104
|
if (this.getParentKey() === null) return this.getDefaultFor(this.parent);
|
|
@@ -2165,7 +2167,7 @@ const HidesAttributes$1 = (Model) => {
|
|
|
2165
2167
|
makeVisible(...keys) {
|
|
2166
2168
|
const visible = flattenDeep(keys);
|
|
2167
2169
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
2168
|
-
this.hidden =
|
|
2170
|
+
this.hidden = collect(this.hidden).diff(visible).all();
|
|
2169
2171
|
return this;
|
|
2170
2172
|
}
|
|
2171
2173
|
makeHidden(key, ...keys) {
|
|
@@ -2253,10 +2255,9 @@ var QueryBuilder = class QueryBuilder extends Inference {
|
|
|
2253
2255
|
asProxy() {
|
|
2254
2256
|
return new Proxy(this, {
|
|
2255
2257
|
get: function(target, prop) {
|
|
2256
|
-
var _target$connector$cli;
|
|
2257
2258
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2258
2259
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2259
|
-
const skipReturning = !!
|
|
2260
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2260
2261
|
if ([
|
|
2261
2262
|
"select",
|
|
2262
2263
|
"from",
|
|
@@ -2459,6 +2460,9 @@ var arquebus = class arquebus {
|
|
|
2459
2460
|
if (this.instance === null) this.instance = new arquebus();
|
|
2460
2461
|
return this.instance;
|
|
2461
2462
|
}
|
|
2463
|
+
static withSchema() {
|
|
2464
|
+
return this;
|
|
2465
|
+
}
|
|
2462
2466
|
/**
|
|
2463
2467
|
* Initialize a new database connection
|
|
2464
2468
|
*
|
|
@@ -2548,12 +2552,12 @@ var arquebus = class arquebus {
|
|
|
2548
2552
|
const tsPath = path.resolve("arquebus.config.ts");
|
|
2549
2553
|
const instance = this.getInstance();
|
|
2550
2554
|
if (existsSync(jsPath)) {
|
|
2551
|
-
config = (await
|
|
2555
|
+
config = (await importFile(jsPath)).default;
|
|
2552
2556
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2553
2557
|
return config;
|
|
2554
2558
|
}
|
|
2555
2559
|
if (existsSync(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
2556
|
-
config = (await
|
|
2560
|
+
config = (await importFile(tsPath)).default;
|
|
2557
2561
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2558
2562
|
return config;
|
|
2559
2563
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2569,7 +2573,7 @@ var arquebus = class arquebus {
|
|
|
2569
2573
|
"cjs"
|
|
2570
2574
|
], dir);
|
|
2571
2575
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
2572
|
-
config = (await
|
|
2576
|
+
config = (await importFile(found)).default;
|
|
2573
2577
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2574
2578
|
return config;
|
|
2575
2579
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2590,7 +2594,7 @@ var arquebus = class arquebus {
|
|
|
2590
2594
|
}
|
|
2591
2595
|
async destroyAll() {
|
|
2592
2596
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
2593
|
-
return connection
|
|
2597
|
+
return connection?.destroy();
|
|
2594
2598
|
}));
|
|
2595
2599
|
}
|
|
2596
2600
|
createModel(name, options = {}) {
|
|
@@ -2599,29 +2603,25 @@ var arquebus = class arquebus {
|
|
|
2599
2603
|
this.models = {
|
|
2600
2604
|
...this.models,
|
|
2601
2605
|
[name]: class extends BaseModel {
|
|
2602
|
-
table =
|
|
2603
|
-
connection =
|
|
2604
|
-
timestamps =
|
|
2605
|
-
primaryKey =
|
|
2606
|
-
keyType =
|
|
2607
|
-
incrementing =
|
|
2608
|
-
with =
|
|
2609
|
-
casts =
|
|
2610
|
-
static CREATED_AT =
|
|
2611
|
-
static UPDATED_AT =
|
|
2612
|
-
static DELETED_AT =
|
|
2606
|
+
table = options?.table ?? null;
|
|
2607
|
+
connection = options?.connection ?? null;
|
|
2608
|
+
timestamps = options?.timestamps ?? true;
|
|
2609
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
2610
|
+
keyType = options?.keyType ?? "int";
|
|
2611
|
+
incrementing = options?.incrementing ?? true;
|
|
2612
|
+
with = options?.with ?? [];
|
|
2613
|
+
casts = options?.casts ?? {};
|
|
2614
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
2615
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
2616
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
2613
2617
|
}
|
|
2614
2618
|
};
|
|
2615
2619
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
2616
2620
|
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
2617
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
2618
|
-
var _options$attributes;
|
|
2619
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
2620
|
-
};
|
|
2621
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
2621
2622
|
}
|
|
2622
2623
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
2623
|
-
|
|
2624
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
2624
|
+
return options.relations?.[relation](this);
|
|
2625
2625
|
};
|
|
2626
2626
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
2627
2627
|
this.models[name].setConnectionResolver(this);
|
|
@@ -2659,6 +2659,9 @@ var Model = class Model extends BaseModel {
|
|
|
2659
2659
|
this.buildRelationships(attributes);
|
|
2660
2660
|
return this.asProxy();
|
|
2661
2661
|
}
|
|
2662
|
+
static define() {
|
|
2663
|
+
return this;
|
|
2664
|
+
}
|
|
2662
2665
|
static query(trx = null) {
|
|
2663
2666
|
return new this().newQuery(trx);
|
|
2664
2667
|
}
|
|
@@ -2842,7 +2845,7 @@ var Model = class Model extends BaseModel {
|
|
|
2842
2845
|
});
|
|
2843
2846
|
}
|
|
2844
2847
|
toData() {
|
|
2845
|
-
return
|
|
2848
|
+
return Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
2846
2849
|
}
|
|
2847
2850
|
toJSON() {
|
|
2848
2851
|
return this.toData();
|
|
@@ -2853,6 +2856,15 @@ var Model = class Model extends BaseModel {
|
|
|
2853
2856
|
toString() {
|
|
2854
2857
|
return this.toJson();
|
|
2855
2858
|
}
|
|
2859
|
+
getAttributes() {
|
|
2860
|
+
return super.getAttributes();
|
|
2861
|
+
}
|
|
2862
|
+
getAttribute(key) {
|
|
2863
|
+
return super.getAttribute(key);
|
|
2864
|
+
}
|
|
2865
|
+
setAttribute(key, value) {
|
|
2866
|
+
return super.setAttribute(key, value);
|
|
2867
|
+
}
|
|
2856
2868
|
fill(attributes) {
|
|
2857
2869
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
2858
2870
|
return this;
|
|
@@ -2893,10 +2905,9 @@ var Model = class Model extends BaseModel {
|
|
|
2893
2905
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
2894
2906
|
const attributes = this.getAttributes();
|
|
2895
2907
|
if (this.getIncrementing()) {
|
|
2896
|
-
var _data$;
|
|
2897
2908
|
const keyName = this.getKeyName();
|
|
2898
2909
|
const data = await query.insert([attributes], [keyName]);
|
|
2899
|
-
this.setAttribute(keyName,
|
|
2910
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
2900
2911
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
2901
2912
|
this.exists = true;
|
|
2902
2913
|
await this.execHooks("created", options);
|
|
@@ -2938,7 +2949,7 @@ var Model = class Model extends BaseModel {
|
|
|
2938
2949
|
if (!this.exists) return Promise.resolve(void 0);
|
|
2939
2950
|
const model = await this.constructor.query().where(this.getKeyName(), this.getKey()).first();
|
|
2940
2951
|
this.attributes = { ...model.attributes };
|
|
2941
|
-
await this.load(collect
|
|
2952
|
+
await this.load(collect(this.relations).reject((relation) => {
|
|
2942
2953
|
return relation instanceof Pivot;
|
|
2943
2954
|
}).keys().all());
|
|
2944
2955
|
this.syncOriginal();
|
|
@@ -3180,10 +3191,10 @@ var HasManyThrough = class extends Relation {
|
|
|
3180
3191
|
}
|
|
3181
3192
|
const model = await this.first(columns);
|
|
3182
3193
|
if (model) return model;
|
|
3183
|
-
return callback
|
|
3194
|
+
return callback?.();
|
|
3184
3195
|
}
|
|
3185
3196
|
async find(id, columns = ["*"]) {
|
|
3186
|
-
if (isArray(id)) return await this.findMany(id, columns);
|
|
3197
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
3187
3198
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
3188
3199
|
}
|
|
3189
3200
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -3211,7 +3222,7 @@ var HasManyThrough = class extends Relation {
|
|
|
3211
3222
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
3212
3223
|
}
|
|
3213
3224
|
shouldSelect(columns = ["*"]) {
|
|
3214
|
-
if (
|
|
3225
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
3215
3226
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
3216
3227
|
}
|
|
3217
3228
|
async chunk(count, callback) {
|
|
@@ -3305,7 +3316,7 @@ const HasRelations$1 = (Model) => {
|
|
|
3305
3316
|
return this;
|
|
3306
3317
|
}
|
|
3307
3318
|
unsetRelation(relation) {
|
|
3308
|
-
this.relations =
|
|
3319
|
+
this.relations = Arr.except(this.relations, [relation]);
|
|
3309
3320
|
return this;
|
|
3310
3321
|
}
|
|
3311
3322
|
relationLoaded(relation) {
|
|
@@ -3328,8 +3339,7 @@ const HasRelations$1 = (Model) => {
|
|
|
3328
3339
|
return data;
|
|
3329
3340
|
}
|
|
3330
3341
|
guessBelongsToRelation() {
|
|
3331
|
-
|
|
3332
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
3342
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
3333
3343
|
return getRelationName(functionName);
|
|
3334
3344
|
}
|
|
3335
3345
|
joiningTable(related, instance = null) {
|