@instantdb/core 0.22.86-experimental.separate-attrs.20122276424.1 → 0.22.86-experimental.split-store.20183617880.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.
Files changed (65) hide show
  1. package/__tests__/src/Reactor.test.js +18 -11
  2. package/__tests__/src/{datalog.test.js → datalog.test.ts} +17 -5
  3. package/__tests__/src/{instaml.test.js → instaml.test.ts} +183 -119
  4. package/__tests__/src/instaql.bench.ts +34 -0
  5. package/__tests__/src/{instaql.test.js → instaql.test.ts} +342 -455
  6. package/__tests__/src/instaqlInference.test.js +13 -9
  7. package/__tests__/src/{store.test.js → store.test.ts} +188 -210
  8. package/dist/commonjs/Reactor.d.ts +23 -6
  9. package/dist/commonjs/Reactor.d.ts.map +1 -1
  10. package/dist/commonjs/Reactor.js +110 -45
  11. package/dist/commonjs/Reactor.js.map +1 -1
  12. package/dist/commonjs/SyncTable.d.ts +4 -1
  13. package/dist/commonjs/SyncTable.d.ts.map +1 -1
  14. package/dist/commonjs/SyncTable.js +35 -37
  15. package/dist/commonjs/SyncTable.js.map +1 -1
  16. package/dist/commonjs/instaml.d.ts +17 -4
  17. package/dist/commonjs/instaml.d.ts.map +1 -1
  18. package/dist/commonjs/instaml.js +105 -76
  19. package/dist/commonjs/instaml.js.map +1 -1
  20. package/dist/commonjs/instaql.d.ts +4 -3
  21. package/dist/commonjs/instaql.d.ts.map +1 -1
  22. package/dist/commonjs/instaql.js +65 -63
  23. package/dist/commonjs/instaql.js.map +1 -1
  24. package/dist/commonjs/reactorTypes.d.ts +29 -0
  25. package/dist/commonjs/reactorTypes.d.ts.map +1 -0
  26. package/dist/commonjs/reactorTypes.js +3 -0
  27. package/dist/commonjs/reactorTypes.js.map +1 -0
  28. package/dist/commonjs/store.d.ts +67 -25
  29. package/dist/commonjs/store.d.ts.map +1 -1
  30. package/dist/commonjs/store.js +170 -70
  31. package/dist/commonjs/store.js.map +1 -1
  32. package/dist/esm/Reactor.d.ts +23 -6
  33. package/dist/esm/Reactor.d.ts.map +1 -1
  34. package/dist/esm/Reactor.js +111 -46
  35. package/dist/esm/Reactor.js.map +1 -1
  36. package/dist/esm/SyncTable.d.ts +4 -1
  37. package/dist/esm/SyncTable.d.ts.map +1 -1
  38. package/dist/esm/SyncTable.js +35 -37
  39. package/dist/esm/SyncTable.js.map +1 -1
  40. package/dist/esm/instaml.d.ts +17 -4
  41. package/dist/esm/instaml.d.ts.map +1 -1
  42. package/dist/esm/instaml.js +102 -71
  43. package/dist/esm/instaml.js.map +1 -1
  44. package/dist/esm/instaql.d.ts +4 -3
  45. package/dist/esm/instaql.d.ts.map +1 -1
  46. package/dist/esm/instaql.js +65 -63
  47. package/dist/esm/instaql.js.map +1 -1
  48. package/dist/esm/reactorTypes.d.ts +29 -0
  49. package/dist/esm/reactorTypes.d.ts.map +1 -0
  50. package/dist/esm/reactorTypes.js +2 -0
  51. package/dist/esm/reactorTypes.js.map +1 -0
  52. package/dist/esm/store.d.ts +67 -25
  53. package/dist/esm/store.d.ts.map +1 -1
  54. package/dist/esm/store.js +167 -70
  55. package/dist/esm/store.js.map +1 -1
  56. package/dist/standalone/index.js +1580 -1392
  57. package/dist/standalone/index.umd.cjs +3 -3
  58. package/package.json +2 -2
  59. package/src/Reactor.js +154 -77
  60. package/src/SyncTable.ts +85 -45
  61. package/src/{instaml.js → instaml.ts} +196 -95
  62. package/src/instaql.ts +88 -62
  63. package/src/reactorTypes.ts +32 -0
  64. package/src/store.ts +248 -90
  65. package/__tests__/src/instaql.bench.js +0 -29
