@instantdb/core 0.22.88 → 0.22.89-experimental.drewh-fix-export.20277749804.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/__tests__/src/Reactor.test.js +18 -11
- package/__tests__/src/{datalog.test.js → datalog.test.ts} +17 -5
- package/__tests__/src/{instaml.test.js → instaml.test.ts} +183 -119
- package/__tests__/src/instaql.bench.ts +34 -0
- package/__tests__/src/{instaql.test.js → instaql.test.ts} +342 -455
- package/__tests__/src/instaqlInference.test.js +13 -9
- package/__tests__/src/{store.test.js → store.test.ts} +215 -212
- package/dist/commonjs/Reactor.d.ts +23 -6
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +110 -45
- 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 +115 -82
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.d.ts +4 -3
- 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 +67 -25
- package/dist/commonjs/store.d.ts.map +1 -1
- package/dist/commonjs/store.js +177 -81
- package/dist/commonjs/store.js.map +1 -1
- package/dist/esm/Reactor.d.ts +23 -6
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +111 -46
- 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 +112 -77
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.d.ts +4 -3
- 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 +67 -25
- package/dist/esm/store.d.ts.map +1 -1
- package/dist/esm/store.js +174 -81
- package/dist/esm/store.js.map +1 -1
- package/dist/standalone/index.js +1605 -1415
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +152 -75
- package/src/SyncTable.ts +85 -45
- package/src/{instaml.js → instaml.ts} +201 -96
- package/src/instaql.ts +88 -62
- package/src/reactorTypes.ts +32 -0
- package/src/store.ts +257 -101
- package/__tests__/src/instaql.bench.js +0 -29
package/src/SyncTable.ts
CHANGED
|
@@ -16,12 +16,12 @@ type SubState = {
|
|
|
16
16
|
|
|
17
17
|
type SubEntity = {
|
|
18
18
|
entity: any;
|
|
19
|
-
store:
|
|
19
|
+
store: s.Store;
|
|
20
20
|
serverCreatedAt: number;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
type SubValues = {
|
|
24
|
-
|
|
24
|
+
attrsStore: s.AttrsStore;
|
|
25
25
|
entities: Array<SubEntity>;
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -38,8 +38,21 @@ type Sub = {
|
|
|
38
38
|
updatedAt: number;
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
type SubEntityInStorage = {
|
|
42
|
+
entity: any;
|
|
43
|
+
store: s.StoreJson;
|
|
44
|
+
serverCreatedAt: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type SubValuesInStorage = {
|
|
48
|
+
attrsStore: s.AttrsStoreJson;
|
|
49
|
+
entities: Array<SubEntityInStorage>;
|
|
50
|
+
};
|
|
51
|
+
|
|
41
52
|
// We could make a better type for this if we had a return type for s.toJSON
|
|
42
|
-
type SubInStorage = Sub
|
|
53
|
+
type SubInStorage = Omit<Sub, 'values'> & {
|
|
54
|
+
values: SubValuesInStorage;
|
|
55
|
+
};
|
|
43
56
|
|
|
44
57
|
type StartMsg = {
|
|
45
58
|
op: 'start-sync';
|
|
@@ -96,36 +109,38 @@ type Config = { useDateObjects: boolean };
|
|
|
96
109
|
function syncSubFromStorage(sub: SubInStorage, useDateObjects: boolean): Sub {
|
|
97
110
|
const values = sub.values;
|
|
98
111
|
if (values) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
e
|
|
102
|
-
|
|
112
|
+
const attrsStore = s.attrsStoreFromJSON(values.attrsStore, null);
|
|
113
|
+
if (attrsStore) {
|
|
114
|
+
for (const e of values.entities || []) {
|
|
115
|
+
e.store.useDateObjects = useDateObjects;
|
|
116
|
+
(e as unknown as SubEntity).store = s.fromJSON(attrsStore, e.store);
|
|
117
|
+
}
|
|
118
|
+
(values as unknown as SubValues).attrsStore = attrsStore;
|
|
103
119
|
}
|
|
104
120
|
}
|
|
105
121
|
|
|
106
|
-
return sub;
|
|
122
|
+
return sub as unknown as Sub;
|
|
107
123
|
}
|
|
108
124
|
|
|
109
125
|
function syncSubToStorage(_k: string, sub: Sub): SubInStorage {
|
|
110
|
-
if (sub.values
|
|
111
|
-
const entities:
|
|
126
|
+
if (sub.values) {
|
|
127
|
+
const entities: SubEntityInStorage[] = [];
|
|
112
128
|
for (const e of sub.values?.entities) {
|
|
113
129
|
const store = s.toJSON(e.store);
|
|
114
|
-
// We'll store the attrs once on values, and put the
|
|
115
|
-
// attrs back into the store on hydration
|
|
116
|
-
// @ts-ignore: ts doesn't want us to delete a non-optional
|
|
117
|
-
delete store['attrs'];
|
|
118
130
|
entities.push({ ...e, store });
|
|
119
131
|
}
|
|
120
|
-
return {
|
|
132
|
+
return {
|
|
133
|
+
...sub,
|
|
134
|
+
values: { attrsStore: sub.values.attrsStore.toJSON(), entities },
|
|
135
|
+
};
|
|
121
136
|
} else {
|
|
122
|
-
return sub;
|
|
137
|
+
return sub as unknown as SubInStorage;
|
|
123
138
|
}
|
|
124
139
|
}
|
|
125
140
|
|
|
126
141
|
function onMergeSub(
|
|
127
142
|
_key: string,
|
|
128
|
-
storageSub:
|
|
143
|
+
storageSub: Sub,
|
|
129
144
|
inMemorySub: Sub | null,
|
|
130
145
|
): Sub {
|
|
131
146
|
const storageTxId = storageSub?.state?.txId;
|
|
@@ -142,13 +157,21 @@ function onMergeSub(
|
|
|
142
157
|
return storageSub || inMemorySub;
|
|
143
158
|
}
|
|
144
159
|
|
|
145
|
-
function queryEntity(sub: Sub, store:
|
|
146
|
-
const res = instaql(
|
|
160
|
+
function queryEntity(sub: Sub, store: s.Store, attrsStore: s.AttrsStore) {
|
|
161
|
+
const res = instaql(
|
|
162
|
+
{ store, attrsStore, pageInfo: null, aggregate: null },
|
|
163
|
+
sub.query,
|
|
164
|
+
);
|
|
147
165
|
return res.data[sub.table][0];
|
|
148
166
|
}
|
|
149
167
|
|
|
150
|
-
function getServerCreatedAt(
|
|
151
|
-
|
|
168
|
+
function getServerCreatedAt(
|
|
169
|
+
sub: Sub,
|
|
170
|
+
store: s.Store,
|
|
171
|
+
attrsStore: s.AttrsStore,
|
|
172
|
+
entityId: string,
|
|
173
|
+
): number {
|
|
174
|
+
const aid = s.getAttrByFwdIdentName(attrsStore, sub.table, 'id')?.id;
|
|
152
175
|
if (!aid) {
|
|
153
176
|
return -1;
|
|
154
177
|
}
|
|
@@ -160,23 +183,25 @@ function getServerCreatedAt(sub: Sub, store: any, entityId: string): number {
|
|
|
160
183
|
}
|
|
161
184
|
|
|
162
185
|
function applyChangesToStore(
|
|
163
|
-
store:
|
|
186
|
+
store: s.Store,
|
|
187
|
+
attrsStore: s.AttrsStore,
|
|
164
188
|
changes: SyncUpdateTriplesMsg['txes'][number]['changes'],
|
|
165
189
|
): void {
|
|
166
190
|
for (const { action, triple } of changes) {
|
|
167
191
|
switch (action) {
|
|
168
192
|
case 'added':
|
|
169
|
-
s.addTriple(store, triple);
|
|
193
|
+
s.addTriple(store, attrsStore, triple);
|
|
170
194
|
break;
|
|
171
195
|
case 'removed':
|
|
172
|
-
s.retractTriple(store, triple);
|
|
196
|
+
s.retractTriple(store, attrsStore, triple);
|
|
173
197
|
break;
|
|
174
198
|
}
|
|
175
199
|
}
|
|
176
200
|
}
|
|
177
201
|
|
|
178
202
|
function changedFieldsOfChanges(
|
|
179
|
-
store:
|
|
203
|
+
store: s.Store,
|
|
204
|
+
attrsStore: s.AttrsStore,
|
|
180
205
|
changes: SyncUpdateTriplesMsg['txes'][number]['changes'],
|
|
181
206
|
): {
|
|
182
207
|
[eid: string]: SyncTransaction<
|
|
@@ -190,7 +215,7 @@ function changedFieldsOfChanges(
|
|
|
190
215
|
const changedFields = {};
|
|
191
216
|
for (const { action, triple } of changes) {
|
|
192
217
|
const [e, a, v] = triple;
|
|
193
|
-
const field =
|
|
218
|
+
const field = attrsStore.getAttr(a)?.['forward-identity']?.[2];
|
|
194
219
|
if (!field) continue;
|
|
195
220
|
|
|
196
221
|
const fields = changedFields[e] ?? {};
|
|
@@ -225,17 +250,19 @@ function subData(sub: Sub, entities: NonNullable<Sub['values']>['entities']) {
|
|
|
225
250
|
return { [sub.table]: entities.map((e) => e.entity) };
|
|
226
251
|
}
|
|
227
252
|
|
|
253
|
+
type CreateStore = (triples: Triple[]) => s.Store;
|
|
254
|
+
|
|
228
255
|
// Updates the sub order field type if it hasn't been set
|
|
229
256
|
// and returns the type. We have to wait until the attrs
|
|
230
257
|
// are loaded before we can determine the type.
|
|
231
|
-
function orderFieldTypeMutative(sub: Sub,
|
|
258
|
+
function orderFieldTypeMutative(sub: Sub, getAttrs: () => s.AttrsStore) {
|
|
232
259
|
if (sub.orderFieldType) {
|
|
233
260
|
return sub.orderFieldType;
|
|
234
261
|
}
|
|
235
262
|
const orderFieldType =
|
|
236
263
|
sub.orderField === 'serverCreatedAt'
|
|
237
264
|
? 'number'
|
|
238
|
-
: s.getAttrByFwdIdentName(
|
|
265
|
+
: s.getAttrByFwdIdentName(getAttrs(), sub.table, sub.orderField)?.[
|
|
239
266
|
'checked-data-type'
|
|
240
267
|
];
|
|
241
268
|
|
|
@@ -414,19 +441,22 @@ export class SyncTable {
|
|
|
414
441
|
private config: Config;
|
|
415
442
|
private idToHash: { [subscriptionId: string]: string } = {};
|
|
416
443
|
private log: Logger;
|
|
417
|
-
private createStore:
|
|
444
|
+
private createStore: CreateStore;
|
|
445
|
+
private getAttrs: () => s.AttrsStore;
|
|
418
446
|
|
|
419
447
|
constructor(
|
|
420
448
|
trySend: TrySend,
|
|
421
449
|
storage: StorageInterface,
|
|
422
450
|
config: Config,
|
|
423
451
|
log: Logger,
|
|
424
|
-
createStore:
|
|
452
|
+
createStore: CreateStore,
|
|
453
|
+
getAttrs: () => s.AttrsStore,
|
|
425
454
|
) {
|
|
426
455
|
this.trySend = trySend;
|
|
427
456
|
this.config = config;
|
|
428
457
|
this.log = log;
|
|
429
458
|
this.createStore = createStore;
|
|
459
|
+
this.getAttrs = getAttrs;
|
|
430
460
|
|
|
431
461
|
this.subs = new PersistedObject<string, Sub, SubInStorage>({
|
|
432
462
|
persister: storage,
|
|
@@ -624,19 +654,23 @@ export class SyncTable {
|
|
|
624
654
|
|
|
625
655
|
const values: SubValues = sub.values ?? {
|
|
626
656
|
entities: [],
|
|
627
|
-
|
|
657
|
+
attrsStore: this.getAttrs(),
|
|
628
658
|
};
|
|
629
659
|
sub.values = values;
|
|
630
660
|
const entities = values.entities;
|
|
631
661
|
|
|
632
662
|
for (const entRows of joinRows) {
|
|
633
663
|
const store = this.createStore(entRows);
|
|
634
|
-
|
|
635
|
-
const entity = queryEntity(sub, store);
|
|
664
|
+
const entity = queryEntity(sub, store, values.attrsStore);
|
|
636
665
|
entities.push({
|
|
637
666
|
store,
|
|
638
667
|
entity,
|
|
639
|
-
serverCreatedAt: getServerCreatedAt(
|
|
668
|
+
serverCreatedAt: getServerCreatedAt(
|
|
669
|
+
sub,
|
|
670
|
+
store,
|
|
671
|
+
values.attrsStore,
|
|
672
|
+
entity.id,
|
|
673
|
+
),
|
|
640
674
|
});
|
|
641
675
|
batch.push(entity);
|
|
642
676
|
}
|
|
@@ -727,7 +761,7 @@ export class SyncTable {
|
|
|
727
761
|
|
|
728
762
|
const values: SubValues = sub.values ?? {
|
|
729
763
|
entities: [],
|
|
730
|
-
|
|
764
|
+
attrsStore: this.getAttrs(),
|
|
731
765
|
};
|
|
732
766
|
const entities = values.entities;
|
|
733
767
|
sub.values = values;
|
|
@@ -738,11 +772,13 @@ export class SyncTable {
|
|
|
738
772
|
for (let i = 0; i < entities.length; i++) {
|
|
739
773
|
const ent = entities[i];
|
|
740
774
|
if (s.hasEntity(ent.store, eid)) {
|
|
741
|
-
applyChangesToStore(ent.store, changes);
|
|
742
|
-
const entity = queryEntity(sub, ent.store);
|
|
743
|
-
const changedFields = changedFieldsOfChanges(
|
|
744
|
-
|
|
745
|
-
|
|
775
|
+
applyChangesToStore(ent.store, values.attrsStore, changes);
|
|
776
|
+
const entity = queryEntity(sub, ent.store, values.attrsStore);
|
|
777
|
+
const changedFields = changedFieldsOfChanges(
|
|
778
|
+
ent.store,
|
|
779
|
+
values.attrsStore,
|
|
780
|
+
changes,
|
|
781
|
+
)[eid];
|
|
746
782
|
if (entity) {
|
|
747
783
|
updated.push({
|
|
748
784
|
oldEntity: ent.entity,
|
|
@@ -763,9 +799,8 @@ export class SyncTable {
|
|
|
763
799
|
// If we have anything left in byEid, then this must be a new entity we don't know about
|
|
764
800
|
for (const [_eid, changes] of Object.entries(byEid)) {
|
|
765
801
|
const store = this.createStore([]);
|
|
766
|
-
values.
|
|
767
|
-
|
|
768
|
-
const entity = queryEntity(sub, store);
|
|
802
|
+
applyChangesToStore(store, values.attrsStore, changes);
|
|
803
|
+
const entity = queryEntity(sub, store, values.attrsStore);
|
|
769
804
|
if (!entity) {
|
|
770
805
|
this.log.error('No entity found after applying change', {
|
|
771
806
|
sub,
|
|
@@ -777,7 +812,12 @@ export class SyncTable {
|
|
|
777
812
|
entities.push({
|
|
778
813
|
store,
|
|
779
814
|
entity,
|
|
780
|
-
serverCreatedAt: getServerCreatedAt(
|
|
815
|
+
serverCreatedAt: getServerCreatedAt(
|
|
816
|
+
sub,
|
|
817
|
+
store,
|
|
818
|
+
values.attrsStore,
|
|
819
|
+
entity.id,
|
|
820
|
+
),
|
|
781
821
|
});
|
|
782
822
|
added.push(entity);
|
|
783
823
|
}
|
|
@@ -789,7 +829,7 @@ export class SyncTable {
|
|
|
789
829
|
entities.splice(idx, 1);
|
|
790
830
|
}
|
|
791
831
|
|
|
792
|
-
const orderFieldType = orderFieldTypeMutative(sub, this.
|
|
832
|
+
const orderFieldType = orderFieldTypeMutative(sub, this.getAttrs);
|
|
793
833
|
|
|
794
834
|
sortEntitiesInPlace(sub, orderFieldType!, entities);
|
|
795
835
|
this.notifyCbs(hash, {
|