@luminix/core 0.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +39 -0
- package/README.md +31 -0
- package/dist/contracts/Builder.d.ts +49 -0
- package/dist/contracts/Collection.d.ts +191 -0
- package/dist/contracts/ModelCollection.d.ts +8 -0
- package/dist/contracts/Plugin.d.ts +7 -0
- package/dist/contracts/PropertyBag.d.ts +28 -0
- package/dist/contracts/Relation/BelongsTo.d.ts +19 -0
- package/dist/contracts/Relation/BelongsToMany.d.ts +30 -0
- package/dist/contracts/Relation/HasMany.d.ts +20 -0
- package/dist/contracts/Relation/HasOne.d.ts +14 -0
- package/dist/contracts/Relation/HasOneOrMany.d.ts +9 -0
- package/dist/contracts/Relation/MorphMany.d.ts +20 -0
- package/dist/contracts/Relation/MorphOne.d.ts +14 -0
- package/dist/contracts/Relation/MorphOneOrMany.d.ts +9 -0
- package/dist/contracts/Relation/MorphTo.d.ts +7 -0
- package/dist/contracts/Relation/MorphToMany.d.ts +15 -0
- package/dist/contracts/Relation.d.ts +43 -0
- package/dist/core.js +2482 -0
- package/dist/exceptions/AttributeNotFillableException.d.ts +4 -0
- package/dist/exceptions/FacadeNotFoundException.d.ts +4 -0
- package/dist/exceptions/MethodNotImplementedException.d.ts +4 -0
- package/dist/exceptions/ModelInvalidRelatedTypeException.d.ts +4 -0
- package/dist/exceptions/ModelNotFoundException.d.ts +4 -0
- package/dist/exceptions/ModelNotPersistedException.d.ts +4 -0
- package/dist/exceptions/ModelWithoutPrimaryKeyException.d.ts +4 -0
- package/dist/exceptions/NoEmbedException.d.ts +4 -0
- package/dist/exceptions/NoInverseRelationException.d.ts +4 -0
- package/dist/exceptions/NotModelException.d.ts +4 -0
- package/dist/exceptions/NotReducibleException.d.ts +4 -0
- package/dist/exceptions/ReducerOverrideException.d.ts +4 -0
- package/dist/exceptions/RouteNotFoundException.d.ts +4 -0
- package/dist/exceptions/UnsupportedRelationException.d.ts +4 -0
- package/dist/facades/App.d.ts +30 -0
- package/dist/facades/Auth.d.ts +13 -0
- package/dist/facades/Error.d.ts +14 -0
- package/dist/facades/Log.d.ts +13 -0
- package/dist/facades/Model.d.ts +41 -0
- package/dist/facades/Route.d.ts +31 -0
- package/dist/helpers/app.d.ts +4 -0
- package/dist/helpers/auth.d.ts +1 -0
- package/dist/helpers/collect.d.ts +1 -0
- package/dist/helpers/config.d.ts +4 -0
- package/dist/helpers/error.d.ts +3 -0
- package/dist/helpers/log.d.ts +4 -0
- package/dist/helpers/model.d.ts +5 -0
- package/dist/helpers/route.d.ts +4 -0
- package/dist/index.d.ts +23 -0
- package/dist/mixins/BaseModel.d.ts +4 -0
- package/dist/mixins/HasEvents.d.ts +15 -0
- package/dist/mixins/Reducible.d.ts +16 -0
- package/dist/support/collection.d.ts +3 -0
- package/dist/support/model.d.ts +2 -0
- package/dist/support/reader.d.ts +2 -0
- package/dist/support/searchParams.d.ts +1 -0
- package/dist/types/App.d.ts +71 -0
- package/dist/types/Auth.d.ts +12 -0
- package/dist/types/Builder.d.ts +45 -0
- package/dist/types/Collection.d.ts +1803 -0
- package/dist/types/Config.d.ts +28 -0
- package/dist/types/Error.d.ts +29 -0
- package/dist/types/Event.d.ts +13 -0
- package/dist/types/Log.d.ts +37 -0
- package/dist/types/Model.d.ts +164 -0
- package/dist/types/Plugin.d.ts +4 -0
- package/dist/types/Reducer.d.ts +16 -0
- package/dist/types/Relation.d.ts +29 -0
- package/dist/types/Route.d.ts +21 -0
- package/dist/types/Support.d.ts +7 -0
- package/graph.png +0 -0
- package/index.html +13 -0
- package/jest.config.js +15 -0
- package/package.json +38 -0
- package/tsconfig.json +25 -0
- package/vite.config.js +18 -0
package/dist/core.js
ADDED
|
@@ -0,0 +1,2482 @@
|
|
|
1
|
+
var ft = Object.defineProperty;
|
|
2
|
+
var dt = (n, t, e) => t in n ? ft(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var p = (n, t, e) => (dt(n, typeof t != "symbol" ? t + "" : t, e), e), P = (n, t, e) => {
|
|
4
|
+
if (!t.has(n))
|
|
5
|
+
throw TypeError("Cannot " + e);
|
|
6
|
+
};
|
|
7
|
+
var o = (n, t, e) => (P(n, t, "read from private field"), e ? e.call(n) : t.get(n)), q = (n, t, e) => {
|
|
8
|
+
if (t.has(n))
|
|
9
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
10
|
+
t instanceof WeakSet ? t.add(n) : t.set(n, e);
|
|
11
|
+
}, Q = (n, t, e, s) => (P(n, t, "write to private field"), s ? s.call(n, e) : t.set(n, e), e);
|
|
12
|
+
var G = (n, t, e) => (P(n, t, "access private method"), e);
|
|
13
|
+
import f from "lodash";
|
|
14
|
+
import { produce as B, isDraftable as pt } from "immer";
|
|
15
|
+
import K, { isAxiosError as mt } from "axios";
|
|
16
|
+
class Y extends Error {
|
|
17
|
+
constructor(t) {
|
|
18
|
+
super(`[Luminix] Facade "${t}" not found`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
p(Y, "name", "FacadeNotFoundException");
|
|
22
|
+
class gt {
|
|
23
|
+
constructor(t) {
|
|
24
|
+
p(this, "_user");
|
|
25
|
+
this.app = t;
|
|
26
|
+
}
|
|
27
|
+
attempt(t, e = !1, s) {
|
|
28
|
+
const r = document.createElement("form");
|
|
29
|
+
r.method = "post", r.action = this.app.make("route").url("login"), r.style.display = "none";
|
|
30
|
+
const i = this.app.make("config").get("auth.csrf");
|
|
31
|
+
if (typeof i == "string") {
|
|
32
|
+
const a = document.createElement("input");
|
|
33
|
+
a.type = "hidden", a.name = "_token", a.value = i, r.appendChild(a);
|
|
34
|
+
}
|
|
35
|
+
const h = document.createElement("input");
|
|
36
|
+
h.type = "email", h.name = "email", h.value = t.email, r.appendChild(h);
|
|
37
|
+
const l = document.createElement("input");
|
|
38
|
+
if (l.type = "password", l.name = "password", l.value = t.password, r.appendChild(l), e) {
|
|
39
|
+
const a = document.createElement("input");
|
|
40
|
+
a.type = "checkbox", a.name = "remember", a.value = "1", a.checked = !0, r.appendChild(a);
|
|
41
|
+
}
|
|
42
|
+
s && r.addEventListener("submit", s), document.body.appendChild(r), r.submit();
|
|
43
|
+
}
|
|
44
|
+
check() {
|
|
45
|
+
return !!this.app.make("config").get("auth.user");
|
|
46
|
+
}
|
|
47
|
+
logout(t) {
|
|
48
|
+
const e = document.createElement("form");
|
|
49
|
+
e.method = "post", e.action = this.app.make("route").url("logout"), e.style.display = "none";
|
|
50
|
+
const s = this.app.make("config").get("auth.csrf");
|
|
51
|
+
if (typeof s == "string") {
|
|
52
|
+
const r = document.createElement("input");
|
|
53
|
+
r.type = "hidden", r.name = "_token", r.value = s, e.appendChild(r);
|
|
54
|
+
}
|
|
55
|
+
document.body.appendChild(e), t && e.addEventListener("submit", t), e.submit();
|
|
56
|
+
}
|
|
57
|
+
user() {
|
|
58
|
+
if (!this._user) {
|
|
59
|
+
const { model: t, config: e } = this.app.make(), s = t.make("user"), r = e.get("auth.user");
|
|
60
|
+
if (!r)
|
|
61
|
+
return null;
|
|
62
|
+
this._user = new s(r);
|
|
63
|
+
}
|
|
64
|
+
return this._user;
|
|
65
|
+
}
|
|
66
|
+
id() {
|
|
67
|
+
var t;
|
|
68
|
+
return ((t = this.user()) == null ? void 0 : t.getKey()) || null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
class yt {
|
|
72
|
+
constructor(t) {
|
|
73
|
+
this._debug = t;
|
|
74
|
+
}
|
|
75
|
+
emergency(...t) {
|
|
76
|
+
this._debug && console.error(...t);
|
|
77
|
+
}
|
|
78
|
+
alert(...t) {
|
|
79
|
+
this._debug && console.error(...t);
|
|
80
|
+
}
|
|
81
|
+
critical(...t) {
|
|
82
|
+
this._debug && console.error(...t);
|
|
83
|
+
}
|
|
84
|
+
error(...t) {
|
|
85
|
+
this._debug && console.error(...t);
|
|
86
|
+
}
|
|
87
|
+
warning(...t) {
|
|
88
|
+
this._debug && console.warn(...t);
|
|
89
|
+
}
|
|
90
|
+
notice(...t) {
|
|
91
|
+
this._debug && console.info(...t);
|
|
92
|
+
}
|
|
93
|
+
info(...t) {
|
|
94
|
+
this._debug && console.info(...t);
|
|
95
|
+
}
|
|
96
|
+
debug(...t) {
|
|
97
|
+
this._debug && console.debug(...t);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
class v {
|
|
101
|
+
constructor(t) {
|
|
102
|
+
p(this, "locked", []);
|
|
103
|
+
this.bag = t, Object.freeze(this.bag);
|
|
104
|
+
}
|
|
105
|
+
get(t, e) {
|
|
106
|
+
return f.get(this.bag, t, e);
|
|
107
|
+
}
|
|
108
|
+
set(t, e) {
|
|
109
|
+
if (this.locked.some((s) => t.startsWith(s)))
|
|
110
|
+
throw new Error(`Cannot set a locked path "${t}"`);
|
|
111
|
+
if (typeof e == "object" && e !== null && this.locked.some((s) => f.has(e, s.slice(t.length + 1))))
|
|
112
|
+
throw new Error(`Cannot set a path "${t}" that would override a locked path`);
|
|
113
|
+
if (t === ".") {
|
|
114
|
+
if (this.locked.length)
|
|
115
|
+
throw new Error("Cannot set the root path when there are locked paths");
|
|
116
|
+
if (typeof e != "object" || e === null)
|
|
117
|
+
throw new TypeError("Value must be an object");
|
|
118
|
+
this.bag = B(this.bag, () => e), this.emit("change", {
|
|
119
|
+
path: t,
|
|
120
|
+
value: e,
|
|
121
|
+
type: "set"
|
|
122
|
+
});
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.bag = B(this.bag, (s) => {
|
|
126
|
+
f.set(s, t, e);
|
|
127
|
+
}), this.emit("change", {
|
|
128
|
+
path: t,
|
|
129
|
+
value: e,
|
|
130
|
+
type: "set"
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
merge(t, e) {
|
|
134
|
+
if (typeof e != "object" || e === null)
|
|
135
|
+
throw new TypeError("Value must be an object");
|
|
136
|
+
if (t === ".") {
|
|
137
|
+
if (this.locked.some((r) => f.has(e, r)))
|
|
138
|
+
throw new Error(`Cannot merge a path "${t}" that would override a locked path`);
|
|
139
|
+
this.bag = B(this.bag, (r) => ({
|
|
140
|
+
...r,
|
|
141
|
+
...e
|
|
142
|
+
})), this.emit("change", {
|
|
143
|
+
path: t,
|
|
144
|
+
value: e,
|
|
145
|
+
type: "merge"
|
|
146
|
+
});
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const s = this.get(t);
|
|
150
|
+
if (typeof s == "object" && s !== null)
|
|
151
|
+
return this.set(t, {
|
|
152
|
+
...s,
|
|
153
|
+
...e
|
|
154
|
+
});
|
|
155
|
+
if (s === null || typeof s > "u")
|
|
156
|
+
return this.set(t, e);
|
|
157
|
+
throw new Error(`Cannot merge a non-object path "${t}"`);
|
|
158
|
+
}
|
|
159
|
+
has(t) {
|
|
160
|
+
return f.has(this.bag, t);
|
|
161
|
+
}
|
|
162
|
+
delete(t) {
|
|
163
|
+
if (this.locked.some((e) => t.startsWith(e)))
|
|
164
|
+
throw new Error(`Cannot delete a locked path "${t}"`);
|
|
165
|
+
this.bag = B(this.bag, (e) => {
|
|
166
|
+
f.unset(e, t);
|
|
167
|
+
}), this.emit("change", {
|
|
168
|
+
path: t,
|
|
169
|
+
value: null,
|
|
170
|
+
type: "delete"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
lock(t) {
|
|
174
|
+
if (!this.has(t))
|
|
175
|
+
throw new Error(`Cannot lock a non-existing path "${t}"`);
|
|
176
|
+
this.locked.push(t);
|
|
177
|
+
}
|
|
178
|
+
clone() {
|
|
179
|
+
return new v(this.bag);
|
|
180
|
+
}
|
|
181
|
+
all() {
|
|
182
|
+
return this.bag;
|
|
183
|
+
}
|
|
184
|
+
isEmpty() {
|
|
185
|
+
return f.isEmpty(this.bag);
|
|
186
|
+
}
|
|
187
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
188
|
+
on(t, e) {
|
|
189
|
+
return () => null;
|
|
190
|
+
}
|
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
192
|
+
once(t, e) {
|
|
193
|
+
}
|
|
194
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
195
|
+
emit(t, e) {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function A(n) {
|
|
199
|
+
var t, e, s, r, Z;
|
|
200
|
+
return e = class extends n {
|
|
201
|
+
constructor(...a) {
|
|
202
|
+
super(...a);
|
|
203
|
+
q(this, r);
|
|
204
|
+
q(this, t, void 0);
|
|
205
|
+
p(this, s, n.name);
|
|
206
|
+
Q(this, t, G(this, r, Z).call(this));
|
|
207
|
+
}
|
|
208
|
+
on(a, d) {
|
|
209
|
+
if (typeof a != "string")
|
|
210
|
+
throw new TypeError("event must be a string");
|
|
211
|
+
if (typeof d != "function")
|
|
212
|
+
throw new TypeError("callback must be a function");
|
|
213
|
+
return o(this, t).on(a, d);
|
|
214
|
+
}
|
|
215
|
+
once(a, d) {
|
|
216
|
+
if (typeof a != "string")
|
|
217
|
+
throw new TypeError("event must be a string");
|
|
218
|
+
if (typeof d != "function")
|
|
219
|
+
throw new TypeError("callback must be a function");
|
|
220
|
+
const g = o(this, t).on(a, (b) => {
|
|
221
|
+
g(), d(b);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
emit(a, d = {}) {
|
|
225
|
+
if (typeof a != "string")
|
|
226
|
+
throw new TypeError("event must be a string");
|
|
227
|
+
if (typeof d != "object")
|
|
228
|
+
throw new TypeError("data must be an object");
|
|
229
|
+
o(this, t).emit(a, {
|
|
230
|
+
...d,
|
|
231
|
+
source: this
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}, s = Symbol.toStringTag, t = new WeakMap(), r = new WeakSet(), Z = function() {
|
|
235
|
+
return {
|
|
236
|
+
emit(a, d) {
|
|
237
|
+
for (let g = 0, b = this.events[a] || [], m = b.length; g < m; g++)
|
|
238
|
+
b[g](d);
|
|
239
|
+
},
|
|
240
|
+
events: {},
|
|
241
|
+
on(a, d) {
|
|
242
|
+
var g;
|
|
243
|
+
return ((g = this.events)[a] || (g[a] = [])).push(d), () => {
|
|
244
|
+
var b;
|
|
245
|
+
this.events[a] = (b = this.events[a]) == null ? void 0 : b.filter((m) => d !== m);
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}, p(e, "name", n.name), e;
|
|
250
|
+
}
|
|
251
|
+
function V(...n) {
|
|
252
|
+
return n.reduce((t, e) => t.flatMap((s) => e.map((r) => [s, r].flat())));
|
|
253
|
+
}
|
|
254
|
+
function T(n) {
|
|
255
|
+
return !(typeof n != "object" || n === null || !Reflect.has(n, "constructor") || Reflect.get(n.constructor, "name") !== "Collection");
|
|
256
|
+
}
|
|
257
|
+
class y extends Error {
|
|
258
|
+
constructor() {
|
|
259
|
+
super("[Luminix] Method not implemented.");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
p(y, "name", "MethodNotImplementedException");
|
|
263
|
+
function c(n = [], t = j) {
|
|
264
|
+
if (!Array.isArray(n))
|
|
265
|
+
throw new TypeError("collect() expects an array");
|
|
266
|
+
return new (A(t))([...n]);
|
|
267
|
+
}
|
|
268
|
+
const $ = (n) => {
|
|
269
|
+
n.emit("change", {
|
|
270
|
+
items: n.all()
|
|
271
|
+
});
|
|
272
|
+
}, I = (n, t) => Array.isArray(n) ? n[t] ?? null : n.get(t);
|
|
273
|
+
var u, Ft;
|
|
274
|
+
class j {
|
|
275
|
+
constructor(t = []) {
|
|
276
|
+
q(this, u, void 0);
|
|
277
|
+
p(this, Ft, this.constructor.name);
|
|
278
|
+
Q(this, u, t);
|
|
279
|
+
}
|
|
280
|
+
get items() {
|
|
281
|
+
return [...o(this, u)];
|
|
282
|
+
}
|
|
283
|
+
[Symbol.iterator]() {
|
|
284
|
+
return o(this, u)[Symbol.iterator]();
|
|
285
|
+
}
|
|
286
|
+
all() {
|
|
287
|
+
return [...o(this, u)];
|
|
288
|
+
}
|
|
289
|
+
average(t) {
|
|
290
|
+
return typeof t == "string" ? this.avg(t) : this.avg();
|
|
291
|
+
}
|
|
292
|
+
avg(t) {
|
|
293
|
+
return typeof t == "string" ? this.sum(t) / o(this, u).length : this.sum() / o(this, u).length;
|
|
294
|
+
}
|
|
295
|
+
chunk(t) {
|
|
296
|
+
const e = [];
|
|
297
|
+
for (let s = 0; s < o(this, u).length; s += t)
|
|
298
|
+
e.push(o(this, u).slice(s, s + t));
|
|
299
|
+
return c(e.map((s) => c(s)));
|
|
300
|
+
}
|
|
301
|
+
chunkWhile(t) {
|
|
302
|
+
const e = [];
|
|
303
|
+
let s = c();
|
|
304
|
+
for (let r = 0; r < o(this, u).length; r++)
|
|
305
|
+
t(o(this, u)[r], r, s) ? s.push(o(this, u)[r]) : (e.push(s), s = c());
|
|
306
|
+
return s.count() > 0 && e.push(s), c(e);
|
|
307
|
+
}
|
|
308
|
+
collapse() {
|
|
309
|
+
return c(o(this, u).flat());
|
|
310
|
+
}
|
|
311
|
+
collect() {
|
|
312
|
+
return c(o(this, u));
|
|
313
|
+
}
|
|
314
|
+
combine(t) {
|
|
315
|
+
const e = {};
|
|
316
|
+
return o(this, u).forEach((s, r) => {
|
|
317
|
+
if (typeof s != "string")
|
|
318
|
+
throw new TypeError("The `combine` method expects the keys to be strings");
|
|
319
|
+
e[s] = I(t, r);
|
|
320
|
+
}), e;
|
|
321
|
+
}
|
|
322
|
+
concat(t) {
|
|
323
|
+
return Array.isArray(t) ? c([...o(this, u), ...t]) : c([...o(this, u), ...t.all()]);
|
|
324
|
+
}
|
|
325
|
+
contains(t, e) {
|
|
326
|
+
return typeof t == "function" ? o(this, u).some((s, r) => t(s, r, this)) : o(this, u).some((s) => {
|
|
327
|
+
if (typeof e > "u")
|
|
328
|
+
return s == t;
|
|
329
|
+
if (typeof t != "string")
|
|
330
|
+
throw new TypeError("The key must be a string");
|
|
331
|
+
return s[t] == e;
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
containsOneItem() {
|
|
335
|
+
return o(this, u).length === 1;
|
|
336
|
+
}
|
|
337
|
+
containsStrict(t, e) {
|
|
338
|
+
return typeof t == "function" ? o(this, u).some((s, r) => t(s, r, this)) : o(this, u).some((s) => {
|
|
339
|
+
if (typeof e > "u")
|
|
340
|
+
return s === t;
|
|
341
|
+
if (typeof t != "string")
|
|
342
|
+
throw new TypeError("The key must be a string");
|
|
343
|
+
return s[t] === e;
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
count() {
|
|
347
|
+
return o(this, u).length;
|
|
348
|
+
}
|
|
349
|
+
countBy(t) {
|
|
350
|
+
return typeof t == "function" ? o(this, u).reduce((e, s, r) => {
|
|
351
|
+
const i = t(s, r, this);
|
|
352
|
+
return e[i] = e[i] ? e[i] + 1 : 1, e;
|
|
353
|
+
}, {}) : o(this, u).reduce((e, s) => {
|
|
354
|
+
if (!["string", "number"].includes(typeof s))
|
|
355
|
+
throw new TypeError("The countBy method expects the items to be strings or numbers");
|
|
356
|
+
return e[String(s)] = e[String(s)] ? e[String(s)] + 1 : 1, e;
|
|
357
|
+
}, {});
|
|
358
|
+
}
|
|
359
|
+
crossJoin(...t) {
|
|
360
|
+
return c(V(
|
|
361
|
+
o(this, u),
|
|
362
|
+
...t.map((e) => Array.isArray(e) ? e : e.all())
|
|
363
|
+
));
|
|
364
|
+
}
|
|
365
|
+
diff(t) {
|
|
366
|
+
return Array.isArray(t) ? c(o(this, u).filter((e) => !t.includes(e))) : c(o(this, u).filter((e) => !t.contains(e)));
|
|
367
|
+
}
|
|
368
|
+
doesntContain(t, e) {
|
|
369
|
+
return typeof t == "function" ? o(this, u).every((s, r) => !t(s, r, this)) : o(this, u).every((s) => {
|
|
370
|
+
if (typeof e > "u")
|
|
371
|
+
return s != t;
|
|
372
|
+
if (typeof t != "string")
|
|
373
|
+
throw new TypeError("The key must be a string");
|
|
374
|
+
return s[t] != e;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
dump() {
|
|
378
|
+
console.log(this.toArray());
|
|
379
|
+
}
|
|
380
|
+
duplicates(t) {
|
|
381
|
+
return c(typeof t == "string" ? o(this, u).reduce((e, s, r) => (o(this, u).slice(r + 1).some((i) => i[t] == s[t]) && e.push(s[t]), e), []) : o(this, u).reduce((e, s, r) => (o(this, u).slice(r + 1).some((i) => i == s) && e.push(s), e), []));
|
|
382
|
+
}
|
|
383
|
+
duplicatesStrict(t) {
|
|
384
|
+
return c(typeof t == "string" ? o(this, u).reduce((e, s, r) => (o(this, u).slice(r + 1).some((i) => i[t] === s[t]) && e.push(s[t]), e), []) : o(this, u).reduce((e, s, r) => (o(this, u).slice(r + 1).some((i) => i === s) && e.push(s), e), []));
|
|
385
|
+
}
|
|
386
|
+
each(t) {
|
|
387
|
+
let e = 0;
|
|
388
|
+
for (const s of this) {
|
|
389
|
+
if (t(s, e, this) === !1)
|
|
390
|
+
break;
|
|
391
|
+
e++;
|
|
392
|
+
}
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
eachSpread(t) {
|
|
396
|
+
for (const e of this) {
|
|
397
|
+
if (!Array.isArray(e) && !T(e))
|
|
398
|
+
throw new TypeError("The items in the collection must be arrays or collections");
|
|
399
|
+
const s = Array.isArray(e) ? e : e.all();
|
|
400
|
+
if (t(...s) === !1)
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
ensure(t) {
|
|
406
|
+
const e = Array.isArray(t) ? t : [t];
|
|
407
|
+
return o(this, u).forEach((s, r) => {
|
|
408
|
+
if (!e.some((i) => typeof i == "string" ? typeof s === i : s instanceof i))
|
|
409
|
+
throw new TypeError(`The item at index ${r} is not of the expected type`);
|
|
410
|
+
}), this;
|
|
411
|
+
}
|
|
412
|
+
every(t) {
|
|
413
|
+
return o(this, u).every((e, s) => t(e, s, this));
|
|
414
|
+
}
|
|
415
|
+
except(t) {
|
|
416
|
+
return c(o(this, u).filter((e, s) => !t.includes(s)));
|
|
417
|
+
}
|
|
418
|
+
filter(t) {
|
|
419
|
+
return c(o(this, u).filter((e, s) => typeof t != "function" ? !!e : t(e, s, this)));
|
|
420
|
+
}
|
|
421
|
+
first(t) {
|
|
422
|
+
return typeof t == "function" ? o(this, u).find((e, s) => t(e, s, this)) ?? null : o(this, u)[0] ?? null;
|
|
423
|
+
}
|
|
424
|
+
firstOrFail(t) {
|
|
425
|
+
const e = this.first(t);
|
|
426
|
+
if (e === null)
|
|
427
|
+
throw new Error("No matching item found");
|
|
428
|
+
return e;
|
|
429
|
+
}
|
|
430
|
+
firstWhere(t, e, s) {
|
|
431
|
+
if (typeof t != "string")
|
|
432
|
+
throw new TypeError("The key must be a string");
|
|
433
|
+
if (typeof e > "u")
|
|
434
|
+
return this.first((r) => !!r[t]);
|
|
435
|
+
if (typeof s > "u")
|
|
436
|
+
return o(this, u).find((r) => r[t] == e) ?? null;
|
|
437
|
+
if (typeof e != "string")
|
|
438
|
+
throw new TypeError("The operator must be a string");
|
|
439
|
+
return s === null ? o(this, u).find((r) => r[t] === null) ?? null : o(this, u).find((r) => {
|
|
440
|
+
switch (e) {
|
|
441
|
+
case "=":
|
|
442
|
+
return r[t] == s;
|
|
443
|
+
case "!=":
|
|
444
|
+
return r[t] != s;
|
|
445
|
+
case ">":
|
|
446
|
+
return r[t] > s;
|
|
447
|
+
case "<":
|
|
448
|
+
return r[t] < s;
|
|
449
|
+
case ">=":
|
|
450
|
+
return r[t] >= s;
|
|
451
|
+
case "<=":
|
|
452
|
+
return r[t] <= s;
|
|
453
|
+
default:
|
|
454
|
+
throw new Error("Unsupported operator");
|
|
455
|
+
}
|
|
456
|
+
}) ?? null;
|
|
457
|
+
}
|
|
458
|
+
flatMap(t) {
|
|
459
|
+
return c(o(this, u).flatMap((e, s) => t(e, s, this)));
|
|
460
|
+
}
|
|
461
|
+
forget(t) {
|
|
462
|
+
return o(this, u).splice(t, 1), $(this), this;
|
|
463
|
+
}
|
|
464
|
+
forPage(t, e) {
|
|
465
|
+
return c(o(this, u).slice((t - 1) * e, t * e));
|
|
466
|
+
}
|
|
467
|
+
get(t, e) {
|
|
468
|
+
if (typeof t != "number")
|
|
469
|
+
throw new TypeError("The key must be a number");
|
|
470
|
+
return typeof e > "u" ? o(this, u)[t] ?? null : typeof e == "function" ? o(this, u)[t] ?? e() : o(this, u)[t] ?? e;
|
|
471
|
+
}
|
|
472
|
+
groupBy(t) {
|
|
473
|
+
const e = Array.isArray(t) ? t : [t];
|
|
474
|
+
return o(this, u).reduce((s, r, i) => {
|
|
475
|
+
const h = e.map((a) => {
|
|
476
|
+
if (typeof a == "function") {
|
|
477
|
+
const d = a(r, i, this);
|
|
478
|
+
return Array.isArray(d) ? d : [d];
|
|
479
|
+
}
|
|
480
|
+
return [String(r[a])];
|
|
481
|
+
});
|
|
482
|
+
return V(...h).forEach((a) => {
|
|
483
|
+
const d = Array.isArray(a) ? a.join(".") : a;
|
|
484
|
+
f.set(s, d, [
|
|
485
|
+
...f.get(s, d, []),
|
|
486
|
+
r
|
|
487
|
+
]);
|
|
488
|
+
}), s;
|
|
489
|
+
}, {});
|
|
490
|
+
}
|
|
491
|
+
has(t) {
|
|
492
|
+
return o(this, u).length > t;
|
|
493
|
+
}
|
|
494
|
+
hasAny(t) {
|
|
495
|
+
return t.some((e) => this.has(e));
|
|
496
|
+
}
|
|
497
|
+
implode(t, e) {
|
|
498
|
+
if (typeof e > "u") {
|
|
499
|
+
if (typeof t != "string")
|
|
500
|
+
throw new TypeError("The glue must be a string");
|
|
501
|
+
if (!o(this, u).every((s) => ["string", "number"].includes(typeof s)))
|
|
502
|
+
throw new TypeError("The items must be strings or numbers");
|
|
503
|
+
return o(this, u).join(t);
|
|
504
|
+
}
|
|
505
|
+
if (typeof t == "function")
|
|
506
|
+
return o(this, u).map((s, r) => t(s, r, this)).join(e);
|
|
507
|
+
if (typeof t != "string")
|
|
508
|
+
throw new TypeError("The key must be a string");
|
|
509
|
+
if (!o(this, u).every((s) => typeof s == "object"))
|
|
510
|
+
throw new TypeError("The items must be objects");
|
|
511
|
+
return o(this, u).map((s) => s[t]).join(e);
|
|
512
|
+
}
|
|
513
|
+
intersect(t) {
|
|
514
|
+
return Array.isArray(t) ? c(o(this, u).filter((e) => t.includes(e))) : c(o(this, u).filter((e) => t.contains(e)));
|
|
515
|
+
}
|
|
516
|
+
isEmpty() {
|
|
517
|
+
return o(this, u).length === 0;
|
|
518
|
+
}
|
|
519
|
+
isNotEmpty() {
|
|
520
|
+
return !this.isEmpty();
|
|
521
|
+
}
|
|
522
|
+
join(t, e) {
|
|
523
|
+
return typeof e > "u" ? o(this, u).join(t) : o(this, u).slice(0, -1).join(t) + e + o(this, u)[o(this, u).length - 1];
|
|
524
|
+
}
|
|
525
|
+
keyBy(t) {
|
|
526
|
+
if (typeof t == "function")
|
|
527
|
+
return o(this, u).reduce((e, s, r) => (e[t(s, r, this)] = s, e), {});
|
|
528
|
+
if (typeof t != "string")
|
|
529
|
+
throw new TypeError("The key must be a string");
|
|
530
|
+
return o(this, u).reduce((e, s) => (e[String(s[t])] = s, e), {});
|
|
531
|
+
}
|
|
532
|
+
last(t) {
|
|
533
|
+
return typeof t == "function" ? o(this, u).toReversed().find((e, s) => t(e, s, this)) ?? null : o(this, u)[o(this, u).length - 1] ?? null;
|
|
534
|
+
}
|
|
535
|
+
map(t) {
|
|
536
|
+
return c(o(this, u).map((e, s) => t(e, s, this)));
|
|
537
|
+
}
|
|
538
|
+
mapInto(t) {
|
|
539
|
+
return c(o(this, u).map((e) => new t(e)));
|
|
540
|
+
}
|
|
541
|
+
mapSpread(t) {
|
|
542
|
+
return c(o(this, u).map((e) => {
|
|
543
|
+
if (!Array.isArray(e) && !T(e))
|
|
544
|
+
throw new TypeError("The items in the collection must be arrays or collections");
|
|
545
|
+
const s = Array.isArray(e) ? e : e.all();
|
|
546
|
+
return t(...s);
|
|
547
|
+
}));
|
|
548
|
+
}
|
|
549
|
+
mapToGroups(t) {
|
|
550
|
+
return o(this, u).reduce((e, s, r) => {
|
|
551
|
+
const i = t(s, r, this);
|
|
552
|
+
return Object.entries(i).forEach(([h, l]) => {
|
|
553
|
+
e[h] = e[h] ?? [], e[h].push(l);
|
|
554
|
+
}), e;
|
|
555
|
+
}, {});
|
|
556
|
+
}
|
|
557
|
+
mapWithKeys(t) {
|
|
558
|
+
return o(this, u).reduce((e, s, r) => {
|
|
559
|
+
const i = t(s, r, this);
|
|
560
|
+
return Object.entries(i).forEach(([h, l]) => {
|
|
561
|
+
e[h] = l;
|
|
562
|
+
}), e;
|
|
563
|
+
}, {});
|
|
564
|
+
}
|
|
565
|
+
max(t) {
|
|
566
|
+
return typeof t == "string" ? o(this, u).reduce((e, s) => s[t] > e ? s[t] : e, o(this, u)[0][t]) : o(this, u).reduce((e, s) => s > e ? s : e, o(this, u)[0]);
|
|
567
|
+
}
|
|
568
|
+
median(t) {
|
|
569
|
+
if (typeof t == "string") {
|
|
570
|
+
const r = this.pluck(t).sort(), i = Math.floor(r.count() / 2);
|
|
571
|
+
return r.count() % 2 === 0 ? c([I(r, i - 1), I(r, i)]).avg() : I(r, i);
|
|
572
|
+
}
|
|
573
|
+
const e = o(this, u).toSorted(), s = Math.floor(e.length / 2);
|
|
574
|
+
return e.length % 2 === 0 ? c([e[s - 1], e[s]]).avg() : e[s] ?? null;
|
|
575
|
+
}
|
|
576
|
+
merge(t) {
|
|
577
|
+
return Array.isArray(t) ? c([...o(this, u), ...t]) : c([...o(this, u), ...t.all()]);
|
|
578
|
+
}
|
|
579
|
+
min(t) {
|
|
580
|
+
return typeof t == "string" ? o(this, u).reduce((e, s) => s[t] < e ? s[t] : e, o(this, u)[0][t]) : o(this, u).reduce((e, s) => s < e ? s : e, o(this, u)[0]);
|
|
581
|
+
}
|
|
582
|
+
mode(t) {
|
|
583
|
+
const e = typeof t == "string" ? this.filter((r) => ["number", "string"].includes(typeof r[t])).countBy((r) => r[t]) : this.countBy(), s = Math.max(...Object.values(e));
|
|
584
|
+
return Object.entries(e).filter(([, r]) => r === s).map(([r]) => r);
|
|
585
|
+
}
|
|
586
|
+
nth(t, e = 0) {
|
|
587
|
+
return this.chunk(t).filter((s) => s.count() > e).map((s) => s.get(e));
|
|
588
|
+
}
|
|
589
|
+
only(t) {
|
|
590
|
+
return c(o(this, u).filter((e, s) => t.includes(s)));
|
|
591
|
+
}
|
|
592
|
+
pad(t, e = null) {
|
|
593
|
+
const s = o(this, u).slice();
|
|
594
|
+
for (; s.length < Math.abs(t); )
|
|
595
|
+
t > 0 ? s.push(e) : s.unshift(e);
|
|
596
|
+
return c(s);
|
|
597
|
+
}
|
|
598
|
+
partition(t) {
|
|
599
|
+
return [
|
|
600
|
+
this.filter(t),
|
|
601
|
+
this.reject(t)
|
|
602
|
+
];
|
|
603
|
+
}
|
|
604
|
+
percentage(t, e = 2) {
|
|
605
|
+
return Math.round(
|
|
606
|
+
100 * (10 ^ e) * this.filter(t).count() / o(this, u).length
|
|
607
|
+
) / (10 ^ e);
|
|
608
|
+
}
|
|
609
|
+
pipe(t) {
|
|
610
|
+
return t(this);
|
|
611
|
+
}
|
|
612
|
+
pipeInto(t) {
|
|
613
|
+
return new t(this);
|
|
614
|
+
}
|
|
615
|
+
pipeThrough(t) {
|
|
616
|
+
return t.reduce((e, s) => {
|
|
617
|
+
if (!T(e) && !Array.isArray(e))
|
|
618
|
+
throw new TypeError("The pipeline expects the carry to be a collection or an array");
|
|
619
|
+
return s(
|
|
620
|
+
T(e) ? e : c(e)
|
|
621
|
+
);
|
|
622
|
+
}, this);
|
|
623
|
+
}
|
|
624
|
+
pluck(t) {
|
|
625
|
+
return this.map((e) => e[t]);
|
|
626
|
+
}
|
|
627
|
+
pop(t = 1) {
|
|
628
|
+
const e = o(this, u).splice(o(this, u).length - t, t);
|
|
629
|
+
return $(this), t === 1 ? e[0] ?? null : c(e);
|
|
630
|
+
}
|
|
631
|
+
prepend(t) {
|
|
632
|
+
const e = o(this, u).unshift(t);
|
|
633
|
+
return $(this), e;
|
|
634
|
+
}
|
|
635
|
+
pull(t) {
|
|
636
|
+
const e = o(this, u).splice(t, 1)[0] ?? null;
|
|
637
|
+
return $(this), e;
|
|
638
|
+
}
|
|
639
|
+
push(...t) {
|
|
640
|
+
const e = o(this, u).push(...t);
|
|
641
|
+
return $(this), e;
|
|
642
|
+
}
|
|
643
|
+
put(t, e) {
|
|
644
|
+
return o(this, u).splice(t, 1, e), $(this), this;
|
|
645
|
+
}
|
|
646
|
+
random(t = 1) {
|
|
647
|
+
if (o(this, u).length < t)
|
|
648
|
+
throw new Error("The collection has fewer items than the requested amount");
|
|
649
|
+
const e = c(f.sampleSize(o(this, u), t));
|
|
650
|
+
return t === 1 ? e.first() : e;
|
|
651
|
+
}
|
|
652
|
+
reduce(t, e = null) {
|
|
653
|
+
return o(this, u).reduce((s, r, i) => t(s, r, i, this), e);
|
|
654
|
+
}
|
|
655
|
+
reject(t) {
|
|
656
|
+
return this.filter((e, s) => !t(e, s, this));
|
|
657
|
+
}
|
|
658
|
+
replace(t) {
|
|
659
|
+
const e = o(this, u).slice();
|
|
660
|
+
return Object.entries(t).forEach(([s, r]) => {
|
|
661
|
+
e[parseInt(s)] = r;
|
|
662
|
+
}), c(e);
|
|
663
|
+
}
|
|
664
|
+
reverse() {
|
|
665
|
+
return c(o(this, u).toReversed());
|
|
666
|
+
}
|
|
667
|
+
search(t, e = !1) {
|
|
668
|
+
if (typeof t != "function" || o(this, u).every((r) => typeof r == "function")) {
|
|
669
|
+
const r = o(this, u).findIndex((i) => e ? i === t : i == t);
|
|
670
|
+
return r === -1 ? !1 : r;
|
|
671
|
+
}
|
|
672
|
+
const s = o(this, u).findIndex((r, i) => t(r, i, this));
|
|
673
|
+
return s === -1 ? !1 : s;
|
|
674
|
+
}
|
|
675
|
+
select(t) {
|
|
676
|
+
return this.map((e) => t.reduce((s, r) => (s[r] = e[r], s), {}));
|
|
677
|
+
}
|
|
678
|
+
shift(t = 1) {
|
|
679
|
+
const e = o(this, u).splice(0, t);
|
|
680
|
+
return $(this), t === 1 ? e[0] ?? null : c(e);
|
|
681
|
+
}
|
|
682
|
+
shuffle() {
|
|
683
|
+
return c(f.shuffle(o(this, u)));
|
|
684
|
+
}
|
|
685
|
+
skip(t) {
|
|
686
|
+
return c(o(this, u).slice(t));
|
|
687
|
+
}
|
|
688
|
+
skipUntil(t) {
|
|
689
|
+
return typeof t == "function" ? this.skip(o(this, u).findIndex((e, s) => t(e, s, this))) : this.skip(o(this, u).findIndex((e) => e == t));
|
|
690
|
+
}
|
|
691
|
+
skipWhile(t) {
|
|
692
|
+
return typeof t == "function" ? this.skip(o(this, u).findIndex((e, s) => !t(e, s, this))) : this.skip(o(this, u).findIndex((e) => e != t));
|
|
693
|
+
}
|
|
694
|
+
slice(t, e) {
|
|
695
|
+
return c(typeof e > "u" ? o(this, u).slice(t) : typeof t > "u" ? o(this, u).slice(0, e) : o(this, u).slice(t, t + e));
|
|
696
|
+
}
|
|
697
|
+
sliding(t, e = 1) {
|
|
698
|
+
const s = [];
|
|
699
|
+
for (let r = 0; r < o(this, u).length && !(r + t > o(this, u).length); r += e)
|
|
700
|
+
s.push(o(this, u).slice(r, r + t));
|
|
701
|
+
return c(s.map((r) => c(r)));
|
|
702
|
+
}
|
|
703
|
+
sole(t, e) {
|
|
704
|
+
if (typeof t == "function") {
|
|
705
|
+
const s = this.filter(t);
|
|
706
|
+
return s.count() === 1 ? s.first() : null;
|
|
707
|
+
}
|
|
708
|
+
if (typeof t == "string") {
|
|
709
|
+
const s = this.where(t, e);
|
|
710
|
+
return s.count() === 1 ? s.first() : null;
|
|
711
|
+
}
|
|
712
|
+
return o(this, u).length === 1 ? this.first() : null;
|
|
713
|
+
}
|
|
714
|
+
some(...t) {
|
|
715
|
+
return this.contains(...t);
|
|
716
|
+
}
|
|
717
|
+
sort(t) {
|
|
718
|
+
return c(o(this, u).toSorted(t));
|
|
719
|
+
}
|
|
720
|
+
sortBy(t, e = "asc") {
|
|
721
|
+
if (typeof t == "function") {
|
|
722
|
+
let s = -1;
|
|
723
|
+
return c(o(this, u).toSorted((r, i) => (s++, t(r, s, this) - t(i, s, this))));
|
|
724
|
+
}
|
|
725
|
+
if (Array.isArray(t))
|
|
726
|
+
return t.every((s) => Array.isArray(s)) ? c(o(this, u).toSorted((s, r) => {
|
|
727
|
+
for (const [i, h] of t) {
|
|
728
|
+
const l = s[i] ?? -1 / 0, a = r[i] ?? -1 / 0;
|
|
729
|
+
if (l > a)
|
|
730
|
+
return h === "asc" ? 1 : -1;
|
|
731
|
+
if (l < a)
|
|
732
|
+
return h === "asc" ? -1 : 1;
|
|
733
|
+
}
|
|
734
|
+
return 0;
|
|
735
|
+
})) : c(o(this, u).toSorted((s, r) => {
|
|
736
|
+
for (const i of t) {
|
|
737
|
+
const h = i(s, r);
|
|
738
|
+
if (h !== 0)
|
|
739
|
+
return h;
|
|
740
|
+
}
|
|
741
|
+
return 0;
|
|
742
|
+
}));
|
|
743
|
+
if (typeof t != "string")
|
|
744
|
+
throw new TypeError("The key must be a string");
|
|
745
|
+
return c(o(this, u).toSorted((s, r) => {
|
|
746
|
+
const i = s[t] ?? -1 / 0, h = r[t] ?? -1 / 0;
|
|
747
|
+
return i > h ? e === "asc" ? 1 : -1 : i < h ? e === "asc" ? -1 : 1 : 0;
|
|
748
|
+
}));
|
|
749
|
+
}
|
|
750
|
+
sortDesc() {
|
|
751
|
+
return this.sort((t, e) => t > e ? -1 : t < e ? 1 : 0);
|
|
752
|
+
}
|
|
753
|
+
splice(t, e, ...s) {
|
|
754
|
+
const r = e === void 0 ? o(this, u).length : e, i = o(this, u).splice(t, r, ...s);
|
|
755
|
+
return $(this), c(i);
|
|
756
|
+
}
|
|
757
|
+
split(t) {
|
|
758
|
+
const e = [];
|
|
759
|
+
for (let s = 0; s < t; s++) {
|
|
760
|
+
const r = o(this, u).slice(
|
|
761
|
+
e.flat().length,
|
|
762
|
+
e.flat().length + Math.min(
|
|
763
|
+
Math.ceil((o(this, u).length - e.flat().length) / (t - s)),
|
|
764
|
+
o(this, u).length - e.flat().length
|
|
765
|
+
)
|
|
766
|
+
);
|
|
767
|
+
e.push(c(r));
|
|
768
|
+
}
|
|
769
|
+
return c(e);
|
|
770
|
+
}
|
|
771
|
+
splitIn(t) {
|
|
772
|
+
const e = Math.ceil(o(this, u).length / t);
|
|
773
|
+
return this.chunk(e);
|
|
774
|
+
}
|
|
775
|
+
sum(t) {
|
|
776
|
+
return typeof t == "string" ? o(this, u).reduce((e, s) => {
|
|
777
|
+
const r = s[t];
|
|
778
|
+
if (typeof r != "number")
|
|
779
|
+
throw new TypeError("The items must be numbers");
|
|
780
|
+
return e + r;
|
|
781
|
+
}, 0) : o(this, u).reduce((e, s) => {
|
|
782
|
+
if (typeof s != "number")
|
|
783
|
+
throw new TypeError("The items must be numbers");
|
|
784
|
+
return e + s;
|
|
785
|
+
}, 0);
|
|
786
|
+
}
|
|
787
|
+
take(t) {
|
|
788
|
+
return c(o(this, u).slice(0, t));
|
|
789
|
+
}
|
|
790
|
+
takeUntil(t) {
|
|
791
|
+
return typeof t == "function" ? this.take(o(this, u).findIndex((e, s) => t(e, s, this))) : this.take(o(this, u).findIndex((e) => e == t));
|
|
792
|
+
}
|
|
793
|
+
takeWhile(t) {
|
|
794
|
+
return typeof t == "function" ? this.take(o(this, u).findIndex((e, s) => !t(e, s, this))) : this.take(o(this, u).findIndex((e) => e != t));
|
|
795
|
+
}
|
|
796
|
+
tap(t) {
|
|
797
|
+
return t(this), this;
|
|
798
|
+
}
|
|
799
|
+
toArray() {
|
|
800
|
+
const t = (e) => typeof e != "object" ? e : Array.isArray(e) ? e.map(t) : e && "toArray" in e && typeof e.toArray == "function" ? e.toArray() : e && "toJson" in e && typeof e.toJson == "function" ? e.toJson() : e;
|
|
801
|
+
return o(this, u).map(t);
|
|
802
|
+
}
|
|
803
|
+
toJson() {
|
|
804
|
+
return JSON.stringify(o(this, u));
|
|
805
|
+
}
|
|
806
|
+
transform(t) {
|
|
807
|
+
for (const [e, s] of o(this, u).entries())
|
|
808
|
+
o(this, u).splice(e, 1, t(s, e, this));
|
|
809
|
+
return $(this), this;
|
|
810
|
+
}
|
|
811
|
+
unique(t) {
|
|
812
|
+
return c(typeof t == "string" ? o(this, u).filter((e, s) => !o(this, u).some((r, i) => r[t] == e[t] && i !== s)) : [...new Set(o(this, u))]);
|
|
813
|
+
}
|
|
814
|
+
uniqueStrict(t) {
|
|
815
|
+
return c(typeof t == "string" ? o(this, u).filter((e, s) => !o(this, u).some((r, i) => r[t] === e[t] && i !== s)) : [...new Set(o(this, u))]);
|
|
816
|
+
}
|
|
817
|
+
unless(t, e, s) {
|
|
818
|
+
return t ? typeof s == "function" && s(this) : e(this), this;
|
|
819
|
+
}
|
|
820
|
+
unlessEmpty(t, e) {
|
|
821
|
+
return this.whenNotEmpty(t, e);
|
|
822
|
+
}
|
|
823
|
+
unlessNotEmpty(t, e) {
|
|
824
|
+
return this.whenEmpty(t, e);
|
|
825
|
+
}
|
|
826
|
+
value(t) {
|
|
827
|
+
return o(this, u).length === 0 ? null : o(this, u)[0][t];
|
|
828
|
+
}
|
|
829
|
+
when(t, e, s) {
|
|
830
|
+
return t ? e(this) : typeof s == "function" && s(this), this;
|
|
831
|
+
}
|
|
832
|
+
whenEmpty(t, e) {
|
|
833
|
+
return this.isEmpty() ? t(this) : typeof e == "function" && e(this), this;
|
|
834
|
+
}
|
|
835
|
+
whenNotEmpty(t, e) {
|
|
836
|
+
return this.isNotEmpty() ? t(this) : typeof e == "function" && e(this), this;
|
|
837
|
+
}
|
|
838
|
+
where(t, e, s) {
|
|
839
|
+
if (typeof s > "u")
|
|
840
|
+
return c(o(this, u).filter((i) => i[t] == e));
|
|
841
|
+
if (typeof e != "string")
|
|
842
|
+
throw new TypeError("The operator must be a string");
|
|
843
|
+
if (s === null)
|
|
844
|
+
return c(o(this, u).filter((i) => i[t] === null));
|
|
845
|
+
const r = {
|
|
846
|
+
"=": (i) => i[t] == s,
|
|
847
|
+
"!=": (i) => i[t] != s,
|
|
848
|
+
">": (i) => i[t] > s,
|
|
849
|
+
"<": (i) => i[t] < s,
|
|
850
|
+
">=": (i) => i[t] >= s,
|
|
851
|
+
"<=": (i) => i[t] <= s
|
|
852
|
+
};
|
|
853
|
+
if (!(e in r))
|
|
854
|
+
throw new Error("Unsupported operator");
|
|
855
|
+
return c(o(this, u).filter((i, h) => r[e](i, h, this)));
|
|
856
|
+
}
|
|
857
|
+
whereStrict(t, e, s) {
|
|
858
|
+
if (typeof s > "u")
|
|
859
|
+
return c(o(this, u).filter((i) => i[t] === e));
|
|
860
|
+
if (typeof e != "string")
|
|
861
|
+
throw new TypeError("The operator must be a string");
|
|
862
|
+
if (s === null)
|
|
863
|
+
return c(o(this, u).filter((i) => i[t] === null));
|
|
864
|
+
const r = {
|
|
865
|
+
"=": (i) => i[t] === s,
|
|
866
|
+
"!=": (i) => i[t] !== s,
|
|
867
|
+
">": (i) => i[t] > s,
|
|
868
|
+
"<": (i) => i[t] < s,
|
|
869
|
+
">=": (i) => i[t] >= s,
|
|
870
|
+
"<=": (i) => i[t] <= s
|
|
871
|
+
};
|
|
872
|
+
if (!(e in r))
|
|
873
|
+
throw new Error("Unsupported operator");
|
|
874
|
+
return c(o(this, u).filter((i, h) => r[e](i, h, this)));
|
|
875
|
+
}
|
|
876
|
+
whereBetween(t, [e, s]) {
|
|
877
|
+
return c(o(this, u).filter((r) => r[t] >= e && r[t] <= s));
|
|
878
|
+
}
|
|
879
|
+
whereIn(t, e) {
|
|
880
|
+
return c(o(this, u).filter((s) => e.includes(s[t])));
|
|
881
|
+
}
|
|
882
|
+
whereInstanceOf(t) {
|
|
883
|
+
return c(o(this, u).filter((e) => e instanceof t));
|
|
884
|
+
}
|
|
885
|
+
whereNotBetween(t, [e, s]) {
|
|
886
|
+
return c(o(this, u).filter((r) => r[t] < e || r[t] > s));
|
|
887
|
+
}
|
|
888
|
+
whereNotIn(t, e) {
|
|
889
|
+
return c(o(this, u).filter((s) => !e.includes(s[t])));
|
|
890
|
+
}
|
|
891
|
+
whereNotNull(t) {
|
|
892
|
+
return c(o(this, u).filter((e) => e[t] !== null));
|
|
893
|
+
}
|
|
894
|
+
whereNull(t) {
|
|
895
|
+
return c(o(this, u).filter((e) => e[t] === null));
|
|
896
|
+
}
|
|
897
|
+
zip(t) {
|
|
898
|
+
return Array.isArray(t) ? c(
|
|
899
|
+
o(this, u).map((e, s) => [e, t[s] ?? null])
|
|
900
|
+
) : c(
|
|
901
|
+
o(this, u).map((e, s) => [e, t.get(s)])
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
// HasEvents methods
|
|
905
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
906
|
+
on(t, e) {
|
|
907
|
+
throw new y();
|
|
908
|
+
}
|
|
909
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
910
|
+
once(t, e) {
|
|
911
|
+
throw new y();
|
|
912
|
+
}
|
|
913
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
914
|
+
emit(t, e) {
|
|
915
|
+
throw new y();
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
Ft = Symbol.toStringTag, u = new WeakMap(), p(j, "name", "Collection");
|
|
919
|
+
const J = A(j);
|
|
920
|
+
class L extends j {
|
|
921
|
+
intersect(t) {
|
|
922
|
+
return this.filter((e) => t.some((s) => s.getKey() === e.getKey()));
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
p(L, "name", "Collection");
|
|
926
|
+
const S = (...n) => {
|
|
927
|
+
const t = new URLSearchParams();
|
|
928
|
+
return n.forEach((e) => {
|
|
929
|
+
const [, ...s] = e.split("?");
|
|
930
|
+
new URLSearchParams(s.join("")).forEach((i, h) => {
|
|
931
|
+
t.set(h, i);
|
|
932
|
+
});
|
|
933
|
+
}), t;
|
|
934
|
+
};
|
|
935
|
+
class z extends Error {
|
|
936
|
+
constructor(t) {
|
|
937
|
+
super(`[Luminix] Model "${t}" does not have a primary key`);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
p(z, "name", "ModelWithoutPrimaryKeyException");
|
|
941
|
+
const wt = A(v);
|
|
942
|
+
class bt {
|
|
943
|
+
constructor(t, e, s = {}) {
|
|
944
|
+
p(this, "bag");
|
|
945
|
+
this.facades = t, this.abstract = e, this.query = s, this.bag = new wt(s), this.bag.on("change", () => {
|
|
946
|
+
this.emit("change", {
|
|
947
|
+
data: this.bag
|
|
948
|
+
});
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
952
|
+
on(t, e) {
|
|
953
|
+
throw new y();
|
|
954
|
+
}
|
|
955
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
956
|
+
once(t, e) {
|
|
957
|
+
throw new y();
|
|
958
|
+
}
|
|
959
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
960
|
+
emit(t, e) {
|
|
961
|
+
throw new y();
|
|
962
|
+
}
|
|
963
|
+
lock(t) {
|
|
964
|
+
this.bag.lock(t);
|
|
965
|
+
}
|
|
966
|
+
whereBetween(t, e) {
|
|
967
|
+
return this.bag.has("filters") || this.bag.set("filters", {}), this.bag.set(`filters.${f.camelCase(t)}Between`, e), this;
|
|
968
|
+
}
|
|
969
|
+
whereNotBetween(t, e) {
|
|
970
|
+
return this.bag.has("filters") || this.bag.set("filters", {}), this.bag.set(`filters.${f.camelCase(t)}NotBetween`, e), this;
|
|
971
|
+
}
|
|
972
|
+
whereNull(t) {
|
|
973
|
+
return this.bag.has("filters") || this.bag.set("filters", {}), this.bag.set(`filters.${f.camelCase(t)}Null`, !0), this;
|
|
974
|
+
}
|
|
975
|
+
whereNotNull(t) {
|
|
976
|
+
return this.bag.has("filters") || this.bag.set("filters", {}), this.bag.set(`filters.${f.camelCase(t)}NotNull`, !0), this;
|
|
977
|
+
}
|
|
978
|
+
limit(t) {
|
|
979
|
+
return this.bag.set("per_page", t), this;
|
|
980
|
+
}
|
|
981
|
+
where(t, e, s) {
|
|
982
|
+
if (this.bag.has("filters") || this.bag.set("filters", {}), typeof t == "function")
|
|
983
|
+
return t(this) || this;
|
|
984
|
+
if (typeof s > "u")
|
|
985
|
+
return this.bag.set(`filters.${f.camelCase(t)}`, e), this;
|
|
986
|
+
if (typeof e != "string")
|
|
987
|
+
throw new Error(`Invalid operator ${e} provided for where clause.`);
|
|
988
|
+
const i = {
|
|
989
|
+
"=": "",
|
|
990
|
+
"!=": "NotEquals",
|
|
991
|
+
">": "GreaterThan",
|
|
992
|
+
">=": "GreaterThanOrEquals",
|
|
993
|
+
"<": "LessThan",
|
|
994
|
+
"<=": "LessThanOrEquals"
|
|
995
|
+
}[e] || f.upperFirst(f.camelCase(e));
|
|
996
|
+
return this.bag.set(`filters.${f.camelCase(t)}${i}`, s), this;
|
|
997
|
+
}
|
|
998
|
+
orderBy(t, e = "asc") {
|
|
999
|
+
return this.bag.set("order_by", `${t}:${e}`), this;
|
|
1000
|
+
}
|
|
1001
|
+
searchBy(t) {
|
|
1002
|
+
return this.bag.set("q", t), this;
|
|
1003
|
+
}
|
|
1004
|
+
minified() {
|
|
1005
|
+
return this.bag.set("minified", !0), this;
|
|
1006
|
+
}
|
|
1007
|
+
unset(t) {
|
|
1008
|
+
return this.bag.delete(t), this;
|
|
1009
|
+
}
|
|
1010
|
+
async exec(t = 1, e) {
|
|
1011
|
+
try {
|
|
1012
|
+
this.bag.set("page", t), this.emit("submit", {
|
|
1013
|
+
data: this.bag
|
|
1014
|
+
});
|
|
1015
|
+
const { data: s } = await this.facades.route.call(`luminix.${this.abstract}.index`, {
|
|
1016
|
+
params: this.bag.all(),
|
|
1017
|
+
errorBag: `${this.abstract}.fetch`
|
|
1018
|
+
}), r = this.facades.model.make(this.abstract), i = c(s.data.map((h) => {
|
|
1019
|
+
const l = new r(h);
|
|
1020
|
+
return l.exists = !0, this.facades.model.emit("fetch", {
|
|
1021
|
+
class: this.abstract,
|
|
1022
|
+
model: l
|
|
1023
|
+
}), l;
|
|
1024
|
+
}), L);
|
|
1025
|
+
if (e) {
|
|
1026
|
+
const [h] = e.split("?");
|
|
1027
|
+
return {
|
|
1028
|
+
...s,
|
|
1029
|
+
data: i,
|
|
1030
|
+
links: {
|
|
1031
|
+
first: `${h}?${S(e, s.links.first).toString()}`,
|
|
1032
|
+
last: `${h}?${S(e, s.links.last).toString()}`,
|
|
1033
|
+
next: s.links.next && `${h}?${S(e, s.links.next).toString()}`,
|
|
1034
|
+
prev: s.links.prev && `${h}?${S(e, s.links.prev).toString()}`
|
|
1035
|
+
},
|
|
1036
|
+
meta: {
|
|
1037
|
+
...s.meta,
|
|
1038
|
+
links: s.meta.links.map((l) => ({
|
|
1039
|
+
...l,
|
|
1040
|
+
url: l.url && `${h}?${S(e, l.url).toString()}`
|
|
1041
|
+
}))
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
return {
|
|
1046
|
+
...s,
|
|
1047
|
+
data: i
|
|
1048
|
+
};
|
|
1049
|
+
} catch (s) {
|
|
1050
|
+
throw this.emit("error", {
|
|
1051
|
+
error: s
|
|
1052
|
+
}), s;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
async get(t = 1, e) {
|
|
1056
|
+
const s = await this.exec(t, e);
|
|
1057
|
+
return this.emit("success", {
|
|
1058
|
+
response: s,
|
|
1059
|
+
items: s.data
|
|
1060
|
+
}), s;
|
|
1061
|
+
}
|
|
1062
|
+
async first() {
|
|
1063
|
+
const t = await this.limit(1).exec(1);
|
|
1064
|
+
return this.emit("success", {
|
|
1065
|
+
response: t,
|
|
1066
|
+
items: t.data.first()
|
|
1067
|
+
}), t.data.first();
|
|
1068
|
+
}
|
|
1069
|
+
async find(t) {
|
|
1070
|
+
const e = this.facades.model.schema(this.abstract).primaryKey;
|
|
1071
|
+
if (!e)
|
|
1072
|
+
throw new z(this.abstract);
|
|
1073
|
+
const s = await this.where(e, t).limit(1).exec(1);
|
|
1074
|
+
return this.emit("success", {
|
|
1075
|
+
response: s,
|
|
1076
|
+
items: s.data.sole()
|
|
1077
|
+
}), s.data.sole();
|
|
1078
|
+
}
|
|
1079
|
+
async all() {
|
|
1080
|
+
const t = this.facades.config.get("luminix.backend.api.max_per_page", 150), e = await this.limit(t).exec(1), s = e.meta.last_page;
|
|
1081
|
+
if (s === 1)
|
|
1082
|
+
return e.data;
|
|
1083
|
+
const r = Array.from({ length: s - 1 }, (l, a) => a + 2), i = await Promise.all(
|
|
1084
|
+
r.map((l) => this.limit(t).exec(l))
|
|
1085
|
+
), h = c(
|
|
1086
|
+
i.reduce((l, a) => (l.push(...a.data), l), e.data).all(),
|
|
1087
|
+
L
|
|
1088
|
+
);
|
|
1089
|
+
return this.emit("success", {
|
|
1090
|
+
response: {
|
|
1091
|
+
...e,
|
|
1092
|
+
data: h
|
|
1093
|
+
},
|
|
1094
|
+
items: h
|
|
1095
|
+
}), h;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
const Et = A(bt);
|
|
1099
|
+
class _ extends Error {
|
|
1100
|
+
constructor(t) {
|
|
1101
|
+
super(`[Luminix] Expected ${t} to be reducible.`);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
p(_, "name", "NotReducibleException");
|
|
1105
|
+
class E extends TypeError {
|
|
1106
|
+
constructor(t, e = "Model") {
|
|
1107
|
+
super(`[Luminix] "${t}" expects ${e}`);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
p(E, "name", "NotModelException");
|
|
1111
|
+
class O extends Error {
|
|
1112
|
+
constructor(t, e, s, r) {
|
|
1113
|
+
super(`[Luminix] Could not determine inverse relation for "${e}" in model "${t}". Please specify a relation in model "${s}" of type ${r} that points back to "${t}".`);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
p(O, "name", "NoInverseRelationException");
|
|
1117
|
+
class k extends Error {
|
|
1118
|
+
constructor(t) {
|
|
1119
|
+
super(`[Luminix] Relation "${t}" is not supported`);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
p(k, "name", "UnsupportedRelationException");
|
|
1123
|
+
function w(n) {
|
|
1124
|
+
return typeof n == "object" && n !== null && n.__isModel === !0;
|
|
1125
|
+
}
|
|
1126
|
+
class F {
|
|
1127
|
+
constructor(t, e, s, r = null) {
|
|
1128
|
+
p(this, "unsubscribeQuery", null);
|
|
1129
|
+
if (this.meta = t, this.facades = e, this.parent = s, this.items = r, r !== null && !w(r) && !(T(r) && r.every(w)))
|
|
1130
|
+
throw new E("Relation.constructor()", "Model, Collection<Model> or null");
|
|
1131
|
+
}
|
|
1132
|
+
make(t) {
|
|
1133
|
+
const e = this.getRelated();
|
|
1134
|
+
if (t === null || typeof t > "u") {
|
|
1135
|
+
this.set(null);
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
if (this.isSingle()) {
|
|
1139
|
+
if (typeof t != "object" || Array.isArray(t))
|
|
1140
|
+
throw new TypeError("Relation.make() expects an object");
|
|
1141
|
+
this.set(new e(t));
|
|
1142
|
+
}
|
|
1143
|
+
if (this.isMultiple()) {
|
|
1144
|
+
if (!Array.isArray(t))
|
|
1145
|
+
throw new TypeError("Relation.make() expects an array");
|
|
1146
|
+
this.set(c(t.map((s) => new e(s))));
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
guessInverseRelation() {
|
|
1150
|
+
const { relations: t } = this.getRelated().getSchema();
|
|
1151
|
+
if (typeof this.facades.model.guessInverseRelation != "function")
|
|
1152
|
+
throw new _("ModelFacade");
|
|
1153
|
+
const e = this.getType(), s = this.facades.model.guessInverseRelation({
|
|
1154
|
+
HasOne: ["BelongsTo"],
|
|
1155
|
+
HasMany: ["BelongsTo"],
|
|
1156
|
+
BelongsTo: ["HasOne", "HasMany"],
|
|
1157
|
+
BelongsToMany: ["BelongsToMany"],
|
|
1158
|
+
MorphTo: ["MorphMany", "MorphOne"],
|
|
1159
|
+
MorphOne: ["MorphTo"],
|
|
1160
|
+
MorphMany: ["MorphTo"],
|
|
1161
|
+
MorphToMany: ["MorphToMany"]
|
|
1162
|
+
}, this.parent, e, this.getRelated());
|
|
1163
|
+
if (!(e in s))
|
|
1164
|
+
throw new k(e);
|
|
1165
|
+
for (const r in t) {
|
|
1166
|
+
const i = t[r];
|
|
1167
|
+
if ((i.model === this.parent.getType() || ["MorphOne", "MorphMany"].includes(e)) && s[e].includes(i.type))
|
|
1168
|
+
return r;
|
|
1169
|
+
}
|
|
1170
|
+
throw new O(this.parent.getType(), e, this.getRelated().getSchemaName(), s[e].join(" or "));
|
|
1171
|
+
}
|
|
1172
|
+
set(t) {
|
|
1173
|
+
if (t !== null && !w(t) && !(T(t) && t.every(w)))
|
|
1174
|
+
throw new E("Relation.set()", "Model, Collection<Model> or null");
|
|
1175
|
+
!this.items || w(this.items) ? this.items = t : T(t) && this.items.splice(0, this.items.count(), ...t);
|
|
1176
|
+
}
|
|
1177
|
+
getForeignKey() {
|
|
1178
|
+
return this.meta.foreignKey;
|
|
1179
|
+
}
|
|
1180
|
+
getName() {
|
|
1181
|
+
return this.meta.name;
|
|
1182
|
+
}
|
|
1183
|
+
getType() {
|
|
1184
|
+
return this.meta.type;
|
|
1185
|
+
}
|
|
1186
|
+
getModel() {
|
|
1187
|
+
return this.meta.model;
|
|
1188
|
+
}
|
|
1189
|
+
getRelated() {
|
|
1190
|
+
return this.facades.model.make(this.meta.model);
|
|
1191
|
+
}
|
|
1192
|
+
query() {
|
|
1193
|
+
const t = this.getRelated().query();
|
|
1194
|
+
return this.unsubscribeQuery && this.unsubscribeQuery(), this.unsubscribeQuery = t.on("success", (e) => {
|
|
1195
|
+
this.items = e.items;
|
|
1196
|
+
}), t;
|
|
1197
|
+
}
|
|
1198
|
+
isLoaded() {
|
|
1199
|
+
return this.items !== null;
|
|
1200
|
+
}
|
|
1201
|
+
getLoadedItems() {
|
|
1202
|
+
return this.items;
|
|
1203
|
+
}
|
|
1204
|
+
isSingle() {
|
|
1205
|
+
return w(this.items);
|
|
1206
|
+
}
|
|
1207
|
+
isMultiple() {
|
|
1208
|
+
return T(this.items);
|
|
1209
|
+
}
|
|
1210
|
+
getParent() {
|
|
1211
|
+
return this.parent;
|
|
1212
|
+
}
|
|
1213
|
+
where(...t) {
|
|
1214
|
+
return this.query().where(...t);
|
|
1215
|
+
}
|
|
1216
|
+
whereNull(t) {
|
|
1217
|
+
return this.query().whereNull(t);
|
|
1218
|
+
}
|
|
1219
|
+
whereNotNull(t) {
|
|
1220
|
+
return this.query().whereNotNull(t);
|
|
1221
|
+
}
|
|
1222
|
+
whereBetween(t, e) {
|
|
1223
|
+
return this.query().whereBetween(t, e);
|
|
1224
|
+
}
|
|
1225
|
+
whereNotBetween(t, e) {
|
|
1226
|
+
return this.query().whereNotBetween(t, e);
|
|
1227
|
+
}
|
|
1228
|
+
orderBy(t, e = "asc") {
|
|
1229
|
+
return this.query().orderBy(t, e);
|
|
1230
|
+
}
|
|
1231
|
+
searchBy(t) {
|
|
1232
|
+
return this.query().searchBy(t);
|
|
1233
|
+
}
|
|
1234
|
+
minified() {
|
|
1235
|
+
return this.query().minified();
|
|
1236
|
+
}
|
|
1237
|
+
limit(t) {
|
|
1238
|
+
return this.query().limit(t);
|
|
1239
|
+
}
|
|
1240
|
+
// get(page = 1, perPage = 15, replaceLinksWith?: string): Promise<ModelPaginatedResponse>
|
|
1241
|
+
// {
|
|
1242
|
+
// return this.query().get(page, perPage, replaceLinksWith);
|
|
1243
|
+
// }
|
|
1244
|
+
// first(): Promise<Model | null>
|
|
1245
|
+
// {
|
|
1246
|
+
// return this.query().first();
|
|
1247
|
+
// }
|
|
1248
|
+
// find(id: string | number): Promise<Model | null>
|
|
1249
|
+
// {
|
|
1250
|
+
// return this.query().find(id);
|
|
1251
|
+
// }
|
|
1252
|
+
}
|
|
1253
|
+
class U extends Error {
|
|
1254
|
+
constructor(t, e) {
|
|
1255
|
+
super(`[Luminix] Attribute "${e}" in model "${t}" is not fillable`);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
p(U, "name", "AttributeNotFillableException");
|
|
1259
|
+
class D extends Error {
|
|
1260
|
+
constructor(t, e) {
|
|
1261
|
+
super(`[Luminix] Model "${t}" must be persisted before calling "${e}"`);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
p(D, "name", "ModelNotPersistedException");
|
|
1265
|
+
function xt(n, t) {
|
|
1266
|
+
class e {
|
|
1267
|
+
constructor(r = {}) {
|
|
1268
|
+
p(this, "_attributes", new v({}));
|
|
1269
|
+
p(this, "_original", {});
|
|
1270
|
+
p(this, "_relations", {});
|
|
1271
|
+
p(this, "_changedKeys", []);
|
|
1272
|
+
p(this, "exists", !1);
|
|
1273
|
+
this.makeRelations(), this.makeAttributes(r);
|
|
1274
|
+
}
|
|
1275
|
+
cast(r, i) {
|
|
1276
|
+
return r == null || !i ? r : ["boolean", "bool"].includes(i) ? !!r : ["date", "datetime", "immutable_date", "immutable_datetime"].includes(i) && typeof r == "string" ? new Date(r) : ["float", "double", "integer", "int"].includes(i) || i.startsWith("decimal:") ? Number(r) : r;
|
|
1277
|
+
}
|
|
1278
|
+
mutate(r, i) {
|
|
1279
|
+
return r == null || !i ? r : ["boolean", "bool"].includes(i) ? !!r : ["date", "datetime", "immutable_date", "immutable_datetime"].includes(i) && r instanceof Date ? r.toISOString() : ["float", "double", "integer", "int"].includes(i) || i.startsWith("decimal:") ? Number(r) : r;
|
|
1280
|
+
}
|
|
1281
|
+
makeRelations() {
|
|
1282
|
+
const { relations: r } = n.model.schema(t);
|
|
1283
|
+
if (this._relations = {}, !r)
|
|
1284
|
+
return;
|
|
1285
|
+
if (typeof n.model.relationMap != "function")
|
|
1286
|
+
throw new _("ModelFacade");
|
|
1287
|
+
const i = n.model.relationMap({}, t);
|
|
1288
|
+
Object.entries(r).forEach(([h, l]) => {
|
|
1289
|
+
const { type: a } = l, d = a in i ? i[a] : F;
|
|
1290
|
+
this._relations[h] = new d(
|
|
1291
|
+
{ name: h, ...l },
|
|
1292
|
+
n,
|
|
1293
|
+
this,
|
|
1294
|
+
null
|
|
1295
|
+
);
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
makeAttributes(r) {
|
|
1299
|
+
const { relations: i } = n.model.schema(t), h = Object.keys(i || {}), l = f.omit(r, h);
|
|
1300
|
+
if (this.fillable.filter((a) => !(a in l)).forEach((a) => {
|
|
1301
|
+
l[a] = null;
|
|
1302
|
+
}), i && Object.keys(i).forEach((a) => {
|
|
1303
|
+
this.relation(f.camelCase(a)).make(r[a]);
|
|
1304
|
+
}), !this.validateJsonObject(l)) {
|
|
1305
|
+
if (n.config.get("app.env", "production") === "production")
|
|
1306
|
+
throw new TypeError(`[Luminix] Invalid attributes for model "${t}"`);
|
|
1307
|
+
n.log.warning(`Invalid attributes for model "${t}".
|
|
1308
|
+
This will throw an error in production.`, {
|
|
1309
|
+
attributes: r,
|
|
1310
|
+
abstract: t
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
this._attributes = new v(l), this._original = l, this._changedKeys = [];
|
|
1314
|
+
}
|
|
1315
|
+
makePrimaryKeyReplacer() {
|
|
1316
|
+
return {
|
|
1317
|
+
[this.primaryKey]: this.getAttribute(this.primaryKey)
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
dispatchChangeEvent(r) {
|
|
1321
|
+
this.emit("change", {
|
|
1322
|
+
value: r
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
dispatchCreateEvent(r) {
|
|
1326
|
+
this.emit("create", {
|
|
1327
|
+
value: r
|
|
1328
|
+
}), n.model.emit("create", {
|
|
1329
|
+
class: t,
|
|
1330
|
+
model: this
|
|
1331
|
+
});
|
|
1332
|
+
}
|
|
1333
|
+
dispatchUpdateEvent(r) {
|
|
1334
|
+
this.emit("update", {
|
|
1335
|
+
value: r
|
|
1336
|
+
}), n.model.emit("update", {
|
|
1337
|
+
class: t,
|
|
1338
|
+
model: this
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1341
|
+
dispatchSaveEvent() {
|
|
1342
|
+
this.emit("save", {
|
|
1343
|
+
value: this.diff()
|
|
1344
|
+
}), n.model.emit("save", {
|
|
1345
|
+
class: t,
|
|
1346
|
+
model: this
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
dispatchDeleteEvent(r = !1) {
|
|
1350
|
+
this.emit("delete", {
|
|
1351
|
+
force: r,
|
|
1352
|
+
[this.getKeyName()]: this.getKey()
|
|
1353
|
+
}), n.model.emit("delete", {
|
|
1354
|
+
class: t,
|
|
1355
|
+
model: this,
|
|
1356
|
+
force: r
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
dispatchRestoreEvent() {
|
|
1360
|
+
this.emit("restore", {
|
|
1361
|
+
value: this.attributes
|
|
1362
|
+
}), n.model.emit("restore", {
|
|
1363
|
+
class: t,
|
|
1364
|
+
model: this
|
|
1365
|
+
});
|
|
1366
|
+
}
|
|
1367
|
+
dispatchErrorEvent(r, i) {
|
|
1368
|
+
this.emit("error", {
|
|
1369
|
+
error: r,
|
|
1370
|
+
operation: i
|
|
1371
|
+
}), n.model.emit("error", {
|
|
1372
|
+
class: t,
|
|
1373
|
+
model: this,
|
|
1374
|
+
error: r,
|
|
1375
|
+
operation: i
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
updateChangedKeys(r) {
|
|
1379
|
+
const i = (h, l) => typeof h == "object" && h !== null ? f.isEqual(h, l) : h == l;
|
|
1380
|
+
!this._changedKeys.includes(r) && !i(this._original[r], this._attributes.get("key")) ? this._changedKeys.push(r) : this._changedKeys.includes(r) && i(this._original[r], this._attributes.get("key")) && this._changedKeys.splice(this._changedKeys.indexOf(r), 1);
|
|
1381
|
+
}
|
|
1382
|
+
validateJsonObject(r) {
|
|
1383
|
+
return typeof r != "object" || r === null ? !1 : Object.entries(r).every(([, i]) => ["boolean", "number", "string"].includes(typeof i) || i === null || this.validateJsonObject(i) || Array.isArray(i) && i.every((h) => this.validateJsonObject(h)));
|
|
1384
|
+
}
|
|
1385
|
+
get attributes() {
|
|
1386
|
+
return this._attributes.all();
|
|
1387
|
+
}
|
|
1388
|
+
get original() {
|
|
1389
|
+
return this._original;
|
|
1390
|
+
}
|
|
1391
|
+
get relations() {
|
|
1392
|
+
return this._relations;
|
|
1393
|
+
}
|
|
1394
|
+
get fillable() {
|
|
1395
|
+
return n.model.schema(t).fillable;
|
|
1396
|
+
}
|
|
1397
|
+
get primaryKey() {
|
|
1398
|
+
return n.model.schema(t).primaryKey;
|
|
1399
|
+
}
|
|
1400
|
+
get timestamps() {
|
|
1401
|
+
return n.model.schema(t).timestamps;
|
|
1402
|
+
}
|
|
1403
|
+
// get softDeletes() {
|
|
1404
|
+
// return facades.model.schema(abstract).softDeletes;
|
|
1405
|
+
// }
|
|
1406
|
+
get casts() {
|
|
1407
|
+
return {
|
|
1408
|
+
...n.model.schema(t).casts,
|
|
1409
|
+
...this.timestamps ? { created_at: "datetime", updated_at: "datetime" } : {}
|
|
1410
|
+
// ...this.softDeletes ? { deleted_at: 'datetime' } : {},
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
get isDirty() {
|
|
1414
|
+
return this._changedKeys.length > 0;
|
|
1415
|
+
}
|
|
1416
|
+
getAttribute(r) {
|
|
1417
|
+
let i = this._attributes.get(r, null);
|
|
1418
|
+
r in this.casts && (i = this.cast(i, this.casts[r]));
|
|
1419
|
+
const h = n.model[`model${f.upperFirst(f.camelCase(t))}Get${f.upperFirst(f.camelCase(r))}Attribute`];
|
|
1420
|
+
if (typeof h != "function")
|
|
1421
|
+
throw new _("ModelFacade");
|
|
1422
|
+
return h.bind(n.model)(i, this);
|
|
1423
|
+
}
|
|
1424
|
+
setAttribute(r, i) {
|
|
1425
|
+
if (!this.fillable.includes(r)) {
|
|
1426
|
+
if (n.config.get("app.env", "production") === "production")
|
|
1427
|
+
throw new U(t, r);
|
|
1428
|
+
n.log.warning(`[Luminix] Trying to set a non-fillable attribute "${r}" in model "${t}".
|
|
1429
|
+
This will throw an error in production.`);
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
const h = n.model[`model${f.upperFirst(f.camelCase(t))}Set${f.upperFirst(f.camelCase(r))}Attribute`];
|
|
1433
|
+
if (typeof h != "function")
|
|
1434
|
+
throw new _("ModelFacade");
|
|
1435
|
+
const l = h.bind(n.model)(
|
|
1436
|
+
this.mutate(i, this.casts[r]),
|
|
1437
|
+
this
|
|
1438
|
+
);
|
|
1439
|
+
if (!this.validateJsonObject({ [r]: l })) {
|
|
1440
|
+
if (n.config.get("app.env", "production") === "production")
|
|
1441
|
+
throw new TypeError(`[Luminix] Attribute "${r}" in model "${t}" must be a boolean, number, string or null`);
|
|
1442
|
+
n.log.warning(`Invalid type for attribute "${r}" in model "${t}" after mutation.
|
|
1443
|
+
This will throw an error in production.`, {
|
|
1444
|
+
key: r,
|
|
1445
|
+
value: i,
|
|
1446
|
+
mutated: l,
|
|
1447
|
+
cast: this.casts[r],
|
|
1448
|
+
item: this.toJson()
|
|
1449
|
+
});
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
1452
|
+
this._attributes.set(r, l), this.updateChangedKeys(r), this.dispatchChangeEvent({ [r]: l });
|
|
1453
|
+
}
|
|
1454
|
+
getKey() {
|
|
1455
|
+
return this.getAttribute(this.primaryKey);
|
|
1456
|
+
}
|
|
1457
|
+
getKeyName() {
|
|
1458
|
+
return this.primaryKey;
|
|
1459
|
+
}
|
|
1460
|
+
fill(r) {
|
|
1461
|
+
const i = f.pick(r, this.fillable), h = Object.entries(i).reduce((l, [a, d]) => {
|
|
1462
|
+
const g = n.model[`model${f.upperFirst(f.camelCase(t))}Set${f.upperFirst(f.camelCase(a))}Attribute`];
|
|
1463
|
+
if (typeof g != "function")
|
|
1464
|
+
throw new _("ModelFacade");
|
|
1465
|
+
return l[a] = g.bind(n.model)(
|
|
1466
|
+
this.mutate(d, this.casts[a]),
|
|
1467
|
+
this
|
|
1468
|
+
), l;
|
|
1469
|
+
}, {});
|
|
1470
|
+
if (!this.validateJsonObject(h)) {
|
|
1471
|
+
if (n.config.get("app.env", "production") === "production")
|
|
1472
|
+
throw new TypeError(`[Luminix] Invalid attributes for model "${t}"`);
|
|
1473
|
+
n.log.warning(`Invalid attributes for model "${t}" after mutation.
|
|
1474
|
+
This will throw an error in production.`, {
|
|
1475
|
+
attributes: r,
|
|
1476
|
+
mutatedAttributes: h,
|
|
1477
|
+
item: this.toJson(),
|
|
1478
|
+
casts: this.casts
|
|
1479
|
+
});
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
this._attributes.merge(".", h), Object.keys(h).forEach((l) => this.updateChangedKeys(l)), this.dispatchChangeEvent(h);
|
|
1483
|
+
}
|
|
1484
|
+
dump() {
|
|
1485
|
+
n.log.info({
|
|
1486
|
+
...this.toJson(),
|
|
1487
|
+
[Symbol.toStringTag]: f.upperFirst(f.camelCase(t))
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
toJson() {
|
|
1491
|
+
const r = Object.entries(this.relations).reduce((h, [l, a]) => (a.isLoaded() && (a.isSingle() ? h[f.snakeCase(l)] = a.getLoadedItems().toJson() : a.isMultiple() && (h[f.snakeCase(l)] = a.getLoadedItems().map((d) => d.toJson()).all())), h), {}), i = n.model[`model${f.upperFirst(f.camelCase(t))}Json`];
|
|
1492
|
+
if (typeof i != "function")
|
|
1493
|
+
throw new _("ModelFacade");
|
|
1494
|
+
return i.bind(n.model)({
|
|
1495
|
+
...this.attributes,
|
|
1496
|
+
...r
|
|
1497
|
+
}, this);
|
|
1498
|
+
}
|
|
1499
|
+
diff() {
|
|
1500
|
+
return this._changedKeys.reduce((r, i) => (r[i] = this._attributes.get(i), r), {});
|
|
1501
|
+
}
|
|
1502
|
+
getType() {
|
|
1503
|
+
return t;
|
|
1504
|
+
}
|
|
1505
|
+
relation(r) {
|
|
1506
|
+
if (r === f.camelCase(r))
|
|
1507
|
+
return this.relations[f.snakeCase(r)];
|
|
1508
|
+
}
|
|
1509
|
+
async refresh() {
|
|
1510
|
+
if (!this.exists)
|
|
1511
|
+
throw new D(t, "refresh");
|
|
1512
|
+
const { data: r } = await n.route.call([
|
|
1513
|
+
`luminix.${t}.show`,
|
|
1514
|
+
this.makePrimaryKeyReplacer()
|
|
1515
|
+
], {
|
|
1516
|
+
errorBag: `${t}[${this.getKey()}].fetch`
|
|
1517
|
+
});
|
|
1518
|
+
this.makeAttributes(r);
|
|
1519
|
+
}
|
|
1520
|
+
async save(r = {}) {
|
|
1521
|
+
try {
|
|
1522
|
+
const {
|
|
1523
|
+
additionalPayload: i = {},
|
|
1524
|
+
sendsOnlyModifiedFields: h = !0
|
|
1525
|
+
} = r, l = this.exists, a = l ? [
|
|
1526
|
+
`luminix.${t}.update`,
|
|
1527
|
+
this.makePrimaryKeyReplacer()
|
|
1528
|
+
] : `luminix.${t}.store`, d = await n.route.call(
|
|
1529
|
+
a,
|
|
1530
|
+
{
|
|
1531
|
+
data: {
|
|
1532
|
+
...f.pick(
|
|
1533
|
+
h && l ? this.diff() : this.attributes,
|
|
1534
|
+
this.fillable
|
|
1535
|
+
),
|
|
1536
|
+
...i
|
|
1537
|
+
},
|
|
1538
|
+
errorBag: l ? `${t}[${this.getKey()}].update` : `${t}.store`
|
|
1539
|
+
}
|
|
1540
|
+
);
|
|
1541
|
+
if ([200, 201].includes(d.status))
|
|
1542
|
+
return this.makeAttributes(d.data), this.exists = !0, this.dispatchSaveEvent(), l ? this.dispatchUpdateEvent(d.data) : this.dispatchCreateEvent(d.data), d;
|
|
1543
|
+
throw d;
|
|
1544
|
+
} catch (i) {
|
|
1545
|
+
throw n.log.error(i), this.dispatchErrorEvent(i, "save"), i;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
async push() {
|
|
1549
|
+
throw new y();
|
|
1550
|
+
}
|
|
1551
|
+
async delete() {
|
|
1552
|
+
try {
|
|
1553
|
+
const r = await n.route.call([
|
|
1554
|
+
`luminix.${t}.destroy`,
|
|
1555
|
+
this.makePrimaryKeyReplacer()
|
|
1556
|
+
], {
|
|
1557
|
+
errorBag: `${t}[${this.getKey()}].delete`
|
|
1558
|
+
});
|
|
1559
|
+
if (r.status === 204)
|
|
1560
|
+
return this.dispatchDeleteEvent(), r;
|
|
1561
|
+
throw r;
|
|
1562
|
+
} catch (r) {
|
|
1563
|
+
throw n.log.error(r), this.dispatchErrorEvent(r, "delete"), r;
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
async update(r) {
|
|
1567
|
+
try {
|
|
1568
|
+
const i = await n.route.call([
|
|
1569
|
+
`luminix.${t}.update`,
|
|
1570
|
+
this.makePrimaryKeyReplacer()
|
|
1571
|
+
], {
|
|
1572
|
+
data: r,
|
|
1573
|
+
errorBag: `${t}[${this.getKey()}].update`
|
|
1574
|
+
});
|
|
1575
|
+
if (i.status === 200) {
|
|
1576
|
+
this.makeAttributes(i.data), this.dispatchUpdateEvent(i.data);
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
throw i;
|
|
1580
|
+
} catch (i) {
|
|
1581
|
+
throw n.log.error(i), this.dispatchErrorEvent(i, "save"), i;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
async forceDelete() {
|
|
1585
|
+
try {
|
|
1586
|
+
const r = await n.route.call(
|
|
1587
|
+
[
|
|
1588
|
+
`luminix.${t}.destroy`,
|
|
1589
|
+
this.makePrimaryKeyReplacer()
|
|
1590
|
+
],
|
|
1591
|
+
{
|
|
1592
|
+
params: { force: !0 },
|
|
1593
|
+
errorBag: `${t}[${this.getKey()}].forceDelete`
|
|
1594
|
+
}
|
|
1595
|
+
);
|
|
1596
|
+
if (r.status === 204)
|
|
1597
|
+
return this.dispatchDeleteEvent(!0), r;
|
|
1598
|
+
throw r;
|
|
1599
|
+
} catch (r) {
|
|
1600
|
+
throw n.log.error(r), this.dispatchErrorEvent(r, "forceDelete"), r;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
async restore() {
|
|
1604
|
+
try {
|
|
1605
|
+
const r = await n.route.call(
|
|
1606
|
+
[
|
|
1607
|
+
`luminix.${t}.update`,
|
|
1608
|
+
this.makePrimaryKeyReplacer()
|
|
1609
|
+
],
|
|
1610
|
+
{
|
|
1611
|
+
params: { restore: !0 },
|
|
1612
|
+
errorBag: `${t}[${this.getKey()}].restore`
|
|
1613
|
+
}
|
|
1614
|
+
);
|
|
1615
|
+
if (r.status === 200)
|
|
1616
|
+
return this.dispatchRestoreEvent(), r;
|
|
1617
|
+
throw r;
|
|
1618
|
+
} catch (r) {
|
|
1619
|
+
throw n.log.error(r), this.dispatchErrorEvent(r, "restore"), r;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
static getSchemaName() {
|
|
1623
|
+
return t;
|
|
1624
|
+
}
|
|
1625
|
+
static getSchema() {
|
|
1626
|
+
return n.model.schema(t);
|
|
1627
|
+
}
|
|
1628
|
+
static query() {
|
|
1629
|
+
return new Et(n, t);
|
|
1630
|
+
}
|
|
1631
|
+
static where(...r) {
|
|
1632
|
+
return this.query().where(...r);
|
|
1633
|
+
}
|
|
1634
|
+
static whereNull(r) {
|
|
1635
|
+
return this.query().whereNull(r);
|
|
1636
|
+
}
|
|
1637
|
+
static whereNotNull(r) {
|
|
1638
|
+
return this.query().whereNotNull(r);
|
|
1639
|
+
}
|
|
1640
|
+
static whereBetween(r, i) {
|
|
1641
|
+
return this.query().whereBetween(r, i);
|
|
1642
|
+
}
|
|
1643
|
+
static whereNotBetween(r, i) {
|
|
1644
|
+
return this.query().whereNotBetween(r, i);
|
|
1645
|
+
}
|
|
1646
|
+
static orderBy(r, i = "asc") {
|
|
1647
|
+
return this.query().orderBy(r, i);
|
|
1648
|
+
}
|
|
1649
|
+
static searchBy(r) {
|
|
1650
|
+
return this.query().searchBy(r);
|
|
1651
|
+
}
|
|
1652
|
+
static minified() {
|
|
1653
|
+
return this.query().minified();
|
|
1654
|
+
}
|
|
1655
|
+
static limit(r) {
|
|
1656
|
+
return this.query().limit(r);
|
|
1657
|
+
}
|
|
1658
|
+
static get(r = 1, i) {
|
|
1659
|
+
return this.query().get(r, i);
|
|
1660
|
+
}
|
|
1661
|
+
static find(r) {
|
|
1662
|
+
return this.query().find(r);
|
|
1663
|
+
}
|
|
1664
|
+
static first() {
|
|
1665
|
+
return this.query().first();
|
|
1666
|
+
}
|
|
1667
|
+
static async create(r) {
|
|
1668
|
+
const i = n.model.make(t), h = new i();
|
|
1669
|
+
return h.fill(r), await h.save(), h;
|
|
1670
|
+
}
|
|
1671
|
+
static async update(r, i) {
|
|
1672
|
+
const h = n.model.make(t), l = new h({ id: r });
|
|
1673
|
+
return l.fill(i), l.exists = !0, await l.save(), l;
|
|
1674
|
+
}
|
|
1675
|
+
static delete(r) {
|
|
1676
|
+
if (Array.isArray(r))
|
|
1677
|
+
return n.route.call(`luminix.${t}.destroyMany`, {
|
|
1678
|
+
params: { ids: r },
|
|
1679
|
+
errorBag: `${t}.deleteMany`
|
|
1680
|
+
});
|
|
1681
|
+
const i = n.model.make(t);
|
|
1682
|
+
return new i({ id: r }).delete();
|
|
1683
|
+
}
|
|
1684
|
+
static async restore(r) {
|
|
1685
|
+
if (Array.isArray(r))
|
|
1686
|
+
return n.route.call(`luminix.${t}.restoreMany`, {
|
|
1687
|
+
data: { ids: r },
|
|
1688
|
+
errorBag: `${t}.restoreMany`
|
|
1689
|
+
});
|
|
1690
|
+
const i = n.model.make(t);
|
|
1691
|
+
return new i({ id: r }).restore();
|
|
1692
|
+
}
|
|
1693
|
+
static forceDelete(r) {
|
|
1694
|
+
if (Array.isArray(r))
|
|
1695
|
+
return n.route.call(`luminix.${t}.destroyMany`, {
|
|
1696
|
+
params: { ids: r, force: !0 },
|
|
1697
|
+
errorBag: `${t}.forceDeleteMany`
|
|
1698
|
+
});
|
|
1699
|
+
const i = n.model.make(t);
|
|
1700
|
+
return new i({ id: r }).forceDelete();
|
|
1701
|
+
}
|
|
1702
|
+
static singular() {
|
|
1703
|
+
return n.model.schema(t).displayName.singular;
|
|
1704
|
+
}
|
|
1705
|
+
static plural() {
|
|
1706
|
+
return n.model.schema(t).displayName.plural;
|
|
1707
|
+
}
|
|
1708
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1709
|
+
on(r, i) {
|
|
1710
|
+
throw new y();
|
|
1711
|
+
}
|
|
1712
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1713
|
+
once(r, i) {
|
|
1714
|
+
throw new y();
|
|
1715
|
+
}
|
|
1716
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1717
|
+
emit(r, i) {
|
|
1718
|
+
throw new y();
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
return p(e, "name", f.upperFirst(f.camelCase(t))), A(e);
|
|
1722
|
+
}
|
|
1723
|
+
function _t(n, t, e) {
|
|
1724
|
+
var s;
|
|
1725
|
+
return s = class extends e {
|
|
1726
|
+
constructor(r = {}) {
|
|
1727
|
+
return super(r), new Proxy(this, {
|
|
1728
|
+
get: (i, h) => {
|
|
1729
|
+
if (h === "__isModel")
|
|
1730
|
+
return !0;
|
|
1731
|
+
if (h in i) {
|
|
1732
|
+
const a = i[h];
|
|
1733
|
+
return typeof a == "function" ? a.bind(i) : a;
|
|
1734
|
+
}
|
|
1735
|
+
if (h !== f.camelCase(h))
|
|
1736
|
+
return;
|
|
1737
|
+
const l = f.snakeCase(h);
|
|
1738
|
+
if (Object.keys(i.relations).includes(l))
|
|
1739
|
+
return i.relations[l].getLoadedItems();
|
|
1740
|
+
if (h.endsWith("Relation") && Object.keys(i.relations).includes(f.snakeCase(h.slice(0, -8))))
|
|
1741
|
+
return () => i.relation(h.slice(0, -8));
|
|
1742
|
+
if (Object.keys(i.attributes).includes(l))
|
|
1743
|
+
return i.getAttribute(l);
|
|
1744
|
+
if (n.model.hasReducer(`model${i.constructor.name}Get${f.upperFirst(h)}Attribute`)) {
|
|
1745
|
+
const a = n.model[`model${i.constructor.name}Get${f.upperFirst(h)}Attribute`];
|
|
1746
|
+
if (typeof a != "function")
|
|
1747
|
+
throw new _("ModelFacade");
|
|
1748
|
+
return a.bind(n.model)(void 0, i);
|
|
1749
|
+
}
|
|
1750
|
+
return Reflect.get(i, h);
|
|
1751
|
+
},
|
|
1752
|
+
set: (i, h, l) => {
|
|
1753
|
+
if (h in i && typeof i[h] != "function")
|
|
1754
|
+
return Reflect.set(i, h, l);
|
|
1755
|
+
if (i.fillable.includes(f.snakeCase(h)))
|
|
1756
|
+
return i.setAttribute(
|
|
1757
|
+
f.snakeCase(h),
|
|
1758
|
+
l
|
|
1759
|
+
), !0;
|
|
1760
|
+
throw new U(t, f.snakeCase(h));
|
|
1761
|
+
}
|
|
1762
|
+
});
|
|
1763
|
+
}
|
|
1764
|
+
}, p(s, "name", f.upperFirst(f.camelCase(t))), s;
|
|
1765
|
+
}
|
|
1766
|
+
class tt extends Error {
|
|
1767
|
+
constructor(t, e) {
|
|
1768
|
+
super(`[Luminix] Cannot create reducer '${t}' on '${e}' as it is a reserved property`);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
p(tt, "name", "ReducerOverrideException");
|
|
1772
|
+
function et(n) {
|
|
1773
|
+
return class extends n {
|
|
1774
|
+
constructor(...e) {
|
|
1775
|
+
super(...e);
|
|
1776
|
+
p(this, "reducers", {});
|
|
1777
|
+
return new Proxy(this, {
|
|
1778
|
+
get(s, r, i) {
|
|
1779
|
+
return typeof r == "symbol" || r in s ? Reflect.get(s, r, i) : (h, ...l) => {
|
|
1780
|
+
const { [r]: a = c() } = s.reducers;
|
|
1781
|
+
return pt(h) ? B(h, (d) => a.sortBy("priority").reduce((g, b) => b.callback(g, ...l), d)) : a.sortBy("priority").reduce((d, g) => g.callback(d, ...l), h);
|
|
1782
|
+
};
|
|
1783
|
+
}
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
reducer(e, s, r = 10) {
|
|
1787
|
+
if (e in this)
|
|
1788
|
+
throw new tt(e, this);
|
|
1789
|
+
return this.reducers[e] || (this.reducers[e] = c()), this.reducers[e].push({ callback: s, priority: r }), () => this.removeReducer(e, s);
|
|
1790
|
+
}
|
|
1791
|
+
removeReducer(e, s) {
|
|
1792
|
+
const r = this.reducers[e].search((i) => i.callback === s);
|
|
1793
|
+
r !== !1 && this.reducers[e].pull(r);
|
|
1794
|
+
}
|
|
1795
|
+
getReducer(e) {
|
|
1796
|
+
return this.reducers[e] || (this.reducers[e] = c()), this.reducers[e];
|
|
1797
|
+
}
|
|
1798
|
+
hasReducer(e) {
|
|
1799
|
+
return !!this.reducers[e] && this.reducers[e].count() > 0;
|
|
1800
|
+
}
|
|
1801
|
+
clearReducer(e) {
|
|
1802
|
+
this.reducers[e].splice(0, this.reducers[e].count());
|
|
1803
|
+
}
|
|
1804
|
+
flushReducers() {
|
|
1805
|
+
Object.values(this.reducers).forEach((e) => e.splice(0, e.count()));
|
|
1806
|
+
}
|
|
1807
|
+
};
|
|
1808
|
+
}
|
|
1809
|
+
class C extends Error {
|
|
1810
|
+
constructor(t) {
|
|
1811
|
+
super(`[Luminix] Model "${t}" not found`);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
p(C, "name", "ModelNotFoundException");
|
|
1815
|
+
class N extends TypeError {
|
|
1816
|
+
constructor(t, e, s) {
|
|
1817
|
+
super(`[Luminix] "${t}" expects a related model of type "${e}". Received "${s}" instead.`);
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
p(N, "name", "ModelInvalidRelatedTypeException");
|
|
1821
|
+
class rt extends F {
|
|
1822
|
+
constructor(t, e, s, r = null) {
|
|
1823
|
+
if (!w(r) && r !== null)
|
|
1824
|
+
throw new E("BelongsTo.constructor()", "Model or null");
|
|
1825
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
1826
|
+
}
|
|
1827
|
+
isSingle() {
|
|
1828
|
+
return !0;
|
|
1829
|
+
}
|
|
1830
|
+
isMultiple() {
|
|
1831
|
+
return !1;
|
|
1832
|
+
}
|
|
1833
|
+
query() {
|
|
1834
|
+
const t = super.query(), e = this.guessInverseRelation();
|
|
1835
|
+
return t.where(e, this.parent.getKey()), t.lock(`filters.${e}`), t;
|
|
1836
|
+
}
|
|
1837
|
+
get() {
|
|
1838
|
+
return this.query().first();
|
|
1839
|
+
}
|
|
1840
|
+
async associate(t) {
|
|
1841
|
+
if (!w(t))
|
|
1842
|
+
throw new E("BelongsTo.associate()");
|
|
1843
|
+
if (t.getType() !== this.getRelated().getSchemaName())
|
|
1844
|
+
throw new N("BelongsTo.associate()", this.getRelated().getSchemaName(), t.getType());
|
|
1845
|
+
if (!t.exists)
|
|
1846
|
+
throw new D(this.getRelated().getSchemaName(), "save");
|
|
1847
|
+
return this.parent.update({
|
|
1848
|
+
[this.getForeignKey()]: t.getKey()
|
|
1849
|
+
});
|
|
1850
|
+
}
|
|
1851
|
+
dissociate() {
|
|
1852
|
+
return this.parent.update({
|
|
1853
|
+
[this.getForeignKey()]: null
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
class st extends F {
|
|
1858
|
+
constructor(t, e, s, r = null) {
|
|
1859
|
+
if (r !== null && !(r instanceof J && r.every(w)))
|
|
1860
|
+
throw new E("BelongsToMany.constructor()", "Collection<Model> or null");
|
|
1861
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
1862
|
+
}
|
|
1863
|
+
isSingle() {
|
|
1864
|
+
return !1;
|
|
1865
|
+
}
|
|
1866
|
+
isMultiple() {
|
|
1867
|
+
return !0;
|
|
1868
|
+
}
|
|
1869
|
+
query() {
|
|
1870
|
+
const t = super.query(), e = this.guessInverseRelation();
|
|
1871
|
+
return t.where(e, this.parent.getKey()), t.lock(`filters.${e}`), t;
|
|
1872
|
+
}
|
|
1873
|
+
get(t = 1, e) {
|
|
1874
|
+
return this.query().get(t, e);
|
|
1875
|
+
}
|
|
1876
|
+
all() {
|
|
1877
|
+
return this.query().all();
|
|
1878
|
+
}
|
|
1879
|
+
first() {
|
|
1880
|
+
return this.query().first();
|
|
1881
|
+
}
|
|
1882
|
+
find(t) {
|
|
1883
|
+
return this.query().find(t);
|
|
1884
|
+
}
|
|
1885
|
+
attachQuietly(t, e = {}) {
|
|
1886
|
+
return this.facades.route.call([
|
|
1887
|
+
`luminix.${this.parent.getType()}.${this.getName()}:attach`,
|
|
1888
|
+
{
|
|
1889
|
+
[this.parent.getKeyName()]: this.parent.getKey(),
|
|
1890
|
+
itemId: t
|
|
1891
|
+
}
|
|
1892
|
+
], {
|
|
1893
|
+
data: e,
|
|
1894
|
+
errorBag: `${this.parent.getType()}[${this.parent.getKey()}].${this.getName()}:attach`
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1897
|
+
async attach(t, e = {}) {
|
|
1898
|
+
if (await this.attachQuietly(t, e), this.items) {
|
|
1899
|
+
const s = this.items.search((i) => i.getKey() === t), r = await this.getRelated().find(t);
|
|
1900
|
+
if (!r)
|
|
1901
|
+
return;
|
|
1902
|
+
s !== !1 ? this.items.put(s, r) : this.items.push(r);
|
|
1903
|
+
} else
|
|
1904
|
+
this.items = await this.all();
|
|
1905
|
+
}
|
|
1906
|
+
async detachQuietly(t) {
|
|
1907
|
+
await this.facades.route.call(
|
|
1908
|
+
[
|
|
1909
|
+
`luminix.${this.parent.getType()}.${this.getName()}:detach`,
|
|
1910
|
+
{
|
|
1911
|
+
[this.parent.getKeyName()]: this.parent.getKey(),
|
|
1912
|
+
itemId: t
|
|
1913
|
+
}
|
|
1914
|
+
],
|
|
1915
|
+
{
|
|
1916
|
+
errorBag: `${this.parent.getType()}[${this.parent.getKey()}].${this.getName()}:detach`
|
|
1917
|
+
}
|
|
1918
|
+
);
|
|
1919
|
+
}
|
|
1920
|
+
async detach(t) {
|
|
1921
|
+
if (await this.detachQuietly(t), this.items) {
|
|
1922
|
+
const e = this.items.search((s) => s.getKey() === t);
|
|
1923
|
+
e !== !1 && this.items.pull(e);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
async syncQuietly(t) {
|
|
1927
|
+
await this.facades.route.call([
|
|
1928
|
+
`luminix.${this.parent.getType()}.${this.getName()}:sync`,
|
|
1929
|
+
{
|
|
1930
|
+
[this.parent.getKeyName()]: this.parent.getKey()
|
|
1931
|
+
}
|
|
1932
|
+
], {
|
|
1933
|
+
data: t,
|
|
1934
|
+
errorBag: `${this.parent.getType()}[${this.parent.getKey()}].${this.getName()}:sync`
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
async syncWithPivotValuesQuietly(t, e) {
|
|
1938
|
+
await this.facades.route.call([
|
|
1939
|
+
`luminix.${this.parent.getType()}.${this.getName()}:sync`,
|
|
1940
|
+
{
|
|
1941
|
+
[this.parent.getKeyName()]: this.parent.getKey()
|
|
1942
|
+
}
|
|
1943
|
+
], {
|
|
1944
|
+
data: t.map((s) => ({
|
|
1945
|
+
[this.getRelated().getSchema().primaryKey]: s,
|
|
1946
|
+
...e
|
|
1947
|
+
})),
|
|
1948
|
+
errorBag: `${this.parent.getType()}[${this.parent.getKey()}].${this.getName()}:sync`
|
|
1949
|
+
});
|
|
1950
|
+
}
|
|
1951
|
+
async sync(t) {
|
|
1952
|
+
await this.syncQuietly(t);
|
|
1953
|
+
const e = await this.all();
|
|
1954
|
+
this.items ? this.items.splice(0, this.items.count(), ...e) : this.items = e;
|
|
1955
|
+
}
|
|
1956
|
+
async syncWithPivotValues(t, e) {
|
|
1957
|
+
await this.syncWithPivotValuesQuietly(t, e);
|
|
1958
|
+
const s = await this.all();
|
|
1959
|
+
this.items ? this.items.splice(0, this.items.count(), ...s) : this.items = s;
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
class H extends F {
|
|
1963
|
+
query() {
|
|
1964
|
+
const t = super.query(), e = this.guessInverseRelation();
|
|
1965
|
+
return t.where(e, this.parent.getKey()), t.lock(`filters.${e}`), t;
|
|
1966
|
+
}
|
|
1967
|
+
async saveQuietly(t) {
|
|
1968
|
+
if (!w(t))
|
|
1969
|
+
throw new E("HasOneOrMany.saveQuietly()");
|
|
1970
|
+
if (t.getType() !== this.getRelated().getSchemaName())
|
|
1971
|
+
throw new N("HasOneOrMany.saveQuietly()", this.getRelated().getSchemaName(), t.getType());
|
|
1972
|
+
t.setAttribute(this.getForeignKey(), this.parent.getKey()), await t.save();
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
class Mt extends H {
|
|
1976
|
+
constructor(t, e, s, r = null) {
|
|
1977
|
+
if (!w(r) && r !== null)
|
|
1978
|
+
throw new E("HasOne.constructor()", "Model or null");
|
|
1979
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
1980
|
+
}
|
|
1981
|
+
isSingle() {
|
|
1982
|
+
return !0;
|
|
1983
|
+
}
|
|
1984
|
+
isMultiple() {
|
|
1985
|
+
return !1;
|
|
1986
|
+
}
|
|
1987
|
+
get() {
|
|
1988
|
+
return this.query().first();
|
|
1989
|
+
}
|
|
1990
|
+
async save(t) {
|
|
1991
|
+
await this.saveQuietly(t), this.items = t;
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
class $t extends H {
|
|
1995
|
+
constructor(t, e, s, r = null) {
|
|
1996
|
+
if (r !== null && !(r instanceof J && r.every(w)))
|
|
1997
|
+
throw new E("HasMany.constructor()", "Collection<Model> or null");
|
|
1998
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
1999
|
+
}
|
|
2000
|
+
isSingle() {
|
|
2001
|
+
return !1;
|
|
2002
|
+
}
|
|
2003
|
+
isMultiple() {
|
|
2004
|
+
return !0;
|
|
2005
|
+
}
|
|
2006
|
+
get(t = 1, e) {
|
|
2007
|
+
return this.query().get(t, e);
|
|
2008
|
+
}
|
|
2009
|
+
all() {
|
|
2010
|
+
return this.query().all();
|
|
2011
|
+
}
|
|
2012
|
+
first() {
|
|
2013
|
+
return this.query().first();
|
|
2014
|
+
}
|
|
2015
|
+
find(t) {
|
|
2016
|
+
return this.query().find(t);
|
|
2017
|
+
}
|
|
2018
|
+
async saveManyQuietly(t) {
|
|
2019
|
+
if (!Array.isArray(t) || !t.every(w))
|
|
2020
|
+
throw new E("HasMany.saveManyQuietly()", "Model[]");
|
|
2021
|
+
if (!t.every((e) => e.getType() === this.getRelated().getSchemaName()))
|
|
2022
|
+
throw new N("HasMany.saveManyQuietly()", this.getRelated().getSchemaName(), t.map((e) => e.getType()).join(", "));
|
|
2023
|
+
await Promise.all(t.map((e) => (e.setAttribute(this.getForeignKey(), this.parent.getKey()), e.save())));
|
|
2024
|
+
}
|
|
2025
|
+
async saveMany(t) {
|
|
2026
|
+
await this.saveManyQuietly(t);
|
|
2027
|
+
const e = await this.all();
|
|
2028
|
+
this.items ? this.items.splice(0, this.items.count(), ...e) : this.items = e;
|
|
2029
|
+
}
|
|
2030
|
+
async save(t) {
|
|
2031
|
+
await this.saveQuietly(t), this.items === null ? this.items = await this.all() : this.items.push(t);
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
class it extends H {
|
|
2035
|
+
query() {
|
|
2036
|
+
const t = this.getRelated().query();
|
|
2037
|
+
t.once("success", (s) => {
|
|
2038
|
+
this.items = s.items;
|
|
2039
|
+
});
|
|
2040
|
+
const e = this.guessInverseRelation();
|
|
2041
|
+
return t.where(e + "_id", this.parent.getKey()), t.where(e + "_type", this.getRelated().getSchemaName()), t.lock(`filters.${e}_id`), t.lock(`filters.${e}_type`), t;
|
|
2042
|
+
}
|
|
2043
|
+
async saveQuietly(t) {
|
|
2044
|
+
if (!w(t))
|
|
2045
|
+
throw new E("MorphOneOrMany.saveQuietly()");
|
|
2046
|
+
if (t.getType() !== this.getRelated().getSchemaName())
|
|
2047
|
+
throw new N("MorphOneOrMany.saveQuietly()", this.getRelated().getSchemaName(), t.getType());
|
|
2048
|
+
const e = this.guessInverseRelation();
|
|
2049
|
+
t.setAttribute(e + "_id", this.parent.getKey()), t.setAttribute(e + "_type", this.parent.getType()), await t.save();
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
class Tt extends it {
|
|
2053
|
+
constructor(t, e, s, r = null) {
|
|
2054
|
+
if (r !== null && !(r instanceof J && r.every(w)))
|
|
2055
|
+
throw new E("MorphMany.constructor()", "Collection<Model> or null");
|
|
2056
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
2057
|
+
}
|
|
2058
|
+
isSingle() {
|
|
2059
|
+
return !1;
|
|
2060
|
+
}
|
|
2061
|
+
isMultiple() {
|
|
2062
|
+
return !0;
|
|
2063
|
+
}
|
|
2064
|
+
get(t = 1, e) {
|
|
2065
|
+
return this.query().get(t, e);
|
|
2066
|
+
}
|
|
2067
|
+
all() {
|
|
2068
|
+
return this.query().all();
|
|
2069
|
+
}
|
|
2070
|
+
first() {
|
|
2071
|
+
return this.query().first();
|
|
2072
|
+
}
|
|
2073
|
+
find(t) {
|
|
2074
|
+
return this.query().find(t);
|
|
2075
|
+
}
|
|
2076
|
+
async saveManyQuietly(t) {
|
|
2077
|
+
if (!Array.isArray(t) || !t.every(w))
|
|
2078
|
+
throw new E("MorphMany.saveManyQuietly()");
|
|
2079
|
+
if (!t.every((e) => e.getType() === this.getRelated().getSchemaName()))
|
|
2080
|
+
throw new N("MorphMany.saveManyQuietly()", this.getRelated().getSchemaName(), t.map((e) => e.getType()).join(", "));
|
|
2081
|
+
await Promise.all(t.map((e) => (e.setAttribute(this.getName() + "_id", this.parent.getKey()), e.setAttribute(this.getName() + "_type", this.parent.getType()), e.save())));
|
|
2082
|
+
}
|
|
2083
|
+
async save(t) {
|
|
2084
|
+
await this.saveQuietly(t), this.items ? this.items.push(t) : this.items = await this.all();
|
|
2085
|
+
}
|
|
2086
|
+
async saveMany(t) {
|
|
2087
|
+
await this.saveManyQuietly(t);
|
|
2088
|
+
const e = await this.all();
|
|
2089
|
+
this.items ? this.items.splice(0, this.items.count(), ...e) : this.items = e;
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
class At extends it {
|
|
2093
|
+
constructor(t, e, s, r = null) {
|
|
2094
|
+
if (super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r, r !== null && !w(r))
|
|
2095
|
+
throw new E("MorphOne.constructor()", "Model or null");
|
|
2096
|
+
}
|
|
2097
|
+
isSingle() {
|
|
2098
|
+
return !0;
|
|
2099
|
+
}
|
|
2100
|
+
isMultiple() {
|
|
2101
|
+
return !1;
|
|
2102
|
+
}
|
|
2103
|
+
get() {
|
|
2104
|
+
return this.query().first();
|
|
2105
|
+
}
|
|
2106
|
+
async save(t) {
|
|
2107
|
+
await this.saveQuietly(t), this.items = t;
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
class vt extends rt {
|
|
2111
|
+
getRelated() {
|
|
2112
|
+
return this.facades.model.make(
|
|
2113
|
+
this.parent.getAttribute(this.getName() + "_type")
|
|
2114
|
+
);
|
|
2115
|
+
}
|
|
2116
|
+
// of(abstract: string): this {
|
|
2117
|
+
// this.meta.model = abstract;
|
|
2118
|
+
// return this;
|
|
2119
|
+
// }
|
|
2120
|
+
// query(): BuilderInterface {
|
|
2121
|
+
// this.of(this.parent.getAttribute(this.getName() + '_type') as string);
|
|
2122
|
+
// return super.query();
|
|
2123
|
+
// }
|
|
2124
|
+
async associate(t) {
|
|
2125
|
+
if (!w(t))
|
|
2126
|
+
throw new E("MorphTo.associate()");
|
|
2127
|
+
return t.exists || await t.save(), this.parent.update({
|
|
2128
|
+
[this.getName() + "_id"]: t.getKey(),
|
|
2129
|
+
[this.getName() + "_type"]: t.getType()
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
dissociate() {
|
|
2133
|
+
return this.parent.update({
|
|
2134
|
+
[this.getName() + "_id"]: null,
|
|
2135
|
+
[this.getName() + "_type"]: null
|
|
2136
|
+
});
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
class Rt extends st {
|
|
2140
|
+
constructor(t, e, s, r = null) {
|
|
2141
|
+
super(t, e, s, r), this.meta = t, this.facades = e, this.parent = s, this.items = r;
|
|
2142
|
+
}
|
|
2143
|
+
query() {
|
|
2144
|
+
const t = super.query(), e = this.guessInverseRelation();
|
|
2145
|
+
return t.where(e + "_id", this.parent.getKey()), t.where(e + "_type", this.parent.getType()), t.lock(`filters.${e}_id`), t.lock(`filters.${e}_type`), t;
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
class nt {
|
|
2149
|
+
constructor(t) {
|
|
2150
|
+
p(this, "_models", {});
|
|
2151
|
+
this._schema = t;
|
|
2152
|
+
}
|
|
2153
|
+
boot(t) {
|
|
2154
|
+
this._schema && (Object.keys(this._schema).forEach((e) => {
|
|
2155
|
+
const s = this[`model${f.upperFirst(f.camelCase(e))}`];
|
|
2156
|
+
if (typeof this.model != "function" || typeof s != "function")
|
|
2157
|
+
throw new _("ModelFacade");
|
|
2158
|
+
const r = this.model(
|
|
2159
|
+
xt(t.make(), e),
|
|
2160
|
+
e
|
|
2161
|
+
), i = s(r);
|
|
2162
|
+
this._models[e] = _t(t.make(), e, i);
|
|
2163
|
+
}), this.reducer("relationMap", () => ({
|
|
2164
|
+
BelongsTo: rt,
|
|
2165
|
+
BelongsToMany: st,
|
|
2166
|
+
HasOne: Mt,
|
|
2167
|
+
HasMany: $t,
|
|
2168
|
+
MorphMany: Tt,
|
|
2169
|
+
MorphOne: At,
|
|
2170
|
+
MorphTo: vt,
|
|
2171
|
+
MorphToMany: Rt
|
|
2172
|
+
}), 0));
|
|
2173
|
+
}
|
|
2174
|
+
schema(t) {
|
|
2175
|
+
if (!this._schema || t && !this._schema[t])
|
|
2176
|
+
throw new C(t || "undefined");
|
|
2177
|
+
return t ? this._schema[t] : this._schema;
|
|
2178
|
+
}
|
|
2179
|
+
make(t) {
|
|
2180
|
+
if (t && !this._models[t])
|
|
2181
|
+
throw new C(t);
|
|
2182
|
+
return t ? this._models[t] : this._models;
|
|
2183
|
+
}
|
|
2184
|
+
toString() {
|
|
2185
|
+
return "model";
|
|
2186
|
+
}
|
|
2187
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2188
|
+
on(t, e) {
|
|
2189
|
+
throw new y();
|
|
2190
|
+
}
|
|
2191
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2192
|
+
once(t, e) {
|
|
2193
|
+
throw new y();
|
|
2194
|
+
}
|
|
2195
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2196
|
+
emit(t, e) {
|
|
2197
|
+
throw new y();
|
|
2198
|
+
}
|
|
2199
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2200
|
+
reducer(t, e, s) {
|
|
2201
|
+
throw new y();
|
|
2202
|
+
}
|
|
2203
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2204
|
+
removeReducer(t) {
|
|
2205
|
+
throw new y();
|
|
2206
|
+
}
|
|
2207
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2208
|
+
getReducer(t) {
|
|
2209
|
+
throw new y();
|
|
2210
|
+
}
|
|
2211
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2212
|
+
hasReducer(t) {
|
|
2213
|
+
throw new y();
|
|
2214
|
+
}
|
|
2215
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2216
|
+
clearReducer(t) {
|
|
2217
|
+
throw new y();
|
|
2218
|
+
}
|
|
2219
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2220
|
+
flushReducers() {
|
|
2221
|
+
throw new y();
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
p(nt, "name", "ModelFacade");
|
|
2225
|
+
const Nt = A(et(nt));
|
|
2226
|
+
class ot extends Error {
|
|
2227
|
+
constructor() {
|
|
2228
|
+
super("[Luminix] Embed element not found. Make sure to include the `@luminixEmbed()` directive in your Blade template.");
|
|
2229
|
+
}
|
|
2230
|
+
}
|
|
2231
|
+
p(ot, "name", "NoEmbedException");
|
|
2232
|
+
const ut = (n, t = "data") => {
|
|
2233
|
+
if (!document.querySelector("#luminix-embed"))
|
|
2234
|
+
throw new ot();
|
|
2235
|
+
const e = document.getElementById(`luminix-${t}::` + n);
|
|
2236
|
+
return e ? e.dataset.json && e.dataset.value ? JSON.parse(e.dataset.value) : e.dataset.value : null;
|
|
2237
|
+
}, X = A(v), St = (n) => mt(n) && n.response !== void 0 && n.response.data !== null && "message" in n.response.data && typeof n.response.data.message == "string" && "errors" in n.response.data && typeof n.response.data.errors == "object" && n.response.data.errors !== null && Object.values(n.response.data.errors).every((t) => Array.isArray(t) && t.every((e) => typeof e == "string")) && n.response.status === 422;
|
|
2238
|
+
let Bt = class {
|
|
2239
|
+
constructor() {
|
|
2240
|
+
p(this, "bags");
|
|
2241
|
+
const t = {};
|
|
2242
|
+
document.querySelectorAll('#luminix-embed [id^="luminix-error"]').forEach((s) => {
|
|
2243
|
+
const i = s.id.replace("luminix-error::", "");
|
|
2244
|
+
t[i] = ut(i, "error");
|
|
2245
|
+
}), this.bags = {
|
|
2246
|
+
default: new X(t)
|
|
2247
|
+
};
|
|
2248
|
+
}
|
|
2249
|
+
bag(t = "default") {
|
|
2250
|
+
return this.bags[t] || (this.bags[t] = new X({})), this.bags[t];
|
|
2251
|
+
}
|
|
2252
|
+
add(t, e, s = "default") {
|
|
2253
|
+
this.bag(s).set(t, e);
|
|
2254
|
+
}
|
|
2255
|
+
set(t, e = "default") {
|
|
2256
|
+
this.bag(e).set(".", t);
|
|
2257
|
+
}
|
|
2258
|
+
all(t = "default") {
|
|
2259
|
+
return this.bag(t).all();
|
|
2260
|
+
}
|
|
2261
|
+
get(t, e = "default") {
|
|
2262
|
+
return this.bag(e).get(t, null);
|
|
2263
|
+
}
|
|
2264
|
+
clear(t = "default") {
|
|
2265
|
+
this.bag(t).set(".", {});
|
|
2266
|
+
}
|
|
2267
|
+
};
|
|
2268
|
+
class ht extends Error {
|
|
2269
|
+
constructor(t) {
|
|
2270
|
+
super(`[Luminix] Route "${t}" not found`);
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
p(ht, "name", "RouteNotFoundException");
|
|
2274
|
+
class qt {
|
|
2275
|
+
constructor(t, e, s = "") {
|
|
2276
|
+
this.routes = t, this.error = e, this.appUrl = s;
|
|
2277
|
+
}
|
|
2278
|
+
isRouteTuple(t) {
|
|
2279
|
+
if (!Array.isArray(t) || t.length < 2)
|
|
2280
|
+
return !1;
|
|
2281
|
+
const [e, ...s] = t;
|
|
2282
|
+
if (typeof e != "string")
|
|
2283
|
+
return !1;
|
|
2284
|
+
const r = ["get", "post", "put", "patch", "delete"];
|
|
2285
|
+
return !!s.every((i) => r.includes(i));
|
|
2286
|
+
}
|
|
2287
|
+
extractGenerator(t) {
|
|
2288
|
+
let e, s = !1;
|
|
2289
|
+
return Array.isArray(t) ? [e, s] = t : e = t, [e, s];
|
|
2290
|
+
}
|
|
2291
|
+
get(t) {
|
|
2292
|
+
if (!this.exists(t))
|
|
2293
|
+
throw new ht(t);
|
|
2294
|
+
return f.get(this.routes, t);
|
|
2295
|
+
}
|
|
2296
|
+
url(t) {
|
|
2297
|
+
const [e, s] = this.extractGenerator(t), r = this.get(e)[0].replace(/^\/|\/$/g, ""), i = /{([^}]+)}/g;
|
|
2298
|
+
if (s === !1) {
|
|
2299
|
+
if (typeof this.replaceRouteParams != "function")
|
|
2300
|
+
throw new _("RouteFacade");
|
|
2301
|
+
return this.appUrl + this.replaceRouteParams(`/${r}`);
|
|
2302
|
+
}
|
|
2303
|
+
const h = r.match(i), l = h ? h.map((m) => m.slice(1, -1)) : [], a = Object.keys(s), d = l.filter((m) => !a.includes(m)), g = a.filter((m) => !l.includes(m));
|
|
2304
|
+
if (d.length > 0)
|
|
2305
|
+
throw new TypeError(`Missing values for parameter(s): ${d.join(", ")}`);
|
|
2306
|
+
if (g.length > 0)
|
|
2307
|
+
throw new TypeError(`Unexpected parameters: ${g.join(", ")}`);
|
|
2308
|
+
const b = l.reduce((m, M) => m.replace(`{${M}}`, `${s[M]}`), r);
|
|
2309
|
+
return this.appUrl + `/${b}`;
|
|
2310
|
+
}
|
|
2311
|
+
methods(t) {
|
|
2312
|
+
const [e] = this.extractGenerator(t);
|
|
2313
|
+
return this.get(e).slice(1);
|
|
2314
|
+
}
|
|
2315
|
+
exists(t) {
|
|
2316
|
+
return f.has(this.routes, t) && this.isRouteTuple(f.get(this.routes, t));
|
|
2317
|
+
}
|
|
2318
|
+
async call(t, e = {}) {
|
|
2319
|
+
if (typeof this.axiosOptions != "function" || typeof this.axiosError != "function")
|
|
2320
|
+
throw new _("RouteFacade");
|
|
2321
|
+
const [s, r] = this.extractGenerator(t), [, ...i] = this.get(s), h = this.url(r ? [s, r] : s), l = this.axiosOptions(e, s), { method: a = i[0], errorBag: d = "route.call", ...g } = l, { data: b, ...m } = g;
|
|
2322
|
+
this.error.clear(d);
|
|
2323
|
+
try {
|
|
2324
|
+
return ["get", "delete"].includes(a) ? await K[a](h, g) : await K[a](h, b, m);
|
|
2325
|
+
} catch (M) {
|
|
2326
|
+
if (St(M)) {
|
|
2327
|
+
const { errors: at } = M.response.data;
|
|
2328
|
+
this.error.set(Object.entries(at).reduce((W, [lt, ct]) => (W[lt] = ct.join(" "), W), {}));
|
|
2329
|
+
} else
|
|
2330
|
+
K.isAxiosError(M) && this.error.set(
|
|
2331
|
+
this.axiosError({ axios: M.message }, {
|
|
2332
|
+
error: M,
|
|
2333
|
+
name: s,
|
|
2334
|
+
replace: r,
|
|
2335
|
+
config: e
|
|
2336
|
+
}),
|
|
2337
|
+
d
|
|
2338
|
+
);
|
|
2339
|
+
throw M;
|
|
2340
|
+
}
|
|
2341
|
+
}
|
|
2342
|
+
toString() {
|
|
2343
|
+
return "route";
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
const It = et(qt);
|
|
2347
|
+
class Kt {
|
|
2348
|
+
constructor() {
|
|
2349
|
+
p(this, "facades", {});
|
|
2350
|
+
p(this, "booted", !1);
|
|
2351
|
+
p(this, "_plugins", []);
|
|
2352
|
+
}
|
|
2353
|
+
make(t = void 0) {
|
|
2354
|
+
if (!t)
|
|
2355
|
+
return this.facades;
|
|
2356
|
+
if (t in this.facades)
|
|
2357
|
+
return this.facades[t];
|
|
2358
|
+
}
|
|
2359
|
+
has(t) {
|
|
2360
|
+
return !!this.facades[t];
|
|
2361
|
+
}
|
|
2362
|
+
bind(t, e) {
|
|
2363
|
+
this.facades[t] || (this.facades[t] = e);
|
|
2364
|
+
}
|
|
2365
|
+
plugins() {
|
|
2366
|
+
return this._plugins;
|
|
2367
|
+
}
|
|
2368
|
+
async boot(t = {}) {
|
|
2369
|
+
var l, a, d, g, b;
|
|
2370
|
+
if (this.booted)
|
|
2371
|
+
throw new window.Error("[Luminix] App already booted");
|
|
2372
|
+
this.booted = !0, (l = t.app) != null && l.debug && console.log("[Luminix] Booting started..."), this.emit("init", {
|
|
2373
|
+
register: (m) => {
|
|
2374
|
+
this._plugins.push(m), typeof m.register == "function" && m.register(this);
|
|
2375
|
+
}
|
|
2376
|
+
});
|
|
2377
|
+
const e = (((a = t.app) == null ? void 0 : a.url) ?? "") + (((d = t.app) == null ? void 0 : d.bootUrl) ?? "/luminix-api/init");
|
|
2378
|
+
if (typeof e == "string" && !document.getElementById("luminix-data::config")) {
|
|
2379
|
+
const { data: m } = await K.get(e);
|
|
2380
|
+
m && typeof m == "object" && f.merge(t, m);
|
|
2381
|
+
} else if (document.getElementById("luminix-data::config")) {
|
|
2382
|
+
const m = ut("config");
|
|
2383
|
+
m && typeof m == "object" && f.merge(t, m);
|
|
2384
|
+
}
|
|
2385
|
+
this.bind("log", new yt(!!((g = t.app) != null && g.debug)));
|
|
2386
|
+
const { log: s } = this.facades, {
|
|
2387
|
+
manifest: { routes: r = {}, models: i = {} } = {},
|
|
2388
|
+
...h
|
|
2389
|
+
} = t;
|
|
2390
|
+
this.bind("config", new v(h)), this.facades.config.has("auth.user") || this.facades.config.set("auth.user", null), this.facades.config.lock("auth.user"), this.bind("error", new Bt()), this.bind("route", new It(r, this.facades.error, ((b = t.app) == null ? void 0 : b.url) ?? "")), this.bind("model", new Nt(i)), this.bind("auth", new gt(this)), this.emit("booting");
|
|
2391
|
+
for (const m of Object.values(this.facades))
|
|
2392
|
+
typeof m == "object" && m !== null && "boot" in m && typeof m.boot == "function" && m.boot(this);
|
|
2393
|
+
for (const m of this._plugins)
|
|
2394
|
+
typeof m.boot == "function" && m.boot(this.facades);
|
|
2395
|
+
return s.info("[Luminix] App boot completed", {
|
|
2396
|
+
config: this.facades.config.all(),
|
|
2397
|
+
plugins: this._plugins,
|
|
2398
|
+
manifest: {
|
|
2399
|
+
routes: r,
|
|
2400
|
+
models: i
|
|
2401
|
+
}
|
|
2402
|
+
}), this.emit("booted"), this.facades;
|
|
2403
|
+
}
|
|
2404
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2405
|
+
on(t, e) {
|
|
2406
|
+
throw new window.Error("Method not implemented.");
|
|
2407
|
+
}
|
|
2408
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2409
|
+
once(t, e) {
|
|
2410
|
+
throw new window.Error("Method not implemented.");
|
|
2411
|
+
}
|
|
2412
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2413
|
+
emit(t, e) {
|
|
2414
|
+
throw new window.Error("Method not implemented.");
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
const jt = A(Kt);
|
|
2418
|
+
let x;
|
|
2419
|
+
function R(n = void 0) {
|
|
2420
|
+
if (x || (x = new jt()), typeof n != "string")
|
|
2421
|
+
return {
|
|
2422
|
+
boot: x.boot.bind(x),
|
|
2423
|
+
make: x.make.bind(x),
|
|
2424
|
+
plugins: x.plugins.bind(x),
|
|
2425
|
+
on: x.once.bind(x)
|
|
2426
|
+
};
|
|
2427
|
+
if (!x.has(n))
|
|
2428
|
+
throw new Y(n);
|
|
2429
|
+
return x.make(n);
|
|
2430
|
+
}
|
|
2431
|
+
function Dt() {
|
|
2432
|
+
return R("auth");
|
|
2433
|
+
}
|
|
2434
|
+
function Ht(n) {
|
|
2435
|
+
return c(n);
|
|
2436
|
+
}
|
|
2437
|
+
function Wt(n, t) {
|
|
2438
|
+
const e = R("config");
|
|
2439
|
+
return typeof n > "u" ? e : e.get(n, t);
|
|
2440
|
+
}
|
|
2441
|
+
function Gt(n, t = "default") {
|
|
2442
|
+
return n ? R().make("error").get(n, t) : R().make("error");
|
|
2443
|
+
}
|
|
2444
|
+
function Vt(...n) {
|
|
2445
|
+
const t = R("log");
|
|
2446
|
+
return n.length ? t.debug(...n) : t;
|
|
2447
|
+
}
|
|
2448
|
+
function Xt(n) {
|
|
2449
|
+
const t = R("model");
|
|
2450
|
+
return n ? t.make(n) : t;
|
|
2451
|
+
}
|
|
2452
|
+
function Yt(n, t = !1) {
|
|
2453
|
+
const e = R("route");
|
|
2454
|
+
return n ? t ? e.url([n, t]) : e.url(n) : e;
|
|
2455
|
+
}
|
|
2456
|
+
class Zt {
|
|
2457
|
+
constructor() {
|
|
2458
|
+
p(this, "name");
|
|
2459
|
+
p(this, "version");
|
|
2460
|
+
}
|
|
2461
|
+
register(t) {
|
|
2462
|
+
}
|
|
2463
|
+
boot(t) {
|
|
2464
|
+
}
|
|
2465
|
+
}
|
|
2466
|
+
export {
|
|
2467
|
+
A as HasEvents,
|
|
2468
|
+
Zt as Plugin,
|
|
2469
|
+
v as PropertyBag,
|
|
2470
|
+
et as Reducible,
|
|
2471
|
+
R as app,
|
|
2472
|
+
Dt as auth,
|
|
2473
|
+
Ht as collect,
|
|
2474
|
+
Wt as config,
|
|
2475
|
+
Gt as error,
|
|
2476
|
+
T as isCollection,
|
|
2477
|
+
w as isModel,
|
|
2478
|
+
St as isValidationError,
|
|
2479
|
+
Vt as log,
|
|
2480
|
+
Xt as model,
|
|
2481
|
+
Yt as route
|
|
2482
|
+
};
|