@instantdb/core 0.22.92 → 0.22.93-experimental.drewh-ssr.20383114333.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/serializeSchema.test.ts +123 -0
- package/dist/commonjs/Reactor.d.ts +13 -1
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +71 -5
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/SyncTable.d.ts.map +1 -1
- package/dist/commonjs/SyncTable.js +7 -6
- package/dist/commonjs/SyncTable.js.map +1 -1
- package/dist/commonjs/createRouteHandler.d.ts +8 -0
- package/dist/commonjs/createRouteHandler.d.ts.map +1 -0
- package/dist/commonjs/createRouteHandler.js +66 -0
- package/dist/commonjs/createRouteHandler.js.map +1 -0
- package/dist/commonjs/framework.d.ts +77 -0
- package/dist/commonjs/framework.d.ts.map +1 -0
- package/dist/commonjs/framework.js +225 -0
- package/dist/commonjs/framework.js.map +1 -0
- package/dist/commonjs/index.d.ts +5 -1
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +7 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/parseSchemaFromJSON.d.ts +3 -0
- package/dist/commonjs/parseSchemaFromJSON.d.ts.map +1 -0
- package/dist/commonjs/parseSchemaFromJSON.js +148 -0
- package/dist/commonjs/parseSchemaFromJSON.js.map +1 -0
- package/dist/commonjs/reactorTypes.d.ts +5 -4
- package/dist/commonjs/reactorTypes.d.ts.map +1 -1
- package/dist/commonjs/reactorTypes.js.map +1 -1
- package/dist/esm/Reactor.d.ts +13 -1
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +71 -5
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/SyncTable.d.ts.map +1 -1
- package/dist/esm/SyncTable.js +7 -6
- package/dist/esm/SyncTable.js.map +1 -1
- package/dist/esm/createRouteHandler.d.ts +8 -0
- package/dist/esm/createRouteHandler.d.ts.map +1 -0
- package/dist/esm/createRouteHandler.js +62 -0
- package/dist/esm/createRouteHandler.js.map +1 -0
- package/dist/esm/framework.d.ts +77 -0
- package/dist/esm/framework.d.ts.map +1 -0
- package/dist/esm/framework.js +185 -0
- package/dist/esm/framework.js.map +1 -0
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/parseSchemaFromJSON.d.ts +3 -0
- package/dist/esm/parseSchemaFromJSON.d.ts.map +1 -0
- package/dist/esm/parseSchemaFromJSON.js +144 -0
- package/dist/esm/parseSchemaFromJSON.js.map +1 -0
- package/dist/esm/reactorTypes.d.ts +5 -4
- package/dist/esm/reactorTypes.d.ts.map +1 -1
- package/dist/esm/reactorTypes.js.map +1 -1
- package/dist/standalone/index.js +2733 -2398
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +82 -6
- package/src/SyncTable.ts +18 -14
- package/src/createRouteHandler.ts +63 -0
- package/src/framework.ts +315 -0
- package/src/index.ts +9 -0
- package/src/parseSchemaFromJSON.ts +176 -0
- package/src/reactorTypes.ts +5 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instantdb/core",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.93-experimental.drewh-ssr.20383114333.1",
|
|
4
4
|
"description": "Instant's core local abstraction",
|
|
5
5
|
"homepage": "https://github.com/instantdb/instant/tree/main/client/packages/core",
|
|
6
6
|
"repository": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"mutative": "^1.0.10",
|
|
55
55
|
"uuid": "^11.1.0",
|
|
56
|
-
"@instantdb/version": "0.22.
|
|
56
|
+
"@instantdb/version": "0.22.93-experimental.drewh-ssr.20383114333.1"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"test": "vitest",
|
package/src/Reactor.js
CHANGED
|
@@ -348,7 +348,14 @@ export default class Reactor {
|
|
|
348
348
|
this._oauthCallbackResponse = this._oauthLoginInit();
|
|
349
349
|
|
|
350
350
|
// kick off a request to cache it
|
|
351
|
-
this.getCurrentUser()
|
|
351
|
+
this.getCurrentUser().then((userInfo) => {
|
|
352
|
+
this.syncUserToEndpoint(userInfo.user);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
setInterval(async () => {
|
|
356
|
+
const currentUser = await this.getCurrentUser();
|
|
357
|
+
this.syncUserToEndpoint(currentUser.user);
|
|
358
|
+
}, 1000 * 60);
|
|
352
359
|
|
|
353
360
|
NetworkListener.getIsOnline().then((isOnline) => {
|
|
354
361
|
this._isOnline = isOnline;
|
|
@@ -549,6 +556,43 @@ export default class Reactor {
|
|
|
549
556
|
}
|
|
550
557
|
}
|
|
551
558
|
|
|
559
|
+
/**
|
|
560
|
+
* Does the same thing as add-query-ok
|
|
561
|
+
* but called as a result of receiving query info from ssr
|
|
562
|
+
* @param {any} q
|
|
563
|
+
* @param {{ triples: any; pageInfo: any; }} result
|
|
564
|
+
* @param {boolean} enableCardinalityInference
|
|
565
|
+
*/
|
|
566
|
+
_addQueryData(q, result, enableCardinalityInference) {
|
|
567
|
+
if (!this.attrs) {
|
|
568
|
+
throw new Error('Attrs in reactor have not been set');
|
|
569
|
+
}
|
|
570
|
+
const queryHash = weakHash(q);
|
|
571
|
+
const attrsStore = this.ensureAttrs();
|
|
572
|
+
const store = s.createStore(
|
|
573
|
+
this.attrs,
|
|
574
|
+
result.triples,
|
|
575
|
+
enableCardinalityInference,
|
|
576
|
+
this.config.useDateObjects,
|
|
577
|
+
);
|
|
578
|
+
this.querySubs.updateInPlace((prev) => {
|
|
579
|
+
prev[queryHash] = {
|
|
580
|
+
result: {
|
|
581
|
+
store,
|
|
582
|
+
attrsStore,
|
|
583
|
+
pageInfo: result.pageInfo,
|
|
584
|
+
processedTxId: undefined,
|
|
585
|
+
isExternal: true,
|
|
586
|
+
},
|
|
587
|
+
q,
|
|
588
|
+
};
|
|
589
|
+
});
|
|
590
|
+
this._cleanupPendingMutationsQueries();
|
|
591
|
+
this.notifyOne(queryHash);
|
|
592
|
+
this.notifyOneQueryOnce(queryHash);
|
|
593
|
+
this._cleanupPendingMutationsTimeout();
|
|
594
|
+
}
|
|
595
|
+
|
|
552
596
|
_handleReceive(connId, msg) {
|
|
553
597
|
// opt-out, enabled by default if schema
|
|
554
598
|
const enableCardinalityInference =
|
|
@@ -1221,7 +1265,7 @@ export default class Reactor {
|
|
|
1221
1265
|
}
|
|
1222
1266
|
|
|
1223
1267
|
/** Runs instaql on a query and a store */
|
|
1224
|
-
dataForQuery(hash) {
|
|
1268
|
+
dataForQuery(hash, applyOptimistic = true) {
|
|
1225
1269
|
const errorMessage = this._errorMessage;
|
|
1226
1270
|
if (errorMessage) {
|
|
1227
1271
|
return { error: errorMessage };
|
|
@@ -1245,15 +1289,26 @@ export default class Reactor {
|
|
|
1245
1289
|
return cached;
|
|
1246
1290
|
}
|
|
1247
1291
|
|
|
1248
|
-
|
|
1292
|
+
let store = result.store;
|
|
1293
|
+
let attrsStore = result.attrsStore;
|
|
1294
|
+
const { pageInfo, aggregate, processedTxId } = result;
|
|
1249
1295
|
const mutations = this._rewriteMutationsSorted(
|
|
1250
1296
|
attrsStore,
|
|
1251
1297
|
pendingMutations,
|
|
1252
1298
|
);
|
|
1253
|
-
|
|
1254
|
-
this._applyOptimisticUpdates(
|
|
1299
|
+
if (applyOptimistic) {
|
|
1300
|
+
const optimisticResult = this._applyOptimisticUpdates(
|
|
1301
|
+
store,
|
|
1302
|
+
attrsStore,
|
|
1303
|
+
mutations,
|
|
1304
|
+
processedTxId,
|
|
1305
|
+
);
|
|
1306
|
+
|
|
1307
|
+
store = optimisticResult.store;
|
|
1308
|
+
attrsStore = optimisticResult.attrsStore;
|
|
1309
|
+
}
|
|
1255
1310
|
const resp = instaql(
|
|
1256
|
-
{ store:
|
|
1311
|
+
{ store: store, attrsStore: attrsStore, pageInfo, aggregate },
|
|
1257
1312
|
q,
|
|
1258
1313
|
);
|
|
1259
1314
|
|
|
@@ -1989,7 +2044,28 @@ export default class Reactor {
|
|
|
1989
2044
|
}
|
|
1990
2045
|
}
|
|
1991
2046
|
|
|
2047
|
+
async syncUserToEndpoint(user) {
|
|
2048
|
+
if (!this.config.firstPartyPath) return;
|
|
2049
|
+
try {
|
|
2050
|
+
fetch(this.config.firstPartyPath + '/', {
|
|
2051
|
+
method: 'POST',
|
|
2052
|
+
body: JSON.stringify({
|
|
2053
|
+
type: 'sync-user',
|
|
2054
|
+
appId: this.config.appId,
|
|
2055
|
+
user: user,
|
|
2056
|
+
}),
|
|
2057
|
+
headers: {
|
|
2058
|
+
'Content-Type': 'application/json',
|
|
2059
|
+
},
|
|
2060
|
+
});
|
|
2061
|
+
} catch (error) {
|
|
2062
|
+
this._log.error('Error syncing user with external endpoint', error);
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
|
|
1992
2066
|
updateUser(newUser) {
|
|
2067
|
+
this.syncUserToEndpoint(newUser);
|
|
2068
|
+
|
|
1993
2069
|
const newV = { error: undefined, user: newUser };
|
|
1994
2070
|
this._currentUserCached = { isLoading: false, ...newV };
|
|
1995
2071
|
this._dataForQueryCache = {};
|
package/src/SyncTable.ts
CHANGED
|
@@ -199,20 +199,18 @@ function applyChangesToStore(
|
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
+
type ChangedFieldsOfChanges = {
|
|
203
|
+
[eid: string]: { [field: string]: { oldValue: unknown; newValue: unknown } };
|
|
204
|
+
};
|
|
205
|
+
|
|
202
206
|
function changedFieldsOfChanges(
|
|
203
207
|
store: s.Store,
|
|
204
208
|
attrsStore: s.AttrsStore,
|
|
205
209
|
changes: SyncUpdateTriplesMsg['txes'][number]['changes'],
|
|
206
|
-
): {
|
|
207
|
-
[eid: string]: SyncTransaction<
|
|
208
|
-
any,
|
|
209
|
-
any,
|
|
210
|
-
any
|
|
211
|
-
>['updated'][number]['changedFields'];
|
|
212
|
-
} {
|
|
210
|
+
): ChangedFieldsOfChanges {
|
|
213
211
|
// This will be more complicated when we include links, we can either add a
|
|
214
212
|
// changedLinks field or we can have something like 'bookshelves.title`
|
|
215
|
-
const changedFields = {};
|
|
213
|
+
const changedFields: ChangedFieldsOfChanges = {};
|
|
216
214
|
for (const { action, triple } of changes) {
|
|
217
215
|
const [e, a, v] = triple;
|
|
218
216
|
const field = attrsStore.getAttr(a)?.['forward-identity']?.[2];
|
|
@@ -222,7 +220,6 @@ function changedFieldsOfChanges(
|
|
|
222
220
|
changedFields[e] = fields;
|
|
223
221
|
|
|
224
222
|
const oldNew = fields[field] ?? {};
|
|
225
|
-
fields[field] = oldNew;
|
|
226
223
|
|
|
227
224
|
switch (action) {
|
|
228
225
|
case 'added':
|
|
@@ -235,12 +232,15 @@ function changedFieldsOfChanges(
|
|
|
235
232
|
}
|
|
236
233
|
break;
|
|
237
234
|
}
|
|
235
|
+
|
|
236
|
+
fields[field] = oldNew;
|
|
238
237
|
}
|
|
239
238
|
|
|
240
|
-
for (const
|
|
241
|
-
const { oldValue, newValue }
|
|
242
|
-
|
|
243
|
-
|
|
239
|
+
for (const [_eid, fields] of Object.entries(changedFields)) {
|
|
240
|
+
for (const [k, { oldValue, newValue }] of Object.entries(fields)) {
|
|
241
|
+
if (oldValue === newValue) {
|
|
242
|
+
delete fields[k];
|
|
243
|
+
}
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
return changedFields;
|
|
@@ -783,7 +783,11 @@ export class SyncTable {
|
|
|
783
783
|
updated.push({
|
|
784
784
|
oldEntity: ent.entity,
|
|
785
785
|
newEntity: entity,
|
|
786
|
-
changedFields: changedFields || {}
|
|
786
|
+
changedFields: (changedFields || {}) as SyncTransaction<
|
|
787
|
+
any,
|
|
788
|
+
any,
|
|
789
|
+
any
|
|
790
|
+
>['updated'][number]['changedFields'],
|
|
787
791
|
});
|
|
788
792
|
ent.entity = entity;
|
|
789
793
|
} else {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { User } from './clientTypes.js';
|
|
2
|
+
|
|
3
|
+
type CreateRouteHandlerConfig = {
|
|
4
|
+
appId: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
function createUserSyncResponse(
|
|
8
|
+
user: User | null,
|
|
9
|
+
config: CreateRouteHandlerConfig,
|
|
10
|
+
) {
|
|
11
|
+
if (user && user.refresh_token) {
|
|
12
|
+
return new Response(JSON.stringify({ ok: true }), {
|
|
13
|
+
headers: {
|
|
14
|
+
'Content-Type': 'application/json',
|
|
15
|
+
// 7 day expiry
|
|
16
|
+
'Set-Cookie': `instant_user_${config.appId}=${JSON.stringify(user)}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
return new Response(JSON.stringify({ ok: true }), {
|
|
21
|
+
headers: {
|
|
22
|
+
'Content-Type': 'application/json',
|
|
23
|
+
// remove the cookie (some browsers)
|
|
24
|
+
'Set-Cookie': `instant_user_${config.appId}=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=-1`,
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function errorResponse(status: number, message: string) {
|
|
31
|
+
return new Response(JSON.stringify({ ok: false, error: message }), {
|
|
32
|
+
status,
|
|
33
|
+
headers: { 'Content-Type': 'application/json' },
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const createInstantRouteHandler = (config: CreateRouteHandlerConfig) => {
|
|
38
|
+
return {
|
|
39
|
+
POST: async (req: Request) => {
|
|
40
|
+
let body: { type?: string; appId?: string; user?: User | null };
|
|
41
|
+
try {
|
|
42
|
+
body = await req.json();
|
|
43
|
+
} catch {
|
|
44
|
+
return errorResponse(400, 'Invalid JSON body');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!body.type) {
|
|
48
|
+
return errorResponse(400, 'Missing "type" field');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (body.appId !== config.appId) {
|
|
52
|
+
return errorResponse(403, 'App ID mismatch');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
switch (body.type) {
|
|
56
|
+
case 'sync-user':
|
|
57
|
+
return createUserSyncResponse(body.user ?? null, config);
|
|
58
|
+
default:
|
|
59
|
+
return errorResponse(400, `Unknown type: ${body.type}`);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
};
|
package/src/framework.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
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
|
+
// represents an eventual result from running a query
|
|
20
|
+
// either via ssr or by using the existing websocket connection.
|
|
21
|
+
type QueryPromise =
|
|
22
|
+
| {
|
|
23
|
+
type: 'http';
|
|
24
|
+
triples: any;
|
|
25
|
+
attrs: any;
|
|
26
|
+
queryHash: any;
|
|
27
|
+
query: any;
|
|
28
|
+
pageInfo?: any;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
type: 'session';
|
|
32
|
+
queryResult: any;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export class FrameworkClient {
|
|
36
|
+
private params: FrameworkConfig;
|
|
37
|
+
private db: InstantCoreDatabase<any, any>;
|
|
38
|
+
|
|
39
|
+
// stores all of the query promises so that ssr can read them
|
|
40
|
+
// and send the relevant results alongside the html that resulted in the query resolving
|
|
41
|
+
public resultMap: Map<
|
|
42
|
+
string,
|
|
43
|
+
{
|
|
44
|
+
status: 'pending' | 'success' | 'error';
|
|
45
|
+
type: 'http' | 'session';
|
|
46
|
+
promise?: Promise<QueryPromise> | null;
|
|
47
|
+
data?: any;
|
|
48
|
+
error?: any;
|
|
49
|
+
}
|
|
50
|
+
> = new Map();
|
|
51
|
+
|
|
52
|
+
private queryResolvedCallbacks: ((result: {
|
|
53
|
+
triples: any;
|
|
54
|
+
attrs: any;
|
|
55
|
+
queryHash: any;
|
|
56
|
+
query: any;
|
|
57
|
+
pageInfo?: any;
|
|
58
|
+
}) => void)[] = [];
|
|
59
|
+
|
|
60
|
+
constructor(params: FrameworkConfig) {
|
|
61
|
+
this.params = params;
|
|
62
|
+
this.db = params.db;
|
|
63
|
+
this.resultMap = new Map<
|
|
64
|
+
string,
|
|
65
|
+
{
|
|
66
|
+
type: 'http' | 'session';
|
|
67
|
+
status: 'pending' | 'success' | 'error';
|
|
68
|
+
promise?: Promise<QueryPromise>;
|
|
69
|
+
data?: any;
|
|
70
|
+
error?: any;
|
|
71
|
+
}
|
|
72
|
+
>();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public subscribe = (
|
|
76
|
+
callback: (result: {
|
|
77
|
+
triples: any;
|
|
78
|
+
attrs: any;
|
|
79
|
+
queryHash: string;
|
|
80
|
+
pageInfo?: any;
|
|
81
|
+
}) => void,
|
|
82
|
+
) => {
|
|
83
|
+
this.queryResolvedCallbacks.push(callback);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Runs on the client when ssr gets html script tags
|
|
87
|
+
public addQueryResult = (queryKey: string, value: any) => {
|
|
88
|
+
this.resultMap.set(queryKey, {
|
|
89
|
+
type: value.type,
|
|
90
|
+
status: 'success',
|
|
91
|
+
data: value,
|
|
92
|
+
promise: null,
|
|
93
|
+
error: null,
|
|
94
|
+
});
|
|
95
|
+
// send the result to the client
|
|
96
|
+
if (!isServer) {
|
|
97
|
+
// make sure the attrs are there to create stores
|
|
98
|
+
if (!this.db._reactor.attrs) {
|
|
99
|
+
this.db._reactor._setAttrs(value.attrs);
|
|
100
|
+
}
|
|
101
|
+
this.db._reactor._addQueryData(
|
|
102
|
+
value.query,
|
|
103
|
+
value,
|
|
104
|
+
!!this.db._reactor.config.schema,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// creates an entry in the results map
|
|
110
|
+
// and returns the same thing added to the map
|
|
111
|
+
public query = (
|
|
112
|
+
_query: any,
|
|
113
|
+
opts?: {
|
|
114
|
+
ruleParams: RuleParams;
|
|
115
|
+
},
|
|
116
|
+
): {
|
|
117
|
+
type: 'http' | 'session';
|
|
118
|
+
status: 'pending' | 'success' | 'error';
|
|
119
|
+
promise?: Promise<QueryPromise>;
|
|
120
|
+
data?: any;
|
|
121
|
+
error?: any;
|
|
122
|
+
} => {
|
|
123
|
+
const { hash, query } = this.hashQuery(_query, opts);
|
|
124
|
+
|
|
125
|
+
if (this.db._reactor.status === 'authenticated') {
|
|
126
|
+
const promise = this.db.queryOnce(_query, opts);
|
|
127
|
+
let entry = {
|
|
128
|
+
status: 'pending' as 'pending' | 'success' | 'error',
|
|
129
|
+
type: 'session' as 'http' | 'session',
|
|
130
|
+
data: undefined as any,
|
|
131
|
+
error: undefined as any,
|
|
132
|
+
promise: promise as any,
|
|
133
|
+
};
|
|
134
|
+
promise.then((result) => {
|
|
135
|
+
entry.status = 'success';
|
|
136
|
+
entry.data = result;
|
|
137
|
+
entry.promise = null;
|
|
138
|
+
});
|
|
139
|
+
promise.catch((error) => {
|
|
140
|
+
entry.status = 'error';
|
|
141
|
+
entry.error = error;
|
|
142
|
+
entry.promise = null;
|
|
143
|
+
});
|
|
144
|
+
this.resultMap.set(hash, entry);
|
|
145
|
+
return entry as any;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const promise = this.getTriplesAndAttrsForQuery(query);
|
|
149
|
+
let entry = {
|
|
150
|
+
status: 'pending' as 'pending' | 'success' | 'error',
|
|
151
|
+
type: 'http' as 'http' | 'session',
|
|
152
|
+
data: undefined as any,
|
|
153
|
+
error: undefined as any,
|
|
154
|
+
promise: promise as any,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
promise.then((result) => {
|
|
158
|
+
entry.status = 'success';
|
|
159
|
+
entry.data = result;
|
|
160
|
+
entry.promise = null;
|
|
161
|
+
});
|
|
162
|
+
promise.catch((error) => {
|
|
163
|
+
entry.status = 'error';
|
|
164
|
+
entry.error = error;
|
|
165
|
+
entry.promise = null;
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
promise.then((result) => {
|
|
169
|
+
this.queryResolvedCallbacks.forEach((callback) => {
|
|
170
|
+
callback({
|
|
171
|
+
queryHash: hash,
|
|
172
|
+
query: query,
|
|
173
|
+
attrs: result.attrs,
|
|
174
|
+
triples: result.triples,
|
|
175
|
+
pageInfo: result.pageInfo,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
this.resultMap.set(hash, entry);
|
|
181
|
+
return entry;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
public getExistingResultForQuery = (
|
|
185
|
+
_query: any,
|
|
186
|
+
opts?: {
|
|
187
|
+
ruleParams: RuleParams;
|
|
188
|
+
},
|
|
189
|
+
) => {
|
|
190
|
+
const { hash } = this.hashQuery(_query, opts);
|
|
191
|
+
return this.resultMap.get(hash);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// creates a query result from a set of triples, query, and attrs
|
|
195
|
+
// can be run server side or client side
|
|
196
|
+
public completeIsomorphic = (
|
|
197
|
+
query: any,
|
|
198
|
+
triples: any[],
|
|
199
|
+
attrs: InstantDBAttr[],
|
|
200
|
+
pageInfo?: any,
|
|
201
|
+
) => {
|
|
202
|
+
const attrMap = {};
|
|
203
|
+
attrs.forEach((attr) => {
|
|
204
|
+
attrMap[attr.id] = attr;
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const enableCardinalityInference =
|
|
208
|
+
Boolean(this.db?._reactor?.config?.schema) &&
|
|
209
|
+
('cardinalityInference' in this.db?._reactor?.config
|
|
210
|
+
? Boolean(this.db?._reactor.config?.cardinalityInference)
|
|
211
|
+
: true);
|
|
212
|
+
|
|
213
|
+
const attrsStore = new s.AttrsStoreClass(
|
|
214
|
+
attrs.reduce((acc, attr) => {
|
|
215
|
+
acc[attr.id] = attr;
|
|
216
|
+
return acc;
|
|
217
|
+
}, {}),
|
|
218
|
+
createLinkIndex(this.db?._reactor.config.schema),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const store = s.createStore(
|
|
222
|
+
attrsStore,
|
|
223
|
+
triples,
|
|
224
|
+
enableCardinalityInference,
|
|
225
|
+
this.params.db._reactor.config.useDateObjects || false,
|
|
226
|
+
);
|
|
227
|
+
const resp = instaql(
|
|
228
|
+
{
|
|
229
|
+
store: store,
|
|
230
|
+
attrsStore: attrsStore,
|
|
231
|
+
pageInfo: pageInfo,
|
|
232
|
+
aggregate: undefined,
|
|
233
|
+
},
|
|
234
|
+
query,
|
|
235
|
+
);
|
|
236
|
+
return resp;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
public hashQuery = (
|
|
240
|
+
_query: any,
|
|
241
|
+
opts?: {
|
|
242
|
+
ruleParams: RuleParams;
|
|
243
|
+
},
|
|
244
|
+
): { hash: string; query: any } => {
|
|
245
|
+
if (_query && opts && 'ruleParams' in opts) {
|
|
246
|
+
_query = { $$ruleParams: opts['ruleParams'], ..._query };
|
|
247
|
+
}
|
|
248
|
+
const query = _query ? coerceQuery(_query) : null;
|
|
249
|
+
return { hash: weakHash(query), query: query };
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// Run by the server to get triples and attrs
|
|
253
|
+
public getTriplesAndAttrsForQuery = async (
|
|
254
|
+
query: any,
|
|
255
|
+
): Promise<{
|
|
256
|
+
triples: any[];
|
|
257
|
+
attrs: InstantDBAttr[];
|
|
258
|
+
query: any;
|
|
259
|
+
queryHash: string;
|
|
260
|
+
type: 'http';
|
|
261
|
+
pageInfo?: any;
|
|
262
|
+
}> => {
|
|
263
|
+
try {
|
|
264
|
+
const response = await fetch(
|
|
265
|
+
`${this.db._reactor.config.apiURI}/runtime/framework/query`,
|
|
266
|
+
{
|
|
267
|
+
method: 'POST',
|
|
268
|
+
headers: {
|
|
269
|
+
'app-id': this.params.db._reactor.config.appId,
|
|
270
|
+
'Content-Type': 'application/json',
|
|
271
|
+
Authorization: this.params.token
|
|
272
|
+
? `Bearer ${this.params.token}`
|
|
273
|
+
: undefined,
|
|
274
|
+
} as Record<string, string>,
|
|
275
|
+
body: JSON.stringify({
|
|
276
|
+
query: query,
|
|
277
|
+
}),
|
|
278
|
+
},
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
if (!response.ok) {
|
|
282
|
+
throw new Error('Error getting triples from server');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const data = await response.json();
|
|
286
|
+
|
|
287
|
+
const attrs = data?.attrs;
|
|
288
|
+
if (!attrs) {
|
|
289
|
+
throw new Error('No attrs');
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// TODO: make safer
|
|
293
|
+
const triples =
|
|
294
|
+
data.result?.[0].data?.['datalog-result']?.['join-rows'][0];
|
|
295
|
+
|
|
296
|
+
const pageInfo = data.result?.[0]?.data?.['page-info'];
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
attrs,
|
|
300
|
+
triples,
|
|
301
|
+
type: 'http',
|
|
302
|
+
queryHash: this.hashQuery(query).hash,
|
|
303
|
+
query,
|
|
304
|
+
pageInfo,
|
|
305
|
+
};
|
|
306
|
+
} catch (err: any) {
|
|
307
|
+
const errWithMessage = new Error(
|
|
308
|
+
'Error getting triples from framework client',
|
|
309
|
+
);
|
|
310
|
+
// @ts-expect-error pre es2022
|
|
311
|
+
errWithMessage.cause = err;
|
|
312
|
+
throw errWithMessage;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -20,10 +20,13 @@ import {
|
|
|
20
20
|
validateTransactions,
|
|
21
21
|
TransactionValidationError,
|
|
22
22
|
} from './transactionValidation.ts';
|
|
23
|
+
|
|
23
24
|
import {
|
|
24
25
|
StorageInterface,
|
|
25
26
|
type StorageInterfaceStoreName,
|
|
26
27
|
} from './utils/PersistedObject.ts';
|
|
28
|
+
import { createInstantRouteHandler } from './createRouteHandler.ts';
|
|
29
|
+
import { parseSchemaFromJSON } from './parseSchemaFromJSON.ts';
|
|
27
30
|
|
|
28
31
|
import type {
|
|
29
32
|
PresenceOpts,
|
|
@@ -103,6 +106,7 @@ import type {
|
|
|
103
106
|
} from './schemaTypes.ts';
|
|
104
107
|
import type { InstantRules } from './rulesTypes.ts';
|
|
105
108
|
import type { UploadFileResponse, DeleteFileResponse } from './StorageAPI.ts';
|
|
109
|
+
import { FrameworkClient, type FrameworkConfig } from './framework.ts';
|
|
106
110
|
|
|
107
111
|
import type {
|
|
108
112
|
ExchangeCodeForTokenParams,
|
|
@@ -153,6 +157,7 @@ export type InstantConfig<
|
|
|
153
157
|
appId: string;
|
|
154
158
|
schema?: S;
|
|
155
159
|
websocketURI?: string;
|
|
160
|
+
firstPartyPath?: string;
|
|
156
161
|
apiURI?: string;
|
|
157
162
|
devtool?: boolean | DevtoolConfig;
|
|
158
163
|
verbose?: boolean;
|
|
@@ -903,7 +908,9 @@ export {
|
|
|
903
908
|
validateQuery,
|
|
904
909
|
QueryValidationError,
|
|
905
910
|
validateTransactions,
|
|
911
|
+
parseSchemaFromJSON,
|
|
906
912
|
TransactionValidationError,
|
|
913
|
+
FrameworkClient,
|
|
907
914
|
|
|
908
915
|
// error
|
|
909
916
|
InstantAPIError,
|
|
@@ -1019,6 +1026,7 @@ export {
|
|
|
1019
1026
|
|
|
1020
1027
|
// SSE
|
|
1021
1028
|
type EventSourceType,
|
|
1029
|
+
type FrameworkConfig,
|
|
1022
1030
|
|
|
1023
1031
|
// sync table types
|
|
1024
1032
|
type SyncTableCallback,
|
|
@@ -1035,4 +1043,5 @@ export {
|
|
|
1035
1043
|
// storage (e.g. indexeddb) interface
|
|
1036
1044
|
StorageInterface,
|
|
1037
1045
|
type StorageInterfaceStoreName,
|
|
1046
|
+
createInstantRouteHandler,
|
|
1038
1047
|
};
|