@h3ravel/arquebus 0.1.4

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