@instantdb/core 0.22.86-experimental.separate-attrs.20122276424.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.
- package/dist/commonjs/Reactor.d.ts +20 -6
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +97 -42
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/SyncTable.d.ts +4 -1
- package/dist/commonjs/SyncTable.d.ts.map +1 -1
- package/dist/commonjs/SyncTable.js +35 -37
- package/dist/commonjs/SyncTable.js.map +1 -1
- package/dist/commonjs/instaml.d.ts +17 -4
- package/dist/commonjs/instaml.d.ts.map +1 -1
- package/dist/commonjs/instaml.js +105 -76
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.d.ts +2 -1
- package/dist/commonjs/instaql.d.ts.map +1 -1
- package/dist/commonjs/instaql.js +65 -63
- package/dist/commonjs/instaql.js.map +1 -1
- package/dist/commonjs/reactorTypes.d.ts +29 -0
- package/dist/commonjs/reactorTypes.d.ts.map +1 -0
- package/dist/commonjs/reactorTypes.js +3 -0
- package/dist/commonjs/reactorTypes.js.map +1 -0
- package/dist/commonjs/store.d.ts +44 -21
- package/dist/commonjs/store.d.ts.map +1 -1
- package/dist/commonjs/store.js +164 -69
- package/dist/commonjs/store.js.map +1 -1
- package/dist/esm/Reactor.d.ts +20 -6
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +98 -43
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/SyncTable.d.ts +4 -1
- package/dist/esm/SyncTable.d.ts.map +1 -1
- package/dist/esm/SyncTable.js +35 -37
- package/dist/esm/SyncTable.js.map +1 -1
- package/dist/esm/instaml.d.ts +17 -4
- package/dist/esm/instaml.d.ts.map +1 -1
- package/dist/esm/instaml.js +102 -71
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.d.ts +2 -1
- package/dist/esm/instaql.d.ts.map +1 -1
- package/dist/esm/instaql.js +65 -63
- package/dist/esm/instaql.js.map +1 -1
- package/dist/esm/reactorTypes.d.ts +29 -0
- package/dist/esm/reactorTypes.d.ts.map +1 -0
- package/dist/esm/reactorTypes.js +2 -0
- package/dist/esm/reactorTypes.js.map +1 -0
- package/dist/esm/store.d.ts +44 -21
- package/dist/esm/store.d.ts.map +1 -1
- package/dist/esm/store.js +161 -69
- package/dist/esm/store.js.map +1 -1
- package/dist/standalone/index.js +1517 -1345
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +126 -58
- package/src/SyncTable.ts +85 -45
- package/src/{instaml.js → instaml.ts} +195 -95
- package/src/instaql.ts +86 -60
- package/src/reactorTypes.ts +32 -0
- package/src/store.ts +209 -79
package/src/store.ts
CHANGED
|
@@ -7,26 +7,22 @@ import { LinkIndex } from './utils/linkIndex.ts';
|
|
|
7
7
|
type Triple = [string, string, any, number];
|
|
8
8
|
type Attrs = Record<string, InstantDBAttr>;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
interface AttrIndexes {
|
|
11
11
|
blobAttrs: Map<string, Map<string, InstantDBAttr>>;
|
|
12
12
|
primaryKeys: Map<string, InstantDBAttr>;
|
|
13
13
|
forwardIdents: Map<string, Map<string, InstantDBAttr>>;
|
|
14
14
|
revIdents: Map<string, Map<string, InstantDBAttr>>;
|
|
15
|
-
}
|
|
15
|
+
}
|
|
16
16
|
|
|
17
17
|
export type Store = {
|
|
18
18
|
eav: Map<string, Map<string, Map<any, Triple>>>;
|
|
19
19
|
aev: Map<string, Map<string, Map<any, Triple>>>;
|
|
20
20
|
vae: Map<any, Map<string, Map<string, Triple>>>;
|
|
21
21
|
useDateObjects: boolean | null;
|
|
22
|
-
attrs: Attrs;
|
|
23
|
-
attrIndexes: AttrIndexes;
|
|
24
22
|
cardinalityInference: boolean | null;
|
|
25
|
-
linkIndex: LinkIndex | null;
|
|
26
|
-
__type: 'store';
|
|
27
23
|
};
|
|
28
24
|
|
|
29
|
-
|
|
25
|
+
type StoreJsonVersion0 = {
|
|
30
26
|
__type: 'store';
|
|
31
27
|
attrs: Attrs;
|
|
32
28
|
triples: Triple[];
|
|
@@ -35,6 +31,131 @@ export type StoreJson = {
|
|
|
35
31
|
useDateObjects: boolean | null;
|
|
36
32
|
};
|
|
37
33
|
|
|
34
|
+
type StoreJsonVersion1 = {
|
|
35
|
+
triples: Triple[];
|
|
36
|
+
cardinalityInference: boolean | null;
|
|
37
|
+
useDateObjects: boolean | null;
|
|
38
|
+
version: 1;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type StoreJson = StoreJsonVersion0 | StoreJsonVersion1;
|
|
42
|
+
|
|
43
|
+
export type AttrsStoreJson = {
|
|
44
|
+
attrs: Attrs;
|
|
45
|
+
linkIndex: LinkIndex | null;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export class AttrsStore {
|
|
49
|
+
public attrs: Attrs;
|
|
50
|
+
public linkIndex: LinkIndex | null;
|
|
51
|
+
private _blobAttrs: Map<string, Map<string, InstantDBAttr>> | null = null;
|
|
52
|
+
private _primaryKeys: Map<string, InstantDBAttr> | null = null;
|
|
53
|
+
private _forwardIdents: Map<string, Map<string, InstantDBAttr>> | null = null;
|
|
54
|
+
private _revIdents: Map<string, Map<string, InstantDBAttr>> | null = null;
|
|
55
|
+
constructor(attrs: Attrs, linkIndex: LinkIndex | null) {
|
|
56
|
+
console.log('attrs init', new Error('trace'));
|
|
57
|
+
this.attrs = attrs;
|
|
58
|
+
this.linkIndex = linkIndex;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public resetAttrIndexes() {
|
|
62
|
+
this._blobAttrs = null;
|
|
63
|
+
this._primaryKeys = null;
|
|
64
|
+
this._forwardIdents = null;
|
|
65
|
+
this._revIdents = null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public addAttr(attr: InstantDBAttr) {
|
|
69
|
+
this.attrs[attr.id] = attr;
|
|
70
|
+
this.resetAttrIndexes();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public deleteAttr(attrId: string) {
|
|
74
|
+
delete this.attrs[attrId];
|
|
75
|
+
this.resetAttrIndexes();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public updateAttr(partialAttr: Partial<InstantDBAttr> & { id: string }) {
|
|
79
|
+
const attr = this.attrs[partialAttr.id];
|
|
80
|
+
if (!attr) return;
|
|
81
|
+
this.attrs[partialAttr.id] = { ...attr, ...partialAttr };
|
|
82
|
+
this.resetAttrIndexes();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public getAttr(id: string): InstantDBAttr | undefined {
|
|
86
|
+
return this.attrs[id];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// XXX: Might be better to create all of the indexes at once as soon as someone
|
|
90
|
+
// requests one index
|
|
91
|
+
get blobAttrs(): Map<string, Map<string, InstantDBAttr>> {
|
|
92
|
+
if (this._blobAttrs) {
|
|
93
|
+
return this._blobAttrs;
|
|
94
|
+
}
|
|
95
|
+
console.log('blobAttrs');
|
|
96
|
+
this._blobAttrs = new Map();
|
|
97
|
+
for (const attr of Object.values(this.attrs)) {
|
|
98
|
+
if (isBlob(attr)) {
|
|
99
|
+
const [_, fwdEtype, fwdLabel] = attr['forward-identity'];
|
|
100
|
+
setInMap(this.blobAttrs, [fwdEtype, fwdLabel], attr);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return this._blobAttrs;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get primaryKeys(): Map<string, InstantDBAttr> {
|
|
107
|
+
if (this._primaryKeys) {
|
|
108
|
+
return this._primaryKeys;
|
|
109
|
+
}
|
|
110
|
+
console.log('primayKeys');
|
|
111
|
+
this._primaryKeys = new Map();
|
|
112
|
+
|
|
113
|
+
for (const attr of Object.values(this.attrs)) {
|
|
114
|
+
if (attr['primary?']) {
|
|
115
|
+
const [_, fwdEtype] = attr['forward-identity'];
|
|
116
|
+
setInMap(this._primaryKeys, [fwdEtype], attr);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return this._primaryKeys;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get forwardIdents(): Map<string, Map<string, InstantDBAttr>> {
|
|
123
|
+
if (this._forwardIdents) {
|
|
124
|
+
return this._forwardIdents;
|
|
125
|
+
}
|
|
126
|
+
console.log('fwdIdents');
|
|
127
|
+
this._forwardIdents = new Map();
|
|
128
|
+
|
|
129
|
+
for (const attr of Object.values(this.attrs)) {
|
|
130
|
+
const fwdIdent = attr['forward-identity'];
|
|
131
|
+
const [_, fwdEtype, fwdLabel] = fwdIdent;
|
|
132
|
+
setInMap(this._forwardIdents, [fwdEtype, fwdLabel], attr);
|
|
133
|
+
}
|
|
134
|
+
return this._forwardIdents;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get revIdents(): Map<string, Map<string, InstantDBAttr>> {
|
|
138
|
+
if (this._revIdents) {
|
|
139
|
+
return this._revIdents;
|
|
140
|
+
}
|
|
141
|
+
console.log('revIdents');
|
|
142
|
+
this._revIdents = new Map();
|
|
143
|
+
|
|
144
|
+
for (const attr of Object.values(this.attrs)) {
|
|
145
|
+
const revIdent = attr['reverse-identity'];
|
|
146
|
+
if (revIdent) {
|
|
147
|
+
const [_, revEtype, revLabel] = revIdent;
|
|
148
|
+
setInMap(this._revIdents, [revEtype, revLabel], attr);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return this._revIdents;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public toJSON(): AttrsStoreJson {
|
|
155
|
+
return { attrs: this.attrs, linkIndex: this.linkIndex };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
38
159
|
function hasEA(attr: InstantDBAttr) {
|
|
39
160
|
return attr['cardinality'] === 'one';
|
|
40
161
|
}
|
|
@@ -86,7 +207,7 @@ function isDateAttr(attr: InstantDBAttr) {
|
|
|
86
207
|
}
|
|
87
208
|
|
|
88
209
|
function createTripleIndexes(
|
|
89
|
-
|
|
210
|
+
attrsStore: AttrsStore,
|
|
90
211
|
triples: Triple[],
|
|
91
212
|
useDateObjects: boolean | null,
|
|
92
213
|
): Pick<Store, 'eav' | 'aev' | 'vae'> {
|
|
@@ -94,10 +215,10 @@ function createTripleIndexes(
|
|
|
94
215
|
const aev = new Map();
|
|
95
216
|
const vae = new Map();
|
|
96
217
|
for (const triple of triples) {
|
|
97
|
-
let [eid, aid, v
|
|
98
|
-
const attr = getAttr(
|
|
218
|
+
let [eid, aid, v] = triple;
|
|
219
|
+
const attr = attrsStore.getAttr(aid);
|
|
99
220
|
if (!attr) {
|
|
100
|
-
console.warn('no such attr',
|
|
221
|
+
console.warn('no such attr', aid, eid);
|
|
101
222
|
continue;
|
|
102
223
|
}
|
|
103
224
|
|
|
@@ -142,27 +263,36 @@ function createAttrIndexes(attrs: Record<string, InstantDBAttr>): AttrIndexes {
|
|
|
142
263
|
return { blobAttrs, primaryKeys, forwardIdents, revIdents };
|
|
143
264
|
}
|
|
144
265
|
|
|
145
|
-
export function toJSON(store: Store):
|
|
266
|
+
export function toJSON(store: Store): StoreJsonVersion1 {
|
|
146
267
|
return {
|
|
147
|
-
__type: store.__type,
|
|
148
|
-
attrs: store.attrs,
|
|
149
268
|
triples: allMapValues(store.eav, 3),
|
|
150
269
|
cardinalityInference: store.cardinalityInference,
|
|
151
|
-
linkIndex: store.linkIndex,
|
|
152
270
|
useDateObjects: store.useDateObjects,
|
|
271
|
+
version: 1,
|
|
153
272
|
};
|
|
154
273
|
}
|
|
155
274
|
|
|
156
|
-
export function fromJSON(storeJSON: StoreJson): Store {
|
|
275
|
+
export function fromJSON(attrsStore: AttrsStore, storeJSON: StoreJson): Store {
|
|
157
276
|
return createStore(
|
|
158
|
-
|
|
277
|
+
attrsStore,
|
|
159
278
|
storeJSON.triples,
|
|
160
279
|
storeJSON.cardinalityInference,
|
|
161
|
-
storeJSON.linkIndex,
|
|
162
280
|
storeJSON.useDateObjects,
|
|
163
281
|
);
|
|
164
282
|
}
|
|
165
283
|
|
|
284
|
+
export function attrsStoreFromJSON(
|
|
285
|
+
attrsStoreJSON: AttrsStoreJson | null,
|
|
286
|
+
storeJSON: StoreJson | null,
|
|
287
|
+
): AttrsStore | undefined {
|
|
288
|
+
if (attrsStoreJSON) {
|
|
289
|
+
return new AttrsStore(attrsStoreJSON.attrs, attrsStoreJSON.linkIndex);
|
|
290
|
+
}
|
|
291
|
+
if (storeJSON && '__type' in storeJSON) {
|
|
292
|
+
return new AttrsStore(storeJSON.attrs, storeJSON.linkIndex);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
166
296
|
export function hasTriple(store: Store, [e, a, v]: [string, string, any]) {
|
|
167
297
|
return getInMap(store.eav, [e, a, v]) !== undefined;
|
|
168
298
|
}
|
|
@@ -171,29 +301,19 @@ export function hasEntity(store: Store, e: string) {
|
|
|
171
301
|
return getInMap(store.eav, [e]) !== undefined;
|
|
172
302
|
}
|
|
173
303
|
|
|
174
|
-
function resetAttrIndexes(store: Store) {
|
|
175
|
-
store.attrIndexes = createAttrIndexes(store.attrs);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
304
|
export function createStore(
|
|
179
|
-
|
|
305
|
+
attrsStore: AttrsStore,
|
|
180
306
|
triples: Triple[],
|
|
181
307
|
enableCardinalityInference: boolean | null,
|
|
182
|
-
linkIndex: LinkIndex | null,
|
|
183
308
|
useDateObjects: boolean | null,
|
|
184
309
|
): Store {
|
|
185
310
|
const store = createTripleIndexes(
|
|
186
|
-
|
|
311
|
+
attrsStore,
|
|
187
312
|
triples,
|
|
188
313
|
useDateObjects,
|
|
189
314
|
) as unknown as Store;
|
|
190
|
-
store.useDateObjects = useDateObjects;
|
|
191
|
-
store.attrs = attrs;
|
|
192
|
-
store.attrIndexes = createAttrIndexes(attrs);
|
|
193
315
|
store.cardinalityInference = enableCardinalityInference;
|
|
194
|
-
store.
|
|
195
|
-
store.__type = 'store';
|
|
196
|
-
|
|
316
|
+
store.useDateObjects = useDateObjects;
|
|
197
317
|
return store;
|
|
198
318
|
}
|
|
199
319
|
|
|
@@ -254,13 +374,17 @@ function resolveLookupRefs(store: Store, triple: Triple): Triple | null {
|
|
|
254
374
|
}
|
|
255
375
|
}
|
|
256
376
|
|
|
257
|
-
export function retractTriple(
|
|
377
|
+
export function retractTriple(
|
|
378
|
+
store: Store,
|
|
379
|
+
attrsStore: AttrsStore,
|
|
380
|
+
rawTriple: Triple,
|
|
381
|
+
): void {
|
|
258
382
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
259
383
|
if (!triple) {
|
|
260
384
|
return;
|
|
261
385
|
}
|
|
262
386
|
const [eid, aid, v] = triple;
|
|
263
|
-
const attr = getAttr(
|
|
387
|
+
const attr = attrsStore.getAttr(aid);
|
|
264
388
|
if (!attr) {
|
|
265
389
|
return;
|
|
266
390
|
}
|
|
@@ -310,13 +434,17 @@ function getCreatedAt(
|
|
|
310
434
|
return createdAt || Date.now() * 10 + _seed++;
|
|
311
435
|
}
|
|
312
436
|
|
|
313
|
-
export function addTriple(
|
|
437
|
+
export function addTriple(
|
|
438
|
+
store: Store,
|
|
439
|
+
attrsStore: AttrsStore,
|
|
440
|
+
rawTriple: Triple,
|
|
441
|
+
) {
|
|
314
442
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
315
443
|
if (!triple) {
|
|
316
444
|
return;
|
|
317
445
|
}
|
|
318
446
|
let [eid, aid, v] = triple;
|
|
319
|
-
const attr = getAttr(
|
|
447
|
+
const attr = attrsStore.getAttr(aid);
|
|
320
448
|
if (!attr) {
|
|
321
449
|
// (XXX): Due to the way we're handling attrs, it's
|
|
322
450
|
// possible to enter a state where we receive a triple without an attr.
|
|
@@ -349,14 +477,14 @@ export function addTriple(store: Store, rawTriple: Triple) {
|
|
|
349
477
|
}
|
|
350
478
|
}
|
|
351
479
|
|
|
352
|
-
function mergeTriple(store: Store, rawTriple: Triple) {
|
|
480
|
+
function mergeTriple(store: Store, attrsStore: AttrsStore, rawTriple: Triple) {
|
|
353
481
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
354
482
|
if (!triple) {
|
|
355
483
|
return;
|
|
356
484
|
}
|
|
357
485
|
|
|
358
486
|
const [eid, aid, update] = triple;
|
|
359
|
-
const attr = getAttr(
|
|
487
|
+
const attr = attrsStore.getAttr(aid);
|
|
360
488
|
|
|
361
489
|
if (!attr) return;
|
|
362
490
|
|
|
@@ -382,7 +510,7 @@ function mergeTriple(store: Store, rawTriple: Triple) {
|
|
|
382
510
|
setInMap(store.eav, [eid, aid], new Map([[updatedValue, enhancedTriple]]));
|
|
383
511
|
}
|
|
384
512
|
|
|
385
|
-
function deleteEntity(store: Store, args: any[]) {
|
|
513
|
+
function deleteEntity(store: Store, attrsStore: AttrsStore, args: any[]) {
|
|
386
514
|
const [lookup, etype] = args;
|
|
387
515
|
const triple = resolveLookupRefs(store, [lookup] as unknown as Triple);
|
|
388
516
|
|
|
@@ -395,13 +523,13 @@ function deleteEntity(store: Store, args: any[]) {
|
|
|
395
523
|
const eMap = store.eav.get(id);
|
|
396
524
|
if (eMap) {
|
|
397
525
|
for (const a of eMap.keys()) {
|
|
398
|
-
const attr =
|
|
526
|
+
const attr = attrsStore.getAttr(a);
|
|
399
527
|
|
|
400
528
|
// delete cascade refs
|
|
401
529
|
if (attr && attr['on-delete-reverse'] === 'cascade') {
|
|
402
530
|
allMapValues(eMap.get(a), 1).forEach(
|
|
403
531
|
([e, a, v]: [string, string, any]) =>
|
|
404
|
-
deleteEntity(store, [v, attr['reverse-identity']?.[1]]),
|
|
532
|
+
deleteEntity(store, attrsStore, [v, attr['reverse-identity']?.[1]]),
|
|
405
533
|
);
|
|
406
534
|
}
|
|
407
535
|
|
|
@@ -430,7 +558,7 @@ function deleteEntity(store: Store, args: any[]) {
|
|
|
430
558
|
if (vaeTriples) {
|
|
431
559
|
vaeTriples.forEach((triple: Triple) => {
|
|
432
560
|
const [e, a, v] = triple;
|
|
433
|
-
const attr =
|
|
561
|
+
const attr = attrsStore.getAttr(a);
|
|
434
562
|
if (!etype || !attr || attr['reverse-identity']?.[1] === etype) {
|
|
435
563
|
deleteInMap(store.eav, [e, a, v]);
|
|
436
564
|
deleteInMap(store.aev, [a, e, v]);
|
|
@@ -441,7 +569,7 @@ function deleteEntity(store: Store, args: any[]) {
|
|
|
441
569
|
attr['on-delete'] === 'cascade' &&
|
|
442
570
|
attr['reverse-identity']?.[1] === etype
|
|
443
571
|
) {
|
|
444
|
-
deleteEntity(store, [e, attr['forward-identity']?.[1]]);
|
|
572
|
+
deleteEntity(store, attrsStore, [e, attr['forward-identity']?.[1]]);
|
|
445
573
|
}
|
|
446
574
|
});
|
|
447
575
|
}
|
|
@@ -458,9 +586,9 @@ function deleteEntity(store: Store, args: any[]) {
|
|
|
458
586
|
// * We could batch this reset at the end
|
|
459
587
|
// * We could add an ave index for all triples, so removing the
|
|
460
588
|
// right triples is easy and fast.
|
|
461
|
-
function resetIndexMap(store: Store, newTriples: Triple[]) {
|
|
589
|
+
function resetIndexMap(store: Store, attrsStore, newTriples: Triple[]) {
|
|
462
590
|
const newIndexMap = createTripleIndexes(
|
|
463
|
-
|
|
591
|
+
attrsStore,
|
|
464
592
|
newTriples,
|
|
465
593
|
store.useDateObjects,
|
|
466
594
|
);
|
|
@@ -469,57 +597,55 @@ function resetIndexMap(store: Store, newTriples: Triple[]) {
|
|
|
469
597
|
});
|
|
470
598
|
}
|
|
471
599
|
|
|
472
|
-
function addAttr(
|
|
473
|
-
|
|
474
|
-
resetAttrIndexes(store);
|
|
600
|
+
function addAttr(attrsStore: AttrsStore, [attr]: [InstantDBAttr]) {
|
|
601
|
+
attrsStore.addAttr(attr);
|
|
475
602
|
}
|
|
476
603
|
|
|
477
604
|
function getAllTriples(store: Store): Triple[] {
|
|
478
605
|
return allMapValues(store.eav, 3);
|
|
479
606
|
}
|
|
480
607
|
|
|
481
|
-
function deleteAttr(store: Store, [id]: [string]) {
|
|
482
|
-
if (!
|
|
608
|
+
function deleteAttr(store: Store, attrsStore: AttrsStore, [id]: [string]) {
|
|
609
|
+
if (!attrsStore.getAttr(id)) return;
|
|
483
610
|
const newTriples = getAllTriples(store).filter(([_, aid]) => aid !== id);
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
resetIndexMap(store, newTriples);
|
|
611
|
+
attrsStore.deleteAttr(id);
|
|
612
|
+
resetIndexMap(store, attrsStore, newTriples);
|
|
487
613
|
}
|
|
488
614
|
|
|
489
615
|
function updateAttr(
|
|
490
616
|
store: Store,
|
|
617
|
+
attrsStore: AttrsStore,
|
|
491
618
|
[partialAttr]: [Partial<InstantDBAttr> & { id: string }],
|
|
492
619
|
) {
|
|
493
|
-
const attr =
|
|
620
|
+
const attr = attrsStore.getAttr(partialAttr.id);
|
|
494
621
|
if (!attr) return;
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
resetIndexMap(store, getAllTriples(store));
|
|
622
|
+
attrsStore.updateAttr(partialAttr);
|
|
623
|
+
resetIndexMap(store, attrsStore, getAllTriples(store));
|
|
498
624
|
}
|
|
499
625
|
|
|
500
|
-
function applyTxStep(store: Store, txStep) {
|
|
626
|
+
function applyTxStep(store: Store, attrsStore: AttrsStore, txStep) {
|
|
501
627
|
const [action, ...args] = txStep;
|
|
502
628
|
switch (action) {
|
|
503
629
|
case 'add-triple':
|
|
504
|
-
addTriple(store, args);
|
|
630
|
+
addTriple(store, attrsStore, args);
|
|
505
631
|
break;
|
|
506
632
|
case 'deep-merge-triple':
|
|
507
|
-
mergeTriple(store, args);
|
|
633
|
+
mergeTriple(store, attrsStore, args);
|
|
508
634
|
break;
|
|
509
635
|
case 'retract-triple':
|
|
510
|
-
retractTriple(store, args);
|
|
636
|
+
retractTriple(store, attrsStore, args);
|
|
511
637
|
break;
|
|
512
638
|
case 'delete-entity':
|
|
513
|
-
deleteEntity(store, args);
|
|
639
|
+
deleteEntity(store, attrsStore, args);
|
|
514
640
|
break;
|
|
515
641
|
case 'add-attr':
|
|
516
|
-
addAttr(
|
|
642
|
+
addAttr(attrsStore, args);
|
|
517
643
|
break;
|
|
518
644
|
case 'delete-attr':
|
|
519
|
-
deleteAttr(store, args);
|
|
645
|
+
deleteAttr(store, attrsStore, args);
|
|
520
646
|
break;
|
|
521
647
|
case 'update-attr':
|
|
522
|
-
updateAttr(store, args);
|
|
648
|
+
updateAttr(store, attrsStore, args);
|
|
523
649
|
break;
|
|
524
650
|
case 'restore-attr':
|
|
525
651
|
break;
|
|
@@ -700,35 +826,36 @@ export function getAsObject(
|
|
|
700
826
|
}
|
|
701
827
|
|
|
702
828
|
export function getAttrByFwdIdentName(
|
|
703
|
-
|
|
829
|
+
attrsStore: AttrsStore,
|
|
704
830
|
inputEtype: string,
|
|
705
831
|
inputLabel: string,
|
|
706
832
|
) {
|
|
707
|
-
return
|
|
833
|
+
return attrsStore.forwardIdents.get(inputEtype)?.get(inputLabel);
|
|
708
834
|
}
|
|
709
835
|
|
|
710
836
|
export function getAttrByReverseIdentName(
|
|
711
|
-
|
|
837
|
+
attrsStore: AttrsStore,
|
|
712
838
|
inputEtype: string,
|
|
713
839
|
inputLabel: string,
|
|
714
840
|
) {
|
|
715
|
-
return
|
|
841
|
+
return attrsStore.revIdents.get(inputEtype)?.get(inputLabel);
|
|
716
842
|
}
|
|
717
843
|
|
|
718
|
-
export function getBlobAttrs(
|
|
719
|
-
return
|
|
844
|
+
export function getBlobAttrs(attrsStore: AttrsStore, etype: string) {
|
|
845
|
+
return attrsStore.blobAttrs.get(etype);
|
|
720
846
|
}
|
|
721
847
|
|
|
722
|
-
export function getPrimaryKeyAttr(
|
|
723
|
-
const fromPrimary =
|
|
848
|
+
export function getPrimaryKeyAttr(attrsStore: AttrsStore, etype: string) {
|
|
849
|
+
const fromPrimary = attrsStore.primaryKeys.get(etype);
|
|
724
850
|
if (fromPrimary) {
|
|
725
851
|
return fromPrimary;
|
|
726
852
|
}
|
|
727
|
-
return
|
|
853
|
+
return attrsStore.forwardIdents.get(etype)?.get('id');
|
|
728
854
|
}
|
|
729
855
|
|
|
730
856
|
function findTriple(
|
|
731
857
|
store: Store,
|
|
858
|
+
attrsStore: AttrsStore,
|
|
732
859
|
rawTriple: [string, string, any] | Triple,
|
|
733
860
|
): Triple | undefined {
|
|
734
861
|
const triple = resolveLookupRefs(store, rawTriple as Triple);
|
|
@@ -737,7 +864,7 @@ function findTriple(
|
|
|
737
864
|
}
|
|
738
865
|
|
|
739
866
|
const [eid, aid, v] = triple;
|
|
740
|
-
const attr = getAttr(
|
|
867
|
+
const attr = attrsStore.getAttr(aid);
|
|
741
868
|
if (!attr) {
|
|
742
869
|
// (XXX): Due to the way we're handling attrs, it's
|
|
743
870
|
// possible to enter a state where we receive a triple without an attr.
|
|
@@ -749,7 +876,7 @@ function findTriple(
|
|
|
749
876
|
return getInMap(store.eav, [eid, aid]);
|
|
750
877
|
}
|
|
751
878
|
|
|
752
|
-
export function transact(store: Store, txSteps) {
|
|
879
|
+
export function transact(store: Store, attrsStore: AttrsStore, txSteps) {
|
|
753
880
|
const txStepsFiltered = txSteps.filter(
|
|
754
881
|
([action, eid, attrId, value, opts]) => {
|
|
755
882
|
if (action !== 'add-triple' && action !== 'deep-merge-triple') {
|
|
@@ -763,10 +890,13 @@ export function transact(store: Store, txSteps) {
|
|
|
763
890
|
|
|
764
891
|
let exists = false;
|
|
765
892
|
|
|
766
|
-
const attr = getAttr(
|
|
893
|
+
const attr = attrsStore.getAttr(attrId);
|
|
767
894
|
if (attr) {
|
|
768
|
-
const idAttr = getPrimaryKeyAttr(
|
|
769
|
-
|
|
895
|
+
const idAttr = getPrimaryKeyAttr(
|
|
896
|
+
attrsStore,
|
|
897
|
+
attr['forward-identity'][1],
|
|
898
|
+
);
|
|
899
|
+
exists = !!findTriple(store, attrsStore, [
|
|
770
900
|
eid as string,
|
|
771
901
|
idAttr?.id as string,
|
|
772
902
|
eid,
|
|
@@ -787,7 +917,7 @@ export function transact(store: Store, txSteps) {
|
|
|
787
917
|
|
|
788
918
|
return create(store, (draft) => {
|
|
789
919
|
txStepsFiltered.forEach((txStep) => {
|
|
790
|
-
applyTxStep(draft, txStep);
|
|
920
|
+
applyTxStep(draft, attrsStore, txStep);
|
|
791
921
|
});
|
|
792
922
|
});
|
|
793
923
|
}
|