@iamjulianacosta/mobx-data 1.0.1 → 1.1.1
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/dist/{JsonApiSerializer-CKB02AgP.cjs → JsonApiSerializer-Bc4iQB0d.cjs} +2 -2
- package/dist/{JsonApiSerializer-CKB02AgP.cjs.map → JsonApiSerializer-Bc4iQB0d.cjs.map} +1 -1
- package/dist/{JsonApiSerializer-CC5HXp4b.js → JsonApiSerializer-wndq5a1n.js} +2 -2
- package/dist/{JsonApiSerializer-CC5HXp4b.js.map → JsonApiSerializer-wndq5a1n.js.map} +1 -1
- package/dist/{MemoryAdapter-D1cTyydm.cjs → MemoryAdapter-BTK2D64s.cjs} +2 -2
- package/dist/{MemoryAdapter-D1cTyydm.cjs.map → MemoryAdapter-BTK2D64s.cjs.map} +1 -1
- package/dist/{MemoryAdapter-Bx1e7ndV.js → MemoryAdapter-ni25N4H0.js} +2 -2
- package/dist/{MemoryAdapter-Bx1e7ndV.js.map → MemoryAdapter-ni25N4H0.js.map} +1 -1
- package/dist/{ODataAdapter-C4IHK4BK.js → ODataAdapter-DAja_jKM.js} +2 -2
- package/dist/{ODataAdapter-C4IHK4BK.js.map → ODataAdapter-DAja_jKM.js.map} +1 -1
- package/dist/{ODataAdapter-DyyF1sdA.cjs → ODataAdapter-lMifLyLD.cjs} +2 -2
- package/dist/{ODataAdapter-DyyF1sdA.cjs.map → ODataAdapter-lMifLyLD.cjs.map} +1 -1
- package/dist/RestAdapter-1V94stW-.cjs +2 -0
- package/dist/{RestAdapter-CJOwTsKK.cjs.map → RestAdapter-1V94stW-.cjs.map} +1 -1
- package/dist/{RestAdapter-B4aRvs4m.js → RestAdapter-CGWqOR_G.js} +32 -23
- package/dist/{RestAdapter-B4aRvs4m.js.map → RestAdapter-CGWqOR_G.js.map} +1 -1
- package/dist/Store-KvjmBTQ9.cjs +2 -0
- package/dist/Store-KvjmBTQ9.cjs.map +1 -0
- package/dist/{Store-CZ7Z-Nme.js → Store-mvrDLQEZ.js} +195 -150
- package/dist/Store-mvrDLQEZ.js.map +1 -0
- package/dist/adapter/RestAdapter.d.ts.map +1 -1
- package/dist/adapter/index.cjs +1 -1
- package/dist/adapter/index.js +2 -2
- package/dist/cache/IndexedDBCache.d.ts +22 -0
- package/dist/cache/IndexedDBCache.d.ts.map +1 -0
- package/dist/cache/cache-utils.d.ts +4 -0
- package/dist/cache/cache-utils.d.ts.map +1 -0
- package/dist/cache/index.d.ts +4 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/types.d.ts +25 -0
- package/dist/cache/types.d.ts.map +1 -0
- package/dist/cache-utils-2lswvJ87.cjs +2 -0
- package/dist/cache-utils-2lswvJ87.cjs.map +1 -0
- package/dist/cache-utils-38Dqu4Qf.js +39 -0
- package/dist/cache-utils-38Dqu4Qf.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +144 -49
- package/dist/index.js.map +1 -1
- package/dist/json-api/index.cjs +1 -1
- package/dist/json-api/index.js +1 -1
- package/dist/odata/index.cjs +1 -1
- package/dist/odata/index.js +1 -1
- package/dist/store/Store.d.ts +6 -0
- package/dist/store/Store.d.ts.map +1 -1
- package/dist/store/index.cjs +1 -1
- package/dist/store/index.js +1 -1
- package/package.json +2 -1
- package/src/adapter/RestAdapter.ts +12 -2
- package/src/cache/IndexedDBCache.ts +171 -0
- package/src/cache/cache-utils.ts +57 -0
- package/src/cache/index.ts +12 -0
- package/src/cache/types.ts +33 -0
- package/src/index.ts +1 -0
- package/src/store/Store.ts +100 -0
- package/dist/RestAdapter-CJOwTsKK.cjs +0 -2
- package/dist/Store-BdwMrbDi.cjs +0 -2
- package/dist/Store-BdwMrbDi.cjs.map +0 -1
- package/dist/Store-CZ7Z-Nme.js.map +0 -1
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { singleton as
|
|
2
|
-
import { makeObservable as
|
|
3
|
-
import { S as
|
|
4
|
-
import { b as
|
|
5
|
-
|
|
1
|
+
import { singleton as M, injectable as F, inject as z } from "tsyringe";
|
|
2
|
+
import { makeObservable as _, action as b, observable as y, computed as u, runInAction as p } from "mobx";
|
|
3
|
+
import { S as D } from "./SchemaService-DZwkFgZu.js";
|
|
4
|
+
import { b as S, A as C, a as E, M as O } from "./relationships-BEXANmWg.js";
|
|
5
|
+
import { e as m, p as v } from "./cache-utils-38Dqu4Qf.js";
|
|
6
|
+
class q {
|
|
6
7
|
constructor() {
|
|
7
|
-
this._buckets = /* @__PURE__ */ new Map(),
|
|
8
|
+
this._buckets = /* @__PURE__ */ new Map(), _(this, {
|
|
8
9
|
_buckets: y.shallow,
|
|
9
|
-
set:
|
|
10
|
-
delete:
|
|
11
|
-
clear:
|
|
10
|
+
set: b,
|
|
11
|
+
delete: b,
|
|
12
|
+
clear: b
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
@@ -69,14 +70,14 @@ class C {
|
|
|
69
70
|
if (e)
|
|
70
71
|
return ((i = this.bucket(e)) == null ? void 0 : i.size) ?? 0;
|
|
71
72
|
let s = 0;
|
|
72
|
-
for (const
|
|
73
|
-
s +=
|
|
73
|
+
for (const a of this._buckets.values())
|
|
74
|
+
s += a.size;
|
|
74
75
|
return s;
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
class k {
|
|
78
79
|
constructor(e) {
|
|
79
|
-
this.updating = !1, this.opts = e,
|
|
80
|
+
this.updating = !1, this.opts = e, _(this, {
|
|
80
81
|
resolved: e.keepAlive ? u({ keepAlive: !0 }) : u,
|
|
81
82
|
updating: y,
|
|
82
83
|
length: u,
|
|
@@ -146,9 +147,9 @@ class k {
|
|
|
146
147
|
return this.resolved[Symbol.iterator]();
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
|
-
class
|
|
150
|
+
class I extends k {
|
|
150
151
|
constructor(e) {
|
|
151
|
-
super(e), this.queryParams = e.query, this.metaData = e.meta ?? {}, this.linksData = e.links ?? {},
|
|
152
|
+
super(e), this.queryParams = e.query, this.metaData = e.meta ?? {}, this.linksData = e.links ?? {}, _(this, {
|
|
152
153
|
metaData: y.ref,
|
|
153
154
|
linksData: y.ref,
|
|
154
155
|
meta: u,
|
|
@@ -177,14 +178,14 @@ class O extends k {
|
|
|
177
178
|
this.linksData = e;
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
|
-
var
|
|
181
|
-
for (var
|
|
182
|
-
(o = t[r]) && (
|
|
183
|
-
return
|
|
184
|
-
},
|
|
181
|
+
var T = Object.getOwnPropertyDescriptor, P = (t, e, s, i) => {
|
|
182
|
+
for (var a = i > 1 ? void 0 : i ? T(e, s) : e, r = t.length - 1, o; r >= 0; r--)
|
|
183
|
+
(o = t[r]) && (a = o(a) || a);
|
|
184
|
+
return a;
|
|
185
|
+
}, j = (t, e) => (s, i) => e(s, i, t);
|
|
185
186
|
let f = class {
|
|
186
187
|
constructor(t) {
|
|
187
|
-
this.identityMap = new
|
|
188
|
+
this.identityMap = new q(), this.adapters = /* @__PURE__ */ new Map(), this.serializers = /* @__PURE__ */ new Map(), this.newRecords = /* @__PURE__ */ new Map(), this.newRecordTypes = /* @__PURE__ */ new WeakMap(), this.relationshipCache = /* @__PURE__ */ new WeakMap(), this.pendingMembers = /* @__PURE__ */ new WeakMap(), this._cache = null, this.coalescePending = /* @__PURE__ */ new Map(), this.coalesceScheduled = /* @__PURE__ */ new Set(), this.schema = t;
|
|
188
189
|
}
|
|
189
190
|
static refEquals(t, e) {
|
|
190
191
|
return t.id === e.id && t.type === e.type;
|
|
@@ -198,6 +199,10 @@ let f = class {
|
|
|
198
199
|
registerSerializer(t, e) {
|
|
199
200
|
this.serializers.set(t, e);
|
|
200
201
|
}
|
|
202
|
+
/** Registers a persistent cache layer (e.g. IndexedDB) for offline-first reads. */
|
|
203
|
+
registerCache(t) {
|
|
204
|
+
this._cache = t;
|
|
205
|
+
}
|
|
201
206
|
/**
|
|
202
207
|
* Returns the adapter for `modelName`, falling back to `'application'`.
|
|
203
208
|
* @throws when no adapter is registered.
|
|
@@ -285,14 +290,14 @@ let f = class {
|
|
|
285
290
|
pushPayload(t, e) {
|
|
286
291
|
let s, i;
|
|
287
292
|
typeof t == "string" ? (s = t, i = e) : (s = null, i = t);
|
|
288
|
-
const
|
|
293
|
+
const a = s ? this.serializerFor(s).normalizeResponse(
|
|
289
294
|
this,
|
|
290
295
|
this.schema.modelFor(s),
|
|
291
296
|
i,
|
|
292
297
|
null,
|
|
293
298
|
"pushPayload"
|
|
294
299
|
) : i;
|
|
295
|
-
this.push(
|
|
300
|
+
this.push(a);
|
|
296
301
|
}
|
|
297
302
|
/**
|
|
298
303
|
* Normalizes a raw payload for `modelName` via the registered serializer
|
|
@@ -323,10 +328,10 @@ let f = class {
|
|
|
323
328
|
throw new Error(`Cannot push a resource of type "${e}" without an id`);
|
|
324
329
|
const i = this.identityMap.get(e, s);
|
|
325
330
|
if (i)
|
|
326
|
-
return
|
|
331
|
+
return p(() => {
|
|
327
332
|
i._applyServerData(null, t.attributes ?? {}, t.relationships);
|
|
328
333
|
}), this.trackInverseForResource(i, t), i;
|
|
329
|
-
const
|
|
334
|
+
const a = this.schema.modelFor(e), r = S.push.call(a, {
|
|
330
335
|
id: s,
|
|
331
336
|
data: t.attributes ?? {},
|
|
332
337
|
relationships: t.relationships,
|
|
@@ -341,12 +346,12 @@ let f = class {
|
|
|
341
346
|
trackInverseForResource(t, e) {
|
|
342
347
|
if (e.relationships)
|
|
343
348
|
for (const [s, i] of Object.entries(e.relationships)) {
|
|
344
|
-
const
|
|
345
|
-
if (!
|
|
349
|
+
const a = this.schema.relationshipsDefinitionFor(t.modelName).get(s);
|
|
350
|
+
if (!a || !a.options.inverse || !i.data)
|
|
346
351
|
continue;
|
|
347
352
|
const r = Array.isArray(i.data) ? i.data : [i.data];
|
|
348
353
|
for (const o of r)
|
|
349
|
-
this.addInverse(o.type, o.id,
|
|
354
|
+
this.addInverse(o.type, o.id, a.options.inverse, t);
|
|
350
355
|
}
|
|
351
356
|
}
|
|
352
357
|
/**
|
|
@@ -354,19 +359,19 @@ let f = class {
|
|
|
354
359
|
* No-ops when the target record is not in the identity map.
|
|
355
360
|
*/
|
|
356
361
|
addInverse(t, e, s, i) {
|
|
357
|
-
const
|
|
358
|
-
if (!
|
|
362
|
+
const a = this.identityMap.get(t, e);
|
|
363
|
+
if (!a)
|
|
359
364
|
return;
|
|
360
365
|
const o = this.schema.relationshipsDefinitionFor(t).get(s);
|
|
361
366
|
if (!o)
|
|
362
367
|
return;
|
|
363
|
-
const
|
|
364
|
-
|
|
368
|
+
const n = a._getRelationshipRef(s), h = { type: i.modelName, id: i.id };
|
|
369
|
+
p(() => {
|
|
365
370
|
if (o.kind === "hasMany") {
|
|
366
|
-
const c =
|
|
367
|
-
c.some((
|
|
371
|
+
const c = n != null && n.data && Array.isArray(n.data) ? n.data : [];
|
|
372
|
+
c.some((d) => f.refEquals(d, h)) || a._setRelationshipRef(s, { data: [...c, h] });
|
|
368
373
|
} else
|
|
369
|
-
|
|
374
|
+
a._setRelationshipRef(s, { data: h });
|
|
370
375
|
});
|
|
371
376
|
}
|
|
372
377
|
/**
|
|
@@ -374,19 +379,19 @@ let f = class {
|
|
|
374
379
|
* No-ops when the target record is not in the identity map.
|
|
375
380
|
*/
|
|
376
381
|
removeInverse(t, e, s, i) {
|
|
377
|
-
const
|
|
378
|
-
if (!
|
|
382
|
+
const a = this.identityMap.get(t, e);
|
|
383
|
+
if (!a)
|
|
379
384
|
return;
|
|
380
385
|
const o = this.schema.relationshipsDefinitionFor(t).get(s);
|
|
381
386
|
if (!o)
|
|
382
387
|
return;
|
|
383
|
-
const
|
|
384
|
-
|
|
388
|
+
const n = a._getRelationshipRef(s);
|
|
389
|
+
p(() => {
|
|
385
390
|
if (o.kind === "hasMany") {
|
|
386
|
-
const c = (
|
|
387
|
-
|
|
391
|
+
const c = (n != null && n.data && Array.isArray(n.data) ? n.data : []).filter((d) => !(d.id === i.id && d.type === i.modelName));
|
|
392
|
+
a._setRelationshipRef(s, { data: c });
|
|
388
393
|
} else
|
|
389
|
-
|
|
394
|
+
a._setRelationshipRef(s, { data: null });
|
|
390
395
|
});
|
|
391
396
|
}
|
|
392
397
|
// --- unload ---
|
|
@@ -423,41 +428,61 @@ let f = class {
|
|
|
423
428
|
const i = this.peekRecord(t, e);
|
|
424
429
|
if (i && !s.reload && !s.include)
|
|
425
430
|
return i;
|
|
426
|
-
|
|
427
|
-
|
|
431
|
+
if (!s.reload && !s.include && this._cache) {
|
|
432
|
+
const l = await this._cache.get(t, e);
|
|
433
|
+
if (l)
|
|
434
|
+
return this.push({
|
|
435
|
+
data: {
|
|
436
|
+
type: l.modelName,
|
|
437
|
+
id: l.id,
|
|
438
|
+
attributes: l.attributes,
|
|
439
|
+
relationships: l.relationships
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
const a = this.adapterFor(t);
|
|
444
|
+
if (a.coalesceFindRequests && a.findMany && !s.include)
|
|
428
445
|
return this.scheduleCoalescedFind(t, e);
|
|
429
|
-
const r = i ? this.createSnapshot(i) : this.createEmptySnapshot(t, e), o = s.include ? { include: s.include, adapterOptions: s.adapterOptions } : s.adapterOptions ? { adapterOptions: s.adapterOptions } : void 0,
|
|
446
|
+
const r = i ? this.createSnapshot(i) : this.createEmptySnapshot(t, e), o = s.include ? { include: s.include, adapterOptions: s.adapterOptions } : s.adapterOptions ? { adapterOptions: s.adapterOptions } : void 0, n = await a.findRecord(this, t, e, r, o), h = m(n), c = this.serializerFor(t).normalizeResponse(
|
|
430
447
|
this,
|
|
431
448
|
this.schema.modelFor(t),
|
|
432
|
-
|
|
449
|
+
n,
|
|
433
450
|
e,
|
|
434
451
|
"findRecord"
|
|
435
|
-
);
|
|
436
|
-
|
|
452
|
+
), d = this.push(c);
|
|
453
|
+
if (this._cache) {
|
|
454
|
+
const l = h ? v(h) : void 0;
|
|
455
|
+
l !== 0 && this.cacheNormalizedDocument(c, l);
|
|
456
|
+
}
|
|
457
|
+
return d;
|
|
437
458
|
}
|
|
438
459
|
/**
|
|
439
460
|
* Fetches all records of `modelName` from the server and returns a
|
|
440
461
|
* `RecordArray` backed by the identity map.
|
|
441
462
|
*/
|
|
442
463
|
async findAll(t, e = {}) {
|
|
443
|
-
const s = this.adapterFor(t), i = e.include ? { include: e.include, adapterOptions: e.adapterOptions } : e.adapterOptions ? { adapterOptions: e.adapterOptions } : void 0,
|
|
464
|
+
const s = this.adapterFor(t), i = e.include ? { include: e.include, adapterOptions: e.adapterOptions } : e.adapterOptions ? { adapterOptions: e.adapterOptions } : void 0, a = await s.findAll(this, t, null, [], i), r = m(a), o = this.serializerFor(t).normalizeResponse(
|
|
444
465
|
this,
|
|
445
466
|
this.schema.modelFor(t),
|
|
446
|
-
|
|
467
|
+
a,
|
|
447
468
|
null,
|
|
448
469
|
"findAll"
|
|
449
470
|
);
|
|
450
|
-
|
|
471
|
+
if (this.push(o), this._cache) {
|
|
472
|
+
const n = r ? v(r) : void 0;
|
|
473
|
+
n !== 0 && this.cacheNormalizedDocument(o, n);
|
|
474
|
+
}
|
|
475
|
+
return this.peekAll(t);
|
|
451
476
|
}
|
|
452
477
|
/**
|
|
453
478
|
* Executes an adapter query and returns an `AdapterPopulatedRecordArray`
|
|
454
479
|
* whose `update()` method re-issues the same query.
|
|
455
480
|
*/
|
|
456
481
|
async query(t, e) {
|
|
457
|
-
const s = [], i = new
|
|
482
|
+
const s = [], i = new I({
|
|
458
483
|
modelName: t,
|
|
459
484
|
query: e,
|
|
460
|
-
source: () => s.map((
|
|
485
|
+
source: () => s.map((a) => this.peekRecord(t, a)).filter((a) => a !== null),
|
|
461
486
|
update: async () => {
|
|
462
487
|
await this.runQuery(t, e, i, s);
|
|
463
488
|
}
|
|
@@ -473,8 +498,8 @@ let f = class {
|
|
|
473
498
|
"query"
|
|
474
499
|
);
|
|
475
500
|
if (this.push(o), i.length = 0, Array.isArray(o.data))
|
|
476
|
-
for (const
|
|
477
|
-
|
|
501
|
+
for (const n of o.data)
|
|
502
|
+
n.id && i.push(n.id);
|
|
478
503
|
o.meta && s._setMeta(o.meta), o.links && s._setLinks(o.links);
|
|
479
504
|
}
|
|
480
505
|
/**
|
|
@@ -482,13 +507,13 @@ let f = class {
|
|
|
482
507
|
* Returns `null` when the adapter returns an empty payload.
|
|
483
508
|
*/
|
|
484
509
|
async queryRecord(t, e) {
|
|
485
|
-
const i = await this.adapterFor(t).queryRecord(this, t, e),
|
|
510
|
+
const i = await this.adapterFor(t).queryRecord(this, t, e), a = this.serializerFor(t).normalizeResponse(
|
|
486
511
|
this,
|
|
487
512
|
this.schema.modelFor(t),
|
|
488
513
|
i,
|
|
489
514
|
null,
|
|
490
515
|
"queryRecord"
|
|
491
|
-
), r = this.push(
|
|
516
|
+
), r = this.push(a);
|
|
492
517
|
return Array.isArray(r) ? r[0] ?? null : r ?? null;
|
|
493
518
|
}
|
|
494
519
|
// --- save / delete / reload from Model ---
|
|
@@ -502,25 +527,33 @@ let f = class {
|
|
|
502
527
|
* record via `_applyServerData` so it transitions to `saved`.
|
|
503
528
|
*/
|
|
504
529
|
async saveRecord(t, e = {}) {
|
|
505
|
-
const s = this.adapterFor(t.modelName), i = this.createSnapshot(t), { isNew:
|
|
530
|
+
const s = this.adapterFor(t.modelName), i = this.createSnapshot(t), { isNew: a } = t;
|
|
506
531
|
let r;
|
|
507
|
-
|
|
532
|
+
a ? r = await s.createRecord(this, t.modelName, i) : e.patch && s.patchRecord ? r = await s.patchRecord(this, t.modelName, i) : r = await s.updateRecord(this, t.modelName, i);
|
|
508
533
|
const o = this.serializerFor(t.modelName).normalizeResponse(
|
|
509
534
|
this,
|
|
510
535
|
this.schema.modelFor(t.modelName),
|
|
511
536
|
r,
|
|
512
537
|
t.id,
|
|
513
|
-
|
|
514
|
-
),
|
|
515
|
-
if (
|
|
516
|
-
const h =
|
|
517
|
-
|
|
518
|
-
t._applyServerData(h,
|
|
519
|
-
}),
|
|
538
|
+
a ? "createRecord" : "updateRecord"
|
|
539
|
+
), n = o.data;
|
|
540
|
+
if (n) {
|
|
541
|
+
const h = n.id ?? t.id;
|
|
542
|
+
p(() => {
|
|
543
|
+
t._applyServerData(h, n.attributes ?? {}, n.relationships);
|
|
544
|
+
}), a && h && (this.untrackNewRecord(t), this.identityMap.set(t.modelName, h, t));
|
|
520
545
|
}
|
|
521
546
|
if (o.included)
|
|
522
547
|
for (const h of o.included)
|
|
523
548
|
this.pushResource(h);
|
|
549
|
+
if (this._cache && t.id) {
|
|
550
|
+
const h = t, c = {};
|
|
551
|
+
for (const [d, l] of h._relationships)
|
|
552
|
+
c[d] = l;
|
|
553
|
+
this._cache.set(t.modelName, t.id, { ...h._data }, {
|
|
554
|
+
relationships: Object.keys(c).length > 0 ? c : void 0
|
|
555
|
+
});
|
|
556
|
+
}
|
|
524
557
|
return t;
|
|
525
558
|
}
|
|
526
559
|
/**
|
|
@@ -528,7 +561,7 @@ let f = class {
|
|
|
528
561
|
*/
|
|
529
562
|
async deleteRecord(t) {
|
|
530
563
|
const e = this.adapterFor(t.modelName), s = this.createSnapshot(t);
|
|
531
|
-
return await e.deleteRecord(this, t.modelName, s), this.unloadRecord(t), t;
|
|
564
|
+
return await e.deleteRecord(this, t.modelName, s), this._cache && t.id && this._cache.invalidate(t.modelName, t.id), this.unloadRecord(t), t;
|
|
532
565
|
}
|
|
533
566
|
/**
|
|
534
567
|
* Re-fetches a record from the server and merges the response into the
|
|
@@ -537,14 +570,14 @@ let f = class {
|
|
|
537
570
|
async reloadRecord(t) {
|
|
538
571
|
if (!t.id)
|
|
539
572
|
throw new Error("Cannot reload a record without an id");
|
|
540
|
-
const e = this.adapterFor(t.modelName), s = this.createSnapshot(t), i = await e.findRecord(this, t.modelName, t.id, s),
|
|
573
|
+
const e = this.adapterFor(t.modelName), s = this.createSnapshot(t), i = await e.findRecord(this, t.modelName, t.id, s), a = this.serializerFor(t.modelName).normalizeResponse(
|
|
541
574
|
this,
|
|
542
575
|
this.schema.modelFor(t.modelName),
|
|
543
576
|
i,
|
|
544
577
|
t.id,
|
|
545
578
|
"findRecord"
|
|
546
579
|
);
|
|
547
|
-
return this.push(
|
|
580
|
+
return this.push(a), t;
|
|
548
581
|
}
|
|
549
582
|
// --- snapshot ---
|
|
550
583
|
/**
|
|
@@ -553,28 +586,28 @@ let f = class {
|
|
|
553
586
|
* reflects the current (possibly dirty) values.
|
|
554
587
|
*/
|
|
555
588
|
createSnapshot(t) {
|
|
556
|
-
const { modelName: e } = t, s = this.schema.attributesDefinitionFor(e), i = this.schema.relationshipsDefinitionFor(e),
|
|
589
|
+
const { modelName: e } = t, s = this.schema.attributesDefinitionFor(e), i = this.schema.relationshipsDefinitionFor(e), a = t;
|
|
557
590
|
return {
|
|
558
591
|
id: t.id,
|
|
559
592
|
modelName: e,
|
|
560
593
|
record: t,
|
|
561
|
-
attr: (r) =>
|
|
594
|
+
attr: (r) => a._data[r],
|
|
562
595
|
belongsTo: (r, o) => {
|
|
563
|
-
const
|
|
564
|
-
return !(
|
|
596
|
+
const n = a._getRelationshipRef(r);
|
|
597
|
+
return !(n != null && n.data) || Array.isArray(n.data) ? null : o != null && o.id ? n.data.id : this.peekRecord(n.data.type, n.data.id);
|
|
565
598
|
},
|
|
566
599
|
hasMany: (r, o) => {
|
|
567
|
-
const
|
|
600
|
+
const n = a._getRelationshipRef(r), h = n != null && n.data && Array.isArray(n.data) ? n.data : [];
|
|
568
601
|
return o != null && o.ids ? h.map((c) => c.id) : h.map((c) => this.peekRecord(c.type, c.id)).filter((c) => c !== null);
|
|
569
602
|
},
|
|
570
|
-
changedAttributes: () =>
|
|
603
|
+
changedAttributes: () => a.changedAttributes(),
|
|
571
604
|
eachAttribute: (r) => {
|
|
572
|
-
for (const [o,
|
|
573
|
-
r(o,
|
|
605
|
+
for (const [o, n] of s)
|
|
606
|
+
r(o, n);
|
|
574
607
|
},
|
|
575
608
|
eachRelationship: (r) => {
|
|
576
|
-
for (const [o,
|
|
577
|
-
r(o,
|
|
609
|
+
for (const [o, n] of i)
|
|
610
|
+
r(o, n);
|
|
578
611
|
}
|
|
579
612
|
};
|
|
580
613
|
}
|
|
@@ -593,13 +626,13 @@ let f = class {
|
|
|
593
626
|
belongsTo: () => null,
|
|
594
627
|
hasMany: () => [],
|
|
595
628
|
changedAttributes: () => ({}),
|
|
596
|
-
eachAttribute: (
|
|
629
|
+
eachAttribute: (a) => {
|
|
597
630
|
for (const [r, o] of s)
|
|
598
|
-
|
|
631
|
+
a(r, o);
|
|
599
632
|
},
|
|
600
|
-
eachRelationship: (
|
|
633
|
+
eachRelationship: (a) => {
|
|
601
634
|
for (const [r, o] of i)
|
|
602
|
-
|
|
635
|
+
a(r, o);
|
|
603
636
|
}
|
|
604
637
|
};
|
|
605
638
|
}
|
|
@@ -624,9 +657,9 @@ let f = class {
|
|
|
624
657
|
* repeated accesses (important for MobX observability).
|
|
625
658
|
*/
|
|
626
659
|
resolveRelationship(t, e, s) {
|
|
627
|
-
const i = s.options.async === !0,
|
|
628
|
-
if (
|
|
629
|
-
return
|
|
660
|
+
const i = s.options.async === !0, a = this.getRelationshipCache(t, e);
|
|
661
|
+
if (a)
|
|
662
|
+
return a;
|
|
630
663
|
const r = {
|
|
631
664
|
parent: t,
|
|
632
665
|
name: e,
|
|
@@ -635,17 +668,17 @@ let f = class {
|
|
|
635
668
|
};
|
|
636
669
|
if (i) {
|
|
637
670
|
if (s.kind === "belongsTo") {
|
|
638
|
-
const h = new
|
|
671
|
+
const h = new C(r);
|
|
639
672
|
return this.setRelationshipCache(t, e, h), h;
|
|
640
673
|
}
|
|
641
|
-
const
|
|
642
|
-
return this.setRelationshipCache(t, e,
|
|
674
|
+
const n = new E(r);
|
|
675
|
+
return this.setRelationshipCache(t, e, n), n;
|
|
643
676
|
}
|
|
644
677
|
if (s.kind === "belongsTo") {
|
|
645
|
-
const
|
|
646
|
-
return !(
|
|
678
|
+
const n = t._getRelationshipRef(e);
|
|
679
|
+
return !(n != null && n.data) || Array.isArray(n.data) ? null : this.peekRecord(n.data.type, n.data.id);
|
|
647
680
|
}
|
|
648
|
-
const o = new
|
|
681
|
+
const o = new O(r);
|
|
649
682
|
return this.setRelationshipCache(t, e, o), o;
|
|
650
683
|
}
|
|
651
684
|
/**
|
|
@@ -655,17 +688,17 @@ let f = class {
|
|
|
655
688
|
setRelationshipValue(t, e, s, i) {
|
|
656
689
|
if (s.kind !== "belongsTo")
|
|
657
690
|
return;
|
|
658
|
-
const
|
|
691
|
+
const a = t._getRelationshipRef(e), r = a != null && a.data && !Array.isArray(a.data) ? a.data : null;
|
|
659
692
|
if (i == null) {
|
|
660
|
-
|
|
693
|
+
p(() => {
|
|
661
694
|
t._setRelationshipRef(e, { data: null });
|
|
662
695
|
}), r && s.options.inverse && this.removeInverse(r.type, r.id, s.options.inverse, t);
|
|
663
696
|
return;
|
|
664
697
|
}
|
|
665
|
-
const o = i,
|
|
666
|
-
|
|
667
|
-
t._setRelationshipRef(e, { data:
|
|
668
|
-
}), s.options.inverse && (r && !f.refEquals(r,
|
|
698
|
+
const o = i, n = { type: o.modelName, id: o.id };
|
|
699
|
+
p(() => {
|
|
700
|
+
t._setRelationshipRef(e, { data: n });
|
|
701
|
+
}), s.options.inverse && (r && !f.refEquals(r, n) && this.removeInverse(r.type, r.id, s.options.inverse, t), this.addInverse(n.type, n.id, s.options.inverse, t));
|
|
669
702
|
}
|
|
670
703
|
// --- hooks used by ManyArray ---
|
|
671
704
|
/** Returns the raw relationship ref stored on `record` for `name`. */
|
|
@@ -680,12 +713,12 @@ let f = class {
|
|
|
680
713
|
addPendingMember(t, e, s) {
|
|
681
714
|
let i = this.pendingMembers.get(t);
|
|
682
715
|
i || (i = /* @__PURE__ */ new Map(), this.pendingMembers.set(t, i));
|
|
683
|
-
let
|
|
684
|
-
|
|
716
|
+
let a = i.get(e);
|
|
717
|
+
a || (a = y.set(), i.set(e, a)), a.add(s);
|
|
685
718
|
}
|
|
686
719
|
removePendingMember(t, e, s) {
|
|
687
|
-
var i,
|
|
688
|
-
(
|
|
720
|
+
var i, a;
|
|
721
|
+
(a = (i = this.pendingMembers.get(t)) == null ? void 0 : i.get(e)) == null || a.delete(s);
|
|
689
722
|
}
|
|
690
723
|
/**
|
|
691
724
|
* Appends `value` to the `hasMany` relationship ref on `record` and syncs
|
|
@@ -695,8 +728,8 @@ let f = class {
|
|
|
695
728
|
_hasManyAppend(t, e, s, i) {
|
|
696
729
|
if (i.id === null) {
|
|
697
730
|
if (this.addPendingMember(t, e, i), s.options.inverse) {
|
|
698
|
-
const
|
|
699
|
-
(
|
|
731
|
+
const n = this.schema.relationshipsDefinitionFor(i.modelName).get(s.options.inverse);
|
|
732
|
+
(n == null ? void 0 : n.kind) === "belongsTo" && p(() => {
|
|
700
733
|
i._setRelationshipRef(s.options.inverse, {
|
|
701
734
|
data: { type: t.modelName, id: t.id }
|
|
702
735
|
});
|
|
@@ -704,8 +737,8 @@ let f = class {
|
|
|
704
737
|
}
|
|
705
738
|
return;
|
|
706
739
|
}
|
|
707
|
-
const
|
|
708
|
-
r.some((
|
|
740
|
+
const a = this._getRelationshipRefFor(t, e), r = a != null && a.data && Array.isArray(a.data) ? a.data : [], o = { type: i.modelName, id: i.id };
|
|
741
|
+
r.some((n) => f.refEquals(n, o)) || p(() => {
|
|
709
742
|
t._setRelationshipRef(e, { data: [...r, o] });
|
|
710
743
|
}), s.options.inverse && this.addInverse(i.modelName, i.id, s.options.inverse, t);
|
|
711
744
|
}
|
|
@@ -718,19 +751,31 @@ let f = class {
|
|
|
718
751
|
this.removePendingMember(t, e, i);
|
|
719
752
|
return;
|
|
720
753
|
}
|
|
721
|
-
const
|
|
722
|
-
(
|
|
754
|
+
const a = this._getRelationshipRefFor(t, e), o = (a != null && a.data && Array.isArray(a.data) ? a.data : []).filter(
|
|
755
|
+
(n) => !(n.id === i.id && n.type === i.modelName)
|
|
723
756
|
);
|
|
724
|
-
|
|
757
|
+
p(() => {
|
|
725
758
|
t._setRelationshipRef(e, { data: o });
|
|
726
759
|
}), s.options.inverse && this.removeInverse(i.modelName, i.id, s.options.inverse, t);
|
|
727
760
|
}
|
|
761
|
+
// --- persistent cache helpers ---
|
|
762
|
+
cacheNormalizedDocument(t, e) {
|
|
763
|
+
if (!this._cache)
|
|
764
|
+
return;
|
|
765
|
+
const s = [];
|
|
766
|
+
t.data && (Array.isArray(t.data) ? s.push(...t.data) : s.push(t.data)), t.included && s.push(...t.included);
|
|
767
|
+
for (const i of s)
|
|
768
|
+
i.id && this._cache.set(i.type, i.id, i.attributes ?? {}, {
|
|
769
|
+
relationships: i.relationships,
|
|
770
|
+
ttl: e
|
|
771
|
+
});
|
|
772
|
+
}
|
|
728
773
|
scheduleCoalescedFind(t, e) {
|
|
729
774
|
return new Promise((s, i) => {
|
|
730
|
-
let
|
|
731
|
-
|
|
732
|
-
let r =
|
|
733
|
-
r || (r = [],
|
|
775
|
+
let a = this.coalescePending.get(t);
|
|
776
|
+
a || (a = /* @__PURE__ */ new Map(), this.coalescePending.set(t, a));
|
|
777
|
+
let r = a.get(e);
|
|
778
|
+
r || (r = [], a.set(e, r)), r.push({ resolve: s, reject: i }), this.coalesceScheduled.has(t) || (this.coalesceScheduled.add(t), queueMicrotask(() => this.flushCoalescedFind(t)));
|
|
734
779
|
});
|
|
735
780
|
}
|
|
736
781
|
async flushCoalescedFind(t) {
|
|
@@ -740,29 +785,29 @@ let f = class {
|
|
|
740
785
|
return;
|
|
741
786
|
const s = new Map(e);
|
|
742
787
|
e.clear();
|
|
743
|
-
const i = Array.from(s.keys()),
|
|
788
|
+
const i = Array.from(s.keys()), a = this.adapterFor(t);
|
|
744
789
|
try {
|
|
745
790
|
const r = i.map((h) => {
|
|
746
791
|
const c = this.peekRecord(t, h);
|
|
747
792
|
return c ? this.createSnapshot(c) : this.createEmptySnapshot(t, h);
|
|
748
|
-
}), o = await
|
|
793
|
+
}), o = await a.findMany(this, t, i, r), n = this.serializerFor(t).normalizeResponse(
|
|
749
794
|
this,
|
|
750
795
|
this.schema.modelFor(t),
|
|
751
796
|
o,
|
|
752
797
|
null,
|
|
753
798
|
"findMany"
|
|
754
799
|
);
|
|
755
|
-
this.push(
|
|
800
|
+
this.push(n);
|
|
756
801
|
for (const h of i) {
|
|
757
|
-
const c = this.peekRecord(t, h),
|
|
758
|
-
if (
|
|
759
|
-
for (const
|
|
760
|
-
c ?
|
|
802
|
+
const c = this.peekRecord(t, h), d = s.get(h);
|
|
803
|
+
if (d)
|
|
804
|
+
for (const l of d)
|
|
805
|
+
c ? l.resolve(c) : l.reject(new Error(`Record not found after findMany: ${t}:${h}`));
|
|
761
806
|
}
|
|
762
807
|
} catch (r) {
|
|
763
808
|
for (const o of s.values())
|
|
764
|
-
for (const
|
|
765
|
-
|
|
809
|
+
for (const n of o)
|
|
810
|
+
n.reject(r);
|
|
766
811
|
}
|
|
767
812
|
}
|
|
768
813
|
// --- liveQuery ---
|
|
@@ -800,16 +845,16 @@ let f = class {
|
|
|
800
845
|
* @throws Re-throws the error from `persistFn` after rollback.
|
|
801
846
|
*/
|
|
802
847
|
async optimisticUpdate(t, e, s) {
|
|
803
|
-
const i = t,
|
|
804
|
-
|
|
848
|
+
const i = t, a = { ...i._data };
|
|
849
|
+
p(() => {
|
|
805
850
|
Object.assign(i._data, e);
|
|
806
851
|
});
|
|
807
852
|
try {
|
|
808
853
|
return await s(), t;
|
|
809
854
|
} catch (r) {
|
|
810
|
-
throw
|
|
811
|
-
for (const [o,
|
|
812
|
-
i._data[o] =
|
|
855
|
+
throw p(() => {
|
|
856
|
+
for (const [o, n] of Object.entries(a))
|
|
857
|
+
i._data[o] = n;
|
|
813
858
|
}), r;
|
|
814
859
|
}
|
|
815
860
|
}
|
|
@@ -822,7 +867,7 @@ let f = class {
|
|
|
822
867
|
* @param callback - Synchronous function containing one or more store mutations.
|
|
823
868
|
*/
|
|
824
869
|
runInTransaction(t) {
|
|
825
|
-
|
|
870
|
+
p(t);
|
|
826
871
|
}
|
|
827
872
|
// --- SSR: serialize / hydrate ---
|
|
828
873
|
/**
|
|
@@ -839,30 +884,30 @@ let f = class {
|
|
|
839
884
|
serialize(t = {}) {
|
|
840
885
|
var i;
|
|
841
886
|
const e = {}, s = this.identityMap._buckets;
|
|
842
|
-
for (const [
|
|
843
|
-
const o = (i = t.exclude) == null ? void 0 : i[
|
|
887
|
+
for (const [a, r] of s) {
|
|
888
|
+
const o = (i = t.exclude) == null ? void 0 : i[a], n = [];
|
|
844
889
|
for (const [h, c] of r) {
|
|
845
|
-
const
|
|
846
|
-
let
|
|
890
|
+
const d = c;
|
|
891
|
+
let l;
|
|
847
892
|
if (o && o.length > 0) {
|
|
848
|
-
|
|
849
|
-
for (const [R,
|
|
850
|
-
o.includes(R) || (
|
|
893
|
+
l = {};
|
|
894
|
+
for (const [R, w] of Object.entries(d._data))
|
|
895
|
+
o.includes(R) || (l[R] = w);
|
|
851
896
|
} else
|
|
852
|
-
|
|
853
|
-
const
|
|
897
|
+
l = { ...d._data };
|
|
898
|
+
const g = {
|
|
854
899
|
id: h,
|
|
855
|
-
attributes:
|
|
900
|
+
attributes: l
|
|
856
901
|
};
|
|
857
|
-
if (
|
|
902
|
+
if (d._relationships && d._relationships.size > 0) {
|
|
858
903
|
const R = {};
|
|
859
|
-
for (const [
|
|
860
|
-
R[
|
|
861
|
-
|
|
904
|
+
for (const [w, A] of d._relationships)
|
|
905
|
+
R[w] = A;
|
|
906
|
+
g.relationships = R;
|
|
862
907
|
}
|
|
863
|
-
|
|
908
|
+
n.push(g);
|
|
864
909
|
}
|
|
865
|
-
|
|
910
|
+
n.length > 0 && (e[a] = n);
|
|
866
911
|
}
|
|
867
912
|
return { records: e };
|
|
868
913
|
}
|
|
@@ -874,7 +919,7 @@ let f = class {
|
|
|
874
919
|
* @param snapshot - A snapshot object previously returned by `serialize()`.
|
|
875
920
|
*/
|
|
876
921
|
hydrate(t) {
|
|
877
|
-
|
|
922
|
+
p(() => {
|
|
878
923
|
for (const [e, s] of Object.entries(t.records))
|
|
879
924
|
for (const i of s)
|
|
880
925
|
this.pushResource({
|
|
@@ -898,15 +943,15 @@ let f = class {
|
|
|
898
943
|
return s.hydrate(e), s;
|
|
899
944
|
}
|
|
900
945
|
};
|
|
901
|
-
f =
|
|
902
|
-
|
|
946
|
+
f = P([
|
|
947
|
+
M(),
|
|
903
948
|
F(),
|
|
904
|
-
|
|
949
|
+
j(0, z(D))
|
|
905
950
|
], f);
|
|
906
951
|
export {
|
|
907
|
-
|
|
908
|
-
|
|
952
|
+
I as A,
|
|
953
|
+
q as I,
|
|
909
954
|
k as R,
|
|
910
955
|
f as S
|
|
911
956
|
};
|
|
912
|
-
//# sourceMappingURL=Store-
|
|
957
|
+
//# sourceMappingURL=Store-mvrDLQEZ.js.map
|