@instantdb/core 0.22.95 → 0.22.96-experimental.add-posthog-frontend.20386914944.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 (63) 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 -72
  4. package/dist/commonjs/Reactor.js.map +1 -1
  5. package/dist/commonjs/SyncTable.d.ts.map +1 -1
  6. package/dist/commonjs/SyncTable.js +6 -7
  7. package/dist/commonjs/SyncTable.js.map +1 -1
  8. package/dist/commonjs/index.d.ts +1 -5
  9. package/dist/commonjs/index.d.ts.map +1 -1
  10. package/dist/commonjs/index.js +1 -7
  11. package/dist/commonjs/index.js.map +1 -1
  12. package/dist/commonjs/reactorTypes.d.ts +4 -5
  13. package/dist/commonjs/reactorTypes.d.ts.map +1 -1
  14. package/dist/commonjs/reactorTypes.js.map +1 -1
  15. package/dist/esm/Reactor.d.ts +1 -13
  16. package/dist/esm/Reactor.d.ts.map +1 -1
  17. package/dist/esm/Reactor.js +5 -72
  18. package/dist/esm/Reactor.js.map +1 -1
  19. package/dist/esm/SyncTable.d.ts.map +1 -1
  20. package/dist/esm/SyncTable.js +6 -7
  21. package/dist/esm/SyncTable.js.map +1 -1
  22. package/dist/esm/index.d.ts +1 -5
  23. package/dist/esm/index.d.ts.map +1 -1
  24. package/dist/esm/index.js +2 -5
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/reactorTypes.d.ts +4 -5
  27. package/dist/esm/reactorTypes.d.ts.map +1 -1
  28. package/dist/esm/reactorTypes.js.map +1 -1
  29. package/dist/standalone/index.js +2400 -2735
  30. package/dist/standalone/index.umd.cjs +3 -3
  31. package/package.json +2 -2
  32. package/src/Reactor.js +6 -83
  33. package/src/SyncTable.ts +14 -18
  34. package/src/index.ts +0 -9
  35. package/src/reactorTypes.ts +4 -5
  36. package/__tests__/src/serializeSchema.test.ts +0 -123
  37. package/dist/commonjs/createRouteHandler.d.ts +0 -8
  38. package/dist/commonjs/createRouteHandler.d.ts.map +0 -1
  39. package/dist/commonjs/createRouteHandler.js +0 -66
  40. package/dist/commonjs/createRouteHandler.js.map +0 -1
  41. package/dist/commonjs/framework.d.ts +0 -77
  42. package/dist/commonjs/framework.d.ts.map +0 -1
  43. package/dist/commonjs/framework.js +0 -228
  44. package/dist/commonjs/framework.js.map +0 -1
  45. package/dist/commonjs/parseSchemaFromJSON.d.ts +0 -3
  46. package/dist/commonjs/parseSchemaFromJSON.d.ts.map +0 -1
  47. package/dist/commonjs/parseSchemaFromJSON.js +0 -148
  48. package/dist/commonjs/parseSchemaFromJSON.js.map +0 -1
  49. package/dist/esm/createRouteHandler.d.ts +0 -8
  50. package/dist/esm/createRouteHandler.d.ts.map +0 -1
  51. package/dist/esm/createRouteHandler.js +0 -62
  52. package/dist/esm/createRouteHandler.js.map +0 -1
  53. package/dist/esm/framework.d.ts +0 -77
  54. package/dist/esm/framework.d.ts.map +0 -1
  55. package/dist/esm/framework.js +0 -188
  56. package/dist/esm/framework.js.map +0 -1
  57. package/dist/esm/parseSchemaFromJSON.d.ts +0 -3
  58. package/dist/esm/parseSchemaFromJSON.d.ts.map +0 -1
  59. package/dist/esm/parseSchemaFromJSON.js +0 -144
  60. package/dist/esm/parseSchemaFromJSON.js.map +0 -1
  61. package/src/createRouteHandler.ts +0 -63
  62. package/src/framework.ts +0 -318
  63. package/src/parseSchemaFromJSON.ts +0 -176
