Skip to content

Commit 4429af3

Browse files
committed
Fixed to useQuery
1 parent e884109 commit 4429af3

File tree

4 files changed

+97
-3
lines changed

4 files changed

+97
-3
lines changed

client/packages/core/src/instaql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ function formatPageInfo(pageInfo) {
730730

731731
export default function query({ store, pageInfo, aggregate }, q) {
732732
const data = Object.keys(q).reduce(function reduceResult(res, k) {
733-
if (aggregate?.[k]) {
733+
if (aggregate?.[k] || '$$ruleParams' === k) {
734734
// Aggregate doesn't return any join rows and has no children,
735735
// so don't bother querying further
736736
return res;

client/packages/react/src/InstantReactAbstractDatabase.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type PresenceResponse,
1212
type RoomSchemaShape,
1313
type InstaQLParams,
14+
type InstaQLOptions,
1415
type InstantConfig,
1516
type PageInfoResponse,
1617
InstantCoreDatabase,
@@ -155,8 +156,9 @@ export default abstract class InstantReactAbstractDatabase<
155156
*/
156157
useQuery = <Q extends InstaQLParams<Schema>>(
157158
query: null | Q,
159+
opts?: InstaQLOptions,
158160
): InstaQLLifecycleState<Schema, Q> => {
159-
return useQueryInternal(this._core, query).state;
161+
return useQueryInternal(this._core, query, opts).state;
160162
};
161163

162164
/**
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { i, id, init, tx } from '@instantdb/react';
2+
import { useEffect } from 'react';
3+
import config from '../../config';
4+
5+
const schema = i.schema({
6+
entities: {
7+
playDocs: i.entity({
8+
title: i.string(),
9+
secret: i.string(),
10+
}),
11+
},
12+
});
13+
14+
const db = init({ ...config, schema });
15+
16+
const secrets = ['one', 'two', 'three'];
17+
18+
function randInt(min: number, max: number) {
19+
return Math.floor(Math.random() * (max - min + 1)) + min;
20+
}
21+
22+
function addDoc() {
23+
return db.transact(
24+
tx.playDocs[id()].update({
25+
title: 'doc ' + randInt(10000, 99999),
26+
secret: secrets[randInt(0, 2)],
27+
}),
28+
);
29+
}
30+
31+
function DocList({ q }) {
32+
if (q.isLoading) {
33+
return <div>Loading...</div>;
34+
}
35+
36+
if (q.error) {
37+
return <div>Error: {q.error.message}</div>;
38+
}
39+
40+
return (
41+
<ul className="pl-4 list-disk">
42+
{q.data.playDocs.map((doc) => {
43+
return (
44+
<li className="list-disk">
45+
'{doc.title}', secret: '{doc.secret}'
46+
</li>
47+
);
48+
})}
49+
</ul>
50+
);
51+
}
52+
53+
function Main() {
54+
const queries = secrets.map((secret) => [
55+
secret,
56+
db.useQuery({ playDocs: {} }, { ruleParams: { secret } }),
57+
]);
58+
59+
return (
60+
<div className="p-1">
61+
<button
62+
className="px-4 py-2 bg-slate-500 text-white rounded"
63+
onClick={addDoc}
64+
>
65+
New doc
66+
</button>
67+
{queries.map(([secret, q]) => {
68+
return (
69+
<>
70+
<div>Docs for {secret}:</div>
71+
<DocList q={q} />
72+
</>
73+
);
74+
})}
75+
</div>
76+
);
77+
}
78+
79+
function App() {
80+
return <Main />;
81+
}
82+
83+
export default App;
84+
85+
// copy this to dashboard
86+
const rules = {
87+
playDocs: {
88+
allow: {
89+
view: 'ruleParams.secret == data.secret',
90+
},
91+
},
92+
};

server/src/tool.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
form))
263263
form)]
264264
(locking p-lock
265-
(println (str "#p " form " " position "\n=> " (pprint res))))
265+
(println (str "\033[90m#p " form " " position "\n=>\033[0m " (pprint res))))
266266
res))
267267

268268
(defn p

0 commit comments

Comments
 (0)