@h3ravel/arquebus 0.2.0 → 0.2.2
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/README.md +20 -5
- package/bin/index.cjs +1 -4948
- package/bin/index.js +1 -4923
- package/dist/browser/index.cjs +1 -1424
- package/dist/browser/index.d.cts +73 -14
- package/dist/browser/index.d.ts +73 -14
- package/dist/browser/index.js +1 -1359
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +1449 -0
- package/dist/index.d.ts +72 -12
- package/dist/index.js +1 -4723
- package/package.json +4 -2
package/dist/browser/index.js
CHANGED
|
@@ -1,1359 +1 @@
|
|
|
1
|
-
// src/casts/attribute.ts
|
|
2
|
-
var Attribute = class _Attribute {
|
|
3
|
-
get;
|
|
4
|
-
set;
|
|
5
|
-
withCaching = false;
|
|
6
|
-
withObjectCaching = true;
|
|
7
|
-
constructor({
|
|
8
|
-
get = null,
|
|
9
|
-
set = null
|
|
10
|
-
}) {
|
|
11
|
-
this.get = get;
|
|
12
|
-
this.set = set;
|
|
13
|
-
}
|
|
14
|
-
static make({ get = null, set = null }) {
|
|
15
|
-
return new _Attribute({ get, set });
|
|
16
|
-
}
|
|
17
|
-
static get(get) {
|
|
18
|
-
return new _Attribute({ get });
|
|
19
|
-
}
|
|
20
|
-
static set(set) {
|
|
21
|
-
return new _Attribute({ set });
|
|
22
|
-
}
|
|
23
|
-
withoutObjectCaching() {
|
|
24
|
-
this.withObjectCaching = false;
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
shouldCache() {
|
|
28
|
-
this.withCaching = true;
|
|
29
|
-
return this;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
var attribute_default = Attribute;
|
|
33
|
-
|
|
34
|
-
// src/casts-attributes.ts
|
|
35
|
-
var CastsAttributes = class _CastsAttributes {
|
|
36
|
-
constructor() {
|
|
37
|
-
if (this.constructor === _CastsAttributes) {
|
|
38
|
-
throw new Error("CastsAttributes cannot be instantiated");
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
static get(_model, _key, _value, _attributes) {
|
|
42
|
-
throw new Error("get not implemented");
|
|
43
|
-
}
|
|
44
|
-
static set(_model, _key, _value, _attributes) {
|
|
45
|
-
throw new Error("set not implemented");
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
var casts_attributes_default = CastsAttributes;
|
|
49
|
-
|
|
50
|
-
// src/browser/collection.ts
|
|
51
|
-
import { Collection as BaseCollection, collect as collect2 } from "collect.js";
|
|
52
|
-
import { isArray as isArray2, isEmpty, omit as omit3, pick } from "radashi";
|
|
53
|
-
|
|
54
|
-
// src/utils.ts
|
|
55
|
-
import { camel, dash, snake, trim } from "radashi";
|
|
56
|
-
import advancedFormat from "dayjs/plugin/advancedFormat.js";
|
|
57
|
-
import dayjs from "dayjs";
|
|
58
|
-
dayjs.extend(advancedFormat);
|
|
59
|
-
var now = (format = "YYYY-MM-DD HH:mm:ss") => dayjs().format(format);
|
|
60
|
-
var getRelationName = (relationMethod) => {
|
|
61
|
-
return snake(relationMethod.substring(8));
|
|
62
|
-
};
|
|
63
|
-
var getScopeName = (scopeMethod) => {
|
|
64
|
-
return snake(scopeMethod.substring(5));
|
|
65
|
-
};
|
|
66
|
-
var getRelationMethod = (relation) => {
|
|
67
|
-
return camel(`relation_${relation}`);
|
|
68
|
-
};
|
|
69
|
-
var getScopeMethod = (scope) => {
|
|
70
|
-
return camel(`scope_${scope}`);
|
|
71
|
-
};
|
|
72
|
-
var getAttrMethod = (attr) => {
|
|
73
|
-
return camel(`attribute_${attr}`);
|
|
74
|
-
};
|
|
75
|
-
var getGetterMethod = (attr) => {
|
|
76
|
-
return camel(`get_${attr}_attribute`);
|
|
77
|
-
};
|
|
78
|
-
var getSetterMethod = (attr) => {
|
|
79
|
-
return camel(`set_${attr}_attribute`);
|
|
80
|
-
};
|
|
81
|
-
var getAttrName = (attrMethod) => {
|
|
82
|
-
return attrMethod.substring(3, attrMethod.length - 9).toLowerCase();
|
|
83
|
-
};
|
|
84
|
-
var tap = (instance, callback) => {
|
|
85
|
-
const result = callback(instance);
|
|
86
|
-
return result instanceof Promise ? result.then(() => instance) : instance;
|
|
87
|
-
};
|
|
88
|
-
function compose(Base, ...mixins) {
|
|
89
|
-
return mixins.reduce((acc, mixin) => mixin(acc), Base);
|
|
90
|
-
}
|
|
91
|
-
var flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
|
|
92
|
-
var kebabCase = (str) => trim(dash(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
93
|
-
var snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
|
|
94
|
-
var defineConfig = (config) => {
|
|
95
|
-
return config;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// src/browser/relations/relation.ts
|
|
99
|
-
var Relation = class {
|
|
100
|
-
parent;
|
|
101
|
-
related;
|
|
102
|
-
eagerKeysWereEmpty = false;
|
|
103
|
-
static constraints = true;
|
|
104
|
-
constructor(related, parent) {
|
|
105
|
-
this.parent = parent;
|
|
106
|
-
this.related = related;
|
|
107
|
-
}
|
|
108
|
-
asProxy() {
|
|
109
|
-
const handler = {
|
|
110
|
-
get: function(target, prop) {
|
|
111
|
-
if (typeof target[prop] !== "undefined") {
|
|
112
|
-
return target[prop];
|
|
113
|
-
}
|
|
114
|
-
if (typeof prop === "string") {
|
|
115
|
-
return () => target.asProxy();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
return new Proxy(this, handler);
|
|
120
|
-
}
|
|
121
|
-
getRelated() {
|
|
122
|
-
return this.related;
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
var relation_default = Relation;
|
|
126
|
-
|
|
127
|
-
// src/browser/relations/belongs-to.ts
|
|
128
|
-
var BelongsTo = class extends relation_default {
|
|
129
|
-
foreignKey;
|
|
130
|
-
ownerKey;
|
|
131
|
-
child;
|
|
132
|
-
relationName;
|
|
133
|
-
constructor(related, child, foreignKey, ownerKey, relationName) {
|
|
134
|
-
super(related, child);
|
|
135
|
-
this.foreignKey = foreignKey;
|
|
136
|
-
this.ownerKey = ownerKey;
|
|
137
|
-
this.child = child;
|
|
138
|
-
this.relationName = relationName;
|
|
139
|
-
return this.asProxy();
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
var belongs_to_default = BelongsTo;
|
|
143
|
-
|
|
144
|
-
// src/browser/relations/belongs-to-many.ts
|
|
145
|
-
var BelongsToMany = class extends relation_default {
|
|
146
|
-
table;
|
|
147
|
-
foreignPivotKey;
|
|
148
|
-
relatedPivotKey;
|
|
149
|
-
parentKey;
|
|
150
|
-
relatedKey;
|
|
151
|
-
pivotColumns = [];
|
|
152
|
-
pivotValues = [];
|
|
153
|
-
pivotWheres = [];
|
|
154
|
-
pivotWhereIns = [];
|
|
155
|
-
pivotWhereNulls = [];
|
|
156
|
-
accessor = "pivot";
|
|
157
|
-
using;
|
|
158
|
-
pivotCreatedAt;
|
|
159
|
-
pivotUpdatedAt;
|
|
160
|
-
constructor(related, parent, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
|
|
161
|
-
super(related, parent);
|
|
162
|
-
this.table = table;
|
|
163
|
-
this.foreignPivotKey = foreignPivotKey;
|
|
164
|
-
this.relatedPivotKey = relatedPivotKey;
|
|
165
|
-
this.parentKey = parentKey;
|
|
166
|
-
this.relatedKey = relatedKey;
|
|
167
|
-
return this.asProxy();
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
var belongs_to_many_default = BelongsToMany;
|
|
171
|
-
|
|
172
|
-
// src/concerns/has-attributes.ts
|
|
173
|
-
import { flat as flatten, omit } from "radashi";
|
|
174
|
-
import collect from "collect.js";
|
|
175
|
-
import dayjs2 from "dayjs";
|
|
176
|
-
var HasAttributes = (Model2) => {
|
|
177
|
-
return class extends Model2 {
|
|
178
|
-
static castTypeCache = {};
|
|
179
|
-
attributes = {};
|
|
180
|
-
original = {};
|
|
181
|
-
casts = {};
|
|
182
|
-
changes = {};
|
|
183
|
-
appends = [];
|
|
184
|
-
setAppends(appends) {
|
|
185
|
-
this.appends = appends;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
append(...keys) {
|
|
189
|
-
const appends = flattenDeep(keys);
|
|
190
|
-
this.appends = [...this.appends, ...appends];
|
|
191
|
-
return this;
|
|
192
|
-
}
|
|
193
|
-
normalizeCastClassResponse(key, value) {
|
|
194
|
-
return value?.constructor?.name === "Object" ? value : {
|
|
195
|
-
[key]: value
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
syncOriginal() {
|
|
199
|
-
this.original = this.getAttributes();
|
|
200
|
-
return this;
|
|
201
|
-
}
|
|
202
|
-
syncChanges() {
|
|
203
|
-
this.changes = this.getDirty();
|
|
204
|
-
return this;
|
|
205
|
-
}
|
|
206
|
-
syncOriginalAttribute(attribute) {
|
|
207
|
-
this.syncOriginalAttributes(attribute);
|
|
208
|
-
}
|
|
209
|
-
syncOriginalAttributes(...attributes) {
|
|
210
|
-
attributes = flattenDeep(attributes);
|
|
211
|
-
const modelAttributes = this.getAttributes();
|
|
212
|
-
for (const attribute of attributes) {
|
|
213
|
-
this.original[attribute] = modelAttributes[attribute];
|
|
214
|
-
}
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
isDirty(...attributes) {
|
|
218
|
-
const changes = this.getDirty();
|
|
219
|
-
attributes = flattenDeep(attributes);
|
|
220
|
-
if (attributes.length === 0) {
|
|
221
|
-
return Object.keys(changes).length > 0;
|
|
222
|
-
}
|
|
223
|
-
for (const attribute of attributes) {
|
|
224
|
-
if (attribute in changes) {
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
getDirty() {
|
|
231
|
-
const dirty = {};
|
|
232
|
-
const attributes = this.getAttributes();
|
|
233
|
-
for (const key in attributes) {
|
|
234
|
-
const value = attributes[key];
|
|
235
|
-
if (!this.originalIsEquivalent(key)) {
|
|
236
|
-
dirty[key] = value;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return dirty;
|
|
240
|
-
}
|
|
241
|
-
originalIsEquivalent(key) {
|
|
242
|
-
if (this.original[key] === void 0) {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
const attribute = this.attributes[key];
|
|
246
|
-
const original = this.original[key];
|
|
247
|
-
if (attribute === original) {
|
|
248
|
-
return true;
|
|
249
|
-
} else {
|
|
250
|
-
return false;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
setAttributes(attributes) {
|
|
254
|
-
this.attributes = { ...attributes };
|
|
255
|
-
}
|
|
256
|
-
setRawAttributes(attributes, sync = false) {
|
|
257
|
-
this.attributes = attributes;
|
|
258
|
-
if (sync) {
|
|
259
|
-
this.syncOriginal();
|
|
260
|
-
}
|
|
261
|
-
return this;
|
|
262
|
-
}
|
|
263
|
-
getAttributes() {
|
|
264
|
-
return { ...this.attributes };
|
|
265
|
-
}
|
|
266
|
-
setAttribute(key, value) {
|
|
267
|
-
const setterMethod = getSetterMethod(key);
|
|
268
|
-
if (typeof this[setterMethod] === "function") {
|
|
269
|
-
this[setterMethod](value);
|
|
270
|
-
return this;
|
|
271
|
-
}
|
|
272
|
-
const attrMethod = getAttrMethod(key);
|
|
273
|
-
if (typeof this[attrMethod] === "function") {
|
|
274
|
-
const attribute = this[attrMethod]();
|
|
275
|
-
const callback = attribute.set || ((value2) => {
|
|
276
|
-
this.attributes[key] = value2;
|
|
277
|
-
});
|
|
278
|
-
this.attributes = {
|
|
279
|
-
...this.attributes,
|
|
280
|
-
...this.normalizeCastClassResponse(key, callback(value, this.attributes))
|
|
281
|
-
};
|
|
282
|
-
return this;
|
|
283
|
-
}
|
|
284
|
-
const casts = this.getCasts();
|
|
285
|
-
const castType = casts[key];
|
|
286
|
-
if (this.isCustomCast(castType) && typeof castType !== "string") {
|
|
287
|
-
value = castType.set(this, key, value, this.attributes) ?? "";
|
|
288
|
-
}
|
|
289
|
-
if (castType === "json") {
|
|
290
|
-
value = JSON.stringify(value);
|
|
291
|
-
}
|
|
292
|
-
if (castType === "collection") {
|
|
293
|
-
value = JSON.stringify(value);
|
|
294
|
-
}
|
|
295
|
-
if (value !== null && this.isDateAttribute(key)) {
|
|
296
|
-
value = this.fromDateTime(value);
|
|
297
|
-
}
|
|
298
|
-
this.attributes[key] = value;
|
|
299
|
-
return this;
|
|
300
|
-
}
|
|
301
|
-
getAttribute(key) {
|
|
302
|
-
if (!key) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
const getterMethod = getGetterMethod(key);
|
|
306
|
-
if (typeof this[getterMethod] === "function") {
|
|
307
|
-
return this[getterMethod](this.attributes[key], this.attributes);
|
|
308
|
-
}
|
|
309
|
-
const attrMethod = getAttrMethod(key);
|
|
310
|
-
if (typeof this[attrMethod] === "function") {
|
|
311
|
-
const caster = this[attrMethod]();
|
|
312
|
-
return caster.get(this.attributes[key], this.attributes);
|
|
313
|
-
}
|
|
314
|
-
if (key in this.attributes) {
|
|
315
|
-
if (this.hasCast(key)) {
|
|
316
|
-
return this.castAttribute(key, this.attributes[key]);
|
|
317
|
-
}
|
|
318
|
-
if (this.getDates().includes(key)) {
|
|
319
|
-
return this.asDateTime(this.attributes[key]);
|
|
320
|
-
}
|
|
321
|
-
return this.attributes[key];
|
|
322
|
-
}
|
|
323
|
-
if (key in this.relations) {
|
|
324
|
-
return this.relations[key];
|
|
325
|
-
}
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
castAttribute(key, value) {
|
|
329
|
-
const castType = this.getCastType(key);
|
|
330
|
-
if (!castType) {
|
|
331
|
-
return value;
|
|
332
|
-
}
|
|
333
|
-
if (value === null) {
|
|
334
|
-
return value;
|
|
335
|
-
}
|
|
336
|
-
switch (castType) {
|
|
337
|
-
case "int":
|
|
338
|
-
case "integer":
|
|
339
|
-
return parseInt(value);
|
|
340
|
-
case "real":
|
|
341
|
-
case "float":
|
|
342
|
-
case "double":
|
|
343
|
-
return parseFloat(value);
|
|
344
|
-
case "decimal":
|
|
345
|
-
return this.asDecimal(value, castType.split(":")[1]);
|
|
346
|
-
case "string":
|
|
347
|
-
return String(value);
|
|
348
|
-
case "bool":
|
|
349
|
-
case "boolean":
|
|
350
|
-
return Boolean(value);
|
|
351
|
-
case "object":
|
|
352
|
-
case "json":
|
|
353
|
-
try {
|
|
354
|
-
return JSON.parse(value);
|
|
355
|
-
} catch {
|
|
356
|
-
return null;
|
|
357
|
-
}
|
|
358
|
-
case "collection":
|
|
359
|
-
try {
|
|
360
|
-
return collect(JSON.parse(value));
|
|
361
|
-
} catch {
|
|
362
|
-
return collect([]);
|
|
363
|
-
}
|
|
364
|
-
case "date":
|
|
365
|
-
return this.asDate(value);
|
|
366
|
-
case "datetime":
|
|
367
|
-
case "custom_datetime":
|
|
368
|
-
return this.asDateTime(value);
|
|
369
|
-
case "timestamp":
|
|
370
|
-
return this.asTimestamp(value);
|
|
371
|
-
}
|
|
372
|
-
if (this.isCustomCast(castType)) {
|
|
373
|
-
return castType.get(this, key, value, this.attributes);
|
|
374
|
-
}
|
|
375
|
-
return value;
|
|
376
|
-
}
|
|
377
|
-
attributesToData() {
|
|
378
|
-
let attributes = { ...this.attributes };
|
|
379
|
-
for (const key in attributes) {
|
|
380
|
-
if (this.hidden.includes(key)) {
|
|
381
|
-
attributes = omit(attributes, [key]);
|
|
382
|
-
}
|
|
383
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) {
|
|
384
|
-
attributes = omit(attributes, [key]);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
for (const key of this.getDates()) {
|
|
388
|
-
if (attributes[key] === void 0) {
|
|
389
|
-
continue;
|
|
390
|
-
}
|
|
391
|
-
attributes[key] = this.serializeDate(this.asDateTime(attributes[key]));
|
|
392
|
-
}
|
|
393
|
-
const casts = this.getCasts();
|
|
394
|
-
for (const key in casts) {
|
|
395
|
-
const value = casts[key];
|
|
396
|
-
if (key in attributes === false) {
|
|
397
|
-
continue;
|
|
398
|
-
}
|
|
399
|
-
attributes[key] = this.castAttribute(key, attributes[key]);
|
|
400
|
-
if (key in attributes && ["date", "datetime"].includes(String(value))) {
|
|
401
|
-
attributes[key] = this.serializeDate(attributes[key]);
|
|
402
|
-
}
|
|
403
|
-
if (key in attributes && this.isCustomDateTimeCast(value)) {
|
|
404
|
-
attributes[key] = dayjs2(attributes[key]).format(String(value).split(":")[1]);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
for (const key of this.appends) {
|
|
408
|
-
attributes[key] = this.mutateAttribute(key, null);
|
|
409
|
-
}
|
|
410
|
-
return attributes;
|
|
411
|
-
}
|
|
412
|
-
mutateAttribute(key, value) {
|
|
413
|
-
if (typeof this[getGetterMethod(key)] === "function") {
|
|
414
|
-
return this[getGetterMethod(key)](value);
|
|
415
|
-
} else if (typeof this[getAttrMethod(key)] === "function") {
|
|
416
|
-
const caster = this[getAttrMethod(key)]();
|
|
417
|
-
return caster.get(key, this.attributes);
|
|
418
|
-
} else if (key in this) {
|
|
419
|
-
return this[key];
|
|
420
|
-
}
|
|
421
|
-
return value;
|
|
422
|
-
}
|
|
423
|
-
mutateAttributeForArray(_key, _value) {
|
|
424
|
-
}
|
|
425
|
-
isDateAttribute(key) {
|
|
426
|
-
return this.getDates().includes(key) || this.isDateCastable(key);
|
|
427
|
-
}
|
|
428
|
-
serializeDate(date) {
|
|
429
|
-
return date ? dayjs2(date).toISOString() : null;
|
|
430
|
-
}
|
|
431
|
-
getDates() {
|
|
432
|
-
return this.usesTimestamps() ? [
|
|
433
|
-
this.getCreatedAtColumn(),
|
|
434
|
-
this.getUpdatedAtColumn()
|
|
435
|
-
] : [];
|
|
436
|
-
}
|
|
437
|
-
getCasts() {
|
|
438
|
-
if (this.getIncrementing()) {
|
|
439
|
-
return {
|
|
440
|
-
[this.getKeyName()]: this.getKeyType(),
|
|
441
|
-
...this.casts
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
return this.casts;
|
|
445
|
-
}
|
|
446
|
-
getCastType(key) {
|
|
447
|
-
const castType = this.getCasts()[key];
|
|
448
|
-
let castTypeCacheKey;
|
|
449
|
-
if (typeof castType === "string") {
|
|
450
|
-
castTypeCacheKey = castType;
|
|
451
|
-
} else if (new castType() instanceof casts_attributes_default) {
|
|
452
|
-
castTypeCacheKey = castType.name;
|
|
453
|
-
}
|
|
454
|
-
if (castTypeCacheKey && this.getConstructor().castTypeCache[castTypeCacheKey] !== void 0) {
|
|
455
|
-
return this.getConstructor().castTypeCache[castTypeCacheKey];
|
|
456
|
-
}
|
|
457
|
-
let convertedCastType;
|
|
458
|
-
if (this.isCustomDateTimeCast(castType)) {
|
|
459
|
-
convertedCastType = "custom_datetime";
|
|
460
|
-
} else if (this.isDecimalCast(castType)) {
|
|
461
|
-
convertedCastType = "decimal";
|
|
462
|
-
} else if (this.isCustomCast(castType)) {
|
|
463
|
-
convertedCastType = castType;
|
|
464
|
-
} else {
|
|
465
|
-
convertedCastType = String(castType).toLocaleLowerCase().trim();
|
|
466
|
-
}
|
|
467
|
-
return this.getConstructor()[castTypeCacheKey] = convertedCastType;
|
|
468
|
-
}
|
|
469
|
-
hasCast(key, types = []) {
|
|
470
|
-
if (key in this.casts) {
|
|
471
|
-
types = flatten(types);
|
|
472
|
-
return types.length > 0 ? types.includes(this.getCastType(key)) : true;
|
|
473
|
-
}
|
|
474
|
-
return false;
|
|
475
|
-
}
|
|
476
|
-
withDayjs(date) {
|
|
477
|
-
return dayjs2(date);
|
|
478
|
-
}
|
|
479
|
-
isCustomCast(cast) {
|
|
480
|
-
return typeof cast === "function" && new cast() instanceof casts_attributes_default;
|
|
481
|
-
}
|
|
482
|
-
isCustomDateTimeCast(cast) {
|
|
483
|
-
if (typeof cast !== "string") {
|
|
484
|
-
return false;
|
|
485
|
-
}
|
|
486
|
-
return cast.startsWith("date:") || cast.startsWith("datetime:");
|
|
487
|
-
}
|
|
488
|
-
isDecimalCast(cast) {
|
|
489
|
-
if (typeof cast !== "string") {
|
|
490
|
-
return false;
|
|
491
|
-
}
|
|
492
|
-
return cast.startsWith("decimal:");
|
|
493
|
-
}
|
|
494
|
-
isDateCastable(key) {
|
|
495
|
-
return this.hasCast(key, ["date", "datetime"]);
|
|
496
|
-
}
|
|
497
|
-
fromDateTime(value) {
|
|
498
|
-
return dayjs2(this.asDateTime(value)).format(this.getDateFormat());
|
|
499
|
-
}
|
|
500
|
-
getDateFormat() {
|
|
501
|
-
return this.dateFormat || "YYYY-MM-DD HH:mm:ss";
|
|
502
|
-
}
|
|
503
|
-
asDecimal(value, decimals) {
|
|
504
|
-
return parseFloat(value).toFixed(decimals);
|
|
505
|
-
}
|
|
506
|
-
asDateTime(value) {
|
|
507
|
-
if (value === null) {
|
|
508
|
-
return null;
|
|
509
|
-
}
|
|
510
|
-
if (value instanceof Date) {
|
|
511
|
-
return value;
|
|
512
|
-
}
|
|
513
|
-
if (typeof value === "number") {
|
|
514
|
-
return new Date(value * 1e3);
|
|
515
|
-
}
|
|
516
|
-
return new Date(value);
|
|
517
|
-
}
|
|
518
|
-
asDate(value) {
|
|
519
|
-
const date = this.asDateTime(value);
|
|
520
|
-
return dayjs2(date).startOf("day").toDate();
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
};
|
|
524
|
-
var has_attributes_default = HasAttributes;
|
|
525
|
-
|
|
526
|
-
// src/browser/relations/has-many.ts
|
|
527
|
-
var HasMany = class extends relation_default {
|
|
528
|
-
foreignKey;
|
|
529
|
-
localKey;
|
|
530
|
-
constructor(related, parent, foreignKey, localKey) {
|
|
531
|
-
super(related, parent);
|
|
532
|
-
this.foreignKey = foreignKey;
|
|
533
|
-
this.localKey = localKey;
|
|
534
|
-
return this.asProxy();
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
var has_many_default = HasMany;
|
|
538
|
-
|
|
539
|
-
// src/browser/relations/has-one.ts
|
|
540
|
-
var HasOne = class extends relation_default {
|
|
541
|
-
foreignKey;
|
|
542
|
-
localKey;
|
|
543
|
-
constructor(related, parent, foreignKey, localKey) {
|
|
544
|
-
super(related, parent);
|
|
545
|
-
this.foreignKey = foreignKey;
|
|
546
|
-
this.localKey = localKey;
|
|
547
|
-
return this.asProxy();
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
var has_one_default = HasOne;
|
|
551
|
-
|
|
552
|
-
// src/browser/relations/has-many-through.ts
|
|
553
|
-
var HasManyThrough = class extends relation_default {
|
|
554
|
-
throughParent;
|
|
555
|
-
farParent;
|
|
556
|
-
firstKey;
|
|
557
|
-
secondKey;
|
|
558
|
-
localKey;
|
|
559
|
-
secondLocalKey;
|
|
560
|
-
constructor(query, farParent, throughParent, firstKey, secondKey, localKey, secondLocalKey) {
|
|
561
|
-
super(query, throughParent);
|
|
562
|
-
this.localKey = localKey;
|
|
563
|
-
this.firstKey = firstKey;
|
|
564
|
-
this.secondKey = secondKey;
|
|
565
|
-
this.farParent = farParent;
|
|
566
|
-
this.throughParent = throughParent;
|
|
567
|
-
this.secondLocalKey = secondLocalKey;
|
|
568
|
-
return this.asProxy();
|
|
569
|
-
}
|
|
570
|
-
};
|
|
571
|
-
var has_many_through_default = HasManyThrough;
|
|
572
|
-
|
|
573
|
-
// src/browser/relations/has-one-through.ts
|
|
574
|
-
var HasOneThrough = class extends has_many_through_default {
|
|
575
|
-
};
|
|
576
|
-
var has_one_through_default = HasOneThrough;
|
|
577
|
-
|
|
578
|
-
// src/errors.ts
|
|
579
|
-
import { isArray } from "radashi";
|
|
580
|
-
var BaseError = class extends Error {
|
|
581
|
-
constructor(message, _entity) {
|
|
582
|
-
super(message);
|
|
583
|
-
Error.captureStackTrace(this, this.constructor);
|
|
584
|
-
this.name = this.constructor.name;
|
|
585
|
-
this.message = message;
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
var ModelNotFoundError = class extends BaseError {
|
|
589
|
-
model;
|
|
590
|
-
ids = [];
|
|
591
|
-
constructor() {
|
|
592
|
-
super("");
|
|
593
|
-
}
|
|
594
|
-
setModel(model, ids = []) {
|
|
595
|
-
this.model = model;
|
|
596
|
-
this.ids = isArray(ids) ? ids : [ids];
|
|
597
|
-
this.message = `No query results for model [${model}]`;
|
|
598
|
-
if (this.ids.length > 0) {
|
|
599
|
-
this.message += " " + this.ids.join(", ");
|
|
600
|
-
} else {
|
|
601
|
-
this.message += ".";
|
|
602
|
-
}
|
|
603
|
-
return this;
|
|
604
|
-
}
|
|
605
|
-
getModel() {
|
|
606
|
-
return this.model;
|
|
607
|
-
}
|
|
608
|
-
getIds() {
|
|
609
|
-
return this.ids;
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
var RelationNotFoundError = class extends BaseError {
|
|
613
|
-
};
|
|
614
|
-
var InvalidArgumentError = class extends BaseError {
|
|
615
|
-
};
|
|
616
|
-
|
|
617
|
-
// src/browser/concerns/has-relations.ts
|
|
618
|
-
import { omit as omit2 } from "radashi";
|
|
619
|
-
var HasRelations = (Model2) => {
|
|
620
|
-
return class extends Model2 {
|
|
621
|
-
relations = {};
|
|
622
|
-
getRelation(relation) {
|
|
623
|
-
return this.relations[relation];
|
|
624
|
-
}
|
|
625
|
-
setRelation(relation, value) {
|
|
626
|
-
this.relations[relation] = value;
|
|
627
|
-
return this;
|
|
628
|
-
}
|
|
629
|
-
unsetRelation(relation) {
|
|
630
|
-
this.relations = omit2(this.relations, [relation]);
|
|
631
|
-
return this;
|
|
632
|
-
}
|
|
633
|
-
relationLoaded(relation) {
|
|
634
|
-
return this.relations[relation] !== void 0;
|
|
635
|
-
}
|
|
636
|
-
related(relation) {
|
|
637
|
-
if (typeof this[getRelationMethod(relation)] !== "function") {
|
|
638
|
-
const message = `Model [${this.constructor.name}]'s relation [${relation}] doesn't exist.`;
|
|
639
|
-
throw new RelationNotFoundError(message);
|
|
640
|
-
}
|
|
641
|
-
return this[getRelationMethod(relation)]();
|
|
642
|
-
}
|
|
643
|
-
async getRelated(relation) {
|
|
644
|
-
return await this.related(relation).getResults();
|
|
645
|
-
}
|
|
646
|
-
relationsToData() {
|
|
647
|
-
const data = {};
|
|
648
|
-
for (const key in this.relations) {
|
|
649
|
-
if (this.hidden.includes(key)) {
|
|
650
|
-
continue;
|
|
651
|
-
}
|
|
652
|
-
if (this.visible.length > 0 && this.visible.includes(key) === false) {
|
|
653
|
-
continue;
|
|
654
|
-
}
|
|
655
|
-
data[key] = this.relations[key] instanceof Array ? this.relations[key].map((item) => item.toData()) : this.relations[key] === null ? null : this.relations[key].toData();
|
|
656
|
-
}
|
|
657
|
-
return data;
|
|
658
|
-
}
|
|
659
|
-
guessBelongsToRelation() {
|
|
660
|
-
const e = new Error();
|
|
661
|
-
const frame = e.stack?.split("\n")[2];
|
|
662
|
-
const functionName = frame?.split(" ")[5];
|
|
663
|
-
return getRelationName(functionName);
|
|
664
|
-
}
|
|
665
|
-
joiningTable(related, instance = null) {
|
|
666
|
-
const segments = [
|
|
667
|
-
instance ? instance.joiningTableSegment() : snakeCase(related.name),
|
|
668
|
-
this.joiningTableSegment()
|
|
669
|
-
];
|
|
670
|
-
return segments.sort().join("_").toLocaleLowerCase();
|
|
671
|
-
}
|
|
672
|
-
joiningTableSegment() {
|
|
673
|
-
return snakeCase(this.constructor.name);
|
|
674
|
-
}
|
|
675
|
-
hasOne(related, foreignKey = null, localKey = null) {
|
|
676
|
-
const instance = new related();
|
|
677
|
-
foreignKey = foreignKey || this.getForeignKey();
|
|
678
|
-
localKey = localKey || this.getKeyName();
|
|
679
|
-
return new has_one_default(related, this, instance.getTable() + "." + foreignKey, localKey);
|
|
680
|
-
}
|
|
681
|
-
hasMany(related, foreignKey = null, localKey = null) {
|
|
682
|
-
const instance = new related();
|
|
683
|
-
foreignKey = foreignKey || this.getForeignKey();
|
|
684
|
-
localKey = localKey || this.getKeyName();
|
|
685
|
-
return new has_many_default(related, this, instance.getTable() + "." + foreignKey, localKey);
|
|
686
|
-
}
|
|
687
|
-
belongsTo(related, foreignKey = null, ownerKey = null, relation = null) {
|
|
688
|
-
const instance = new related();
|
|
689
|
-
foreignKey = foreignKey || instance.getForeignKey();
|
|
690
|
-
ownerKey = ownerKey || instance.getKeyName();
|
|
691
|
-
relation = relation || this.guessBelongsToRelation();
|
|
692
|
-
return new belongs_to_default(related, this, foreignKey, ownerKey, relation);
|
|
693
|
-
}
|
|
694
|
-
belongsToMany(related, table = null, foreignPivotKey = null, relatedPivotKey = null, parentKey = null, relatedKey = null) {
|
|
695
|
-
const instance = new related();
|
|
696
|
-
const query = related.query();
|
|
697
|
-
table = table || this.joiningTable(related, instance);
|
|
698
|
-
foreignPivotKey = foreignPivotKey || this.getForeignKey();
|
|
699
|
-
relatedPivotKey = relatedPivotKey || instance.getForeignKey();
|
|
700
|
-
parentKey = parentKey || this.getKeyName();
|
|
701
|
-
relatedKey = relatedKey || instance.getKeyName();
|
|
702
|
-
return new belongs_to_many_default(query, this, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey);
|
|
703
|
-
}
|
|
704
|
-
hasOneThrough(related, through, firstKey = null, secondKey = null, localKey = null, secondLocalKey = null) {
|
|
705
|
-
through = new through();
|
|
706
|
-
const query = related.query();
|
|
707
|
-
firstKey = firstKey || this.getForeignKey();
|
|
708
|
-
secondKey = secondKey || through.getForeignKey();
|
|
709
|
-
return new has_one_through_default(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
|
|
710
|
-
}
|
|
711
|
-
hasManyThrough(related, through, firstKey = null, secondKey = null, localKey = null, secondLocalKey = null) {
|
|
712
|
-
through = new through();
|
|
713
|
-
const query = related.query();
|
|
714
|
-
firstKey = firstKey || this.getForeignKey();
|
|
715
|
-
secondKey = secondKey || through.getForeignKey();
|
|
716
|
-
return new has_many_through_default(query, this, through, firstKey, secondKey, localKey || this.getKeyName(), secondLocalKey || through.getKeyName());
|
|
717
|
-
}
|
|
718
|
-
};
|
|
719
|
-
};
|
|
720
|
-
var has_relations_default = HasRelations;
|
|
721
|
-
|
|
722
|
-
// src/concerns/has-timestamps.ts
|
|
723
|
-
var HasTimestamps = (Model2) => {
|
|
724
|
-
return class extends Model2 {
|
|
725
|
-
static CREATED_AT = "created_at";
|
|
726
|
-
static UPDATED_AT = "updated_at";
|
|
727
|
-
static DELETED_AT = "deleted_at";
|
|
728
|
-
timestamps = true;
|
|
729
|
-
dateFormat = "YYYY-MM-DD HH:mm:ss";
|
|
730
|
-
usesTimestamps() {
|
|
731
|
-
return this.timestamps;
|
|
732
|
-
}
|
|
733
|
-
updateTimestamps() {
|
|
734
|
-
const time = this.freshTimestampString();
|
|
735
|
-
const updatedAtColumn = this.getUpdatedAtColumn();
|
|
736
|
-
if (updatedAtColumn && !this.isDirty(updatedAtColumn)) {
|
|
737
|
-
this.setUpdatedAt(time);
|
|
738
|
-
}
|
|
739
|
-
const createdAtColumn = this.getCreatedAtColumn();
|
|
740
|
-
if (!this.exists && createdAtColumn && !this.isDirty(createdAtColumn)) {
|
|
741
|
-
this.setCreatedAt(time);
|
|
742
|
-
}
|
|
743
|
-
return this;
|
|
744
|
-
}
|
|
745
|
-
getCreatedAtColumn() {
|
|
746
|
-
return this.constructor.CREATED_AT;
|
|
747
|
-
}
|
|
748
|
-
getUpdatedAtColumn() {
|
|
749
|
-
return this.constructor.UPDATED_AT;
|
|
750
|
-
}
|
|
751
|
-
setCreatedAt(value) {
|
|
752
|
-
this.attributes[this.getCreatedAtColumn()] = value;
|
|
753
|
-
return this;
|
|
754
|
-
}
|
|
755
|
-
setUpdatedAt(value) {
|
|
756
|
-
this.attributes[this.getUpdatedAtColumn()] = value;
|
|
757
|
-
return this;
|
|
758
|
-
}
|
|
759
|
-
freshTimestamp() {
|
|
760
|
-
const time = /* @__PURE__ */ new Date();
|
|
761
|
-
time.setMilliseconds(0);
|
|
762
|
-
return time;
|
|
763
|
-
}
|
|
764
|
-
freshTimestampString() {
|
|
765
|
-
return this.fromDateTime(this.freshTimestamp());
|
|
766
|
-
}
|
|
767
|
-
};
|
|
768
|
-
};
|
|
769
|
-
var has_timestamps_default = HasTimestamps;
|
|
770
|
-
|
|
771
|
-
// src/concerns/hides-attributes.ts
|
|
772
|
-
import { diff as difference } from "radashi";
|
|
773
|
-
var HidesAttributes = (Model2) => {
|
|
774
|
-
return class extends Model2 {
|
|
775
|
-
hidden = [];
|
|
776
|
-
visible = [];
|
|
777
|
-
makeVisible(...keys) {
|
|
778
|
-
const visible = flattenDeep(keys);
|
|
779
|
-
if (this.visible.length > 0) {
|
|
780
|
-
this.visible = [...this.visible, ...visible];
|
|
781
|
-
}
|
|
782
|
-
this.hidden = difference(this.hidden, visible);
|
|
783
|
-
return this;
|
|
784
|
-
}
|
|
785
|
-
makeHidden(key, ...keys) {
|
|
786
|
-
const hidden = flattenDeep([...key, ...keys]);
|
|
787
|
-
if (this.hidden.length > 0) {
|
|
788
|
-
this.hidden = [...this.hidden, ...hidden];
|
|
789
|
-
}
|
|
790
|
-
return this;
|
|
791
|
-
}
|
|
792
|
-
getHidden() {
|
|
793
|
-
return this.hidden;
|
|
794
|
-
}
|
|
795
|
-
getVisible() {
|
|
796
|
-
return this.visible;
|
|
797
|
-
}
|
|
798
|
-
setHidden(hidden) {
|
|
799
|
-
this.hidden = hidden;
|
|
800
|
-
return this;
|
|
801
|
-
}
|
|
802
|
-
setVisible(visible) {
|
|
803
|
-
this.visible = visible;
|
|
804
|
-
return this;
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
};
|
|
808
|
-
var hides_attributes_default = HidesAttributes;
|
|
809
|
-
|
|
810
|
-
// src/browser/model.ts
|
|
811
|
-
import { assign as merge } from "radashi";
|
|
812
|
-
import pluralize from "pluralize";
|
|
813
|
-
var BaseModel = compose(
|
|
814
|
-
class {
|
|
815
|
-
},
|
|
816
|
-
has_attributes_default,
|
|
817
|
-
hides_attributes_default,
|
|
818
|
-
has_relations_default,
|
|
819
|
-
has_timestamps_default
|
|
820
|
-
);
|
|
821
|
-
var Model = class _Model extends BaseModel {
|
|
822
|
-
primaryKey = "id";
|
|
823
|
-
perPage = 15;
|
|
824
|
-
static globalScopes = {};
|
|
825
|
-
static pluginInitializers = {};
|
|
826
|
-
static _booted = {};
|
|
827
|
-
static resolver;
|
|
828
|
-
static browser = true;
|
|
829
|
-
connection = null;
|
|
830
|
-
constructor(attributes = {}) {
|
|
831
|
-
super();
|
|
832
|
-
this.bootIfNotBooted();
|
|
833
|
-
this.initializePlugins();
|
|
834
|
-
this.syncOriginal();
|
|
835
|
-
this.fill(attributes);
|
|
836
|
-
return this.asProxy();
|
|
837
|
-
}
|
|
838
|
-
static init(attributes = {}) {
|
|
839
|
-
return new this(attributes);
|
|
840
|
-
}
|
|
841
|
-
static extend(plugin, options) {
|
|
842
|
-
plugin(this, options);
|
|
843
|
-
}
|
|
844
|
-
static make(attributes = {}) {
|
|
845
|
-
const instance = new this();
|
|
846
|
-
for (const attribute in attributes) {
|
|
847
|
-
if (typeof instance[getRelationMethod(attribute)] !== "function") {
|
|
848
|
-
instance.setAttribute(attribute, attributes[attribute]);
|
|
849
|
-
} else {
|
|
850
|
-
const relation = instance[getRelationMethod(attribute)]();
|
|
851
|
-
if (relation instanceof has_one_default || relation instanceof belongs_to_default) {
|
|
852
|
-
instance.setRelation(attribute, relation.related.make(attributes[attribute]));
|
|
853
|
-
} else if ((relation instanceof has_many_default || relation instanceof belongs_to_many_default) && Array.isArray(attributes[attribute])) {
|
|
854
|
-
instance.setRelation(attribute, new collection_default(attributes[attribute].map((item) => relation.related.make(item))));
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
}
|
|
858
|
-
return instance;
|
|
859
|
-
}
|
|
860
|
-
bootIfNotBooted() {
|
|
861
|
-
if (this.constructor._booted[this.constructor.name] === void 0) {
|
|
862
|
-
this.constructor._booted[this.constructor.name] = true;
|
|
863
|
-
this.constructor.booting();
|
|
864
|
-
this.initialize();
|
|
865
|
-
this.constructor.boot();
|
|
866
|
-
this.constructor.booted();
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
static booting() {
|
|
870
|
-
}
|
|
871
|
-
static boot() {
|
|
872
|
-
}
|
|
873
|
-
static booted() {
|
|
874
|
-
}
|
|
875
|
-
static setConnectionResolver(resolver) {
|
|
876
|
-
this.resolver = resolver;
|
|
877
|
-
}
|
|
878
|
-
initialize() {
|
|
879
|
-
}
|
|
880
|
-
initializePlugins() {
|
|
881
|
-
if (typeof this.constructor.pluginInitializers[this.constructor.name] === "undefined") {
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
|
-
for (const method of this.constructor.pluginInitializers[this.constructor.name]) {
|
|
885
|
-
this[method]();
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
addPluginInitializer(method) {
|
|
889
|
-
if (!this.constructor.pluginInitializers[this.constructor.name]) {
|
|
890
|
-
this.constructor.pluginInitializers[this.constructor.name] = [];
|
|
891
|
-
}
|
|
892
|
-
this.constructor.pluginInitializers[this.constructor.name].push(method);
|
|
893
|
-
}
|
|
894
|
-
newInstance(attributes = {}, exists = false) {
|
|
895
|
-
const model = new this.constructor();
|
|
896
|
-
model.exists = exists;
|
|
897
|
-
model.setTable(this.getTable());
|
|
898
|
-
model.fill(attributes);
|
|
899
|
-
return model;
|
|
900
|
-
}
|
|
901
|
-
asProxy() {
|
|
902
|
-
const handler = {
|
|
903
|
-
get: function(target, prop) {
|
|
904
|
-
if (target[prop] !== void 0) {
|
|
905
|
-
return target[prop];
|
|
906
|
-
}
|
|
907
|
-
if (typeof prop === "string") {
|
|
908
|
-
return target.getAttribute(prop);
|
|
909
|
-
}
|
|
910
|
-
},
|
|
911
|
-
set: function(target, prop, value) {
|
|
912
|
-
if (target[prop] !== void 0 && typeof target !== "function") {
|
|
913
|
-
target[prop] = value;
|
|
914
|
-
return target;
|
|
915
|
-
}
|
|
916
|
-
if (typeof prop === "string") {
|
|
917
|
-
return target.setAttribute(prop, value);
|
|
918
|
-
}
|
|
919
|
-
return target;
|
|
920
|
-
}
|
|
921
|
-
};
|
|
922
|
-
return new Proxy(this, handler);
|
|
923
|
-
}
|
|
924
|
-
getKey() {
|
|
925
|
-
return this.getAttribute(this.getKeyName());
|
|
926
|
-
}
|
|
927
|
-
getKeyName() {
|
|
928
|
-
return this.primaryKey;
|
|
929
|
-
}
|
|
930
|
-
getForeignKey() {
|
|
931
|
-
return snakeCase(this.constructor.name) + "_" + this.getKeyName();
|
|
932
|
-
}
|
|
933
|
-
getConnectionName() {
|
|
934
|
-
return this.connection;
|
|
935
|
-
}
|
|
936
|
-
getTable() {
|
|
937
|
-
return this.table || pluralize(snakeCase(this.constructor.name));
|
|
938
|
-
}
|
|
939
|
-
setConnection(connection) {
|
|
940
|
-
this.connection = connection;
|
|
941
|
-
return this;
|
|
942
|
-
}
|
|
943
|
-
getKeyType() {
|
|
944
|
-
return this.keyType;
|
|
945
|
-
}
|
|
946
|
-
hasNamedScope(name) {
|
|
947
|
-
const scope = getScopeMethod(name);
|
|
948
|
-
return typeof this[scope] === "function";
|
|
949
|
-
}
|
|
950
|
-
callNamedScope(scope, parameters) {
|
|
951
|
-
const scopeMethod = getScopeMethod(scope);
|
|
952
|
-
return this[scopeMethod](...parameters);
|
|
953
|
-
}
|
|
954
|
-
setTable(table) {
|
|
955
|
-
this.table = table;
|
|
956
|
-
return this;
|
|
957
|
-
}
|
|
958
|
-
newCollection(models = []) {
|
|
959
|
-
return new collection_default(models);
|
|
960
|
-
}
|
|
961
|
-
getIncrementing() {
|
|
962
|
-
return this.incrementing;
|
|
963
|
-
}
|
|
964
|
-
setIncrementing(value) {
|
|
965
|
-
this.incrementing = value;
|
|
966
|
-
return this;
|
|
967
|
-
}
|
|
968
|
-
toData() {
|
|
969
|
-
return merge(this.attributesToData(), this.relationsToData());
|
|
970
|
-
}
|
|
971
|
-
toJSON() {
|
|
972
|
-
return this.toData();
|
|
973
|
-
}
|
|
974
|
-
toJson(...args) {
|
|
975
|
-
return JSON.stringify(this.toData(), ...args);
|
|
976
|
-
}
|
|
977
|
-
toString() {
|
|
978
|
-
return this.toJson();
|
|
979
|
-
}
|
|
980
|
-
fill(attributes) {
|
|
981
|
-
for (const key in attributes) {
|
|
982
|
-
this.setAttribute(key, attributes[key]);
|
|
983
|
-
}
|
|
984
|
-
return this;
|
|
985
|
-
}
|
|
986
|
-
transacting(trx) {
|
|
987
|
-
this.trx = trx;
|
|
988
|
-
return this;
|
|
989
|
-
}
|
|
990
|
-
trashed() {
|
|
991
|
-
return this[this.getDeletedAtColumn()] !== null;
|
|
992
|
-
}
|
|
993
|
-
newPivot(parent, attributes, table, exists, using = null) {
|
|
994
|
-
return using ? using.fromRawAttributes(parent, attributes, table, exists) : Pivot.fromAttributes(parent, attributes, table, exists);
|
|
995
|
-
}
|
|
996
|
-
qualifyColumn(column) {
|
|
997
|
-
if (column.includes(".")) {
|
|
998
|
-
return column;
|
|
999
|
-
}
|
|
1000
|
-
return `${this.getTable()}.${column}`;
|
|
1001
|
-
}
|
|
1002
|
-
getQualifiedKeyName() {
|
|
1003
|
-
return this.qualifyColumn(this.getKeyName());
|
|
1004
|
-
}
|
|
1005
|
-
is(model) {
|
|
1006
|
-
return model && model instanceof _Model && this.getKey() === model.getKey() && this.getTable() === model.getTable();
|
|
1007
|
-
}
|
|
1008
|
-
isNot(model) {
|
|
1009
|
-
return !this.is(model);
|
|
1010
|
-
}
|
|
1011
|
-
};
|
|
1012
|
-
var Pivot = class extends Model {
|
|
1013
|
-
incrementing = false;
|
|
1014
|
-
guarded = [];
|
|
1015
|
-
pivotParent = null;
|
|
1016
|
-
foreignKey = null;
|
|
1017
|
-
relatedKey = null;
|
|
1018
|
-
setPivotKeys(foreignKey, relatedKey) {
|
|
1019
|
-
this.foreignKey = foreignKey;
|
|
1020
|
-
this.relatedKey = relatedKey;
|
|
1021
|
-
return this;
|
|
1022
|
-
}
|
|
1023
|
-
static fromRawAttributes(parent, attributes, table, exists = false) {
|
|
1024
|
-
const instance = this.fromAttributes(parent, {}, table, exists);
|
|
1025
|
-
instance.timestamps = instance.hasTimestampAttributes(attributes);
|
|
1026
|
-
instance.attributes = attributes;
|
|
1027
|
-
instance.exists = exists;
|
|
1028
|
-
return instance;
|
|
1029
|
-
}
|
|
1030
|
-
static fromAttributes(parent, attributes, table, exists = false) {
|
|
1031
|
-
const instance = new this();
|
|
1032
|
-
instance.timestamps = instance.hasTimestampAttributes(attributes);
|
|
1033
|
-
instance.setConnection(parent.connection).setTable(table).fill(attributes).syncOriginal();
|
|
1034
|
-
instance.pivotParent = parent;
|
|
1035
|
-
instance.exists = exists;
|
|
1036
|
-
return instance;
|
|
1037
|
-
}
|
|
1038
|
-
hasTimestampAttributes(attributes = null) {
|
|
1039
|
-
return (attributes || this.attributes)[this.constructor.CREATED_AT] !== void 0;
|
|
1040
|
-
}
|
|
1041
|
-
};
|
|
1042
|
-
var model_default = Model;
|
|
1043
|
-
|
|
1044
|
-
// src/browser/collection.ts
|
|
1045
|
-
var Collection = class _Collection extends BaseCollection {
|
|
1046
|
-
mapThen(callback) {
|
|
1047
|
-
return Promise.all(this.map(callback));
|
|
1048
|
-
}
|
|
1049
|
-
modelKeys() {
|
|
1050
|
-
return this.all().map((item) => item.getKey());
|
|
1051
|
-
}
|
|
1052
|
-
contains(key, operator, value) {
|
|
1053
|
-
if (arguments.length > 1) {
|
|
1054
|
-
return super.contains(key, value ?? operator);
|
|
1055
|
-
}
|
|
1056
|
-
if (key instanceof model_default) {
|
|
1057
|
-
return super.contains((model) => {
|
|
1058
|
-
return model.is(key);
|
|
1059
|
-
});
|
|
1060
|
-
}
|
|
1061
|
-
return super.contains((model) => {
|
|
1062
|
-
return model.getKey() == key;
|
|
1063
|
-
});
|
|
1064
|
-
}
|
|
1065
|
-
diff(items) {
|
|
1066
|
-
const diff = new this.constructor();
|
|
1067
|
-
const dictionary = this.getDictionary(items);
|
|
1068
|
-
this.items.map((item) => {
|
|
1069
|
-
if (dictionary[item.getKey()] === void 0) {
|
|
1070
|
-
diff.add(item);
|
|
1071
|
-
}
|
|
1072
|
-
});
|
|
1073
|
-
return diff;
|
|
1074
|
-
}
|
|
1075
|
-
except(keys) {
|
|
1076
|
-
const dictionary = omit3(this.getDictionary(), keys);
|
|
1077
|
-
return new this.constructor(Object.values(dictionary));
|
|
1078
|
-
}
|
|
1079
|
-
intersect(items) {
|
|
1080
|
-
const intersect = new this.constructor();
|
|
1081
|
-
if (isEmpty(items)) {
|
|
1082
|
-
return intersect;
|
|
1083
|
-
}
|
|
1084
|
-
const dictionary = this.getDictionary(items);
|
|
1085
|
-
for (const item of this.items) {
|
|
1086
|
-
if (dictionary[item.getKey()] !== void 0) {
|
|
1087
|
-
intersect.add(item);
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
return intersect;
|
|
1091
|
-
}
|
|
1092
|
-
unique(key, _strict = false) {
|
|
1093
|
-
if (key) {
|
|
1094
|
-
return super.unique(key);
|
|
1095
|
-
}
|
|
1096
|
-
return new this.constructor(Object.values(this.getDictionary()));
|
|
1097
|
-
}
|
|
1098
|
-
find(key, defaultValue = null) {
|
|
1099
|
-
if (key instanceof model_default) {
|
|
1100
|
-
key = key.getKey();
|
|
1101
|
-
}
|
|
1102
|
-
if (isArray2(key)) {
|
|
1103
|
-
if (this.isEmpty()) {
|
|
1104
|
-
return new this.constructor();
|
|
1105
|
-
}
|
|
1106
|
-
return this.whereIn(this.first().getKeyName(), key);
|
|
1107
|
-
}
|
|
1108
|
-
collect2(this.items).first((model) => {
|
|
1109
|
-
return model.getKey() == key;
|
|
1110
|
-
});
|
|
1111
|
-
return this.items.filter((model) => {
|
|
1112
|
-
return model.getKey() == key;
|
|
1113
|
-
})[0] || defaultValue;
|
|
1114
|
-
}
|
|
1115
|
-
makeVisible(attributes) {
|
|
1116
|
-
return this.each((item) => {
|
|
1117
|
-
item.makeVisible(attributes);
|
|
1118
|
-
});
|
|
1119
|
-
}
|
|
1120
|
-
makeHidden(attributes) {
|
|
1121
|
-
return this.each((item) => {
|
|
1122
|
-
item.makeHidden(attributes);
|
|
1123
|
-
});
|
|
1124
|
-
}
|
|
1125
|
-
append(attributes) {
|
|
1126
|
-
return this.each((item) => {
|
|
1127
|
-
item.append(attributes);
|
|
1128
|
-
});
|
|
1129
|
-
}
|
|
1130
|
-
only(keys) {
|
|
1131
|
-
if (keys === null) {
|
|
1132
|
-
return new _Collection(this.items);
|
|
1133
|
-
}
|
|
1134
|
-
const dictionary = pick(this.getDictionary(), keys);
|
|
1135
|
-
return new this.constructor(Object.values(dictionary));
|
|
1136
|
-
}
|
|
1137
|
-
getDictionary(items) {
|
|
1138
|
-
items = !items ? this.items : items;
|
|
1139
|
-
const dictionary = {};
|
|
1140
|
-
items.map((value) => {
|
|
1141
|
-
dictionary[value.getKey()] = value;
|
|
1142
|
-
});
|
|
1143
|
-
return dictionary;
|
|
1144
|
-
}
|
|
1145
|
-
toData() {
|
|
1146
|
-
return this.all().map((item) => typeof item.toData == "function" ? item.toData() : item);
|
|
1147
|
-
}
|
|
1148
|
-
toJSON() {
|
|
1149
|
-
return this.toData();
|
|
1150
|
-
}
|
|
1151
|
-
toJson(...args) {
|
|
1152
|
-
return JSON.stringify(this.toData(), ...args);
|
|
1153
|
-
}
|
|
1154
|
-
[Symbol.iterator] = () => {
|
|
1155
|
-
const items = this.items;
|
|
1156
|
-
const length = this.items.length;
|
|
1157
|
-
let n = 0;
|
|
1158
|
-
return {
|
|
1159
|
-
next() {
|
|
1160
|
-
return n < length ? {
|
|
1161
|
-
value: items[n++],
|
|
1162
|
-
done: false
|
|
1163
|
-
} : {
|
|
1164
|
-
done: true
|
|
1165
|
-
};
|
|
1166
|
-
}
|
|
1167
|
-
};
|
|
1168
|
-
};
|
|
1169
|
-
};
|
|
1170
|
-
var collection_default = Collection;
|
|
1171
|
-
|
|
1172
|
-
// src/concerns/has-unique-ids.ts
|
|
1173
|
-
var HasUniqueIds = (Model2) => {
|
|
1174
|
-
return class extends Model2 {
|
|
1175
|
-
useUniqueIds = true;
|
|
1176
|
-
uniqueIds() {
|
|
1177
|
-
return [this.getKeyName()];
|
|
1178
|
-
}
|
|
1179
|
-
getKeyType() {
|
|
1180
|
-
if (this.uniqueIds().includes(this.getKeyName())) {
|
|
1181
|
-
return "string";
|
|
1182
|
-
}
|
|
1183
|
-
return this.keyType;
|
|
1184
|
-
}
|
|
1185
|
-
getIncrementing() {
|
|
1186
|
-
if (this.uniqueIds().includes(this.getKeyName())) {
|
|
1187
|
-
return false;
|
|
1188
|
-
}
|
|
1189
|
-
return this.incrementing;
|
|
1190
|
-
}
|
|
1191
|
-
};
|
|
1192
|
-
};
|
|
1193
|
-
var has_unique_ids_default = HasUniqueIds;
|
|
1194
|
-
|
|
1195
|
-
// src/browser/paginator.ts
|
|
1196
|
-
var Paginator = class {
|
|
1197
|
-
static formatter;
|
|
1198
|
-
_items;
|
|
1199
|
-
_total;
|
|
1200
|
-
_perPage;
|
|
1201
|
-
_lastPage;
|
|
1202
|
-
_currentPage;
|
|
1203
|
-
hasMore = false;
|
|
1204
|
-
options = {};
|
|
1205
|
-
constructor(items, total, perPage, currentPage = 1, options = {}) {
|
|
1206
|
-
this.options = options;
|
|
1207
|
-
for (const key in options) {
|
|
1208
|
-
const value = options[key];
|
|
1209
|
-
this[key] = value;
|
|
1210
|
-
}
|
|
1211
|
-
this._items = new collection_default([]);
|
|
1212
|
-
this._total = total;
|
|
1213
|
-
this._perPage = parseInt(String(perPage));
|
|
1214
|
-
this._lastPage = Math.max(Math.ceil(total / perPage), 1);
|
|
1215
|
-
this._currentPage = currentPage;
|
|
1216
|
-
this.setItems(items);
|
|
1217
|
-
}
|
|
1218
|
-
static setFormatter(formatter) {
|
|
1219
|
-
if (typeof formatter !== "function" && formatter !== null && formatter !== void 0) {
|
|
1220
|
-
throw new Error("Paginator formatter must be a function or null");
|
|
1221
|
-
}
|
|
1222
|
-
this.formatter = formatter;
|
|
1223
|
-
}
|
|
1224
|
-
setItems(items) {
|
|
1225
|
-
this._items = items instanceof collection_default ? items : new collection_default(items);
|
|
1226
|
-
this.hasMore = this._items.count() > this._perPage;
|
|
1227
|
-
this._items = this._items.slice(0, this._perPage);
|
|
1228
|
-
}
|
|
1229
|
-
firstItem() {
|
|
1230
|
-
return this.count() > 0 ? (this._currentPage - 1) * this._perPage + 1 : null;
|
|
1231
|
-
}
|
|
1232
|
-
lastItem() {
|
|
1233
|
-
return this.count() > 0 ? (this.firstItem() ?? 0) + this.count() - 1 : null;
|
|
1234
|
-
}
|
|
1235
|
-
hasMorePages() {
|
|
1236
|
-
return this._currentPage < this._lastPage;
|
|
1237
|
-
}
|
|
1238
|
-
get(index) {
|
|
1239
|
-
return this._items.get(index);
|
|
1240
|
-
}
|
|
1241
|
-
count() {
|
|
1242
|
-
return this._items.count();
|
|
1243
|
-
}
|
|
1244
|
-
items() {
|
|
1245
|
-
return this._items;
|
|
1246
|
-
}
|
|
1247
|
-
map(callback) {
|
|
1248
|
-
return this._items.map(callback);
|
|
1249
|
-
}
|
|
1250
|
-
currentPage() {
|
|
1251
|
-
return this._currentPage;
|
|
1252
|
-
}
|
|
1253
|
-
onFirstPage() {
|
|
1254
|
-
return this._currentPage === 1;
|
|
1255
|
-
}
|
|
1256
|
-
perPage() {
|
|
1257
|
-
return this._perPage;
|
|
1258
|
-
}
|
|
1259
|
-
lastPage() {
|
|
1260
|
-
return this._lastPage;
|
|
1261
|
-
}
|
|
1262
|
-
total() {
|
|
1263
|
-
return this._total;
|
|
1264
|
-
}
|
|
1265
|
-
toData() {
|
|
1266
|
-
if (this.constructor.formatter && typeof this.constructor.formatter === "function") {
|
|
1267
|
-
return this.constructor.formatter(this);
|
|
1268
|
-
}
|
|
1269
|
-
return {
|
|
1270
|
-
current_page: this._currentPage,
|
|
1271
|
-
data: this._items.toData(),
|
|
1272
|
-
per_page: this._perPage,
|
|
1273
|
-
total: this._total,
|
|
1274
|
-
last_page: this._lastPage,
|
|
1275
|
-
count: this.count()
|
|
1276
|
-
};
|
|
1277
|
-
}
|
|
1278
|
-
toJSON() {
|
|
1279
|
-
return this.toData();
|
|
1280
|
-
}
|
|
1281
|
-
toJson(...args) {
|
|
1282
|
-
return JSON.stringify(this.toData(), ...args);
|
|
1283
|
-
}
|
|
1284
|
-
};
|
|
1285
|
-
var paginator_default = Paginator;
|
|
1286
|
-
|
|
1287
|
-
// src/browser/pivot.ts
|
|
1288
|
-
var pivot_default = Pivot;
|
|
1289
|
-
|
|
1290
|
-
// src/browser/index.ts
|
|
1291
|
-
import { isArray as isArray3 } from "radashi";
|
|
1292
|
-
var make = (model, data, options = {}) => {
|
|
1293
|
-
const { paginated } = options;
|
|
1294
|
-
if (paginated) {
|
|
1295
|
-
return new paginator_default(
|
|
1296
|
-
data.data.map((item) => model.make(item)),
|
|
1297
|
-
data.total,
|
|
1298
|
-
data.per_page,
|
|
1299
|
-
data.current_page
|
|
1300
|
-
);
|
|
1301
|
-
}
|
|
1302
|
-
if (isArray3(data)) {
|
|
1303
|
-
return new collection_default(data.map((item) => model.make(item)));
|
|
1304
|
-
}
|
|
1305
|
-
return model.make(data);
|
|
1306
|
-
};
|
|
1307
|
-
var makeCollection = (model, data) => new collection_default(data.map((item) => model.make(item)));
|
|
1308
|
-
var makePaginator = (model, data) => new paginator_default(
|
|
1309
|
-
data.data.map((item) => model.make(item)),
|
|
1310
|
-
data.total,
|
|
1311
|
-
data.per_page,
|
|
1312
|
-
data.current_page
|
|
1313
|
-
);
|
|
1314
|
-
var isBrowser = true;
|
|
1315
|
-
var index_default = {
|
|
1316
|
-
isBrowser,
|
|
1317
|
-
Paginator: paginator_default,
|
|
1318
|
-
Collection: collection_default,
|
|
1319
|
-
Model: model_default,
|
|
1320
|
-
Pivot: pivot_default,
|
|
1321
|
-
Attribute: attribute_default,
|
|
1322
|
-
CastsAttributes: casts_attributes_default,
|
|
1323
|
-
HasUniqueIds: has_unique_ids_default,
|
|
1324
|
-
make,
|
|
1325
|
-
makeCollection,
|
|
1326
|
-
makePaginator
|
|
1327
|
-
};
|
|
1328
|
-
export {
|
|
1329
|
-
attribute_default as Attribute,
|
|
1330
|
-
casts_attributes_default as CastsAttributes,
|
|
1331
|
-
collection_default as Collection,
|
|
1332
|
-
has_unique_ids_default as HasUniqueIds,
|
|
1333
|
-
InvalidArgumentError,
|
|
1334
|
-
model_default as Model,
|
|
1335
|
-
ModelNotFoundError,
|
|
1336
|
-
paginator_default as Paginator,
|
|
1337
|
-
pivot_default as Pivot,
|
|
1338
|
-
RelationNotFoundError,
|
|
1339
|
-
compose,
|
|
1340
|
-
index_default as default,
|
|
1341
|
-
defineConfig,
|
|
1342
|
-
flattenDeep,
|
|
1343
|
-
getAttrMethod,
|
|
1344
|
-
getAttrName,
|
|
1345
|
-
getGetterMethod,
|
|
1346
|
-
getRelationMethod,
|
|
1347
|
-
getRelationName,
|
|
1348
|
-
getScopeMethod,
|
|
1349
|
-
getScopeName,
|
|
1350
|
-
getSetterMethod,
|
|
1351
|
-
isBrowser,
|
|
1352
|
-
kebabCase,
|
|
1353
|
-
make,
|
|
1354
|
-
makeCollection,
|
|
1355
|
-
makePaginator,
|
|
1356
|
-
now,
|
|
1357
|
-
snakeCase,
|
|
1358
|
-
tap
|
|
1359
|
-
};
|
|
1
|
+
import t,{Collection as e,collect as s}from"collect.js";import{omit as i,flat as r,diff as n,trim as a,camel as o,snake as u,dash as h,isArray as c,assign as l,isEmpty as g,pick as m}from"radashi";import d from"dayjs/plugin/advancedFormat.js";import p from"dayjs";import y from"pluralize";var f=Object.defineProperty,b=class t{get;set;withCaching=!1;withObjectCaching=!0;constructor({get:t=null,set:e=null}){this.get=t,this.set=e}static make({get:e=null,set:s=null}){return new t({get:e,set:s})}static get(e){return new t({get:e})}static set(e){return new t({set:e})}withoutObjectCaching(){return this.withObjectCaching=!1,this}shouldCache(){return this.withCaching=!0,this}},D=class t{constructor(){if(this.constructor===t)throw new Error("CastsAttributes cannot be instantiated")}static get(t,e,s,i){throw new Error("get not implemented")}static set(t,e,s,i){throw new Error("set not implemented")}},C={};function K(t,...e){return e.reduce((t,e)=>"function"==typeof e&&e.prototype?class extends t{constructor(...t){super(...t),Object.getOwnPropertyNames(e.prototype).forEach(t=>{"constructor"!==t&&Object.defineProperty(this,t,Object.getOwnPropertyDescriptor(e.prototype,t))})}}:"function"==typeof e?e(t):t,t)}((t,e)=>{for(var s in e)f(t,s,{get:e[s],enumerable:!0})})(C,{compose:()=>K}),p.extend(d);var w=(t="YYYY-MM-DD HH:mm:ss")=>p().format(t),T=t=>u(t.substring(8)),A=t=>u(t.substring(5)),_=t=>o(`relation_${t}`),P=t=>o(`scope_${t}`),v=t=>o(`attribute_${t}`),x=t=>o(`get_${t}_attribute`),N=t=>o(`set_${t}_attribute`),O=t=>t.substring(3,t.length-9).toLowerCase(),I=(t,e)=>{const s=e(t);return s instanceof Promise?s.then(()=>t):t},{compose:S}=C,j=t=>Array.isArray(t)?t.reduce((t,e)=>t.concat(j(e)),[]):[t],k=t=>a(h(t.replace(/[^a-zA-Z0-9_-]/g,"-")),"_-"),E=t=>a(u(t.replace(/[^a-zA-Z0-9_-]/g,"-")),"_-"),F=t=>t,M=class{parent;related;eagerKeysWereEmpty=!1;static constraints=!0;constructor(t,e){this.parent=e,this.related=t}asProxy(){return new Proxy(this,{get:function(t,e){return void 0!==t[e]?t[e]:"string"==typeof e?()=>t.asProxy():void 0}})}getRelated(){return this.related}},z=class extends M{foreignKey;ownerKey;child;relationName;constructor(t,e,s,i,r){return super(t,e),this.foreignKey=s,this.ownerKey=i,this.child=e,this.relationName=r,this.asProxy()}},R=class extends M{table;foreignPivotKey;relatedPivotKey;parentKey;relatedKey;pivotColumns=[];pivotValues=[];pivotWheres=[];pivotWhereIns=[];pivotWhereNulls=[];accessor="pivot";using;pivotCreatedAt;pivotUpdatedAt;constructor(t,e,s,i,r,n,a){return super(t,e),this.table=s,this.foreignPivotKey=i,this.relatedPivotKey=r,this.parentKey=n,this.relatedKey=a,this.asProxy()}},q=e=>class extends e{static castTypeCache={};attributes={};original={};casts={};changes={};appends=[];setAppends(t){return this.appends=t,this}append(...t){const e=j(t);return this.appends=[...this.appends,...e],this}normalizeCastClassResponse(t,e){return"Object"===e?.constructor?.name?e:{[t]:e}}syncOriginal(){return this.original=this.getAttributes(),this}syncChanges(){return this.changes=this.getDirty(),this}syncOriginalAttribute(t){this.syncOriginalAttributes(t)}syncOriginalAttributes(...t){t=j(t);const e=this.getAttributes();for(const s of t)this.original[s]=e[s];return this}isDirty(...t){const e=this.getDirty();if(0===(t=j(t)).length)return Object.keys(e).length>0;for(const s of t)if(s in e)return!0;return!1}getDirty(){const t={},e=this.getAttributes();for(const s in e){const i=e[s];this.originalIsEquivalent(s)||(t[s]=i)}return t}originalIsEquivalent(t){if(void 0===this.original[t])return!1;return this.attributes[t]===this.original[t]}setAttributes(t){this.attributes={...t}}setRawAttributes(t,e=!1){return this.attributes=t,e&&this.syncOriginal(),this}getAttributes(){return{...this.attributes}}setAttribute(t,e){const s=N(t);if("function"==typeof this[s])return this[s](e),this;const i=v(t);if("function"==typeof this[i]){const s=this[i]().set||(e=>{this.attributes[t]=e});return this.attributes={...this.attributes,...this.normalizeCastClassResponse(t,s(e,this.attributes))},this}const r=this.getCasts()[t];return this.isCustomCast(r)&&"string"!=typeof r&&(e=r.set(this,t,e,this.attributes)??""),"json"===r&&(e=JSON.stringify(e)),"collection"===r&&(e=JSON.stringify(e)),null!==e&&this.isDateAttribute(t)&&(e=this.fromDateTime(e)),this.attributes[t]=e,this}getAttribute(t){if(!t)return;const e=x(t);if("function"==typeof this[e])return this[e](this.attributes[t],this.attributes);const s=v(t);if("function"==typeof this[s]){return this[s]().get(this.attributes[t],this.attributes)}return t in this.attributes?this.hasCast(t)?this.castAttribute(t,this.attributes[t]):this.getDates().includes(t)?this.asDateTime(this.attributes[t]):this.attributes[t]:t in this.relations?this.relations[t]:void 0}castAttribute(e,s){const i=this.getCastType(e);if(!i)return s;if(null===s)return s;switch(i){case"int":case"integer":return parseInt(s);case"real":case"float":case"double":return parseFloat(s);case"decimal":return this.asDecimal(s,i.split(":")[1]);case"string":return String(s);case"bool":case"boolean":return Boolean(s);case"object":case"json":try{return JSON.parse(s)}catch{return null}case"collection":try{return t(JSON.parse(s))}catch{return t([])}case"date":return this.asDate(s);case"datetime":case"custom_datetime":return this.asDateTime(s);case"timestamp":return this.asTimestamp(s)}return this.isCustomCast(i)?i.get(this,e,s,this.attributes):s}attributesToData(){let t={...this.attributes};for(const e in t)this.hidden.includes(e)&&(t=i(t,[e])),this.visible.length>0&&!1===this.visible.includes(e)&&(t=i(t,[e]));for(const e of this.getDates())void 0!==t[e]&&(t[e]=this.serializeDate(this.asDateTime(t[e])));const e=this.getCasts();for(const s in e){const i=e[s];s in t!=!1&&(t[s]=this.castAttribute(s,t[s]),s in t&&["date","datetime"].includes(String(i))&&(t[s]=this.serializeDate(t[s])),s in t&&this.isCustomDateTimeCast(i)&&(t[s]=p(t[s]).format(String(i).split(":")[1])))}for(const e of this.appends)t[e]=this.mutateAttribute(e,null);return t}mutateAttribute(t,e){if("function"==typeof this[x(t)])return this[x(t)](e);if("function"==typeof this[v(t)]){return this[v(t)]().get(t,this.attributes)}return t in this?this[t]:e}mutateAttributeForArray(t,e){}isDateAttribute(t){return this.getDates().includes(t)||this.isDateCastable(t)}serializeDate(t){return t?p(t).toISOString():null}getDates(){return this.usesTimestamps()?[this.getCreatedAtColumn(),this.getUpdatedAtColumn()]:[]}getCasts(){return this.getIncrementing()?{[this.getKeyName()]:this.getKeyType(),...this.casts}:this.casts}getCastType(t){const e=this.getCasts()[t];let s,i;return"string"==typeof e?s=e:new e instanceof D&&(s=e.name),s&&void 0!==this.getConstructor().castTypeCache[s]?this.getConstructor().castTypeCache[s]:(i=this.isCustomDateTimeCast(e)?"custom_datetime":this.isDecimalCast(e)?"decimal":this.isCustomCast(e)?e:String(e).toLocaleLowerCase().trim(),this.getConstructor()[s]=i)}hasCast(t,e=[]){return t in this.casts&&(!((e=r(e)).length>0)||e.includes(this.getCastType(t)))}withDayjs(t){return p(t)}isCustomCast(t){return"function"==typeof t&&new t instanceof D}isCustomDateTimeCast(t){return"string"==typeof t&&(t.startsWith("date:")||t.startsWith("datetime:"))}isDecimalCast(t){return"string"==typeof t&&t.startsWith("decimal:")}isDateCastable(t){return this.hasCast(t,["date","datetime"])}fromDateTime(t){return p(this.asDateTime(t)).format(this.getDateFormat())}getDateFormat(){return this.dateFormat||"YYYY-MM-DD HH:mm:ss"}asDecimal(t,e){return parseFloat(t).toFixed(e)}asDateTime(t){return null===t?null:t instanceof Date?t:"number"==typeof t?new Date(1e3*t):new Date(t)}asDate(t){const e=this.asDateTime(t);return p(e).startOf("day").toDate()}},J=class extends M{foreignKey;localKey;constructor(t,e,s,i){return super(t,e),this.foreignKey=s,this.localKey=i,this.asProxy()}},H=class extends M{foreignKey;localKey;constructor(t,e,s,i){return super(t,e),this.foreignKey=s,this.localKey=i,this.asProxy()}},U=class extends M{throughParent;farParent;firstKey;secondKey;localKey;secondLocalKey;constructor(t,e,s,i,r,n,a){return super(t,s),this.localKey=n,this.firstKey=i,this.secondKey=r,this.farParent=e,this.throughParent=s,this.secondLocalKey=a,this.asProxy()}},Y=class extends U{},$=class extends Error{constructor(t,e){super(t),Error.captureStackTrace(this,this.constructor),this.name=this.constructor.name,this.message=t}},L=class extends ${model;ids=[];constructor(){super("")}setModel(t,e=[]){return this.model=t,this.ids=c(e)?e:[e],this.message=`No query results for model [${t}]`,this.ids.length>0?this.message+=" "+this.ids.join(", "):this.message+=".",this}getModel(){return this.model}getIds(){return this.ids}},W=class extends ${},B=class extends ${},V=t=>class extends t{relations={};getRelation(t){return this.relations[t]}setRelation(t,e){return this.relations[t]=e,this}unsetRelation(t){return this.relations=i(this.relations,[t]),this}relationLoaded(t){return void 0!==this.relations[t]}related(t){if("function"!=typeof this[_(t)]){const e=`Model [${this.constructor.name}]'s relation [${t}] doesn't exist.`;throw new W(e)}return this[_(t)]()}async getRelated(t){return await this.related(t).getResults()}relationsToData(){const t={};for(const e in this.relations)this.hidden.includes(e)||this.visible.length>0&&!1===this.visible.includes(e)||(t[e]=this.relations[e]instanceof Array?this.relations[e].map(t=>t.toData()):null===this.relations[e]?null:this.relations[e].toData());return t}guessBelongsToRelation(){const t=new Error,e=t.stack?.split("\n")[2],s=e?.split(" ")[5];return T(s)}joiningTable(t,e=null){return[e?e.joiningTableSegment():E(t.name),this.joiningTableSegment()].sort().join("_").toLocaleLowerCase()}joiningTableSegment(){return E(this.constructor.name)}hasOne(t,e=null,s=null){const i=new t;return e=e||this.getForeignKey(),s=s||this.getKeyName(),new H(t,this,i.getTable()+"."+e,s)}hasMany(t,e=null,s=null){const i=new t;return e=e||this.getForeignKey(),s=s||this.getKeyName(),new J(t,this,i.getTable()+"."+e,s)}belongsTo(t,e=null,s=null,i=null){const r=new t;return e=e||r.getForeignKey(),s=s||r.getKeyName(),i=i||this.guessBelongsToRelation(),new z(t,this,e,s,i)}belongsToMany(t,e=null,s=null,i=null,r=null,n=null){const a=new t,o=t.query();return e=e||this.joiningTable(t,a),s=s||this.getForeignKey(),i=i||a.getForeignKey(),r=r||this.getKeyName(),n=n||a.getKeyName(),new R(o,this,e,s,i,r,n)}hasOneThrough(t,e,s=null,i=null,r=null,n=null){e=new e;const a=t.query();return s=s||this.getForeignKey(),i=i||e.getForeignKey(),new Y(a,this,e,s,i,r||this.getKeyName(),n||e.getKeyName())}hasManyThrough(t,e,s=null,i=null,r=null,n=null){e=new e;const a=t.query();return s=s||this.getForeignKey(),i=i||e.getForeignKey(),new U(a,this,e,s,i,r||this.getKeyName(),n||e.getKeyName())}},Z=t=>class extends t{static CREATED_AT="created_at";static UPDATED_AT="updated_at";static DELETED_AT="deleted_at";timestamps=!0;dateFormat="YYYY-MM-DD HH:mm:ss";usesTimestamps(){return this.timestamps}updateTimestamps(){const t=this.freshTimestampString(),e=this.getUpdatedAtColumn();e&&!this.isDirty(e)&&this.setUpdatedAt(t);const s=this.getCreatedAtColumn();return this.exists||!s||this.isDirty(s)||this.setCreatedAt(t),this}getCreatedAtColumn(){return this.constructor.CREATED_AT}getUpdatedAtColumn(){return this.constructor.UPDATED_AT}setCreatedAt(t){return this.attributes[this.getCreatedAtColumn()]=t,this}setUpdatedAt(t){return this.attributes[this.getUpdatedAtColumn()]=t,this}freshTimestamp(){const t=new Date;return t.setMilliseconds(0),t}freshTimestampString(){return this.fromDateTime(this.freshTimestamp())}},Q=t=>class extends t{hidden=[];visible=[];makeVisible(...t){const e=j(t);return this.visible.length>0&&(this.visible=[...this.visible,...e]),this.hidden=n(this.hidden,e),this}makeHidden(t,...e){const s=j([...t,...e]);return this.hidden.length>0&&(this.hidden=[...this.hidden,...s]),this}getHidden(){return this.hidden}getVisible(){return this.visible}setHidden(t){return this.hidden=t,this}setVisible(t){return this.visible=t,this}},G=S(class{},q,Q,V,Z),X=class t extends G{primaryKey="id";perPage=15;static globalScopes={};static pluginInitializers={};static _booted={};static resolver;static browser=!0;connection=null;constructor(t={}){return super(),this.bootIfNotBooted(),this.initializePlugins(),this.syncOriginal(),this.fill(t),this.asProxy()}static init(t={}){return new this(t)}static extend(t,e){t(this,e)}static make(t={}){const e=new this;for(const s in t)if("function"!=typeof e[_(s)])e.setAttribute(s,t[s]);else{const i=e[_(s)]();i instanceof H||i instanceof z?e.setRelation(s,i.related.make(t[s])):(i instanceof J||i instanceof R)&&Array.isArray(t[s])&&e.setRelation(s,new it(t[s].map(t=>i.related.make(t))))}return e}bootIfNotBooted(){void 0===this.constructor._booted[this.constructor.name]&&(this.constructor._booted[this.constructor.name]=!0,this.constructor.booting(),this.initialize(),this.constructor.boot(),this.constructor.booted())}static booting(){}static boot(){}static booted(){}static setConnectionResolver(t){this.resolver=t}initialize(){}initializePlugins(){if(void 0!==this.constructor.pluginInitializers[this.constructor.name])for(const t of this.constructor.pluginInitializers[this.constructor.name])this[t]()}addPluginInitializer(t){this.constructor.pluginInitializers[this.constructor.name]||(this.constructor.pluginInitializers[this.constructor.name]=[]),this.constructor.pluginInitializers[this.constructor.name].push(t)}newInstance(t={},e=!1){const s=new this.constructor;return s.exists=e,s.setTable(this.getTable()),s.fill(t),s}asProxy(){return new Proxy(this,{get:function(t,e){return void 0!==t[e]?t[e]:"string"==typeof e?t.getAttribute(e):void 0},set:function(t,e,s){return void 0!==t[e]&&"function"!=typeof t?(t[e]=s,t):"string"==typeof e?t.setAttribute(e,s):t}})}getKey(){return this.getAttribute(this.getKeyName())}getKeyName(){return this.primaryKey}getForeignKey(){return E(this.constructor.name)+"_"+this.getKeyName()}getConnectionName(){return this.connection}getTable(){return this.table||y(E(this.constructor.name))}setConnection(t){return this.connection=t,this}getKeyType(){return this.keyType}hasNamedScope(t){return"function"==typeof this[P(t)]}callNamedScope(t,e){return this[P(t)](...e)}setTable(t){return this.table=t,this}newCollection(t=[]){return new it(t)}getIncrementing(){return this.incrementing}setIncrementing(t){return this.incrementing=t,this}toData(){return l(this.attributesToData(),this.relationsToData())}toJSON(){return this.toData()}toJson(...t){return JSON.stringify(this.toData(),...t)}toString(){return this.toJson()}fill(t){for(const e in t)this.setAttribute(e,t[e]);return this}transacting(t){return this.trx=t,this}trashed(){return null!==this[this.getDeletedAtColumn()]}newPivot(t,e,s,i,r=null){return r?r.fromRawAttributes(t,e,s,i):tt.fromAttributes(t,e,s,i)}qualifyColumn(t){return t.includes(".")?t:`${this.getTable()}.${t}`}getQualifiedKeyName(){return this.qualifyColumn(this.getKeyName())}is(e){return e&&e instanceof t&&this.getKey()===e.getKey()&&this.getTable()===e.getTable()}isNot(t){return!this.is(t)}},tt=class extends X{incrementing=!1;guarded=[];pivotParent=null;foreignKey=null;relatedKey=null;setPivotKeys(t,e){return this.foreignKey=t,this.relatedKey=e,this}static fromRawAttributes(t,e,s,i=!1){const r=this.fromAttributes(t,{},s,i);return r.timestamps=r.hasTimestampAttributes(e),r.attributes=e,r.exists=i,r}static fromAttributes(t,e,s,i=!1){const r=new this;return r.timestamps=r.hasTimestampAttributes(e),r.setConnection(t.connection).setTable(s).fill(e).syncOriginal(),r.pivotParent=t,r.exists=i,r}hasTimestampAttributes(t=null){return void 0!==(t||this.attributes)[this.constructor.CREATED_AT]}},et=X,st=class t extends e{mapThen(t){return Promise.all(this.map(t))}modelKeys(){return this.all().map(t=>t.getKey())}contains(t,e,s){return arguments.length>1?super.contains(t,s??e):t instanceof et?super.contains(e=>e.is(t)):super.contains(e=>e.getKey()==t)}diff(t){const e=new this.constructor,s=this.getDictionary(t);return this.items.map(t=>{void 0===s[t.getKey()]&&e.add(t)}),e}except(t){const e=i(this.getDictionary(),t);return new this.constructor(Object.values(e))}intersect(t){const e=new this.constructor;if(g(t))return e;const s=this.getDictionary(t);for(const t of this.items)void 0!==s[t.getKey()]&&e.add(t);return e}unique(t,e=!1){return t?super.unique(t):new this.constructor(Object.values(this.getDictionary()))}find(t,e=null){return t instanceof et&&(t=t.getKey()),c(t)?this.isEmpty()?new this.constructor:this.whereIn(this.first().getKeyName(),t):(s(this.items).first(e=>e.getKey()==t),this.items.filter(e=>e.getKey()==t)[0]||e)}makeVisible(t){return this.each(e=>{e.makeVisible(t)})}makeHidden(t){return this.each(e=>{e.makeHidden(t)})}append(t){return this.each(e=>{e.append(t)})}only(e){if(null===e)return new t(this.items);const s=m(this.getDictionary(),e);return new this.constructor(Object.values(s))}getDictionary(t){t=t||this.items;const e={};return t.map(t=>{e[t.getKey()]=t}),e}toData(){return this.all().map(t=>"function"==typeof t.toData?t.toData():t)}toJSON(){return this.toData()}toJson(...t){return JSON.stringify(this.toData(),...t)}[Symbol.iterator]=()=>{const t=this.items,e=this.items.length;let s=0;return{next:()=>s<e?{value:t[s++],done:!1}:{done:!0}}}},it=st,rt=t=>class extends t{useUniqueIds=!0;uniqueIds(){return[this.getKeyName()]}getKeyType(){return this.uniqueIds().includes(this.getKeyName())?"string":this.keyType}getIncrementing(){return!this.uniqueIds().includes(this.getKeyName())&&this.incrementing}},nt=class{static formatter;_items;_total;_perPage;_lastPage;_currentPage;hasMore=!1;options={};constructor(t,e,s,i=1,r={}){this.options=r;for(const t in r){const e=r[t];this[t]=e}this._items=new it([]),this._total=e,this._perPage=parseInt(String(s)),this._lastPage=Math.max(Math.ceil(e/s),1),this._currentPage=i,this.setItems(t)}static setFormatter(t){if("function"!=typeof t&&null!=t)throw new Error("Paginator formatter must be a function or null");this.formatter=t}setItems(t){this._items=t instanceof it?t:new it(t),this.hasMore=this._items.count()>this._perPage,this._items=this._items.slice(0,this._perPage)}firstItem(){return this.count()>0?(this._currentPage-1)*this._perPage+1:null}lastItem(){return this.count()>0?(this.firstItem()??0)+this.count()-1:null}hasMorePages(){return this._currentPage<this._lastPage}get(t){return this._items.get(t)}count(){return this._items.count()}items(){return this._items}map(t){return this._items.map(t)}currentPage(){return this._currentPage}onFirstPage(){return 1===this._currentPage}perPage(){return this._perPage}lastPage(){return this._lastPage}total(){return this._total}toData(){return this.constructor.formatter&&"function"==typeof this.constructor.formatter?this.constructor.formatter(this):{current_page:this._currentPage,data:this._items.toData(),per_page:this._perPage,total:this._total,last_page:this._lastPage,count:this.count()}}toJSON(){return this.toData()}toJson(...t){return JSON.stringify(this.toData(),...t)}},at=tt,ot=(t,e,s={})=>{const{paginated:i}=s;return i?new nt(e.data.map(e=>t.make(e)),e.total,e.per_page,e.current_page):c(e)?new it(e.map(e=>t.make(e))):t.make(e)},ut=(t,e)=>new it(e.map(e=>t.make(e))),ht=!0,ct={isBrowser:ht,Paginator:nt,Collection:it,Model:et,Pivot:at,Attribute:b,CastsAttributes:D,HasUniqueIds:rt,make:ot,makeCollection:ut,makePaginator:(t,e)=>new nt(e.data.map(e=>t.make(e)),e.total,e.per_page,e.current_page)};export{b as Attribute,D as CastsAttributes,it as Collection,rt as HasUniqueIds,B as InvalidArgumentError,et as Model,L as ModelNotFoundError,nt as Paginator,at as Pivot,W as RelationNotFoundError,S as compose,ct as default,F as defineConfig,j as flattenDeep,v as getAttrMethod,O as getAttrName,x as getGetterMethod,_ as getRelationMethod,T as getRelationName,P as getScopeMethod,A as getScopeName,N as getSetterMethod,ht as isBrowser,k as kebabCase,ot as make,ut as makeCollection,w as now,E as snakeCase,I as tap};
|