@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/relations/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,11 +130,12 @@ 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/collection.ts
|
|
133
|
-
var Collection$1 = class
|
|
138
|
+
var Collection$1 = class extends Collection {
|
|
134
139
|
newConstructor(...args) {
|
|
135
140
|
return new (this.getConstructor())(...args);
|
|
136
141
|
}
|
|
@@ -139,17 +144,18 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
139
144
|
}
|
|
140
145
|
async load(...relations) {
|
|
141
146
|
if (this.isNotEmpty()) {
|
|
142
|
-
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.
|
|
147
|
+
const items = await this.first().constructor.query().with(...relations).eagerLoadRelations(this.all());
|
|
143
148
|
return this.newConstructor(items);
|
|
144
149
|
}
|
|
145
150
|
return this;
|
|
146
151
|
}
|
|
147
152
|
async loadAggregate(relations, column, action = null) {
|
|
148
153
|
if (this.isEmpty()) return this;
|
|
149
|
-
const
|
|
150
|
-
const
|
|
154
|
+
const first = this.first();
|
|
155
|
+
const models = (await first.newModelQuery().whereIn(first.getKeyName(), this.modelKeys()).select(first.getKeyName()).withAggregate(relations, column, action).get()).keyBy(first.getKeyName());
|
|
156
|
+
const attributes = collect(Object.keys(models.first().getAttributes())).diff([models.first().getKeyName()]).all();
|
|
151
157
|
this.each((model) => {
|
|
152
|
-
const extraAttributes =
|
|
158
|
+
const extraAttributes = Arr.select(models.get(model.getKey()).getAttributes(), attributes);
|
|
153
159
|
model.fill(extraAttributes).syncOriginalAttributes(...attributes);
|
|
154
160
|
});
|
|
155
161
|
return this;
|
|
@@ -187,20 +193,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
187
193
|
diff(items) {
|
|
188
194
|
const diff = new this.constructor();
|
|
189
195
|
const dictionary = this.getDictionary(items);
|
|
190
|
-
this.
|
|
196
|
+
this.all().map((item) => {
|
|
191
197
|
if (dictionary[item.getKey()] === void 0) diff.add(item);
|
|
192
198
|
});
|
|
193
199
|
return diff;
|
|
194
200
|
}
|
|
195
|
-
except(keys) {
|
|
196
|
-
const
|
|
201
|
+
except(...keys) {
|
|
202
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
203
|
+
const dictionary = Arr.except(this.getDictionary(), values);
|
|
197
204
|
return new this.constructor(Object.values(dictionary));
|
|
198
205
|
}
|
|
199
206
|
intersect(items) {
|
|
200
207
|
const intersect = new this.constructor();
|
|
201
|
-
if (isEmpty(items)) return intersect;
|
|
208
|
+
if (Arr.isEmpty(items)) return intersect;
|
|
202
209
|
const dictionary = this.getDictionary(items);
|
|
203
|
-
for (const item of this.
|
|
210
|
+
for (const item of this.all()) if (dictionary[item.getKey()] !== void 0) intersect.add(item);
|
|
204
211
|
return intersect;
|
|
205
212
|
}
|
|
206
213
|
unique(key, _strict = false) {
|
|
@@ -209,20 +216,21 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
209
216
|
}
|
|
210
217
|
find(key, defaultValue = null) {
|
|
211
218
|
if (key instanceof Model) key = key.getKey();
|
|
212
|
-
if (isArray(key)) {
|
|
219
|
+
if (Array.isArray(key)) {
|
|
213
220
|
if (this.isEmpty()) return new this.constructor();
|
|
214
221
|
return this.whereIn(this.first().getKeyName(), key);
|
|
215
222
|
}
|
|
216
|
-
collect(this.
|
|
223
|
+
collect(this.all()).first((model) => {
|
|
217
224
|
return model.getKey() == key;
|
|
218
225
|
});
|
|
219
|
-
return this.
|
|
226
|
+
return this.all().filter((model) => {
|
|
220
227
|
return model.getKey() == key;
|
|
221
228
|
})[0] || defaultValue;
|
|
222
229
|
}
|
|
223
230
|
async fresh(...args) {
|
|
224
231
|
if (this.isEmpty()) return new this.constructor();
|
|
225
232
|
const model = this.first();
|
|
233
|
+
if (!model) return new this.constructor();
|
|
226
234
|
const freshModels = (await model.newQuery().with(...args).whereIn(model.getKeyName(), this.modelKeys()).get()).getDictionary();
|
|
227
235
|
return this.filter((model) => {
|
|
228
236
|
return model.exists && freshModels[model.getKey()] !== void 0;
|
|
@@ -245,15 +253,15 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
245
253
|
item.append(attributes);
|
|
246
254
|
});
|
|
247
255
|
}
|
|
248
|
-
only(keys) {
|
|
249
|
-
|
|
250
|
-
const dictionary =
|
|
256
|
+
only(...keys) {
|
|
257
|
+
const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
|
|
258
|
+
const dictionary = Arr.select(this.getDictionary(), values);
|
|
251
259
|
return new this.constructor(Object.values(dictionary));
|
|
252
260
|
}
|
|
253
261
|
getDictionary(items) {
|
|
254
|
-
|
|
262
|
+
const values = !items ? this.all() : items instanceof Collection ? items.all() : items;
|
|
255
263
|
const dictionary = {};
|
|
256
|
-
|
|
264
|
+
values.map((value) => {
|
|
257
265
|
dictionary[value.getKey()] = value;
|
|
258
266
|
});
|
|
259
267
|
return dictionary;
|
|
@@ -277,8 +285,8 @@ var Collection$1 = class Collection$1 extends Collection {
|
|
|
277
285
|
return JSON.stringify(this.toData(), ...args);
|
|
278
286
|
}
|
|
279
287
|
[Symbol.iterator] = () => {
|
|
280
|
-
const items = this.
|
|
281
|
-
const length =
|
|
288
|
+
const items = this.all();
|
|
289
|
+
const length = items.length;
|
|
282
290
|
let n = 0;
|
|
283
291
|
return { next() {
|
|
284
292
|
return n < length ? {
|
|
@@ -325,12 +333,12 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
325
333
|
let records;
|
|
326
334
|
const results = await this.getCurrentlyAttachedPivots();
|
|
327
335
|
const current = results.length === 0 ? [] : results.map((result) => result.toData()).pluck(this.relatedPivotKey).all().map((i) => String(i));
|
|
328
|
-
const detach = diff(
|
|
336
|
+
const detach = collect(current).diff(Object.keys(records = this.formatRecordsList(this.parseIds(ids)))).all();
|
|
329
337
|
if (detaching && detach.length > 0) {
|
|
330
338
|
await this.detach(detach);
|
|
331
339
|
changes.detached = this.castKeys(detach);
|
|
332
340
|
}
|
|
333
|
-
changes =
|
|
341
|
+
changes = Obj.deepMerge(changes, await this.attachNew(records, current, false));
|
|
334
342
|
return changes;
|
|
335
343
|
}
|
|
336
344
|
syncWithoutDetaching(ids) {
|
|
@@ -338,11 +346,11 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
338
346
|
}
|
|
339
347
|
syncWithPivotValues(ids, values, detaching = true) {
|
|
340
348
|
return this.sync(collect(this.parseIds(ids)).mapWithKeys((id) => {
|
|
341
|
-
return [id, values];
|
|
349
|
+
return [String(id), values];
|
|
342
350
|
}), detaching);
|
|
343
351
|
}
|
|
344
352
|
withPivot(columns) {
|
|
345
|
-
this.pivotColumns = this.pivotColumns.concat(isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
353
|
+
this.pivotColumns = this.pivotColumns.concat(Array.isArray(columns) ? columns : Array.prototype.slice.call(columns));
|
|
346
354
|
return this;
|
|
347
355
|
}
|
|
348
356
|
async attachNew(records, current, touch = true) {
|
|
@@ -379,8 +387,8 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
379
387
|
}
|
|
380
388
|
formatRecordsList(records) {
|
|
381
389
|
return collect(records).mapWithKeys((attributes, id) => {
|
|
382
|
-
if (!isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
383
|
-
return [id, attributes];
|
|
390
|
+
if (!Array.isArray(attributes)) [id, attributes] = [attributes, {}];
|
|
391
|
+
return [String(id), attributes];
|
|
384
392
|
}).all();
|
|
385
393
|
}
|
|
386
394
|
async getCurrentlyAttachedPivots() {
|
|
@@ -450,7 +458,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
450
458
|
}
|
|
451
459
|
formatAttachRecord(key, value, attributes, hasTimestamps) {
|
|
452
460
|
const [id, newAttributes] = this.extractAttachIdAndAttributes(key, value, attributes);
|
|
453
|
-
return
|
|
461
|
+
return Obj.deepMerge(this.baseAttachRecord(id, hasTimestamps), newAttributes);
|
|
454
462
|
}
|
|
455
463
|
baseAttachRecord(id, timed) {
|
|
456
464
|
let record = {};
|
|
@@ -463,7 +471,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
463
471
|
return record;
|
|
464
472
|
}
|
|
465
473
|
extractAttachIdAndAttributes(key, value, newAttributes) {
|
|
466
|
-
return isArray(value) ? [key, {
|
|
474
|
+
return Array.isArray(value) ? [key, {
|
|
467
475
|
...value,
|
|
468
476
|
...newAttributes
|
|
469
477
|
}] : [value, newAttributes];
|
|
@@ -474,7 +482,7 @@ const InteractsWithPivotTable$1 = (Relation) => {
|
|
|
474
482
|
parseIds(value) {
|
|
475
483
|
if (value instanceof Model) return [value[this.relatedKey]];
|
|
476
484
|
if (value instanceof Collection$1) return value.pluck(this.relatedKey).all();
|
|
477
|
-
return isArray(value) ? value : [value];
|
|
485
|
+
return Array.isArray(value) ? value : [value];
|
|
478
486
|
}
|
|
479
487
|
};
|
|
480
488
|
};
|
|
@@ -496,7 +504,7 @@ var ModelNotFoundError = class extends BaseError {
|
|
|
496
504
|
}
|
|
497
505
|
setModel(model, ids = []) {
|
|
498
506
|
this.model = model;
|
|
499
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
507
|
+
this.ids = Array.isArray(ids) ? ids : [ids];
|
|
500
508
|
this.message = `No query results for model [${model}]`;
|
|
501
509
|
if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
|
|
502
510
|
else this.message += ".";
|
|
@@ -597,8 +605,7 @@ var Relation$1 = class {
|
|
|
597
605
|
return this.parent.getQualifiedKeyName();
|
|
598
606
|
}
|
|
599
607
|
getExistenceCompareKey() {
|
|
600
|
-
|
|
601
|
-
return (_this$getQualifiedFor = this.getQualifiedForeignKeyName) === null || _this$getQualifiedFor === void 0 ? void 0 : _this$getQualifiedFor.call(this);
|
|
608
|
+
return this.getQualifiedForeignKeyName?.();
|
|
602
609
|
}
|
|
603
610
|
};
|
|
604
611
|
//#endregion
|
|
@@ -657,9 +664,8 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
657
664
|
return this;
|
|
658
665
|
}
|
|
659
666
|
async get(columns) {
|
|
660
|
-
var _builder$query;
|
|
661
667
|
const builder = this.query.applyScopes();
|
|
662
|
-
columns =
|
|
668
|
+
columns = builder.query?._statements?.find((item) => item.grouping == "columns") ? [] : columns;
|
|
663
669
|
let models = await builder.select(this.shouldSelect(columns)).getModels();
|
|
664
670
|
this.hydratePivotRelation(models);
|
|
665
671
|
if (models.length > 0) models = await builder.eagerLoadRelations(models);
|
|
@@ -708,7 +714,7 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
708
714
|
const value = model.attributes[key];
|
|
709
715
|
if (key.startsWith("pivot_")) {
|
|
710
716
|
values[key.substring(6)] = value;
|
|
711
|
-
model.attributes =
|
|
717
|
+
model.attributes = Arr.except(model.attributes, [key]);
|
|
712
718
|
}
|
|
713
719
|
}
|
|
714
720
|
return values;
|
|
@@ -719,7 +725,7 @@ var BelongsToMany$1 = class extends compose(Relation$1, InteractsWithPivotTable$
|
|
|
719
725
|
return this.withPivot(this.createdAt(), this.updatedAt());
|
|
720
726
|
}
|
|
721
727
|
shouldSelect(columns = ["*"]) {
|
|
722
|
-
if (
|
|
728
|
+
if (columns.length === 1 && columns[0] === "*") columns = [this.related.getTable() + ".*"];
|
|
723
729
|
return columns.concat(this.aliasedPivotColumns());
|
|
724
730
|
}
|
|
725
731
|
aliasedPivotColumns() {
|
|
@@ -941,9 +947,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
941
947
|
}
|
|
942
948
|
asProxy() {
|
|
943
949
|
return new Proxy(this, { get(target, prop) {
|
|
944
|
-
var _target$query$connect;
|
|
945
950
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
946
|
-
const skipReturning = !!
|
|
951
|
+
const skipReturning = !!target.query.connector?.client.config?.client?.includes("mysql") && prop === "returning";
|
|
947
952
|
if ([
|
|
948
953
|
"select",
|
|
949
954
|
"from",
|
|
@@ -1037,7 +1042,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1037
1042
|
};
|
|
1038
1043
|
}
|
|
1039
1044
|
if (prop.startsWith("where")) {
|
|
1040
|
-
const column = snake(prop.substring(5));
|
|
1045
|
+
const column = Str.snake(prop.substring(5));
|
|
1041
1046
|
return (...args) => {
|
|
1042
1047
|
target.query.where(column, ...args);
|
|
1043
1048
|
return target.asProxy();
|
|
@@ -1112,7 +1117,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1112
1117
|
}
|
|
1113
1118
|
addUpdatedAtColumn(values) {
|
|
1114
1119
|
if (!this.model.usesTimestamps() || this.model.getUpdatedAtColumn() === null) return values;
|
|
1115
|
-
|
|
1120
|
+
const column = this.model.getUpdatedAtColumn();
|
|
1121
|
+
values = Obj.deepMerge({ [column]: this.model.freshTimestampString() }, values);
|
|
1116
1122
|
return values;
|
|
1117
1123
|
}
|
|
1118
1124
|
delete() {
|
|
@@ -1140,9 +1146,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1140
1146
|
return this.model;
|
|
1141
1147
|
}
|
|
1142
1148
|
setModel(model) {
|
|
1143
|
-
var _this$query;
|
|
1144
1149
|
this.model = model;
|
|
1145
|
-
if (typeof
|
|
1150
|
+
if (typeof this.query?.client?.table == "function") this.query = this.query.client.table(this.model.getTable());
|
|
1146
1151
|
else this.query = this.query.table(this.model.getTable());
|
|
1147
1152
|
return this;
|
|
1148
1153
|
}
|
|
@@ -1185,7 +1190,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1185
1190
|
}
|
|
1186
1191
|
withoutGlobalScope(scope) {
|
|
1187
1192
|
if (typeof scope !== "string") scope = scope.constructor.name;
|
|
1188
|
-
this.globalScopes =
|
|
1193
|
+
this.globalScopes = Arr.except(this.globalScopes, [scope]);
|
|
1189
1194
|
return this;
|
|
1190
1195
|
}
|
|
1191
1196
|
macro(name, callback) {
|
|
@@ -1202,7 +1207,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1202
1207
|
let eagerLoads = {};
|
|
1203
1208
|
if (typeof args[1] === "function") {
|
|
1204
1209
|
const eagerLoad = this.parseWithRelations({ [args[0]]: args[1] });
|
|
1205
|
-
this.eagerLoad =
|
|
1210
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, eagerLoad);
|
|
1206
1211
|
return this;
|
|
1207
1212
|
}
|
|
1208
1213
|
const relations = flattenDeep(args);
|
|
@@ -1211,13 +1216,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1211
1216
|
let eagerLoad;
|
|
1212
1217
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
1213
1218
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
1214
|
-
eagerLoads =
|
|
1219
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
1215
1220
|
}
|
|
1216
|
-
this.eagerLoad =
|
|
1221
|
+
this.eagerLoad = Obj.deepMerge(this.eagerLoad, this.parseWithRelations(eagerLoads));
|
|
1217
1222
|
return this;
|
|
1218
1223
|
}
|
|
1219
1224
|
has(relation, operator = ">=", count = 1, boolean = "and", callback = null) {
|
|
1220
|
-
if (
|
|
1225
|
+
if (typeof relation === "string") {
|
|
1221
1226
|
if (relation.includes(".")) return this.hasNested(relation, operator, count, boolean, callback);
|
|
1222
1227
|
relation = this.getRelationWithoutConstraints(getRelationMethod(relation));
|
|
1223
1228
|
}
|
|
@@ -1294,7 +1299,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1294
1299
|
let eagerLoad;
|
|
1295
1300
|
if (typeof relation === "string") eagerLoad = { [relation]: (q) => q };
|
|
1296
1301
|
else if (typeof relation === "object") eagerLoad = relation;
|
|
1297
|
-
eagerLoads =
|
|
1302
|
+
eagerLoads = Obj.deepMerge(eagerLoads, eagerLoad);
|
|
1298
1303
|
}
|
|
1299
1304
|
relations = eagerLoads;
|
|
1300
1305
|
const db = this.model.getConnection();
|
|
@@ -1313,7 +1318,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1313
1318
|
} else expression = column;
|
|
1314
1319
|
const query = relation.getRelationExistenceQuery(relation.getRelated().newModelQuery(), this, db.raw(expression));
|
|
1315
1320
|
constraints(query);
|
|
1316
|
-
alias = alias || snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
1321
|
+
alias = alias || Str.snake(`${name} ${action} ${column}`.replace("/[^[:alnum:][:space:]_]/u", ""));
|
|
1317
1322
|
if (action === "exists") this.select(db.raw(`exists(${query.toSql().sql}) as ${alias}`));
|
|
1318
1323
|
else this.selectSub(action ? query : query.limit(1), alias);
|
|
1319
1324
|
}
|
|
@@ -1337,7 +1342,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1337
1342
|
}
|
|
1338
1343
|
parseSub(query) {
|
|
1339
1344
|
if (query instanceof Builder || query instanceof Relation$1) return [query.toSql().sql, query.toSql().bindings];
|
|
1340
|
-
else if (
|
|
1345
|
+
else if (typeof query === "string") return [query, []];
|
|
1341
1346
|
else throw new Error("A subquery must be a query builder instance, a Closure, or a string.");
|
|
1342
1347
|
}
|
|
1343
1348
|
prependDatabaseNameIfCrossDatabaseQuery(query) {
|
|
@@ -1394,15 +1399,15 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1394
1399
|
if (prefix !== "") prefix += ".";
|
|
1395
1400
|
for (const key in relations) {
|
|
1396
1401
|
const value = relations[key];
|
|
1397
|
-
if (
|
|
1402
|
+
if (typeof value === "string" || Number.isFinite(parseInt(value))) continue;
|
|
1398
1403
|
const [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(key, value);
|
|
1399
1404
|
preparedRelationships = Object.assign({}, preparedRelationships, { [`${prefix}${attribute}`]: attributeSelectConstraint }, this.prepareNestedWithRelationships(value, `${prefix}${attribute}`));
|
|
1400
|
-
relations =
|
|
1405
|
+
relations = Arr.except(relations, [key]);
|
|
1401
1406
|
}
|
|
1402
1407
|
for (const key in relations) {
|
|
1403
1408
|
const value = relations[key];
|
|
1404
1409
|
let attribute = key, attributeSelectConstraint = value;
|
|
1405
|
-
if (
|
|
1410
|
+
if (typeof value === "string") [attribute, attributeSelectConstraint] = this.parseNameAndAttributeSelectionConstraint(value);
|
|
1406
1411
|
preparedRelationships[`${prefix}${attribute}`] = this.combineConstraints([attributeSelectConstraint, preparedRelationships[`${prefix}${attribute}`] || (() => {})]);
|
|
1407
1412
|
}
|
|
1408
1413
|
return preparedRelationships;
|
|
@@ -1450,8 +1455,8 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1450
1455
|
}
|
|
1451
1456
|
async findOrFail(...args) {
|
|
1452
1457
|
const data = await this.find(...args);
|
|
1453
|
-
if (isArray(args[0])) {
|
|
1454
|
-
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name,
|
|
1458
|
+
if (Array.isArray(args[0])) {
|
|
1459
|
+
if (data.count() !== args[0].length) throw new ModelNotFoundError().setModel(this.model.constructor.name, collect(args[0]).diff(data.modelKeys()).all());
|
|
1455
1460
|
return data;
|
|
1456
1461
|
}
|
|
1457
1462
|
if (data === null) throw new ModelNotFoundError().setModel(this.model.constructor.name, args[0]);
|
|
@@ -1465,12 +1470,12 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1465
1470
|
async firstOrNew(attributes = {}, values = {}) {
|
|
1466
1471
|
const instance = await this.where(attributes).first();
|
|
1467
1472
|
if (instance !== null) return instance;
|
|
1468
|
-
return this.newModelInstance(
|
|
1473
|
+
return this.newModelInstance(Obj.deepMerge(attributes, values));
|
|
1469
1474
|
}
|
|
1470
1475
|
async firstOrCreate(attributes = {}, values = {}) {
|
|
1471
1476
|
const instance = await this.where(attributes).first();
|
|
1472
1477
|
if (instance !== null) return instance;
|
|
1473
|
-
return tap(this.newModelInstance(
|
|
1478
|
+
return tap(this.newModelInstance(Obj.deepMerge(attributes, values)), async (instance) => {
|
|
1474
1479
|
await instance.save({ client: this.query });
|
|
1475
1480
|
});
|
|
1476
1481
|
}
|
|
@@ -1490,14 +1495,13 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1490
1495
|
return this;
|
|
1491
1496
|
}
|
|
1492
1497
|
async find(id, columns) {
|
|
1493
|
-
if (isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
1498
|
+
if (Array.isArray(id) || id instanceof Collection$1) return await this.findMany(id, columns);
|
|
1494
1499
|
return await this.where(this.model.getKeyName(), id).first(columns);
|
|
1495
1500
|
}
|
|
1496
1501
|
async findMany(ids, columns = ["*"]) {
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
return await this.whereIn(this.model.getKeyName(), ids).get(columns);
|
|
1502
|
+
const values = ids instanceof Collection$1 ? ids.modelKeys() : Array.isArray(ids) ? ids : [ids];
|
|
1503
|
+
if (values.length === 0) return new Collection$1([]);
|
|
1504
|
+
return await this.whereIn(this.model.getKeyName(), values).get(columns);
|
|
1501
1505
|
}
|
|
1502
1506
|
async pluck(column) {
|
|
1503
1507
|
return new Collection$1(await this.query.pluck(column));
|
|
@@ -1505,7 +1509,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1505
1509
|
async destroy(ids) {
|
|
1506
1510
|
if (ids instanceof Collection$1) ids = ids.modelKeys();
|
|
1507
1511
|
if (ids instanceof Collection) ids = ids.all();
|
|
1508
|
-
ids = isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
1512
|
+
ids = Array.isArray(ids) ? ids : Array.prototype.slice.call(ids);
|
|
1509
1513
|
if (ids.length === 0) return 0;
|
|
1510
1514
|
const key = this.model.newInstance().getKeyName();
|
|
1511
1515
|
let count = 0;
|
|
@@ -1523,12 +1527,11 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1523
1527
|
return await this.model.newModelQuery().get(columns);
|
|
1524
1528
|
}
|
|
1525
1529
|
async paginate(page = 1, perPage = 10) {
|
|
1526
|
-
var _this;
|
|
1527
1530
|
page = page || 1;
|
|
1528
|
-
perPage = perPage ||
|
|
1531
|
+
perPage = perPage || this?.model?.perPage || 15;
|
|
1529
1532
|
this.applyScopes();
|
|
1530
1533
|
const total = await this.query.clone().clearOrder().clearSelect().count(this.primaryKey);
|
|
1531
|
-
let results
|
|
1534
|
+
let results;
|
|
1532
1535
|
if (total > 0) {
|
|
1533
1536
|
const skip = (page - 1) * (perPage ?? 10);
|
|
1534
1537
|
this.take(perPage).skip(skip);
|
|
@@ -1538,7 +1541,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
1538
1541
|
return new Paginator(results, parseInt(total), perPage, page);
|
|
1539
1542
|
}
|
|
1540
1543
|
async getModels(...columns) {
|
|
1541
|
-
columns =
|
|
1544
|
+
columns = flatten(columns);
|
|
1542
1545
|
if (columns.length > 0) {
|
|
1543
1546
|
if (this.query._statements.filter((item) => item.grouping == "columns").length == 0 && columns[0] !== "*") this.query.select(...columns);
|
|
1544
1547
|
}
|
|
@@ -1615,8 +1618,7 @@ const HasAttributes = (Model) => {
|
|
|
1615
1618
|
return this;
|
|
1616
1619
|
}
|
|
1617
1620
|
normalizeCastClassResponse(key, value) {
|
|
1618
|
-
|
|
1619
|
-
return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
|
|
1621
|
+
return value?.constructor?.name === "Object" ? value : { [key]: value };
|
|
1620
1622
|
}
|
|
1621
1623
|
syncOriginal() {
|
|
1622
1624
|
this.original = this.getAttributes();
|
|
@@ -1726,9 +1728,9 @@ const HasAttributes = (Model) => {
|
|
|
1726
1728
|
return null;
|
|
1727
1729
|
}
|
|
1728
1730
|
case "collection": try {
|
|
1729
|
-
return collect
|
|
1731
|
+
return collect(JSON.parse(value));
|
|
1730
1732
|
} catch {
|
|
1731
|
-
return collect
|
|
1733
|
+
return collect([]);
|
|
1732
1734
|
}
|
|
1733
1735
|
case "date": return this.asDate(value);
|
|
1734
1736
|
case "datetime":
|
|
@@ -1741,8 +1743,8 @@ const HasAttributes = (Model) => {
|
|
|
1741
1743
|
attributesToData() {
|
|
1742
1744
|
let attributes = { ...this.attributes };
|
|
1743
1745
|
for (const key in attributes) {
|
|
1744
|
-
if (this.hidden.includes(key)) attributes =
|
|
1745
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes =
|
|
1746
|
+
if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
|
|
1747
|
+
if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
|
|
1746
1748
|
}
|
|
1747
1749
|
for (const key of this.getDates()) {
|
|
1748
1750
|
if (attributes[key] === void 0) continue;
|
|
@@ -1797,7 +1799,7 @@ const HasAttributes = (Model) => {
|
|
|
1797
1799
|
}
|
|
1798
1800
|
hasCast(key, types = []) {
|
|
1799
1801
|
if (key in this.casts) {
|
|
1800
|
-
types =
|
|
1802
|
+
types = flatten(types);
|
|
1801
1803
|
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
1802
1804
|
}
|
|
1803
1805
|
return false;
|
|
@@ -1846,10 +1848,14 @@ const HasGlobalScopes = (Model) => {
|
|
|
1846
1848
|
static globalScopes;
|
|
1847
1849
|
static addGlobalScope(scope, implementation = null) {
|
|
1848
1850
|
if (typeof scope === "string" && implementation instanceof Scope) {
|
|
1849
|
-
|
|
1851
|
+
const scopes = this.globalScopes ?? {};
|
|
1852
|
+
data_set(scopes, this.name + "." + scope, implementation);
|
|
1853
|
+
this.globalScopes = scopes;
|
|
1850
1854
|
return implementation;
|
|
1851
1855
|
} else if (scope instanceof Scope) {
|
|
1852
|
-
|
|
1856
|
+
const scopes = this.globalScopes ?? {};
|
|
1857
|
+
data_set(scopes, this.name + "." + scope.constructor.name, scope);
|
|
1858
|
+
this.globalScopes = scopes;
|
|
1853
1859
|
return scope;
|
|
1854
1860
|
}
|
|
1855
1861
|
throw new InvalidArgumentError("Global scope must be an instance of Scope.");
|
|
@@ -1858,8 +1864,8 @@ const HasGlobalScopes = (Model) => {
|
|
|
1858
1864
|
return this.getGlobalScope(scope) !== null;
|
|
1859
1865
|
}
|
|
1860
1866
|
static getGlobalScope(scope) {
|
|
1861
|
-
if (typeof scope === "string") return
|
|
1862
|
-
return
|
|
1867
|
+
if (typeof scope === "string") return data_get(this.globalScopes ?? {}, this.name + "." + scope);
|
|
1868
|
+
return data_get(this.globalScopes ?? {}, this.name + "." + scope.constructor.name);
|
|
1863
1869
|
}
|
|
1864
1870
|
static getAllGlobalScopes() {
|
|
1865
1871
|
return this.globalScopes;
|
|
@@ -1868,7 +1874,7 @@ const HasGlobalScopes = (Model) => {
|
|
|
1868
1874
|
this.globalScopes = scopes;
|
|
1869
1875
|
}
|
|
1870
1876
|
getGlobalScopes() {
|
|
1871
|
-
return
|
|
1877
|
+
return data_get(this.constructor.globalScopes ?? {}, this.constructor.name, {});
|
|
1872
1878
|
}
|
|
1873
1879
|
};
|
|
1874
1880
|
};
|
|
@@ -1968,7 +1974,7 @@ const HasOneOrMany$1 = (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$1 = class extends compose(Relation$1, HasOneOrMany$1) {
|
|
|
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$1 = class extends compose(Relation$1, HasOneOrMany$1, SupportsDefault
|
|
|
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);
|
|
@@ -2205,10 +2207,10 @@ var HasManyThrough$1 = class extends Relation$1 {
|
|
|
2205
2207
|
}
|
|
2206
2208
|
const model = await this.first(columns);
|
|
2207
2209
|
if (model) return model;
|
|
2208
|
-
return callback
|
|
2210
|
+
return callback?.();
|
|
2209
2211
|
}
|
|
2210
2212
|
async find(id, columns = ["*"]) {
|
|
2211
|
-
if (isArray(id)) return await this.findMany(id, columns);
|
|
2213
|
+
if (Array.isArray(id)) return await this.findMany(id, columns);
|
|
2212
2214
|
return await this.where(this.getRelated().getQualifiedKeyName(), "=", id).first(columns);
|
|
2213
2215
|
}
|
|
2214
2216
|
async findMany(ids, columns = ["*"]) {
|
|
@@ -2236,7 +2238,7 @@ var HasManyThrough$1 = class extends Relation$1 {
|
|
|
2236
2238
|
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
2237
2239
|
}
|
|
2238
2240
|
shouldSelect(columns = ["*"]) {
|
|
2239
|
-
if (
|
|
2241
|
+
if (columns?.at(0) == "*") columns = [this.related.getTable() + ".*"];
|
|
2240
2242
|
return [...columns, this.getQualifiedFirstKeyName() + " as laravel_through_key"];
|
|
2241
2243
|
}
|
|
2242
2244
|
async chunk(count, callback) {
|
|
@@ -2330,7 +2332,7 @@ const HasRelations = (Model) => {
|
|
|
2330
2332
|
return this;
|
|
2331
2333
|
}
|
|
2332
2334
|
unsetRelation(relation) {
|
|
2333
|
-
this.relations =
|
|
2335
|
+
this.relations = Arr.except(this.relations, [relation]);
|
|
2334
2336
|
return this;
|
|
2335
2337
|
}
|
|
2336
2338
|
relationLoaded(relation) {
|
|
@@ -2353,8 +2355,7 @@ const HasRelations = (Model) => {
|
|
|
2353
2355
|
return data;
|
|
2354
2356
|
}
|
|
2355
2357
|
guessBelongsToRelation() {
|
|
2356
|
-
|
|
2357
|
-
const functionName = (((_e$stack = (/* @__PURE__ */ new Error()).stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split("\n")[2]) ?? "").split(" ")[5];
|
|
2358
|
+
const functionName = ((/* @__PURE__ */ new Error()).stack?.split("\n")[2] ?? "").split(" ")[5];
|
|
2358
2359
|
return getRelationName(functionName);
|
|
2359
2360
|
}
|
|
2360
2361
|
joiningTable(related, instance = null) {
|
|
@@ -2464,7 +2465,7 @@ const HidesAttributes = (Model) => {
|
|
|
2464
2465
|
makeVisible(...keys) {
|
|
2465
2466
|
const visible = flattenDeep(keys);
|
|
2466
2467
|
if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
|
|
2467
|
-
this.hidden =
|
|
2468
|
+
this.hidden = collect(this.hidden).diff(visible).all();
|
|
2468
2469
|
return this;
|
|
2469
2470
|
}
|
|
2470
2471
|
makeHidden(key, ...keys) {
|
|
@@ -2552,10 +2553,9 @@ var QueryBuilder = class QueryBuilder extends Inference {
|
|
|
2552
2553
|
asProxy() {
|
|
2553
2554
|
return new Proxy(this, {
|
|
2554
2555
|
get: function(target, prop) {
|
|
2555
|
-
var _target$connector$cli;
|
|
2556
2556
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2557
2557
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2558
|
-
const skipReturning = !!
|
|
2558
|
+
const skipReturning = !!target.connector.client.config?.client?.includes("mysql") && prop === "returning";
|
|
2559
2559
|
if ([
|
|
2560
2560
|
"select",
|
|
2561
2561
|
"from",
|
|
@@ -2758,6 +2758,9 @@ var arquebus = class arquebus {
|
|
|
2758
2758
|
if (this.instance === null) this.instance = new arquebus();
|
|
2759
2759
|
return this.instance;
|
|
2760
2760
|
}
|
|
2761
|
+
static withSchema() {
|
|
2762
|
+
return this;
|
|
2763
|
+
}
|
|
2761
2764
|
/**
|
|
2762
2765
|
* Initialize a new database connection
|
|
2763
2766
|
*
|
|
@@ -2847,12 +2850,12 @@ var arquebus = class arquebus {
|
|
|
2847
2850
|
const tsPath = path.resolve("arquebus.config.ts");
|
|
2848
2851
|
const instance = this.getInstance();
|
|
2849
2852
|
if (existsSync(jsPath)) {
|
|
2850
|
-
config = (await
|
|
2853
|
+
config = (await importFile(jsPath)).default;
|
|
2851
2854
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2852
2855
|
return config;
|
|
2853
2856
|
}
|
|
2854
2857
|
if (existsSync(tsPath)) if (process.env.NODE_ENV !== "production") {
|
|
2855
|
-
config = (await
|
|
2858
|
+
config = (await importFile(tsPath)).default;
|
|
2856
2859
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2857
2860
|
return config;
|
|
2858
2861
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2868,7 +2871,7 @@ var arquebus = class arquebus {
|
|
|
2868
2871
|
"cjs"
|
|
2869
2872
|
], dir);
|
|
2870
2873
|
if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
|
|
2871
|
-
config = (await
|
|
2874
|
+
config = (await importFile(found)).default;
|
|
2872
2875
|
if (addConnection) instance.addConnection(config, config.client);
|
|
2873
2876
|
return config;
|
|
2874
2877
|
} else throw new Error("arquebus.config.ts found in production without build step");
|
|
@@ -2889,7 +2892,7 @@ var arquebus = class arquebus {
|
|
|
2889
2892
|
}
|
|
2890
2893
|
async destroyAll() {
|
|
2891
2894
|
await Promise.all(Object.values(this.manager).map((connection) => {
|
|
2892
|
-
return connection
|
|
2895
|
+
return connection?.destroy();
|
|
2893
2896
|
}));
|
|
2894
2897
|
}
|
|
2895
2898
|
createModel(name, options = {}) {
|
|
@@ -2898,29 +2901,25 @@ var arquebus = class arquebus {
|
|
|
2898
2901
|
this.models = {
|
|
2899
2902
|
...this.models,
|
|
2900
2903
|
[name]: class extends BaseModel {
|
|
2901
|
-
table =
|
|
2902
|
-
connection =
|
|
2903
|
-
timestamps =
|
|
2904
|
-
primaryKey =
|
|
2905
|
-
keyType =
|
|
2906
|
-
incrementing =
|
|
2907
|
-
with =
|
|
2908
|
-
casts =
|
|
2909
|
-
static CREATED_AT =
|
|
2910
|
-
static UPDATED_AT =
|
|
2911
|
-
static DELETED_AT =
|
|
2904
|
+
table = options?.table ?? null;
|
|
2905
|
+
connection = options?.connection ?? null;
|
|
2906
|
+
timestamps = options?.timestamps ?? true;
|
|
2907
|
+
primaryKey = options?.primaryKey ?? "id";
|
|
2908
|
+
keyType = options?.keyType ?? "int";
|
|
2909
|
+
incrementing = options?.incrementing ?? true;
|
|
2910
|
+
with = options?.with ?? [];
|
|
2911
|
+
casts = options?.casts ?? {};
|
|
2912
|
+
static CREATED_AT = options?.CREATED_AT ?? "created_at";
|
|
2913
|
+
static UPDATED_AT = options?.UPDATED_AT ?? "updated_at";
|
|
2914
|
+
static DELETED_AT = options?.DELETED_AT ?? "deleted_at";
|
|
2912
2915
|
}
|
|
2913
2916
|
};
|
|
2914
2917
|
if ("attributes" in options) for (const attribute in options.attributes) {
|
|
2915
2918
|
if (options.attributes[attribute] instanceof Attribute === false) throw new Error("Attribute must be an instance of \"Attribute\"");
|
|
2916
|
-
this.models[name].prototype[getAttrMethod(attribute)] = () =>
|
|
2917
|
-
var _options$attributes;
|
|
2918
|
-
return (_options$attributes = options.attributes) === null || _options$attributes === void 0 ? void 0 : _options$attributes[attribute];
|
|
2919
|
-
};
|
|
2919
|
+
this.models[name].prototype[getAttrMethod(attribute)] = () => options.attributes?.[attribute];
|
|
2920
2920
|
}
|
|
2921
2921
|
if ("relations" in options) for (const relation in options.relations) this.models[name].prototype[getRelationMethod(relation)] = function() {
|
|
2922
|
-
|
|
2923
|
-
return (_options$relations = options.relations) === null || _options$relations === void 0 ? void 0 : _options$relations[relation](this);
|
|
2922
|
+
return options.relations?.[relation](this);
|
|
2924
2923
|
};
|
|
2925
2924
|
if ("scopes" in options) for (const scope in options.scopes) this.models[name].prototype[getScopeMethod(scope)] = options.scopes[scope];
|
|
2926
2925
|
this.models[name].setConnectionResolver(this);
|
|
@@ -2958,6 +2957,9 @@ var Model = class Model extends BaseModel {
|
|
|
2958
2957
|
this.buildRelationships(attributes);
|
|
2959
2958
|
return this.asProxy();
|
|
2960
2959
|
}
|
|
2960
|
+
static define() {
|
|
2961
|
+
return this;
|
|
2962
|
+
}
|
|
2961
2963
|
static query(trx = null) {
|
|
2962
2964
|
return new this().newQuery(trx);
|
|
2963
2965
|
}
|
|
@@ -3141,7 +3143,7 @@ var Model = class Model extends BaseModel {
|
|
|
3141
3143
|
});
|
|
3142
3144
|
}
|
|
3143
3145
|
toData() {
|
|
3144
|
-
return
|
|
3146
|
+
return Obj.deepMerge(this.attributesToData(), this.relationsToData());
|
|
3145
3147
|
}
|
|
3146
3148
|
toJSON() {
|
|
3147
3149
|
return this.toData();
|
|
@@ -3152,6 +3154,15 @@ var Model = class Model extends BaseModel {
|
|
|
3152
3154
|
toString() {
|
|
3153
3155
|
return this.toJson();
|
|
3154
3156
|
}
|
|
3157
|
+
getAttributes() {
|
|
3158
|
+
return super.getAttributes();
|
|
3159
|
+
}
|
|
3160
|
+
getAttribute(key) {
|
|
3161
|
+
return super.getAttribute(key);
|
|
3162
|
+
}
|
|
3163
|
+
setAttribute(key, value) {
|
|
3164
|
+
return super.setAttribute(key, value);
|
|
3165
|
+
}
|
|
3155
3166
|
fill(attributes) {
|
|
3156
3167
|
for (const key in attributes) this.setAttribute(key, attributes[key]);
|
|
3157
3168
|
return this;
|
|
@@ -3192,10 +3203,9 @@ var Model = class Model extends BaseModel {
|
|
|
3192
3203
|
if (this.usesTimestamps()) this.updateTimestamps();
|
|
3193
3204
|
const attributes = this.getAttributes();
|
|
3194
3205
|
if (this.getIncrementing()) {
|
|
3195
|
-
var _data$;
|
|
3196
3206
|
const keyName = this.getKeyName();
|
|
3197
3207
|
const data = await query.insert([attributes], [keyName]);
|
|
3198
|
-
this.setAttribute(keyName,
|
|
3208
|
+
this.setAttribute(keyName, data[0]?.[keyName] || data[0]);
|
|
3199
3209
|
} else if (Object.keys(attributes).length > 0) await query.insert(attributes);
|
|
3200
3210
|
this.exists = true;
|
|
3201
3211
|
await this.execHooks("created", options);
|
|
@@ -3237,7 +3247,7 @@ var Model = class Model extends BaseModel {
|
|
|
3237
3247
|
if (!this.exists) return Promise.resolve(void 0);
|
|
3238
3248
|
const model = await this.constructor.query().where(this.getKeyName(), this.getKey()).first();
|
|
3239
3249
|
this.attributes = { ...model.attributes };
|
|
3240
|
-
await this.load(collect
|
|
3250
|
+
await this.load(collect(this.relations).reject((relation) => {
|
|
3241
3251
|
return relation instanceof Pivot;
|
|
3242
3252
|
}).keys().all());
|
|
3243
3253
|
this.syncOriginal();
|