@instantdb/core 0.22.89-experimental.drewh-ssr.20285043124.1 → 0.22.89-experimental.split-store.20276199573.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 (56) hide show
  1. package/dist/commonjs/Reactor.d.ts +1 -13
  2. package/dist/commonjs/Reactor.d.ts.map +1 -1
  3. package/dist/commonjs/Reactor.js +5 -69
  4. package/dist/commonjs/Reactor.js.map +1 -1
  5. package/dist/commonjs/index.d.ts +1 -5
  6. package/dist/commonjs/index.d.ts.map +1 -1
  7. package/dist/commonjs/index.js +1 -7
  8. package/dist/commonjs/index.js.map +1 -1
  9. package/dist/commonjs/reactorTypes.d.ts +4 -5
  10. package/dist/commonjs/reactorTypes.d.ts.map +1 -1
  11. package/dist/commonjs/reactorTypes.js.map +1 -1
  12. package/dist/esm/Reactor.d.ts +1 -13
  13. package/dist/esm/Reactor.d.ts.map +1 -1
  14. package/dist/esm/Reactor.js +5 -69
  15. package/dist/esm/Reactor.js.map +1 -1
  16. package/dist/esm/index.d.ts +1 -5
  17. package/dist/esm/index.d.ts.map +1 -1
  18. package/dist/esm/index.js +2 -5
  19. package/dist/esm/index.js.map +1 -1
  20. package/dist/esm/reactorTypes.d.ts +4 -5
  21. package/dist/esm/reactorTypes.d.ts.map +1 -1
  22. package/dist/esm/reactorTypes.js.map +1 -1
  23. package/dist/standalone/index.js +2377 -2697
  24. package/dist/standalone/index.umd.cjs +3 -3
  25. package/package.json +2 -2
  26. package/src/Reactor.js +6 -84
  27. package/src/index.ts +0 -9
  28. package/src/reactorTypes.ts +4 -5
  29. package/__tests__/src/serializeSchema.test.ts +0 -123
  30. package/dist/commonjs/createRouteHandler.d.ts +0 -8
  31. package/dist/commonjs/createRouteHandler.d.ts.map +0 -1
  32. package/dist/commonjs/createRouteHandler.js +0 -57
  33. package/dist/commonjs/createRouteHandler.js.map +0 -1
  34. package/dist/commonjs/framework.d.ts +0 -77
  35. package/dist/commonjs/framework.d.ts.map +0 -1
  36. package/dist/commonjs/framework.js +0 -209
  37. package/dist/commonjs/framework.js.map +0 -1
  38. package/dist/commonjs/parseSchemaFromJSON.d.ts +0 -3
  39. package/dist/commonjs/parseSchemaFromJSON.d.ts.map +0 -1
  40. package/dist/commonjs/parseSchemaFromJSON.js +0 -148
  41. package/dist/commonjs/parseSchemaFromJSON.js.map +0 -1
  42. package/dist/esm/createRouteHandler.d.ts +0 -8
  43. package/dist/esm/createRouteHandler.d.ts.map +0 -1
  44. package/dist/esm/createRouteHandler.js +0 -53
  45. package/dist/esm/createRouteHandler.js.map +0 -1
  46. package/dist/esm/framework.d.ts +0 -77
  47. package/dist/esm/framework.d.ts.map +0 -1
  48. package/dist/esm/framework.js +0 -169
  49. package/dist/esm/framework.js.map +0 -1
  50. package/dist/esm/parseSchemaFromJSON.d.ts +0 -3
  51. package/dist/esm/parseSchemaFromJSON.d.ts.map +0 -1
  52. package/dist/esm/parseSchemaFromJSON.js +0 -144
  53. package/dist/esm/parseSchemaFromJSON.js.map +0 -1
  54. package/src/createRouteHandler.ts +0 -44
  55. package/src/framework.ts +0 -294
  56. package/src/parseSchemaFromJSON.ts +0 -176