package/src/framework.ts DELETED
@@ -1,318 +0,0 @@
1
- // The FrameworkClient class is a mini version of a query store that allows making queries on both the frontend and backend
2
- // you can register queries, await their results and serialize them over a server/client boundary.
3
- // The class is generic so that it can be a good starting off point to make other ssr adapters.
4
- import {
5
- coerceQuery,
6
- InstantCoreDatabase,
7
- InstantDBAttr,
8
- weakHash,
9
- } from './index.ts';
10
- import * as s from './store.js';
11
- import instaql from './instaql.js';
12
- import { RuleParams } from './schemaTypes.ts';
13
- import { createLinkIndex } from './utils/linkIndex.ts';
14
-
15
- export const isServer = typeof window === 'undefined' || 'Deno' in globalThis;
16
-
17
- export type FrameworkConfig = {
18
- token?: string | null;
19
- db: InstantCoreDatabase<any, any>;
20
- };
21
-
22
- // represents an eventual result from running a query
23
- // either via ssr or by using the existing websocket connection.
24
- type QueryPromise =
25
- | {
26
- type: 'http';
27
- triples: any;
28
- attrs: any;
29
- queryHash: any;
30
- query: any;
31
- pageInfo?: any;
32
- }
33
- | {
34
- type: 'session';
35
- queryResult: any;
36
- };
37
-
38
- export class FrameworkClient {
39
- private params: FrameworkConfig;
40
- private db: InstantCoreDatabase<any, any>;
41
-
42
- // stores all of the query promises so that ssr can read them
43
- // and send the relevant results alongside the html that resulted in the query resolving
44
- public resultMap: Map<
45
- string,
46
- {
47
- status: 'pending' | 'success' | 'error';
48
- type: 'http' | 'session';
49
- promise?: Promise<QueryPromise> | null;
50
- data?: any;
51
- error?: any;
52
- }
53
- > = new Map();
54
-
55
- private queryResolvedCallbacks: ((result: {
56
- triples: any;
57
- attrs: any;
58
- queryHash: any;
59
- query: any;
60
- pageInfo?: any;
61
- }) => void)[] = [];
62
-
63
- constructor(params: FrameworkConfig) {
64
- this.params = params;
65
- this.db = params.db;
66
- this.resultMap = new Map<
67
- string,
68
- {
69
- type: 'http' | 'session';
70
- status: 'pending' | 'success' | 'error';
71
- promise?: Promise<QueryPromise>;
72
- data?: any;
73
- error?: any;
74
- }
75
- >();
76
- }
77
-
78
- public subscribe = (
79
- callback: (result: {
80
- triples: any;
81
- attrs: any;
82
- queryHash: string;
83
- pageInfo?: any;
84
- }) => void,
85
- ) => {
86
- this.queryResolvedCallbacks.push(callback);
87
- };
88
-
89
- // Runs on the client when ssr gets html script tags
90
- public addQueryResult = (queryKey: string, value: any) => {
91
- this.resultMap.set(queryKey, {
92
- type: value.type,
93
- status: 'success',
94
- data: value,
95
- promise: null,
96
- error: null,
97
- });
98
- // send the result to the client
99
- if (!isServer) {
100
- // make sure the attrs are there to create stores
101
- if (!this.db._reactor.attrs) {
102
- this.db._reactor._setAttrs(value.attrs);
103
- }
104
- this.db._reactor._addQueryData(
105
- value.query,
106
- value,
107
- !!this.db._reactor.config.schema,
108
- );
109
- }
110
- };
111
-
112
- // creates an entry in the results map
113
- // and returns the same thing added to the map
114
- public query = (
115
- _query: any,
116
- opts?: {
117
- ruleParams: RuleParams;
118
- },
119
- ): {
120
- type: 'http' | 'session';
121
- status: 'pending' | 'success' | 'error';
122
- promise?: Promise<QueryPromise>;
123
- data?: any;
124
- error?: any;
125
- } => {
126
- const { hash, query } = this.hashQuery(_query, opts);
127
-
128
- if (this.db._reactor.status === 'authenticated') {
129
- const promise = this.db.queryOnce(_query, opts);
130
- let entry = {
131
- status: 'pending' as 'pending' | 'success' | 'error',
132
- type: 'session' as 'http' | 'session',
133
- data: undefined as any,
134
- error: undefined as any,
135
- promise: promise as any,
136
- };
137
- promise.then((result) => {
138
- entry.status = 'success';
139
- entry.data = result;
140
- entry.promise = null;
141
- });
142
- promise.catch((error) => {
143
- entry.status = 'error';
144
- entry.error = error;
145
- entry.promise = null;
146
- });
147
- this.resultMap.set(hash, entry);
148
- return entry as any;
149
- }
150
-
151
- const promise = this.getTriplesAndAttrsForQuery(query);
152
- let entry = {
153
- status: 'pending' as 'pending' | 'success' | 'error',
154
- type: 'http' as 'http' | 'session',
155
- data: undefined as any,
156
- error: undefined as any,
157
- promise: promise as any,
158
- };
159
-
160
- promise.then((result) => {
161
- entry.status = 'success';
162
- entry.data = result;
163
- entry.promise = null;
164
- });
165
- promise.catch((error) => {
166
- entry.status = 'error';
167
- entry.error = error;
168
- entry.promise = null;
169
- });
170
-
171
- promise.then((result) => {
172
- this.queryResolvedCallbacks.forEach((callback) => {
173
- callback({
174
- queryHash: hash,
175
- query: query,
176
- attrs: result.attrs,
177
- triples: result.triples,
178
- pageInfo: result.pageInfo,
179
- });
180
- });
181
- });
182
-
183
- this.resultMap.set(hash, entry);
184
- return entry;
185
- };
186
-
187
- public getExistingResultForQuery = (
188
- _query: any,
189
- opts?: {
190
- ruleParams: RuleParams;
191
- },
192
- ) => {
193
- const { hash } = this.hashQuery(_query, opts);
194
- return this.resultMap.get(hash);
195
- };
196
-
197
- // creates a query result from a set of triples, query, and attrs
198
- // can be run server side or client side
199
- public completeIsomorphic = (
200
- query: any,
201
- triples: any[],
202
- attrs: InstantDBAttr[],
203
- pageInfo?: any,
204
- ) => {
205
- const attrMap = {};
206
- attrs.forEach((attr) => {
207
- attrMap[attr.id] = attr;
208
- });
209
-
210
- const enableCardinalityInference =
211
- Boolean(this.db?._reactor?.config?.schema) &&
212
- ('cardinalityInference' in this.db?._reactor?.config
213
- ? Boolean(this.db?._reactor.config?.cardinalityInference)
214
- : true);
215
-
216
- const attrsStore = new s.AttrsStoreClass(
217
- attrs.reduce((acc, attr) => {
218
- acc[attr.id] = attr;
219
- return acc;
220
- }, {}),
221
- createLinkIndex(this.db?._reactor.config.schema),
222
- );
223
-
224
- const store = s.createStore(
225
- attrsStore,
226
- triples,
227
- enableCardinalityInference,
228
- this.params.db._reactor.config.useDateObjects || false,
229
- );
230
- const resp = instaql(
231
- {
232
- store: store,
233
- attrsStore: attrsStore,
234
- pageInfo: pageInfo,
235
- aggregate: undefined,
236
- },
237
- query,
238
- );
239
- return resp;
240
- };
241
-
242
- public hashQuery = (
243
- _query: any,
244
- opts?: {
245
- ruleParams: RuleParams;
246
- },
247
- ): { hash: string; query: any } => {
248
- if (_query && opts && 'ruleParams' in opts) {
249
- _query = { $$ruleParams: opts['ruleParams'], ..._query };
250
- }
251
- const query = _query ? coerceQuery(_query) : null;
252
- return { hash: weakHash(query), query: query };
253
- };
254
-
255
- // Run by the server to get triples and attrs
256
- public getTriplesAndAttrsForQuery = async (
257
- query: any,
258
- ): Promise<{
259
- triples: any[];
260
- attrs: InstantDBAttr[];
261
- query: any;
262
- queryHash: string;
263
- type: 'http';
264
- pageInfo?: any;
265
- }> => {
266
- try {
267
- const response = await fetch(
268
- `${this.db._reactor.config.apiURI}/runtime/framework/query`,
269
- {
270
- method: 'POST',
271
- headers: {
272
- 'app-id': this.params.db._reactor.config.appId,
273
- 'Content-Type': 'application/json',
274
- Authorization: this.params.token
275
- ? `Bearer ${this.params.token}`
276
- : undefined,
277
- } as Record<string, string>,
278
- body: JSON.stringify({
279
- query: query,
280
- }),
281
- },
282
- );
283
-
284
- if (!response.ok) {
285
- throw new Error('Error getting triples from server');
286
- }
287
-
288
- const data = await response.json();
289
-
290
- const attrs = data?.attrs;
291
- if (!attrs) {
292
- throw new Error('No attrs');
293
- }
294
-
295
- // TODO: make safer
296
- const triples =
297
- data.result?.[0].data?.['datalog-result']?.['join-rows'][0];
298
-
299
- const pageInfo = data.result?.[0]?.data?.['page-info'];
300
-
301
- return {
302
- attrs,
303
- triples,
304
- type: 'http',
305
- queryHash: this.hashQuery(query).hash,
306
- query,
307
- pageInfo,
308
- };
309
- } catch (err: any) {
310
- const errWithMessage = new Error(
311
- 'Error getting triples from framework client',
312
- );
313
- // @ts-expect-error pre es2022
314
- errWithMessage.cause = err;
315
- throw errWithMessage;
316
- }
317
- };
318
- }
@@ -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
- };