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