@instantdb/core 0.22.88 → 0.22.89-experimental.drewh-ssr.20277611943.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 (102) 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/serializeSchema.test.ts +123 -0
  8. package/__tests__/src/{store.test.js → store.test.ts} +215 -212
  9. package/dist/commonjs/Reactor.d.ts +36 -7
  10. package/dist/commonjs/Reactor.d.ts.map +1 -1
  11. package/dist/commonjs/Reactor.js +176 -47
  12. package/dist/commonjs/Reactor.js.map +1 -1
  13. package/dist/commonjs/SyncTable.d.ts +4 -1
  14. package/dist/commonjs/SyncTable.d.ts.map +1 -1
  15. package/dist/commonjs/SyncTable.js +35 -37
  16. package/dist/commonjs/SyncTable.js.map +1 -1
  17. package/dist/commonjs/createRouteHandler.d.ts +8 -0
  18. package/dist/commonjs/createRouteHandler.d.ts.map +1 -0
  19. package/dist/commonjs/createRouteHandler.js +57 -0
  20. package/dist/commonjs/createRouteHandler.js.map +1 -0
  21. package/dist/commonjs/framework.d.ts +77 -0
  22. package/dist/commonjs/framework.d.ts.map +1 -0
  23. package/dist/commonjs/framework.js +209 -0
  24. package/dist/commonjs/framework.js.map +1 -0
  25. package/dist/commonjs/index.d.ts +5 -1
  26. package/dist/commonjs/index.d.ts.map +1 -1
  27. package/dist/commonjs/index.js +7 -1
  28. package/dist/commonjs/index.js.map +1 -1
  29. package/dist/commonjs/instaml.d.ts +17 -4
  30. package/dist/commonjs/instaml.d.ts.map +1 -1
  31. package/dist/commonjs/instaml.js +115 -82
  32. package/dist/commonjs/instaml.js.map +1 -1
  33. package/dist/commonjs/instaql.d.ts +4 -3
  34. package/dist/commonjs/instaql.d.ts.map +1 -1
  35. package/dist/commonjs/instaql.js +65 -63
  36. package/dist/commonjs/instaql.js.map +1 -1
  37. package/dist/commonjs/parseSchemaFromJSON.d.ts +3 -0
  38. package/dist/commonjs/parseSchemaFromJSON.d.ts.map +1 -0
  39. package/dist/commonjs/parseSchemaFromJSON.js +148 -0
  40. package/dist/commonjs/parseSchemaFromJSON.js.map +1 -0
  41. package/dist/commonjs/reactorTypes.d.ts +30 -0
  42. package/dist/commonjs/reactorTypes.d.ts.map +1 -0
  43. package/dist/commonjs/reactorTypes.js +3 -0
  44. package/dist/commonjs/reactorTypes.js.map +1 -0
  45. package/dist/commonjs/store.d.ts +67 -25
  46. package/dist/commonjs/store.d.ts.map +1 -1
  47. package/dist/commonjs/store.js +177 -81
  48. package/dist/commonjs/store.js.map +1 -1
  49. package/dist/esm/Reactor.d.ts +36 -7
  50. package/dist/esm/Reactor.d.ts.map +1 -1
  51. package/dist/esm/Reactor.js +177 -48
  52. package/dist/esm/Reactor.js.map +1 -1
  53. package/dist/esm/SyncTable.d.ts +4 -1
  54. package/dist/esm/SyncTable.d.ts.map +1 -1
  55. package/dist/esm/SyncTable.js +35 -37
  56. package/dist/esm/SyncTable.js.map +1 -1
  57. package/dist/esm/createRouteHandler.d.ts +8 -0
  58. package/dist/esm/createRouteHandler.d.ts.map +1 -0
  59. package/dist/esm/createRouteHandler.js +53 -0
  60. package/dist/esm/createRouteHandler.js.map +1 -0
  61. package/dist/esm/framework.d.ts +77 -0
  62. package/dist/esm/framework.d.ts.map +1 -0
  63. package/dist/esm/framework.js +169 -0
  64. package/dist/esm/framework.js.map +1 -0
  65. package/dist/esm/index.d.ts +5 -1
  66. package/dist/esm/index.d.ts.map +1 -1
  67. package/dist/esm/index.js +5 -2
  68. package/dist/esm/index.js.map +1 -1
  69. package/dist/esm/instaml.d.ts +17 -4
  70. package/dist/esm/instaml.d.ts.map +1 -1
  71. package/dist/esm/instaml.js +112 -77
  72. package/dist/esm/instaml.js.map +1 -1
  73. package/dist/esm/instaql.d.ts +4 -3
  74. package/dist/esm/instaql.d.ts.map +1 -1
  75. package/dist/esm/instaql.js +65 -63
  76. package/dist/esm/instaql.js.map +1 -1
  77. package/dist/esm/parseSchemaFromJSON.d.ts +3 -0
  78. package/dist/esm/parseSchemaFromJSON.d.ts.map +1 -0
  79. package/dist/esm/parseSchemaFromJSON.js +144 -0
  80. package/dist/esm/parseSchemaFromJSON.js.map +1 -0
  81. package/dist/esm/reactorTypes.d.ts +30 -0
  82. package/dist/esm/reactorTypes.d.ts.map +1 -0
  83. package/dist/esm/reactorTypes.js +2 -0
  84. package/dist/esm/reactorTypes.js.map +1 -0
  85. package/dist/esm/store.d.ts +67 -25
  86. package/dist/esm/store.d.ts.map +1 -1
  87. package/dist/esm/store.js +174 -81
  88. package/dist/esm/store.js.map +1 -1
  89. package/dist/standalone/index.js +2899 -2389
  90. package/dist/standalone/index.umd.cjs +3 -3
  91. package/package.json +2 -2
  92. package/src/Reactor.js +232 -77
  93. package/src/SyncTable.ts +85 -45
  94. package/src/createRouteHandler.ts +44 -0
  95. package/src/framework.ts +294 -0
  96. package/src/index.ts +9 -0
  97. package/src/{instaml.js → instaml.ts} +201 -96
  98. package/src/instaql.ts +88 -62
  99. package/src/parseSchemaFromJSON.ts +176 -0
  100. package/src/reactorTypes.ts +33 -0
  101. package/src/store.ts +257 -101
  102. package/__tests__/src/instaql.bench.js +0 -29
