@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.
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} +215 -212
  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 +115 -82
  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 +177 -81
  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 +112 -77
  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 +174 -81
  55. package/dist/esm/store.js.map +1 -1
  56. package/dist/standalone/index.js +1605 -1415
  57. package/dist/standalone/index.umd.cjs +3 -3
  58. package/package.json +2 -2
  59. package/src/Reactor.js +152 -75
  60. package/src/SyncTable.ts +85 -45
  61. package/src/{instaml.js → instaml.ts} +201 -96
  62. package/src/instaql.ts +88 -62
  63. package/src/reactorTypes.ts +32 -0
  64. package/src/store.ts +257 -101
  65. 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: any;
19
+ store: s.Store;
20
20
  serverCreatedAt: number;
21
21
  };
22
22
 
23
23
  type SubValues = {
24
- attrs: Record<string, any>;
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
- for (const e of values.entities || []) {
100
- e.store.useDateObjects = useDateObjects;
101
- e.store.attrs = values.attrs;
102
- e.store = s.fromJSON(e.store);
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?.entities) {
111
- const entities: SubEntity[] = [];
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 { ...sub, values: { ...sub.values, entities } };
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: SubInStorage,
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: any) {
146
- const res = instaql({ store, pageInfo: null, aggregate: null }, sub.query);
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(sub: Sub, store: any, entityId: string): number {
151
- const aid = s.getAttrByFwdIdentName(store, sub.table, 'id')?.id;
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: any,
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: any,
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 = store.attrs[a]?.['forward-identity']?.[2];
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, createStore) {
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(createStore([]), sub.table, sub.orderField)?.[
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: (triples: Triple[]) => any;
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: (triples: Triple[]) => any,
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
- attrs: this.createStore([]).attrs,
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
- values.attrs = store.attrs;
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(sub, store, entity.id),
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
- attrs: this.createStore([]).attrs,
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(ent.store, changes)[
744
- eid
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.attrs = store.attrs;
767
- applyChangesToStore(store, changes);
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(sub, store, entity.id),
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.createStore);
832
+ const orderFieldType = orderFieldTypeMutative(sub, this.getAttrs);
793
833
 
794
834
  sortEntitiesInPlace(sub, orderFieldType!, entities);
795
835
  this.notifyCbs(hash, {