package/src/framework.ts DELETED
@@ -1,294 +0,0 @@
1
- import {
2
- coerceQuery,
3
- InstantCoreDatabase,
4
- InstantDBAttr,
5
- weakHash,
6
- } from './index.ts';
7
- import * as s from './store.js';
8
- import instaql from './instaql.js';
9
- import { RuleParams } from './schemaTypes.ts';
10
- import { createLinkIndex } from './utils/linkIndex.ts';
11
-
12
- export const isServer = typeof window === 'undefined' || 'Deno' in globalThis;
13
-
14
- export type FrameworkConfig = {
15
- token?: string | null;
16
- db: InstantCoreDatabase<any, any>;
17
- };
18
-
19
- type QueryPromise =
20
- | {
21
- type: 'http';
22
- triples: any;
23
- attrs: any;
24
- queryHash: any;
25
- query: any;
26
- pageInfo?: any;
27
- }
28
- | {
29
- type: 'session';
30
- queryResult: any;
31
- };
32
-
33
- export class FrameworkClient {
34
- private params: FrameworkConfig;
35
- private db: InstantCoreDatabase<any, any>;
36
- public resultMap: Map<
37
- string,
38
- {
39
- status: 'pending' | 'success' | 'error';
40
- type: 'http' | 'session';
41
- promise?: Promise<QueryPromise> | null;
42
- data?: any;
43
- error?: any;
44
- }
45
- > = new Map();
46
-
47
- private queryResolvedCallbacks: ((result: {
48
- triples: any;
49
- attrs: any;
50
- queryHash: any;
51
- query: any;
52
- pageInfo?: any;
53
- }) => void)[] = [];
54
-
55
- constructor(params: FrameworkConfig) {
56
- this.params = params;
57
- this.db = params.db;
58
- this.resultMap = new Map<
59
- string,
60
- {
61
- type: 'http' | 'session';
62
- status: 'pending' | 'success' | 'error';
63
- promise?: Promise<QueryPromise>;
64
- data?: any;
65
- error?: any;
66
- }
67
- >();
68
- }
69
-
70
- public subscribe = (
71
- callback: (result: {
72
- triples: any;
73
- attrs: any;
74
- queryHash: string;
75
- pageInfo?: any;
76
- }) => void,
77
- ) => {
78
- this.queryResolvedCallbacks.push(callback);
79
- };
80
-
81
- // Runs on the client when ssr gets html script tags
82
- public addQueryResult = (queryKey: string, value: any) => {
83
- this.resultMap.set(queryKey, {
84
- type: value.type,
85
- status: 'success',
86
- data: value,
87
- promise: null,
88
- error: null,
89
- });
90
- // send the result to the client
91
- if (!isServer) {
92
- // make sure the attrs are there to create stores
93
- if (!this.db._reactor.attrs) {
94
- this.db._reactor._setAttrs(value.attrs);
95
- }
96
- this.db._reactor._addQueryData(
97
- value.query,
98
- value,
99
- !!this.db._reactor.config.schema,
100
- );
101
- }
102
- };
103
-
104
- public query = (
105
- _query: any,
106
- opts?: {
107
- ruleParams: RuleParams;
108
- },
109
- ): {
110
- type: 'http' | 'session';
111
- status: 'pending' | 'success' | 'error';
112
- promise?: Promise<QueryPromise>;
113
- data?: any;
114
- error?: any;
115
- } => {
116
- const { hash, query } = this.hashQuery(_query, opts);
117
-
118
- if (this.db._reactor.status === 'authenticated') {
119
- const promise = this.db.queryOnce(_query, opts);
120
- let entry = {
121
- status: 'pending' as 'pending' | 'success' | 'error',
122
- type: 'session' as 'http' | 'session',
123
- data: undefined as any,
124
- error: undefined as any,
125
- promise: promise as any,
126
- };
127
- promise.then((result) => {
128
- entry.status = 'success';
129
- entry.data = result;
130
- entry.promise = null;
131
- });
132
- promise.catch((error) => {
133
- entry.status = 'error';
134
- entry.error = error;
135
- entry.promise = null;
136
- });
137
- return entry as any;
138
- }
139
-
140
- const promise = this.getTriplesAndAttrsForQuery(query);
141
- let entry = {
142
- status: 'pending' as 'pending' | 'success' | 'error',
143
- type: 'http' as 'http' | 'session',
144
- data: undefined as any,
145
- error: undefined as any,
146
- promise: promise as any,
147
- };
148
-
149
- promise.then((result) => {
150
- entry.status = 'success';
151
- entry.data = result;
152
- entry.promise = null;
153
- });
154
- promise.catch((error) => {
155
- entry.status = 'error';
156
- entry.error = error;
157
- entry.promise = null;
158
- });
159
-
160
- promise.then((result) => {
161
- this.queryResolvedCallbacks.forEach((callback) => {
162
- callback({
163
- queryHash: hash,
164
- query: query,
165
- attrs: result.attrs,
166
- triples: result.triples,
167
- pageInfo: result.pageInfo,
168
- });
169
- });
170
- });
171
-
172
- this.resultMap.set(hash, entry);
173
- return entry;
174
- };
175
-
176
- public getExistingResultForQuery = (
177
- _query: any,
178
- opts?: {
179
- ruleParams: RuleParams;
180
- },
181
- ) => {
182
- const { hash } = this.hashQuery(_query, opts);
183
- return this.resultMap.get(hash);
184
- };
185
-
186
- public completeIsomorphic = (
187
- query: any,
188
- triples: any[],
189
- attrs: InstantDBAttr[],
190
- pageInfo?: any,
191
- ) => {
192
- const attrMap = {};
193
- attrs.forEach((attr) => {
194
- attrMap[attr.id] = attr;
195
- });
196
-
197
- const enableCardinalityInference =
198
- Boolean(this.db?._reactor?.config?.schema) &&
199
- ('cardinalityInference' in this.db?._reactor?.config
200
- ? Boolean(this.db?._reactor.config?.cardinalityInference)
201
- : true);
202
-
203
- const attrsStore = new s.AttrsStoreClass(
204
- attrs.reduce((acc, attr) => {
205
- acc[attr.id] = attr;
206
- return acc;
207
- }, {}),
208
- createLinkIndex(this.db?._reactor.config.schema),
209
- );
210
-
211
- const store = s.createStore(
212
- attrsStore,
213
- triples,
214
- enableCardinalityInference,
215
- this.params.db._reactor.config.useDateObjects || false,
216
- );
217
- const resp = instaql(
218
- {
219
- store: store,
220
- attrsStore: attrsStore,
221
- pageInfo: pageInfo,
222
- aggregate: undefined,
223
- },
224
- query,
225
- );
226
- return resp;
227
- };
228
-
229
- public hashQuery = (
230
- _query: any,
231
- opts?: {
232
- ruleParams: RuleParams;
233
- },
234
- ): { hash: string; query: any } => {
235
- if (_query && opts && 'ruleParams' in opts) {
236
- _query = { $$ruleParams: opts['ruleParams'], ..._query };
237
- }
238
- const query = _query ? coerceQuery(_query) : null;
239
- return { hash: weakHash(query), query: query };
240
- };
241
-
242
- public getTriplesAndAttrsForQuery = async (
243
- query: any,
244
- ): Promise<{
245
- triples: any[];
246
- attrs: InstantDBAttr[];
247
- query: any;
248
- queryHash: string;
249
- type: 'http';
250
- pageInfo?: any;
251
- }> => {
252
- const response = await fetch(
253
- `${this.db._reactor.config.apiURI}/runtime/framework/query`,
254
- {
255
- method: 'POST',
256
- headers: {
257
- 'app-id': this.params.db._reactor.config.appId,
258
- 'Content-Type': 'application/json',
259
- Authorization: this.params.token
260
- ? `Bearer ${this.params.token}`
261
- : undefined,
262
- } as Record<string, string>,
263
- body: JSON.stringify({
264
- query: query,
265
- }),
266
- },
267
- );
268
-
269
- if (!response.ok) {
270
- throw new Error('Error getting triples from server');
271
- }
272
-
273
- const data = await response.json();
274
-
275
- const attrs = data?.attrs;
276
- if (!attrs) {
277
- throw new Error('No attrs');
278
- }
279
-
280
- // TODO: make safer
281
- const triples = data.result?.[0].data?.['datalog-result']?.['join-rows'][0];
282
-
283
- const pageInfo = data.result?.[0]?.data?.['page-info'];
284
-
285
- return {
286
- attrs,
287
- triples,
288
- type: 'http',
289
- queryHash: this.hashQuery(query).hash,
290
- query,
291
- pageInfo,
292
- };
293
- };
294
- }
@@ -1,176 +0,0 @@
1
- import { i } from './schema.ts';
2
- import { DataAttrDef, InstantSchemaDef } from './schemaTypes.ts';
3
-
4
- export const parseSchemaFromJSON = (
5
- s: any,
6
- ): InstantSchemaDef<any, any, any> => {
7
- // Parse entities
8
- const entities: Record<string, any> = {};
9
-
10
- for (const [entityName, entityInfo] of Object.entries(s.entities)) {
11
- const entityDef = entityInfo as any;
12
- const attrs: Record<string, any> = {};
13
-
14
- // Parse attributes
15
- for (const [attrName, attrInfo] of Object.entries(entityDef.attrs)) {
16
- const attrDef = attrInfo as any;
17
- let attr: DataAttrDef<any, any, any>;
18
-
19
- // Create the appropriate attribute type
20
- switch (attrDef.valueType) {
21
- case 'string':
22
- attr = i.string();
23
- break;
24
- case 'number':
25
- attr = i.number();
26
- break;
27
- case 'boolean':
28
- attr = i.boolean();
29
- break;
30
- case 'date':
31
- attr = i.date();
32
- break;
33
- case 'json':
34
- attr = i.json();
35
- break;
36
- default:
37
- attr = i.json();
38
- }
39
-
40
- // Apply modifiers
41
- if (!attrDef.required) {
42
- attr = attr.optional();
43
- }
44
-
45
- if (attrDef.config?.indexed) {
46
- attr = attr.indexed();
47
- }
48
-
49
- if (attrDef.config?.unique) {
50
- attr = attr.unique();
51
- }
52
-
53
- attrs[attrName] = attr;
54
- }
55
-
56
- entities[entityName] = i.entity(attrs);
57
- }
58
-
59
- // Parse links
60
- const links: Record<string, any> = s.links || {};
61
-
62
- // Parse rooms
63
- const rooms: Record<string, any> = {};
64
-
65
- if (s.rooms) {
66
- for (const [roomName, roomInfo] of Object.entries(s.rooms)) {
67
- const roomDef = roomInfo as any;
68
-
69
- // Parse presence
70
- const presenceAttrs: Record<string, any> = {};
71
- for (const [attrName, attrInfo] of Object.entries(
72
- roomDef.presence.attrs,
73
- )) {
74
- const attrDef = attrInfo as any;
75
- let attr: DataAttrDef<any, any, any>;
76
-
77
- switch (attrDef.valueType) {
78
- case 'string':
79
- attr = i.string();
80
- break;
81
- case 'number':
82
- attr = i.number();
83
- break;
84
- case 'boolean':
85
- attr = i.boolean();
86
- break;
87
- case 'date':
88
- attr = i.date();
89
- break;
90
- case 'json':
91
- attr = i.json();
92
- break;
93
- default:
94
- attr = i.json();
95
- }
96
-
97
- if (!attrDef.required) {
98
- attr = attr.optional();
99
- }
100
-
101
- if (attrDef.config?.indexed) {
102
- attr = attr.indexed();
103
- }
104
-
105
- if (attrDef.config?.unique) {
106
- attr = attr.unique();
107
- }
108
-
109
- presenceAttrs[attrName] = attr;
110
- }
111
-
112
- // Parse topics
113
- const topics: Record<string, any> = {};
114
- if (roomDef.topics) {
115
- for (const [topicName, topicInfo] of Object.entries(roomDef.topics)) {
116
- const topicDef = topicInfo as any;
117
- const topicAttrs: Record<string, any> = {};
118
-
119
- for (const [attrName, attrInfo] of Object.entries(topicDef.attrs)) {
120
- const attrDef = attrInfo as any;
121
- let attr: DataAttrDef<any, any, any>;
122
-
123
- switch (attrDef.valueType) {
124
- case 'string':
125
- attr = i.string();
126
- break;
127
- case 'number':
128
- attr = i.number();
129
- break;
130
- case 'boolean':
131
- attr = i.boolean();
132
- break;
133
- case 'date':
134
- attr = i.date();
135
- break;
136
- case 'json':
137
- attr = i.json();
138
- break;
139
- default:
140
- attr = i.json();
141
- }
142
-
143
- if (!attrDef.required) {
144
- attr = attr.optional();
145
- }
146
-
147
- if (attrDef.config?.indexed) {
148
- attr = attr.indexed();
149
- }
150
-
151
- if (attrDef.config?.unique) {
152
- attr = attr.unique();
153
- }
154
-
155
- topicAttrs[attrName] = attr;
156
- }
157
-
158
- topics[topicName] = i.entity(topicAttrs);
159
- }
160
- }
161
-
162
- rooms[roomName] = {
163
- presence: i.entity(presenceAttrs),
164
- topics,
165
- };
166
- }
167
- }
168
-
169
- const resultingSchema = i.schema({
170
- entities,
171
- links,
172
- rooms,
173
- });
174
-
175
- return resultingSchema;
176
- };