@@ -1,5 +1,5 @@
1
1
  import { test, expect } from 'vitest';
2
- import { createStore } from '../../src/store';
2
+ import { createStore, AttrsStoreClass } from '../../src/store';
3
3
  import query from '../../src/instaql';
4
4
  import { i, id } from '../../src';
5
5
  import { createLinkIndex } from '../../src/utils/linkIndex';
@@ -221,19 +221,23 @@ test('one-to-one without inference', () => {
221
221
  expect(result.data.profiles.at(0).user.at(0).id).toBe(ids.user1);
222
222
  });
223
223
 
224
- function indexAttrs(attrs) {
225
- return attrs.reduce((res, x) => {
226
- res[x.id] = x;
227
- return res;
228
- }, {});
224
+ function indexAttrs(attrs, schema) {
225
+ const linkIndex = schema ? createLinkIndex(schema) : undefined;
226
+ return new AttrsStoreClass(
227
+ attrs.reduce((acc, attr) => {
228
+ acc[attr.id] = attr;
229
+ return acc;
230
+ }, {}),
231
+ linkIndex,
232
+ );
229
233
  }
230
234
 
231
235
  function queryData(config, attrs, triples, q) {
232
- const store = createStore(indexAttrs(attrs), triples);
236
+ const attrsStore = indexAttrs(attrs, config.schema);
237
+ const store = createStore(attrsStore, triples);
233
238
  store.cardinalityInference = config.cardinalityInference;
234
- store.linkIndex = config.schema ? createLinkIndex(config.schema) : undefined;
235
239
 
236
- const result = query({ store }, q);
240
+ const result = query({ store, attrsStore }, q);
237
241
 
238
242
  return { result, store };
239
243
  }
@@ -0,0 +1,123 @@
1
+ import { expect, test } from 'vitest';
2
+ import { i } from '../../src/schema';
3
+ import { parseSchemaFromJSON } from '../../src/parseSchemaFromJSON';
4
+ import { InstantSchemaDef } from '../../src/schemaTypes';
5
+
6
+ const schema = i.schema({
7
+ entities: {
8
+ users: i.entity({
9
+ name: i.string(),
10
+ email: i.string().indexed().unique(),
11
+ bio: i.string().optional(),
12
+ // this is a convenient way to typecheck custom JSON fields
13
+ // though we should probably have a backend solution for this
14
+ stuff: i.json<{ custom: string }>(),
15
+ junk: i.any(),
16
+ }),
17
+ posts: i.entity({
18
+ title: i.string().optional(),
19
+ body: i.string(),
20
+ }),
21
+ comments: i.entity({
22
+ body: i.string().indexed(),
23
+ likes: i.number(),
24
+ }),
25
+
26
+ birthdays: i.entity({
27
+ date: i.date(),
28
+ message: i.string(),
29
+ prizes: i.json<string | number>(),
30
+ }),
31
+ },
32
+ links: {
33
+ usersPosts: {
34
+ forward: {
35
+ on: 'users',
36
+ has: 'many',
37
+ label: 'posts',
38
+ },
39
+ reverse: {
40
+ on: 'posts',
41
+ has: 'one',
42
+ label: 'author',
43
+ },
44
+ },
45
+ postsComments: {
46
+ forward: {
47
+ on: 'posts',
48
+ has: 'many',
49
+ label: 'comments',
50
+ },
51
+ reverse: {
52
+ on: 'comments',
53
+ has: 'one',
54
+ label: 'post',
55
+ },
56
+ },
57
+ friendships: {
58
+ forward: {
59
+ on: 'users',
60
+ has: 'many',
61
+ label: 'friends',
62
+ },
63
+ reverse: {
64
+ on: 'users',
65
+ has: 'many',
66
+ label: '_friends',
67
+ },
68
+ },
69
+ referrals: {
70
+ forward: {
71
+ on: 'users',
72
+ has: 'many',
73
+ label: 'referred',
74
+ },
75
+ reverse: {
76
+ on: 'users',
77
+ has: 'one',
78
+ label: 'referrer',
79
+ },
80
+ },
81
+ },
82
+ rooms: {
83
+ chat: {
84
+ presence: i.entity({
85
+ name: i.string(),
86
+ status: i.string(),
87
+ }),
88
+ topics: {
89
+ sendEmoji: i.entity({
90
+ emoji: i.string(),
91
+ }),
92
+ },
93
+ },
94
+ },
95
+ });
96
+
97
+ type AnySchema = InstantSchemaDef<any, any, any>;
98
+
99
+ // compare schemas by stringifying them with json and comparing the strings
100
+ const compareSchemas = (schema1: AnySchema, schema2: AnySchema) => {
101
+ expect(JSON.stringify(schema1, null, 2)).toBe(
102
+ JSON.stringify(schema2, null, 2),
103
+ );
104
+ };
105
+
106
+ test('ability to parse stringified schema into real schema object', () => {
107
+ const stringified = JSON.stringify(schema, null, 2);
108
+ const parsed = JSON.parse(stringified);
109
+ console.log(stringified);
110
+
111
+ const otherSide = parseSchemaFromJSON(parsed);
112
+
113
+ compareSchemas(schema, otherSide);
114
+
115
+ expect(schema.entities.comments.links).toEqual(
116
+ otherSide.entities.comments.links,
117
+ );
118
+ expect(schema.entities.comments.asType).toEqual(
119
+ otherSide.entities.comments.asType,
120
+ );
121
+
122
+ expect(schema).toStrictEqual(otherSide);
123
+ });