Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 89e1e8e

Browse files
committed
fix!: correct implementation of queryKey factory and internal usages of it
1 parent dae12c3 commit 89e1e8e

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

.changeset/hot-bobcats-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-query-pocketbase': patch
3+
---
4+
5+
(breaking) fix!: correct implementation of queryKey factory and internal usages of it

src/lib/queries/collection.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const createCollectionQueryPrefetch = <
6161
queryParams = undefined,
6262
queryKey = collectionKeys({
6363
collection,
64-
...(queryParams && { queryParams })
64+
...(queryParams && queryParams)
6565
}) as unknown as TQueryKey,
6666
...options
6767
}: QueryPrefetchOptions<Array<T>, ClientResponseError, Array<T>, TQueryKey> = {}
@@ -100,7 +100,7 @@ export const createCollectionQuery = <
100100
queryParams = undefined,
101101
queryKey = collectionKeys({
102102
collection,
103-
...(queryParams && { queryParams })
103+
...(queryParams && queryParams)
104104
}) as unknown as TQueryKey,
105105
enabled = true,
106106
disableRealtime = false,
@@ -132,7 +132,7 @@ export const createCollectionQuery = <
132132
)
133133
.then((r) => {
134134
console.log(
135-
`(C) [${JSON.stringify(queryKey)}]: updating with realtime action:`,
135+
`(C) ${JSON.stringify(queryKey)}: updating with realtime action:`,
136136
data.action,
137137
data.record.id
138138
);
@@ -146,7 +146,7 @@ export const createCollectionQuery = <
146146
})
147147
.catch((e) => {
148148
console.log(
149-
`(C) [${JSON.stringify(queryKey)}]: invalidating query due to callback error:`,
149+
`(C) ${JSON.stringify(queryKey)}: invalidating query due to callback error:`,
150150
e
151151
);
152152
if (invalidateQueryOnRealtimeError) {
@@ -170,10 +170,10 @@ export const createCollectionQuery = <
170170

171171
return {
172172
subscribe: (...args) => {
173-
console.log(`(C) [${JSON.stringify(queryKey)}]: subscribing to changes...`);
173+
console.log(`(C) ${JSON.stringify(queryKey)}: subscribing to changes...`);
174174
let unsubscriber = store.subscribe(...args);
175175
return () => {
176-
console.log(`(C) [${JSON.stringify(queryKey)}]: unsubscribing from store.`);
176+
console.log(`(C) ${JSON.stringify(queryKey)}: unsubscribing from store.`);
177177
(async () => {
178178
await (
179179
await unsubscribePromise
@@ -184,7 +184,7 @@ export const createCollectionQuery = <
184184
)
185185
) {
186186
console.log(
187-
`(C) [${JSON.stringify(queryKey)}]: no realtime listeners, marking query as stale.`
187+
`(C) ${JSON.stringify(queryKey)}: no realtime listeners, marking query as stale.`
188188
);
189189
queryClient.invalidateQueries({ queryKey, exact: true });
190190
}

src/lib/queries/infinite-collection.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ export const infiniteCollectionQueryPrefetch = <
144144
queryParams = undefined,
145145
queryKey = collectionKeys({
146146
collection,
147+
...(queryParams && queryParams),
147148
...(page && { page }),
148-
...(perPage && { perPage }),
149-
...(queryParams && { queryParams })
149+
...(perPage && { perPage })
150150
}) as unknown as TQueryKey,
151151
...options
152152
}: InfiniteQueryPrefetchOptions<ListResult<T>, ClientResponseError, ListResult<T>, TQueryKey> = {}
@@ -171,9 +171,9 @@ export const createInfiniteCollectionQuery = <
171171
queryParams = undefined,
172172
queryKey = collectionKeys({
173173
collection,
174+
...(queryParams && queryParams),
174175
...(page && { page }),
175-
...(perPage && { perPage }),
176-
...(queryParams && { queryParams })
176+
...(perPage && { perPage })
177177
}) as unknown as TQueryKey,
178178
enabled = true,
179179
disableRealtime = false,
@@ -257,7 +257,7 @@ export const createInfiniteCollectionQuery = <
257257
)
258258
.then((r) => {
259259
console.log(
260-
`(IC) [${JSON.stringify(queryKey)}]: updating with realtime action:`,
260+
`(IC) ${JSON.stringify(queryKey)}: updating with realtime action:`,
261261
data.action,
262262
data.record.id
263263
);
@@ -271,7 +271,7 @@ export const createInfiniteCollectionQuery = <
271271
})
272272
.catch((e) => {
273273
console.log(
274-
`(IC) [${JSON.stringify(queryKey)}]: invalidating query due to callback error:`,
274+
`(IC) ${JSON.stringify(queryKey)}: invalidating query due to callback error:`,
275275
e
276276
);
277277
if (invalidateQueryOnRealtimeError) {
@@ -295,10 +295,10 @@ export const createInfiniteCollectionQuery = <
295295

296296
return {
297297
subscribe: (...args) => {
298-
console.log(`(IC) [${JSON.stringify(queryKey)}]: subscribing to changes...`);
298+
console.log(`(IC) ${JSON.stringify(queryKey)}: subscribing to changes...`);
299299
let unsubscriber = store.subscribe(...args);
300300
return () => {
301-
console.log(`(IC) [${JSON.stringify(queryKey)}]: unsubscribing from store.`);
301+
console.log(`(IC) ${JSON.stringify(queryKey)}: unsubscribing from store.`);
302302
(async () => {
303303
await (
304304
await unsubscribePromise
@@ -309,7 +309,7 @@ export const createInfiniteCollectionQuery = <
309309
)
310310
) {
311311
console.log(
312-
`(IC) [${JSON.stringify(queryKey)}]: no realtime listeners, marking query as stale.`
312+
`(IC) ${JSON.stringify(queryKey)}: no realtime listeners, marking query as stale.`
313313
);
314314
queryClient.invalidateQueries({ queryKey, exact: true });
315315
}

src/lib/queries/record.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const createRecordQueryPrefetch = <
5454
queryKey = collectionKeys({
5555
collection,
5656
id,
57-
...(queryParams && { queryParams })
57+
...(queryParams && queryParams)
5858
}) as unknown as TQueryKey,
5959
...options
6060
}: QueryPrefetchOptions<T, ClientResponseError, T, TQueryKey> = {}
@@ -91,7 +91,7 @@ export const createRecordQuery = <
9191
queryKey = collectionKeys({
9292
collection,
9393
id,
94-
...(queryParams && { queryParams })
94+
...(queryParams && queryParams)
9595
}) as unknown as TQueryKey,
9696
enabled = true,
9797
disableRealtime = false,
@@ -129,15 +129,15 @@ export const createRecordQuery = <
129129
)
130130
.then((r) => {
131131
console.log(
132-
`(R) [${JSON.stringify(queryKey)}]: updating with realtime action:`,
132+
`(R) ${JSON.stringify(queryKey)}: updating with realtime action:`,
133133
data.action,
134134
data.record.id
135135
);
136136
queryClient.setQueryData<T | null>(queryKey, () => r);
137137
})
138138
.catch((e) => {
139139
console.log(
140-
`(R) [${JSON.stringify(queryKey)}]: invalidating query due to callback error:`,
140+
`(R) ${JSON.stringify(queryKey)}: invalidating query due to callback error:`,
141141
e
142142
);
143143
if (invalidateQueryOnRealtimeError) {
@@ -161,10 +161,10 @@ export const createRecordQuery = <
161161

162162
return {
163163
subscribe: (...args) => {
164-
console.log(`(R) [${JSON.stringify(queryKey)}]: subscribing to changes...`);
164+
console.log(`(R) ${JSON.stringify(queryKey)}: subscribing to changes...`);
165165
let unsubscriber = store.subscribe(...args);
166166
return () => {
167-
console.log(`(R) [${JSON.stringify(queryKey)}]: unsubscribing from store.`);
167+
console.log(`(R) ${JSON.stringify(queryKey)}: unsubscribing from store.`);
168168
(async () => {
169169
await (
170170
await unsubscribePromise
@@ -175,7 +175,7 @@ export const createRecordQuery = <
175175
)
176176
) {
177177
console.log(
178-
`(R) [${JSON.stringify(queryKey)}]: no realtime listeners, marking query as stale.`
178+
`(R) ${JSON.stringify(queryKey)}: no realtime listeners, marking query as stale.`
179179
);
180180
queryClient.invalidateQueries({ queryKey, exact: true });
181181
}

src/lib/query-key-factory.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ import type Client from 'pocketbase';
44
export const collectionKeys = ({
55
collection,
66
id = '*',
7-
page,
8-
perPage,
9-
sort,
10-
filter,
117
...queryParams
128
}: {
139
collection: ReturnType<Client['collection']>;
1410
id?: string;
1511
} & RecordListQueryParams) => {
16-
if (Object.keys(queryParams).length > 0) {
17-
return [
18-
{
19-
collection: collection.collectionIdOrName,
20-
id,
21-
...(page && { page }),
22-
...(perPage && { perPage }),
23-
...(sort && { sort }),
24-
...(filter && { filter })
25-
}
26-
] as const;
27-
} else {
28-
return [{ collection: collection.collectionIdOrName, id }] as const;
29-
}
12+
return [
13+
{
14+
collection: collection.collectionIdOrName,
15+
id,
16+
...(queryParams && queryParams)
17+
}
18+
] as const;
3019
};

src/routes/+page.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
77
const pocketbase = new Pocketbase('https://voel.local');
88
9-
const query = createInfiniteCollectionQuery(pocketbase.collection('test'), { page: 2 });
9+
const query = createCollectionQuery(pocketbase.collection('test'), {
10+
queryParams: { filter: '' }
11+
});
1012
1113
// onMount(() => {
1214
// setTimeout(() => {

0 commit comments

Comments
 (0)