@@ -2,47 +2,89 @@ import { InstantDBAttr } from './attrTypes.ts';
2
2
  import { LinkIndex } from './utils/linkIndex.ts';
3
3
  type Triple = [string, string, any, number];
4
4
  type Attrs = Record<string, InstantDBAttr>;
5
- type AttrIndexes = {
6
- blobAttrs: Map<string, Map<string, InstantDBAttr>>;
7
- primaryKeys: Map<string, InstantDBAttr>;
8
- forwardIdents: Map<string, Map<string, InstantDBAttr>>;
9
- revIdents: Map<string, Map<string, InstantDBAttr>>;
10
- };
11
5
  export type Store = {
12
6
  eav: Map<string, Map<string, Map<any, Triple>>>;
13
7
  aev: Map<string, Map<string, Map<any, Triple>>>;
14
8
  vae: Map<any, Map<string, Map<string, Triple>>>;
15
- useDateObjects: boolean | null;
16
- attrs: Attrs;
17
- attrIndexes: AttrIndexes;
18
- cardinalityInference: boolean | null;
19
- linkIndex: LinkIndex | null;
20
- __type: 'store';
9
+ useDateObjects: boolean | null | undefined;
10
+ cardinalityInference: boolean | null | undefined;
21
11
  };
22
- export type StoreJson = {
12
+ type StoreJsonVersion0 = {
23
13
  __type: 'store';
24
14
  attrs: Attrs;
25
15
  triples: Triple[];
26
- cardinalityInference: boolean | null;
16
+ cardinalityInference: boolean | null | undefined;
17
+ linkIndex: LinkIndex | null;
18
+ useDateObjects: boolean | null | undefined;
19
+ };
20
+ type StoreJsonVersion1 = {
21
+ triples: Triple[];
22
+ cardinalityInference: boolean | null | undefined;
23
+ useDateObjects: boolean | null | undefined;
24
+ version: 1;
25
+ };
26
+ export type StoreJson = StoreJsonVersion0 | StoreJsonVersion1;
27
+ export type AttrsStoreJson = {
28
+ attrs: Attrs;
27
29
  linkIndex: LinkIndex | null;
28
- useDateObjects: boolean | null;
29
30
  };
31
+ export interface AttrsStore {
32
+ attrs: Attrs;
33
+ linkIndex: LinkIndex | null;
34
+ resetAttrIndexes(): void;
35
+ addAttr(attr: InstantDBAttr): void;
36
+ deleteAttr(attrId: string): void;
37
+ updateAttr(partialAttr: Partial<InstantDBAttr> & {
38
+ id: string;
39
+ }): void;
40
+ getAttr(id: string): InstantDBAttr | undefined;
41
+ blobAttrs: Map<string, Map<string, InstantDBAttr>>;
42
+ primaryKeys: Map<string, InstantDBAttr>;
43
+ forwardIdents: Map<string, Map<string, InstantDBAttr>>;
44
+ revIdents: Map<string, Map<string, InstantDBAttr>>;
45
+ toJSON(): AttrsStoreJson;
46
+ }
47
+ export declare class AttrsStoreClass implements AttrsStore {
48
+ attrs: Attrs;
49
+ linkIndex: LinkIndex | null;
50
+ private _blobAttrs;
51
+ private _primaryKeys;
52
+ private _forwardIdents;
53
+ private _revIdents;
54
+ constructor(attrs: Attrs, linkIndex: LinkIndex | null);
55
+ resetAttrIndexes(): void;
56
+ addAttr(attr: InstantDBAttr): void;
57
+ deleteAttr(attrId: string): void;
58
+ updateAttr(partialAttr: Partial<InstantDBAttr> & {
59
+ id: string;
60
+ }): void;
61
+ getAttr(id: string): InstantDBAttr | undefined;
62
+ get blobAttrs(): Map<string, Map<string, InstantDBAttr>>;
63
+ get primaryKeys(): Map<string, InstantDBAttr>;
64
+ get forwardIdents(): Map<string, Map<string, InstantDBAttr>>;
65
+ get revIdents(): Map<string, Map<string, InstantDBAttr>>;
66
+ toJSON(): AttrsStoreJson;
67
+ }
30
68
  export declare function isBlob(attr: InstantDBAttr): boolean;
31
69
  export declare function getInMap(obj: any, path: any): any;
32
- export declare function toJSON(store: Store): StoreJson;
33
- export declare function fromJSON(storeJSON: StoreJson): Store;
70
+ export declare function toJSON(store: Store): StoreJsonVersion1;
71
+ export declare function fromJSON(attrsStore: AttrsStore, storeJSON: StoreJson): Store;
72
+ export declare function attrsStoreFromJSON(attrsStoreJSON: AttrsStoreJson | null, storeJSON: StoreJson | null): AttrsStore | undefined;
34
73
  export declare function hasTriple(store: Store, [e, a, v]: [string, string, any]): boolean;
35
74
  export declare function hasEntity(store: Store, e: string): boolean;
36
- export declare function createStore(attrs: Record<string, InstantDBAttr>, triples: Triple[], enableCardinalityInference: boolean | null, linkIndex: LinkIndex | null, useDateObjects: boolean | null): Store;
37
- export declare function retractTriple(store: Store, rawTriple: Triple): void;
38
- export declare function addTriple(store: Store, rawTriple: Triple): void;
75
+ export declare function createStore(attrsStore: AttrsStore, triples: Triple[], enableCardinalityInference?: boolean | null, useDateObjects?: boolean | null): Store;
76
+ export declare function retractTriple(store: Store, attrsStore: AttrsStore, rawTriple: Triple): void;
77
+ export declare function addTriple(store: Store, attrsStore: AttrsStore, rawTriple: Triple): void;
39
78
  export declare function allMapValues(m: any, level: any, res?: any[]): any[];
40
79
  export declare function getTriples(store: any, [e, a, v]: [any, any, any]): any[];
41
80
  export declare function getAsObject(store: Store, attrs: Map<string, InstantDBAttr> | undefined, e: string): {};
42
- export declare function getAttrByFwdIdentName(store: Store, inputEtype: string, inputLabel: string): InstantDBAttr | undefined;
43
- export declare function getAttrByReverseIdentName(store: Store, inputEtype: string, inputLabel: string): InstantDBAttr | undefined;
44
- export declare function getBlobAttrs(store: Store, etype: string): Map<string, InstantDBAttr> | undefined;
45
- export declare function getPrimaryKeyAttr(store: Store, etype: string): InstantDBAttr | undefined;
46
- export declare function transact(store: Store, txSteps: any): Store;
81
+ export declare function getAttrByFwdIdentName(attrsStore: AttrsStore, inputEtype: string, inputLabel: string): InstantDBAttr | undefined;
82
+ export declare function getAttrByReverseIdentName(attrsStore: AttrsStore, inputEtype: string, inputLabel: string): InstantDBAttr | undefined;
83
+ export declare function getBlobAttrs(attrsStore: AttrsStore, etype: string): Map<string, InstantDBAttr> | undefined;
84
+ export declare function getPrimaryKeyAttr(attrsStore: AttrsStore, etype: string): InstantDBAttr | undefined;
85
+ export declare function transact(store: Store, attrsStore: AttrsStore, txSteps: any): {
86
+ store: Store;
87
+ attrsStore: AttrsStore;
88
+ };
47
89
  export {};
48
90
  //# sourceMappingURL=store.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;AAC5C,KAAK,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAE3C,KAAK,WAAW,GAAG;IACjB,SAAS,EAAE,GAA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AAa,CAAC,GAAG,SAAS,EAC7C,CAAC,EAAE,MAAM,MAiBV;AAED,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,6BAGnB;AAED,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,UAAU,EACtB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,6BAGnB;AAED,wBAAgB,YAAY,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,0CAEjE;AAED,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,6BAMtE;AAyBD,wBAAgB,QAAQ,CACtB,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,UAAU,EACtB,OAAO,KAAA,GACN;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAsD1C"}
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AttrsStoreClass = void 0;
3
4
  exports.isBlob = isBlob;
4
5
  exports.getInMap = getInMap;
5
6
  exports.toJSON = toJSON;
6
7
  exports.fromJSON = fromJSON;
8
+ exports.attrsStoreFromJSON = attrsStoreFromJSON;
7
9
  exports.hasTriple = hasTriple;
8
10
  exports.hasEntity = hasEntity;
9
11
  exports.createStore = createStore;
@@ -20,6 +22,102 @@ exports.transact = transact;
20
22
  const mutative_1 = require("mutative");
21
23
  const object_js_1 = require("./utils/object.js");
22
24
  const dates_ts_1 = require("./utils/dates.js");
25
+ class AttrsStoreClass {
26
+ constructor(attrs, linkIndex) {
27
+ this._blobAttrs = null;
28
+ this._primaryKeys = null;
29
+ this._forwardIdents = null;
30
+ this._revIdents = null;
31
+ this.attrs = attrs;
32
+ this.linkIndex = linkIndex;
33
+ }
34
+ resetAttrIndexes() {
35
+ this._blobAttrs = null;
36
+ this._primaryKeys = null;
37
+ this._forwardIdents = null;
38
+ this._revIdents = null;
39
+ }
40
+ addAttr(attr) {
41
+ this.attrs[attr.id] = attr;
42
+ this.resetAttrIndexes();
43
+ }
44
+ deleteAttr(attrId) {
45
+ delete this.attrs[attrId];
46
+ this.resetAttrIndexes();
47
+ }
48
+ updateAttr(partialAttr) {
49
+ const attr = this.attrs[partialAttr.id];
50
+ if (!attr)
51
+ return;
52
+ this.attrs[partialAttr.id] = Object.assign(Object.assign({}, attr), partialAttr);
53
+ this.resetAttrIndexes();
54
+ }
55
+ getAttr(id) {
56
+ return this.attrs[id];
57
+ }
58
+ // XXX: Might be better to create all of the indexes at once as soon as someone
59
+ // requests one index
60
+ get blobAttrs() {
61
+ if (this._blobAttrs) {
62
+ return this._blobAttrs;
63
+ }
64
+ console.log('blobAttrs');
65
+ this._blobAttrs = new Map();
66
+ for (const attr of Object.values(this.attrs)) {
67
+ if (isBlob(attr)) {
68
+ const [_, fwdEtype, fwdLabel] = attr['forward-identity'];
69
+ setInMap(this.blobAttrs, [fwdEtype, fwdLabel], attr);
70
+ }
71
+ }
72
+ return this._blobAttrs;
73
+ }
74
+ get primaryKeys() {
75
+ if (this._primaryKeys) {
76
+ return this._primaryKeys;
77
+ }
78
+ console.log('primayKeys');
79
+ this._primaryKeys = new Map();
80
+ for (const attr of Object.values(this.attrs)) {
81
+ if (attr['primary?']) {
82
+ const [_, fwdEtype] = attr['forward-identity'];
83
+ setInMap(this._primaryKeys, [fwdEtype], attr);
84
+ }
85
+ }
86
+ return this._primaryKeys;
87
+ }
88
+ get forwardIdents() {
89
+ if (this._forwardIdents) {
90
+ return this._forwardIdents;
91
+ }
92
+ console.log('fwdIdents');
93
+ this._forwardIdents = new Map();
94
+ for (const attr of Object.values(this.attrs)) {
95
+ const fwdIdent = attr['forward-identity'];
96
+ const [_, fwdEtype, fwdLabel] = fwdIdent;
97
+ setInMap(this._forwardIdents, [fwdEtype, fwdLabel], attr);
98
+ }
99
+ return this._forwardIdents;
100
+ }
101
+ get revIdents() {
102
+ if (this._revIdents) {
103
+ return this._revIdents;
104
+ }
105
+ console.log('revIdents');
106
+ this._revIdents = new Map();
107
+ for (const attr of Object.values(this.attrs)) {
108
+ const revIdent = attr['reverse-identity'];
109
+ if (revIdent) {
110
+ const [_, revEtype, revLabel] = revIdent;
111
+ setInMap(this._revIdents, [revEtype, revLabel], attr);
112
+ }
113
+ }
114
+ return this._revIdents;
115
+ }
116
+ toJSON() {
117
+ return { attrs: this.attrs, linkIndex: this.linkIndex };
118
+ }
119
+ }
120
+ exports.AttrsStoreClass = AttrsStoreClass;
23
121
  function hasEA(attr) {
24
122
  return attr['cardinality'] === 'one';
25
123
  }
@@ -65,15 +163,15 @@ function setInMap(m, path, value) {
65
163
  function isDateAttr(attr) {
66
164
  return attr['checked-data-type'] === 'date';
67
165
  }
68
- function createTripleIndexes(attrs, triples, useDateObjects) {
166
+ function createTripleIndexes(attrsStore, triples, useDateObjects) {
69
167
  const eav = new Map();
70
168
  const aev = new Map();
71
169
  const vae = new Map();
72
170
  for (const triple of triples) {
73
- let [eid, aid, v, t] = triple;
74
- const attr = getAttr(attrs, aid);
171
+ let [eid, aid, v] = triple;
172
+ const attr = attrsStore.getAttr(aid);
75
173
  if (!attr) {
76
- console.warn('no such attr', eid, attrs);
174
+ console.warn('no such attr', aid, eid);
77
175
  continue;
78
176
  }
79
177
  if (attr['checked-data-type'] === 'date' && useDateObjects) {
@@ -113,16 +211,22 @@ function createAttrIndexes(attrs) {
113
211
  }
114
212
  function toJSON(store) {
115
213
  return {
116
- __type: store.__type,
117
- attrs: store.attrs,
118
214
  triples: allMapValues(store.eav, 3),
119
215
  cardinalityInference: store.cardinalityInference,
120
- linkIndex: store.linkIndex,
121
216
  useDateObjects: store.useDateObjects,
217
+ version: 1,
122
218
  };
123
219
  }
124
- function fromJSON(storeJSON) {
125
- return createStore(storeJSON.attrs, storeJSON.triples, storeJSON.cardinalityInference, storeJSON.linkIndex, storeJSON.useDateObjects);
220
+ function fromJSON(attrsStore, storeJSON) {
221
+ return createStore(attrsStore, storeJSON.triples, storeJSON.cardinalityInference, storeJSON.useDateObjects);
222
+ }
223
+ function attrsStoreFromJSON(attrsStoreJSON, storeJSON) {
224
+ if (attrsStoreJSON) {
225
+ return new AttrsStoreClass(attrsStoreJSON.attrs, attrsStoreJSON.linkIndex);
226
+ }
227
+ if (storeJSON && '__type' in storeJSON) {
228
+ return new AttrsStoreClass(storeJSON.attrs, storeJSON.linkIndex);
229
+ }
126
230
  }
127
231
  function hasTriple(store, [e, a, v]) {
128
232
  return getInMap(store.eav, [e, a, v]) !== undefined;
@@ -130,17 +234,10 @@ function hasTriple(store, [e, a, v]) {
130
234
  function hasEntity(store, e) {
131
235
  return getInMap(store.eav, [e]) !== undefined;
132
236
  }
133
- function resetAttrIndexes(store) {
134
- store.attrIndexes = createAttrIndexes(store.attrs);
135
- }
136
- function createStore(attrs, triples, enableCardinalityInference, linkIndex, useDateObjects) {
137
- const store = createTripleIndexes(attrs, triples, useDateObjects);
138
- store.useDateObjects = useDateObjects;
139
- store.attrs = attrs;
140
- store.attrIndexes = createAttrIndexes(attrs);
237
+ function createStore(attrsStore, triples, enableCardinalityInference, useDateObjects) {
238
+ const store = createTripleIndexes(attrsStore, triples, useDateObjects);
141
239
  store.cardinalityInference = enableCardinalityInference;
142
- store.linkIndex = linkIndex;
143
- store.__type = 'store';
240
+ store.useDateObjects = useDateObjects;
144
241
  return store;
145
242
  }
146
243
  // We may have local triples with lookup refs in them,
@@ -197,13 +294,13 @@ function resolveLookupRefs(store, triple) {
197
294
  return [eid, ...rest];
198
295
  }
199
296
  }
200
- function retractTriple(store, rawTriple) {
297
+ function retractTriple(store, attrsStore, rawTriple) {
201
298
  const triple = resolveLookupRefs(store, rawTriple);
202
299
  if (!triple) {
203
300
  return;
204
301
  }
205
302
  const [eid, aid, v] = triple;
206
- const attr = getAttr(store.attrs, aid);
303
+ const attr = attrsStore.getAttr(aid);
207
304
  if (!attr) {
208
305
  return;
209
306
  }
@@ -244,14 +341,14 @@ function getCreatedAt(store, attr, triple) {
244
341
  */
245
342
  return createdAt || Date.now() * 10 + _seed++;
246
343
  }
247
- function addTriple(store, rawTriple) {
344
+ function addTriple(store, attrsStore, rawTriple) {
248
345
  var _a;
249
346
  const triple = resolveLookupRefs(store, rawTriple);
250
347
  if (!triple) {
251
348
  return;
252
349
  }
253
350
  let [eid, aid, v] = triple;
254
- const attr = getAttr(store.attrs, aid);
351
+ const attr = attrsStore.getAttr(aid);
255
352
  if (!attr) {
256
353
  // (XXX): Due to the way we're handling attrs, it's
257
354
  // possible to enter a state where we receive a triple without an attr.
@@ -280,14 +377,14 @@ function addTriple(store, rawTriple) {
280
377
  setInMap(store.vae, [v, aid, eid], enhancedTriple);
281
378
  }
282
379
  }
283
- function mergeTriple(store, rawTriple) {
380
+ function mergeTriple(store, attrsStore, rawTriple) {
284
381
  var _a;
285
382
  const triple = resolveLookupRefs(store, rawTriple);
286
383
  if (!triple) {
287
384
  return;
288
385
  }
289
386
  const [eid, aid, update] = triple;
290
- const attr = getAttr(store.attrs, aid);
387
+ const attr = attrsStore.getAttr(aid);
291
388
  if (!attr)
292
389
  return;
293
390
  if (!isBlob(attr))
@@ -308,7 +405,7 @@ function mergeTriple(store, rawTriple) {
308
405
  ];
309
406
  setInMap(store.eav, [eid, aid], new Map([[updatedValue, enhancedTriple]]));
310
407
  }
311
- function deleteEntity(store, args) {
408
+ function deleteEntity(store, attrsStore, args) {
312
409
  var _a, _b;
313
410
  const [lookup, etype] = args;
314
411
  const triple = resolveLookupRefs(store, [lookup]);
@@ -320,10 +417,10 @@ function deleteEntity(store, args) {
320
417
  const eMap = store.eav.get(id);
321
418
  if (eMap) {
322
419
  for (const a of eMap.keys()) {
323
- const attr = store.attrs[a];
420
+ const attr = attrsStore.getAttr(a);
324
421
  // delete cascade refs
325
422
  if (attr && attr['on-delete-reverse'] === 'cascade') {
326
- allMapValues(eMap.get(a), 1).forEach(([e, a, v]) => { var _a; return deleteEntity(store, [v, (_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]]); });
423
+ allMapValues(eMap.get(a), 1).forEach(([e, a, v]) => { var _a; return deleteEntity(store, attrsStore, [v, (_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]]); });
327
424
  }
328
425
  if (
329
426
  // Fall back to deleting everything if we've rehydrated tx-steps from
@@ -348,7 +445,7 @@ function deleteEntity(store, args) {
348
445
  vaeTriples.forEach((triple) => {
349
446
  var _a, _b, _c;
350
447
  const [e, a, v] = triple;
351
- const attr = store.attrs[a];
448
+ const attr = attrsStore.getAttr(a);
352
449
  if (!etype || !attr || ((_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]) === etype) {
353
450
  deleteInMap(store.eav, [e, a, v]);
354
451
  deleteInMap(store.aev, [a, e, v]);
@@ -357,7 +454,7 @@ function deleteEntity(store, args) {
357
454
  if (attr &&
358
455
  attr['on-delete'] === 'cascade' &&
359
456
  ((_b = attr['reverse-identity']) === null || _b === void 0 ? void 0 : _b[1]) === etype) {
360
- deleteEntity(store, [e, (_c = attr['forward-identity']) === null || _c === void 0 ? void 0 : _c[1]]);
457
+ deleteEntity(store, attrsStore, [e, (_c = attr['forward-identity']) === null || _c === void 0 ? void 0 : _c[1]]);
361
458
  }
362
459
  });
363
460
  }
@@ -373,58 +470,55 @@ function deleteEntity(store, args) {
373
470
  // * We could batch this reset at the end
374
471
  // * We could add an ave index for all triples, so removing the
375
472
  // right triples is easy and fast.
376
- function resetIndexMap(store, newTriples) {
377
- const newIndexMap = createTripleIndexes(store.attrs, newTriples, store.useDateObjects);
473
+ function resetIndexMap(store, attrsStore, newTriples) {
474
+ const newIndexMap = createTripleIndexes(attrsStore, newTriples, store.useDateObjects);
378
475
  Object.keys(newIndexMap).forEach((key) => {
379
476
  store[key] = newIndexMap[key];
380
477
  });
381
478
  }
382
- function addAttr(store, [attr]) {
383
- store.attrs[attr.id] = attr;
384
- resetAttrIndexes(store);
479
+ function addAttr(attrsStore, [attr]) {
480
+ attrsStore.addAttr(attr);
385
481
  }
386
482
  function getAllTriples(store) {
387
483
  return allMapValues(store.eav, 3);
388
484
  }
389
- function deleteAttr(store, [id]) {
390
- if (!store.attrs[id])
485
+ function deleteAttr(store, attrsStore, [id]) {
486
+ if (!attrsStore.getAttr(id))
391
487
  return;
392
488
  const newTriples = getAllTriples(store).filter(([_, aid]) => aid !== id);
393
- delete store.attrs[id];
394
- resetAttrIndexes(store);
395
- resetIndexMap(store, newTriples);
489
+ attrsStore.deleteAttr(id);
490
+ resetIndexMap(store, attrsStore, newTriples);
396
491
  }
397
- function updateAttr(store, [partialAttr]) {
398
- const attr = store.attrs[partialAttr.id];
492
+ function updateAttr(store, attrsStore, [partialAttr]) {
493
+ const attr = attrsStore.getAttr(partialAttr.id);
399
494
  if (!attr)
400
495
  return;
401
- store.attrs[partialAttr.id] = Object.assign(Object.assign({}, attr), partialAttr);
402
- resetAttrIndexes(store);
403
- resetIndexMap(store, getAllTriples(store));
496
+ attrsStore.updateAttr(partialAttr);
497
+ resetIndexMap(store, attrsStore, getAllTriples(store));
404
498
  }
405
- function applyTxStep(store, txStep) {
499
+ function applyTxStep(store, attrsStore, txStep) {
406
500
  const [action, ...args] = txStep;
407
501
  switch (action) {
408
502
  case 'add-triple':
409
- addTriple(store, args);
503
+ addTriple(store, attrsStore, args);
410
504
  break;
411
505
  case 'deep-merge-triple':
412
- mergeTriple(store, args);
506
+ mergeTriple(store, attrsStore, args);
413
507
  break;
414
508
  case 'retract-triple':
415
- retractTriple(store, args);
509
+ retractTriple(store, attrsStore, args);
416
510
  break;
417
511
  case 'delete-entity':
418
- deleteEntity(store, args);
512
+ deleteEntity(store, attrsStore, args);
419
513
  break;
420
514
  case 'add-attr':
421
- addAttr(store, args);
515
+ addAttr(attrsStore, args);
422
516
  break;
423
517
  case 'delete-attr':
424
- deleteAttr(store, args);
518
+ deleteAttr(store, attrsStore, args);
425
519
  break;
426
520
  case 'update-attr':
427
- updateAttr(store, args);
521
+ updateAttr(store, attrsStore, args);
428
522
  break;
429
523
  case 'restore-attr':
430
524
  break;
@@ -587,32 +681,32 @@ function getAsObject(store, attrs, e) {
587
681
  }
588
682
  return obj;
589
683
  }
590
- function getAttrByFwdIdentName(store, inputEtype, inputLabel) {
684
+ function getAttrByFwdIdentName(attrsStore, inputEtype, inputLabel) {
591
685
  var _a;
592
- return (_a = store.attrIndexes.forwardIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
686
+ return (_a = attrsStore.forwardIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
593
687
  }
594
- function getAttrByReverseIdentName(store, inputEtype, inputLabel) {
688
+ function getAttrByReverseIdentName(attrsStore, inputEtype, inputLabel) {
595
689
  var _a;
596
- return (_a = store.attrIndexes.revIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
690
+ return (_a = attrsStore.revIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
597
691
  }
598
- function getBlobAttrs(store, etype) {
599
- return store.attrIndexes.blobAttrs.get(etype);
692
+ function getBlobAttrs(attrsStore, etype) {
693
+ return attrsStore.blobAttrs.get(etype);
600
694
  }
601
- function getPrimaryKeyAttr(store, etype) {
695
+ function getPrimaryKeyAttr(attrsStore, etype) {
602
696
  var _a;
603
- const fromPrimary = store.attrIndexes.primaryKeys.get(etype);
697
+ const fromPrimary = attrsStore.primaryKeys.get(etype);
604
698
  if (fromPrimary) {
605
699
  return fromPrimary;
606
700
  }
607
- return (_a = store.attrIndexes.forwardIdents.get(etype)) === null || _a === void 0 ? void 0 : _a.get('id');
701
+ return (_a = attrsStore.forwardIdents.get(etype)) === null || _a === void 0 ? void 0 : _a.get('id');
608
702
  }
609
- function findTriple(store, rawTriple) {
703
+ function findTriple(store, attrsStore, rawTriple) {
610
704
  const triple = resolveLookupRefs(store, rawTriple);
611
705
  if (!triple) {
612
706
  return;
613
707
  }
614
708
  const [eid, aid, v] = triple;
615
- const attr = getAttr(store.attrs, aid);
709
+ const attr = attrsStore.getAttr(aid);
616
710
  if (!attr) {
617
711
  // (XXX): Due to the way we're handling attrs, it's
618
712
  // possible to enter a state where we receive a triple without an attr.
@@ -622,7 +716,7 @@ function findTriple(store, rawTriple) {
622
716
  }
623
717
  return getInMap(store.eav, [eid, aid]);
624
718
  }
625
- function transact(store, txSteps) {
719
+ function transact(store, attrsStore, txSteps) {
626
720
  const txStepsFiltered = txSteps.filter(([action, eid, attrId, value, opts]) => {
627
721
  if (action !== 'add-triple' && action !== 'deep-merge-triple') {
628
722
  return true;
@@ -632,10 +726,10 @@ function transact(store, txSteps) {
632
726
  return true;
633
727
  }
634
728
  let exists = false;
635
- const attr = getAttr(store.attrs, attrId);
729
+ const attr = attrsStore.getAttr(attrId);
636
730
  if (attr) {
637
- const idAttr = getPrimaryKeyAttr(store, attr['forward-identity'][1]);
638
- exists = !!findTriple(store, [
731
+ const idAttr = getPrimaryKeyAttr(attrsStore, attr['forward-identity'][1]);
732
+ exists = !!findTriple(store, attrsStore, [
639
733
  eid,
640
734
  idAttr === null || idAttr === void 0 ? void 0 : idAttr.id,
641
735
  eid,
@@ -649,10 +743,16 @@ function transact(store, txSteps) {
649
743
  }
650
744
  return true;
651
745
  });
652
- return (0, mutative_1.create)(store, (draft) => {
746
+ return (0, mutative_1.create)({ store, attrsStore }, (draft) => {
653
747
  txStepsFiltered.forEach((txStep) => {
654
- applyTxStep(draft, txStep);
748
+ applyTxStep(draft.store, draft.attrsStore, txStep);
655
749
  });
750
+ }, {
751
+ mark: (target) => {
752
+ if (target instanceof AttrsStoreClass) {
753
+ return 'immutable';
754
+ }
755
+ },
656
756
  });
657
757
  }
658
758
  //# sourceMappingURL=store.js.map