@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"}
package/dist/esm/store.js CHANGED
@@ -1,6 +1,101 @@
1
1
  import { create } from 'mutative';
2
2
  import { immutableDeepMerge } from './utils/object.js';
3
3
  import { coerceToDate } from "./utils/dates.js";
4
+ export class AttrsStoreClass {
5
+ constructor(attrs, linkIndex) {
6
+ this._blobAttrs = null;
7
+ this._primaryKeys = null;
8
+ this._forwardIdents = null;
9
+ this._revIdents = null;
10
+ this.attrs = attrs;
11
+ this.linkIndex = linkIndex;
12
+ }
13
+ resetAttrIndexes() {
14
+ this._blobAttrs = null;
15
+ this._primaryKeys = null;
16
+ this._forwardIdents = null;
17
+ this._revIdents = null;
18
+ }
19
+ addAttr(attr) {
20
+ this.attrs[attr.id] = attr;
21
+ this.resetAttrIndexes();
22
+ }
23
+ deleteAttr(attrId) {
24
+ delete this.attrs[attrId];
25
+ this.resetAttrIndexes();
26
+ }
27
+ updateAttr(partialAttr) {
28
+ const attr = this.attrs[partialAttr.id];
29
+ if (!attr)
30
+ return;
31
+ this.attrs[partialAttr.id] = Object.assign(Object.assign({}, attr), partialAttr);
32
+ this.resetAttrIndexes();
33
+ }
34
+ getAttr(id) {
35
+ return this.attrs[id];
36
+ }
37
+ // XXX: Might be better to create all of the indexes at once as soon as someone
38
+ // requests one index
39
+ get blobAttrs() {
40
+ if (this._blobAttrs) {
41
+ return this._blobAttrs;
42
+ }
43
+ console.log('blobAttrs');
44
+ this._blobAttrs = new Map();
45
+ for (const attr of Object.values(this.attrs)) {
46
+ if (isBlob(attr)) {
47
+ const [_, fwdEtype, fwdLabel] = attr['forward-identity'];
48
+ setInMap(this.blobAttrs, [fwdEtype, fwdLabel], attr);
49
+ }
50
+ }
51
+ return this._blobAttrs;
52
+ }
53
+ get primaryKeys() {
54
+ if (this._primaryKeys) {
55
+ return this._primaryKeys;
56
+ }
57
+ console.log('primayKeys');
58
+ this._primaryKeys = new Map();
59
+ for (const attr of Object.values(this.attrs)) {
60
+ if (attr['primary?']) {
61
+ const [_, fwdEtype] = attr['forward-identity'];
62
+ setInMap(this._primaryKeys, [fwdEtype], attr);
63
+ }
64
+ }
65
+ return this._primaryKeys;
66
+ }
67
+ get forwardIdents() {
68
+ if (this._forwardIdents) {
69
+ return this._forwardIdents;
70
+ }
71
+ console.log('fwdIdents');
72
+ this._forwardIdents = new Map();
73
+ for (const attr of Object.values(this.attrs)) {
74
+ const fwdIdent = attr['forward-identity'];
75
+ const [_, fwdEtype, fwdLabel] = fwdIdent;
76
+ setInMap(this._forwardIdents, [fwdEtype, fwdLabel], attr);
77
+ }
78
+ return this._forwardIdents;
79
+ }
80
+ get revIdents() {
81
+ if (this._revIdents) {
82
+ return this._revIdents;
83
+ }
84
+ console.log('revIdents');
85
+ this._revIdents = new Map();
86
+ for (const attr of Object.values(this.attrs)) {
87
+ const revIdent = attr['reverse-identity'];
88
+ if (revIdent) {
89
+ const [_, revEtype, revLabel] = revIdent;
90
+ setInMap(this._revIdents, [revEtype, revLabel], attr);
91
+ }
92
+ }
93
+ return this._revIdents;
94
+ }
95
+ toJSON() {
96
+ return { attrs: this.attrs, linkIndex: this.linkIndex };
97
+ }
98
+ }
4
99
  function hasEA(attr) {
5
100
  return attr['cardinality'] === 'one';
6
101
  }
