@instantdb/core 0.22.89 → 0.22.90-experimental.drewh-ssr.20286580593.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/Reactor.test.js +1 -1
- package/__tests__/src/instaml.test.ts +1 -1
- package/__tests__/src/serializeSchema.test.ts +123 -0
- package/__tests__/src/store.test.ts +1 -1
- package/__tests__/src/transactionValidation.test.ts +1 -1
- package/dist/commonjs/Reactor.d.ts +13 -1
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +79 -15
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/SyncTable.js +3 -3
- 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 +57 -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 +209 -0
- package/dist/commonjs/framework.js.map +1 -0
- package/dist/commonjs/index.d.ts +6 -2
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +9 -3
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/instaml.js +8 -8
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.js +2 -2
- package/dist/commonjs/instaql.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/commonjs/utils/{uuid.d.ts → id.d.ts} +1 -1
- package/dist/commonjs/utils/id.d.ts.map +1 -0
- package/dist/commonjs/utils/{uuid.js → id.js} +1 -1
- package/dist/commonjs/utils/id.js.map +1 -0
- package/dist/esm/Reactor.d.ts +13 -1
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +70 -6
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/SyncTable.js +1 -1
- 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 +53 -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 +169 -0
- package/dist/esm/framework.js.map +1 -0
- package/dist/esm/index.d.ts +6 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/instaml.js +1 -1
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.js +1 -1
- package/dist/esm/instaql.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/esm/utils/{uuid.d.ts → id.d.ts} +1 -1
- package/dist/esm/utils/id.d.ts.map +1 -0
- package/dist/esm/utils/{uuid.js → id.js} +1 -1
- package/dist/esm/utils/id.js.map +1 -0
- package/dist/standalone/index.js +2697 -2377
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +85 -7
- package/src/SyncTable.ts +1 -1
- package/src/createRouteHandler.ts +44 -0
- package/src/framework.ts +294 -0
- package/src/index.ts +10 -1
- package/src/instaml.ts +1 -1
- package/src/instaql.ts +1 -1
- package/src/parseSchemaFromJSON.ts +176 -0
- package/src/reactorTypes.ts +5 -4
- package/dist/commonjs/utils/uuid.d.ts.map +0 -1
- package/dist/commonjs/utils/uuid.js.map +0 -1
- package/dist/esm/utils/uuid.d.ts.map +0 -1
- package/dist/esm/utils/uuid.js.map +0 -1
- /package/src/utils/{uuid.ts → id.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instantdb/core",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.90-experimental.drewh-ssr.20286580593.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.90-experimental.drewh-ssr.20286580593.1"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"test": "vitest",
|
package/src/Reactor.js
CHANGED
|
@@ -3,7 +3,7 @@ import weakHash from './utils/weakHash.ts';
|
|
|
3
3
|
import instaql from './instaql.ts';
|
|
4
4
|
import * as instaml from './instaml.ts';
|
|
5
5
|
import * as s from './store.ts';
|
|
6
|
-
import uuid from './utils/
|
|
6
|
+
import uuid from './utils/id.ts';
|
|
7
7
|
import IndexedDBStorage from './IndexedDBStorage.ts';
|
|
8
8
|
import WindowNetworkListener from './WindowNetworkListener.js';
|
|
9
9
|
import * as authAPI from './authAPI.ts';
|
|
@@ -348,7 +348,17 @@ 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(
|
|
356
|
+
async () => {
|
|
357
|
+
const currentUser = await this.getCurrentUser();
|
|
358
|
+
this.syncUserToEndpoint(currentUser.user);
|
|
359
|
+
},
|
|
360
|
+
1000 * 60 * 20,
|
|
361
|
+
);
|
|
352
362
|
|
|
353
363
|
NetworkListener.getIsOnline().then((isOnline) => {
|
|
354
364
|
this._isOnline = isOnline;
|
|
@@ -549,6 +559,43 @@ export default class Reactor {
|
|
|
549
559
|
}
|
|
550
560
|
}
|
|
551
561
|
|
|
562
|
+
/**
|
|
563
|
+
* Does the same thing as add-query-ok
|
|
564
|
+
* but called as a result of receiving query info from ssr
|
|
565
|
+
* @param {any} q
|
|
566
|
+
* @param {{ triples: any; pageInfo: any; }} result
|
|
567
|
+
* @param {boolean} enableCardinalityInference
|
|
568
|
+
*/
|
|
569
|
+
_addQueryData(q, result, enableCardinalityInference) {
|
|
570
|
+
if (!this.attrs) {
|
|
571
|
+
throw new Error('Attrs in reactor have not been set');
|
|
572
|
+
}
|
|
573
|
+
const queryHash = weakHash(q);
|
|
574
|
+
const attrsStore = this.ensureAttrs();
|
|
575
|
+
const store = s.createStore(
|
|
576
|
+
this.attrs,
|
|
577
|
+
result.triples,
|
|
578
|
+
enableCardinalityInference,
|
|
579
|
+
this.config.useDateObjects,
|
|
580
|
+
);
|
|
581
|
+
this.querySubs.updateInPlace((prev) => {
|
|
582
|
+
prev[queryHash] = {
|
|
583
|
+
result: {
|
|
584
|
+
store,
|
|
585
|
+
attrsStore,
|
|
586
|
+
pageInfo: result.pageInfo,
|
|
587
|
+
processedTxId: undefined,
|
|
588
|
+
isExternal: true,
|
|
589
|
+
},
|
|
590
|
+
q,
|
|
591
|
+
};
|
|
592
|
+
});
|
|
593
|
+
this._cleanupPendingMutationsQueries();
|
|
594
|
+
this.notifyOne(queryHash);
|
|
595
|
+
this.notifyOneQueryOnce(queryHash);
|
|
596
|
+
this._cleanupPendingMutationsTimeout();
|
|
597
|
+
}
|
|
598
|
+
|
|
552
599
|
_handleReceive(connId, msg) {
|
|
553
600
|
// opt-out, enabled by default if schema
|
|
554
601
|
const enableCardinalityInference =
|
|
@@ -1221,7 +1268,7 @@ export default class Reactor {
|
|
|
1221
1268
|
}
|
|
1222
1269
|
|
|
1223
1270
|
/** Runs instaql on a query and a store */
|
|
1224
|
-
dataForQuery(hash) {
|
|
1271
|
+
dataForQuery(hash, applyOptimistic = true) {
|
|
1225
1272
|
const errorMessage = this._errorMessage;
|
|
1226
1273
|
if (errorMessage) {
|
|
1227
1274
|
return { error: errorMessage };
|
|
@@ -1245,15 +1292,26 @@ export default class Reactor {
|
|
|
1245
1292
|
return cached;
|
|
1246
1293
|
}
|
|
1247
1294
|
|
|
1248
|
-
|
|
1295
|
+
let store = result.store;
|
|
1296
|
+
let attrsStore = result.attrsStore;
|
|
1297
|
+
const { pageInfo, aggregate, processedTxId } = result;
|
|
1249
1298
|
const mutations = this._rewriteMutationsSorted(
|
|
1250
1299
|
attrsStore,
|
|
1251
1300
|
pendingMutations,
|
|
1252
1301
|
);
|
|
1253
|
-
|
|
1254
|
-
this._applyOptimisticUpdates(
|
|
1302
|
+
if (applyOptimistic) {
|
|
1303
|
+
const optimisticResult = this._applyOptimisticUpdates(
|
|
1304
|
+
store,
|
|
1305
|
+
attrsStore,
|
|
1306
|
+
mutations,
|
|
1307
|
+
processedTxId,
|
|
1308
|
+
);
|
|
1309
|
+
|
|
1310
|
+
store = optimisticResult.store;
|
|
1311
|
+
attrsStore = optimisticResult.attrsStore;
|
|
1312
|
+
}
|
|
1255
1313
|
const resp = instaql(
|
|
1256
|
-
{ store:
|
|
1314
|
+
{ store: store, attrsStore: attrsStore, pageInfo, aggregate },
|
|
1257
1315
|
q,
|
|
1258
1316
|
);
|
|
1259
1317
|
|
|
@@ -1989,7 +2047,27 @@ export default class Reactor {
|
|
|
1989
2047
|
}
|
|
1990
2048
|
}
|
|
1991
2049
|
|
|
2050
|
+
async syncUserToEndpoint(user) {
|
|
2051
|
+
if (this.config.cookieEndpoint) {
|
|
2052
|
+
try {
|
|
2053
|
+
fetch(this.config.cookieEndpoint + '/sync-auth', {
|
|
2054
|
+
method: 'POST',
|
|
2055
|
+
body: JSON.stringify({
|
|
2056
|
+
user: user,
|
|
2057
|
+
}),
|
|
2058
|
+
headers: {
|
|
2059
|
+
'Content-Type': 'application/json',
|
|
2060
|
+
},
|
|
2061
|
+
});
|
|
2062
|
+
} catch (error) {
|
|
2063
|
+
console.error('Error syncing user with external endpoint', error);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
|
|
1992
2068
|
updateUser(newUser) {
|
|
2069
|
+
this.syncUserToEndpoint(newUser);
|
|
2070
|
+
|
|
1993
2071
|
const newV = { error: undefined, user: newUser };
|
|
1994
2072
|
this._currentUserCached = { isLoading: false, ...newV };
|
|
1995
2073
|
this._dataForQueryCache = {};
|
package/src/SyncTable.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PersistedObject } from './utils/PersistedObject.ts';
|
|
2
2
|
import * as s from './store.ts';
|
|
3
3
|
import weakHash from './utils/weakHash.ts';
|
|
4
|
-
import uuid from './utils/
|
|
4
|
+
import uuid from './utils/id.ts';
|
|
5
5
|
import { Logger } from './Reactor.js';
|
|
6
6
|
import instaql, { compareOrder } from './instaql.ts';
|
|
7
7
|
import { InstaQLResponse, ValidQuery } from './queryTypes.ts';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const createInstantRouteHandler = (config: {
|
|
2
|
+
appId: string;
|
|
3
|
+
apiURI?: string;
|
|
4
|
+
}) => {
|
|
5
|
+
async function handleUserSync(req: Request) {
|
|
6
|
+
const body = await req.json();
|
|
7
|
+
if (body.user && body.user.refresh_token) {
|
|
8
|
+
return new Response('sync', {
|
|
9
|
+
headers: {
|
|
10
|
+
// 7 day expiry
|
|
11
|
+
'Set-Cookie': `instant_user=${JSON.stringify(body.user)}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
} else {
|
|
15
|
+
return new Response('sync', {
|
|
16
|
+
headers: {
|
|
17
|
+
// remove the cookie (some browsers)
|
|
18
|
+
'Set-Cookie': `instant_user=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=-1`,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
GET: async (_req: Request) => {
|
|
26
|
+
return new Response('Method not allowed', {
|
|
27
|
+
status: 405,
|
|
28
|
+
statusText: 'Method Not Allowed',
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
POST: async (req: Request) => {
|
|
32
|
+
const url = new URL(req.url);
|
|
33
|
+
const pathname = url.pathname;
|
|
34
|
+
const route = pathname.split('/')[pathname.split('/').length - 1];
|
|
35
|
+
switch (route) {
|
|
36
|
+
case 'sync-auth':
|
|
37
|
+
return await handleUserSync(req);
|
|
38
|
+
}
|
|
39
|
+
return new Response('Route not found', {
|
|
40
|
+
status: 404,
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
};
|
package/src/framework.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type TransactionChunk,
|
|
9
9
|
} from './instatx.js';
|
|
10
10
|
import weakHash from './utils/weakHash.js';
|
|
11
|
-
import id from './utils/
|
|
11
|
+
import id from './utils/id.ts';
|
|
12
12
|
import IndexedDBStorage from './IndexedDBStorage.ts';
|
|
13
13
|
import { coerceToDate } from './utils/dates.js';
|
|
14
14
|
import WindowNetworkListener from './WindowNetworkListener.js';
|
|
@@ -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
|
+
cookieEndpoint?: string;
|
|
156
161
|
apiURI?: string;
|
|
157
162
|
devtool?: boolean | DevtoolConfig;
|
|
158
163
|
verbose?: boolean;
|
|
@@ -902,7 +907,9 @@ export {
|
|
|
902
907
|
validateQuery,
|
|
903
908
|
QueryValidationError,
|
|
904
909
|
validateTransactions,
|
|
910
|
+
parseSchemaFromJSON,
|
|
905
911
|
TransactionValidationError,
|
|
912
|
+
FrameworkClient,
|
|
906
913
|
|
|
907
914
|
// error
|
|
908
915
|
InstantAPIError,
|
|
@@ -1018,6 +1025,7 @@ export {
|
|
|
1018
1025
|
|
|
1019
1026
|
// SSE
|
|
1020
1027
|
type EventSourceType,
|
|
1028
|
+
type FrameworkConfig,
|
|
1021
1029
|
|
|
1022
1030
|
// sync table types
|
|
1023
1031
|
type SyncTableCallback,
|
|
@@ -1034,4 +1042,5 @@ export {
|
|
|
1034
1042
|
// storage (e.g. indexeddb) interface
|
|
1035
1043
|
StorageInterface,
|
|
1036
1044
|
type StorageInterfaceStoreName,
|
|
1045
|
+
createInstantRouteHandler,
|
|
1037
1046
|
};
|
package/src/instaml.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import { getOps, isLookup, parseLookup } from './instatx.ts';
|
|
10
10
|
import { immutableRemoveUndefined } from './utils/object.js';
|
|
11
11
|
import { coerceToDate } from './utils/dates.ts';
|
|
12
|
-
import uuid from './utils/
|
|
12
|
+
import uuid from './utils/id.ts';
|
|
13
13
|
import {
|
|
14
14
|
EntitiesWithLinks,
|
|
15
15
|
IContainEntitiesAndLinks,
|
package/src/instaql.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { query as datalogQuery } from './datalog.js';
|
|
2
|
-
import { uuidCompare } from './utils/
|
|
2
|
+
import { uuidCompare } from './utils/id.ts';
|
|
3
3
|
import { stringCompare } from './utils/strings.ts';
|
|
4
4
|
import * as s from './store.ts';
|
|
5
5
|
import { InstantDBAttr } from './attrTypes.ts';
|