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