@@ -46,15 +141,15 @@ function setInMap(m, path, value) {
46
141
  function isDateAttr(attr) {
47
142
  return attr['checked-data-type'] === 'date';
48
143
  }
49
- function createTripleIndexes(attrs, triples, useDateObjects) {
144
+ function createTripleIndexes(attrsStore, triples, useDateObjects) {
50
145
  const eav = new Map();
51
146
  const aev = new Map();
52
147
  const vae = new Map();
53
148
  for (const triple of triples) {
54
- let [eid, aid, v, t] = triple;
55
- const attr = getAttr(attrs, aid);
149
+ let [eid, aid, v] = triple;
150
+ const attr = attrsStore.getAttr(aid);
56
151
  if (!attr) {
57
- console.warn('no such attr', eid, attrs);
152
+ console.warn('no such attr', aid, eid);
58
153
  continue;
59
154
  }
60
155
  if (attr['checked-data-type'] === 'date' && useDateObjects) {
@@ -94,16 +189,22 @@ function createAttrIndexes(attrs) {
94
189
  }
95
190
  export function toJSON(store) {
96
191
  return {
97
- __type: store.__type,
98
- attrs: store.attrs,
99
192
  triples: allMapValues(store.eav, 3),
100
193
  cardinalityInference: store.cardinalityInference,
101
- linkIndex: store.linkIndex,
102
194
  useDateObjects: store.useDateObjects,
195
+ version: 1,
103
196
  };
104
197
  }
105
- export function fromJSON(storeJSON) {
106
- return createStore(storeJSON.attrs, storeJSON.triples, storeJSON.cardinalityInference, storeJSON.linkIndex, storeJSON.useDateObjects);
198
+ export function fromJSON(attrsStore, storeJSON) {
199
+ return createStore(attrsStore, storeJSON.triples, storeJSON.cardinalityInference, storeJSON.useDateObjects);
200
+ }
201
+ export function attrsStoreFromJSON(attrsStoreJSON, storeJSON) {
202
+ if (attrsStoreJSON) {
203
+ return new AttrsStoreClass(attrsStoreJSON.attrs, attrsStoreJSON.linkIndex);
204
+ }
205
+ if (storeJSON && '__type' in storeJSON) {
206
+ return new AttrsStoreClass(storeJSON.attrs, storeJSON.linkIndex);
207
+ }
107
208
  }
108
209
  export function hasTriple(store, [e, a, v]) {
109
210
  return getInMap(store.eav, [e, a, v]) !== undefined;
@@ -111,17 +212,10 @@ export function hasTriple(store, [e, a, v]) {
111
212
  export function hasEntity(store, e) {
112
213
  return getInMap(store.eav, [e]) !== undefined;
113
214
  }
114
- function resetAttrIndexes(store) {
115
- store.attrIndexes = createAttrIndexes(store.attrs);
116
- }
117
- export function createStore(attrs, triples, enableCardinalityInference, linkIndex, useDateObjects) {
118
- const store = createTripleIndexes(attrs, triples, useDateObjects);
119
- store.useDateObjects = useDateObjects;
120
- store.attrs = attrs;
121
- store.attrIndexes = createAttrIndexes(attrs);
215
+ export function createStore(attrsStore, triples, enableCardinalityInference, useDateObjects) {
216
+ const store = createTripleIndexes(attrsStore, triples, useDateObjects);
122
217
  store.cardinalityInference = enableCardinalityInference;
123
- store.linkIndex = linkIndex;
124
- store.__type = 'store';
218
+ store.useDateObjects = useDateObjects;
125
219
  return store;
126
220
  }
127
221
  // We may have local triples with lookup refs in them,
@@ -178,13 +272,13 @@ function resolveLookupRefs(store, triple) {
178
272
  return [eid, ...rest];
179
273
  }
180
274
  }
181
- export function retractTriple(store, rawTriple) {
275
+ export function retractTriple(store, attrsStore, rawTriple) {
182
276
  const triple = resolveLookupRefs(store, rawTriple);
183
277
  if (!triple) {
184
278
  return;
185
279
  }
186
280
  const [eid, aid, v] = triple;
187
- const attr = getAttr(store.attrs, aid);
281
+ const attr = attrsStore.getAttr(aid);
188
282
  if (!attr) {
189
283
  return;
190
284
  }
@@ -225,14 +319,14 @@ function getCreatedAt(store, attr, triple) {
225
319
  */
226
320
  return createdAt || Date.now() * 10 + _seed++;
227
321
  }
228
- export function addTriple(store, rawTriple) {
322
+ export function addTriple(store, attrsStore, rawTriple) {
229
323
  var _a;
230
324
  const triple = resolveLookupRefs(store, rawTriple);
231
325
  if (!triple) {
232
326
  return;
233
327
  }
234
328
  let [eid, aid, v] = triple;
235
- const attr = getAttr(store.attrs, aid);
329
+ const attr = attrsStore.getAttr(aid);
236
330
  if (!attr) {
237
331
  // (XXX): Due to the way we're handling attrs, it's
238
332
  // possible to enter a state where we receive a triple without an attr.
@@ -261,14 +355,14 @@ export function addTriple(store, rawTriple) {
261
355
  setInMap(store.vae, [v, aid, eid], enhancedTriple);
262
356
  }
263
357
  }
264
- function mergeTriple(store, rawTriple) {
358
+ function mergeTriple(store, attrsStore, rawTriple) {
265
359
  var _a;
266
360
  const triple = resolveLookupRefs(store, rawTriple);
267
361
  if (!triple) {
268
362
  return;
269
363
  }
270
364
  const [eid, aid, update] = triple;
271
- const attr = getAttr(store.attrs, aid);
365
+ const attr = attrsStore.getAttr(aid);
272
366
  if (!attr)
273
367
  return;
274
368
  if (!isBlob(attr))
@@ -289,7 +383,7 @@ function mergeTriple(store, rawTriple) {
289
383
  ];
290
384
  setInMap(store.eav, [eid, aid], new Map([[updatedValue, enhancedTriple]]));
291
385
  }
292
- function deleteEntity(store, args) {
386
+ function deleteEntity(store, attrsStore, args) {
293
387
  var _a, _b;
294
388
  const [lookup, etype] = args;
295
389
  const triple = resolveLookupRefs(store, [lookup]);
@@ -301,10 +395,10 @@ function deleteEntity(store, args) {
301
395
  const eMap = store.eav.get(id);
302
396
  if (eMap) {
303
397
  for (const a of eMap.keys()) {
304
- const attr = store.attrs[a];
398
+ const attr = attrsStore.getAttr(a);
305
399
  // delete cascade refs
306
400
  if (attr && attr['on-delete-reverse'] === 'cascade') {
307
- 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]]); });
401
+ 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]]); });
308
402
  }
309
403
  if (
310
404
  // Fall back to deleting everything if we've rehydrated tx-steps from
@@ -329,7 +423,7 @@ function deleteEntity(store, args) {
329
423
  vaeTriples.forEach((triple) => {
330
424
  var _a, _b, _c;
331
425
  const [e, a, v] = triple;
332
- const attr = store.attrs[a];
426
+ const attr = attrsStore.getAttr(a);
333
427
  if (!etype || !attr || ((_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]) === etype) {
334
428
  deleteInMap(store.eav, [e, a, v]);
335
429
  deleteInMap(store.aev, [a, e, v]);
@@ -338,7 +432,7 @@ function deleteEntity(store, args) {
338
432
  if (attr &&
339
433
  attr['on-delete'] === 'cascade' &&
340
434
  ((_b = attr['reverse-identity']) === null || _b === void 0 ? void 0 : _b[1]) === etype) {
341
- deleteEntity(store, [e, (_c = attr['forward-identity']) === null || _c === void 0 ? void 0 : _c[1]]);
435
+ deleteEntity(store, attrsStore, [e, (_c = attr['forward-identity']) === null || _c === void 0 ? void 0 : _c[1]]);
342
436
  }
343
437
  });
344
438
  }
@@ -354,58 +448,55 @@ function deleteEntity(store, args) {
354
448
  // * We could batch this reset at the end
355
449
  // * We could add an ave index for all triples, so removing the
356
450
  // right triples is easy and fast.
357
- function resetIndexMap(store, newTriples) {
358
- const newIndexMap = createTripleIndexes(store.attrs, newTriples, store.useDateObjects);
451
+ function resetIndexMap(store, attrsStore, newTriples) {
452
+ const newIndexMap = createTripleIndexes(attrsStore, newTriples, store.useDateObjects);
359
453
  Object.keys(newIndexMap).forEach((key) => {
360
454
  store[key] = newIndexMap[key];
361
455
  });
362
456
  }
363
- function addAttr(store, [attr]) {
364
- store.attrs[attr.id] = attr;
365
- resetAttrIndexes(store);
457
+ function addAttr(attrsStore, [attr]) {
458
+ attrsStore.addAttr(attr);
366
459
  }
367
460
  function getAllTriples(store) {
368
461
  return allMapValues(store.eav, 3);
369
462
  }
370
- function deleteAttr(store, [id]) {
371
- if (!store.attrs[id])
463
+ function deleteAttr(store, attrsStore, [id]) {
464
+ if (!attrsStore.getAttr(id))
372
465
  return;
373
466
  const newTriples = getAllTriples(store).filter(([_, aid]) => aid !== id);
374
- delete store.attrs[id];
375
- resetAttrIndexes(store);
376
- resetIndexMap(store, newTriples);
467
+ attrsStore.deleteAttr(id);
468
+ resetIndexMap(store, attrsStore, newTriples);
377
469
  }
378
- function updateAttr(store, [partialAttr]) {
379
- const attr = store.attrs[partialAttr.id];
470
+ function updateAttr(store, attrsStore, [partialAttr]) {
471
+ const attr = attrsStore.getAttr(partialAttr.id);
380
472
  if (!attr)
381
473
  return;
382
- store.attrs[partialAttr.id] = Object.assign(Object.assign({}, attr), partialAttr);
383
- resetAttrIndexes(store);
384
- resetIndexMap(store, getAllTriples(store));
474
+ attrsStore.updateAttr(partialAttr);
475
+ resetIndexMap(store, attrsStore, getAllTriples(store));
385
476
  }
386
- function applyTxStep(store, txStep) {
477
+ function applyTxStep(store, attrsStore, txStep) {
387
478
  const [action, ...args] = txStep;
388
479
  switch (action) {
389
480
  case 'add-triple':
390
- addTriple(store, args);
481
+ addTriple(store, attrsStore, args);
391
482
  break;
392
483
  case 'deep-merge-triple':
393
- mergeTriple(store, args);
484
+ mergeTriple(store, attrsStore, args);
394
485
  break;
395
486
  case 'retract-triple':
396
- retractTriple(store, args);
487
+ retractTriple(store, attrsStore, args);
397
488
  break;
398
489
  case 'delete-entity':
399
- deleteEntity(store, args);
490
+ deleteEntity(store, attrsStore, args);
400
491
  break;
401
492
  case 'add-attr':
402
- addAttr(store, args);
493
+ addAttr(attrsStore, args);
403
494
  break;
404
495
  case 'delete-attr':
405
- deleteAttr(store, args);
496
+ deleteAttr(store, attrsStore, args);
406
497
  break;
407
498
  case 'update-attr':
408
- updateAttr(store, args);
499
+ updateAttr(store, attrsStore, args);
409
500
  break;
410
501
  case 'restore-attr':
411
502
  break;
@@ -568,32 +659,32 @@ export function getAsObject(store, attrs, e) {
568
659
  }
569
660
  return obj;
570
661
  }
571
- export function getAttrByFwdIdentName(store, inputEtype, inputLabel) {
662
+ export function getAttrByFwdIdentName(attrsStore, inputEtype, inputLabel) {
572
663
  var _a;
573
- return (_a = store.attrIndexes.forwardIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
664
+ return (_a = attrsStore.forwardIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
574
665
  }
575
- export function getAttrByReverseIdentName(store, inputEtype, inputLabel) {
666
+ export function getAttrByReverseIdentName(attrsStore, inputEtype, inputLabel) {
576
667
  var _a;
577
- return (_a = store.attrIndexes.revIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
668
+ return (_a = attrsStore.revIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
578
669
  }
579
- export function getBlobAttrs(store, etype) {
580
- return store.attrIndexes.blobAttrs.get(etype);
670
+ export function getBlobAttrs(attrsStore, etype) {
671
+ return attrsStore.blobAttrs.get(etype);
581
672
  }
582
- export function getPrimaryKeyAttr(store, etype) {
673
+ export function getPrimaryKeyAttr(attrsStore, etype) {
583
674
  var _a;
584
- const fromPrimary = store.attrIndexes.primaryKeys.get(etype);
675
+ const fromPrimary = attrsStore.primaryKeys.get(etype);
585
676
  if (fromPrimary) {
586
677
  return fromPrimary;
587
678
  }
588
- return (_a = store.attrIndexes.forwardIdents.get(etype)) === null || _a === void 0 ? void 0 : _a.get('id');
679
+ return (_a = attrsStore.forwardIdents.get(etype)) === null || _a === void 0 ? void 0 : _a.get('id');
589
680
  }
590
- function findTriple(store, rawTriple) {
681
+ function findTriple(store, attrsStore, rawTriple) {
591
682
  const triple = resolveLookupRefs(store, rawTriple);
592
683
  if (!triple) {
593
684
  return;
594
685
  }
595
686
  const [eid, aid, v] = triple;
596
- const attr = getAttr(store.attrs, aid);
687
+ const attr = attrsStore.getAttr(aid);
597
688
  if (!attr) {
598
689
  // (XXX): Due to the way we're handling attrs, it's
599
690
  // possible to enter a state where we receive a triple without an attr.
@@ -603,7 +694,7 @@ function findTriple(store, rawTriple) {
603
694
  }
604
695
  return getInMap(store.eav, [eid, aid]);
605
696
  }
606
- export function transact(store, txSteps) {
697
+ export function transact(store, attrsStore, txSteps) {
607
698
  const txStepsFiltered = txSteps.filter(([action, eid, attrId, value, opts]) => {
608
699
  if (action !== 'add-triple' && action !== 'deep-merge-triple') {
609
700
  return true;
@@ -613,10 +704,10 @@ export function transact(store, txSteps) {
613
704
  return true;
614
705
  }
615
706
  let exists = false;
616
- const attr = getAttr(store.attrs, attrId);
707
+ const attr = attrsStore.getAttr(attrId);
617
708
  if (attr) {
618
- const idAttr = getPrimaryKeyAttr(store, attr['forward-identity'][1]);
619
- exists = !!findTriple(store, [
709
+ const idAttr = getPrimaryKeyAttr(attrsStore, attr['forward-identity'][1]);
710
+ exists = !!findTriple(store, attrsStore, [
620
711
  eid,
621
712
  idAttr === null || idAttr === void 0 ? void 0 : idAttr.id,
622
713
  eid,
@@ -630,10 +721,16 @@ export function transact(store, txSteps) {
630
721
  }
631
722
  return true;
632
723
  });
633
- return create(store, (draft) => {
724
+ return create({ store, attrsStore }, (draft) => {
634
725
  txStepsFiltered.forEach((txStep) => {
635
- applyTxStep(draft, txStep);
726
+ applyTxStep(draft.store, draft.attrsStore, txStep);
636
727
  });
728
+ }, {
729
+ mark: (target) => {
730
+ if (target instanceof AttrsStoreClass) {
731
+ return 'immutable';
732
+ }
733
+ },
637
734
  });
638
735
  }
639
736
  //# sourceMappingURL=store.js.map