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