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