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

Commit f5c1603

Browse files
committed
feat: add support for the optional fields introduced in v0.15.0 of the pocketbase sdk
1 parent f17e3e9 commit f5c1603

File tree

10 files changed

+75
-31
lines changed

10 files changed

+75
-31
lines changed

.changeset/witty-doors-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-query-pocketbase': minor
3+
---
4+
5+
feat: add support for the optional fields introduced in v0.15.0 of the pocketbase sdk

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ Creates a TanStack Query that updates a Pocketbase record in realtime. View the
7272
const someRecord = createRecordQuery<SomeCollectionResponse>(
7373
pocketbase.collection(Collections.SomeCollection),
7474
'some_id',
75-
{ queryParams: { expand: 'some_field' } }
75+
{
76+
queryParams: {
77+
expand: 'some_field',
78+
fields: 'some_field' // the library will internally add id and updated to this
79+
}
80+
}
7681
);
7782
</script>
7883
@@ -287,7 +292,8 @@ Creates a TanStack Query that updates an array of Pocketbase records in realtime
287292
queryParams: {
288293
expand: 'some_field',
289294
sort: '-created', // sort by date created, descending
290-
filter: 'created >= "2022-01-01 00:00:00"'
295+
filter: 'created >= "2022-01-01 00:00:00"',
296+
fields: 'some_field' // the library will internally add id and updated to this
291297
},
292298
// sortFunction and filterFunction are applied after a realtime update is applied
293299
sortFunction: (a, b) => new Date(a.created) - new Date(b.created) // sort by date created, descending
@@ -515,7 +521,8 @@ Creates a TanStack Infinite Query that updates paginated Pocketbase records in r
515521
queryParams: {
516522
expand: 'some_field',
517523
sort: '-created', // sort by date created, descending
518-
filter: 'created >= "2022-01-01 00:00:00"'
524+
filter: 'created >= "2022-01-01 00:00:00"',
525+
fields: 'some_field' // the library will internally add id and updated to this
519526
},
520527
// sortFunction and filterFunction are applied after a realtime update is applied
521528
sortFunction: (a, b) => new Date(a.created) - new Date(b.created) // sort by date created, descending

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"dependencies": {
2020
"@tanstack/svelte-query": "^4.22.2",
2121
"immer": "^9.0.19",
22-
"pocketbase": "^0.11.1 || ^0.12.0 || ^0.13.0 || ^0.14.0",
22+
"pocketbase": "^0.11.1 || ^0.12.0 || ^0.13.0 || ^0.14.0 || ^0.15.0",
2323
"svelte": "^3.54.0"
2424
},
2525
"devDependencies": {

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/internal/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Record, RecordService } from 'pocketbase';
1+
import type { BaseQueryParams, Record, RecordService } from 'pocketbase';
22

33
/**
44
* Meant for internal use, simply returns the expanded record if there is an expand query param.
@@ -16,3 +16,28 @@ export const realtimeStoreExpand = async <T extends Pick<Record, 'id'>>(
1616
expand
1717
? await collection.getOne<T>(record.id, typeof expand === 'string' ? { expand } : undefined)
1818
: record;
19+
20+
export const reconcileOptionalFields = (queryParams: BaseQueryParams | undefined) => {
21+
if (
22+
queryParams &&
23+
'fields' in queryParams &&
24+
typeof queryParams.fields === 'string' &&
25+
queryParams.fields.length > 0
26+
) {
27+
// it is ok to split by comma since pocketbase doesn't allow collection fields to contain commas
28+
const fields = queryParams.fields.split(',');
29+
let shouldJoin = false;
30+
if (fields.findIndex((field) => field === 'id') === -1) {
31+
fields.push('id');
32+
shouldJoin = true;
33+
}
34+
if (fields.findIndex((field) => field === 'updated') === -1) {
35+
fields.push('updated');
36+
shouldJoin = true;
37+
}
38+
if (shouldJoin) {
39+
queryParams.fields = fields.join(',');
40+
}
41+
}
42+
return queryParams;
43+
};

src/lib/queries/collection.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
RecordSubscription
1818
} from 'pocketbase';
1919

20-
import { realtimeStoreExpand } from '../internal';
20+
import { realtimeStoreExpand, reconcileOptionalFields } from '../internal';
2121
import { collectionKeys } from '../query-key-factory';
2222
import type { CollectionQueryPrefetchOptions, CollectionStoreOptions } from '../types';
2323

@@ -31,7 +31,7 @@ const collectionStoreCallback = async <
3131
queryKey: TQueryKey,
3232
subscription: RecordSubscription<T>,
3333
collection: RecordService,
34-
queryParams: RecordListQueryParams | undefined = undefined,
34+
queryParams: Pick<RecordListQueryParams, 'expand'> | undefined = undefined,
3535
sortFunction?: (a: T, b: T) => number,
3636
filterFunction?: (value: T, index: number, array: T[]) => boolean,
3737
filterFunctionThisArg?: any
@@ -100,21 +100,23 @@ const collectionStoreCallback = async <
100100
* Meant for SSR use, simply returns all the records from a collection. See [TanStack's documentation](https://tanstack.com/query/v4/docs/svelte/ssr#using-initialdata) and this project's README.md for some examples.
101101
*
102102
* @param collection The collection from which to get all the records.
103-
* @param [options.queryParams] The query params that will be passed on to `getFullList`.
103+
* @param [options.queryParams] The query params that will be passed on to `getFullList`. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
104104
* @returns The initial data required for a collection query, i.e. an array of Pocketbase records.
105105
*/
106106
export const createCollectionQueryInitialData = async <
107107
T extends Pick<Record, 'id' | 'updated'> = Pick<Record, 'id' | 'updated'>
108108
>(
109109
collection: RecordService,
110110
{ queryParams = undefined }: { queryParams?: RecordListQueryParams }
111-
): Promise<Array<T>> => [...(await collection.getFullList<T>(queryParams))];
111+
): Promise<Array<T>> => [
112+
...(await collection.getFullList<T>(reconcileOptionalFields(queryParams)))
113+
];
112114

113115
/**
114116
* Meant for SSR use, allows for prefetching queries on the server so that data is already available in the cache, and no initial fetch occurs client-side. See [TanStack's documentation](https://tanstack.com/query/v4/docs/svelte/ssr#using-prefetchquery) and this project's README.md for some examples.
115117
*
116118
* @param collection The collection from which to get all the records.
117-
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received.
119+
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
118120
* @param [options.queryKey] Provides the query key option for TanStack Query. By default, uses the `collectionKeys` function from this package.
119121
* @param [options.staleTime] Provides the stale time option for TanStack Query. By default, `Infinity` since the query receives realtime updates. Note that the package will take care of automatically marking the query as stale when the last subscriber unsubscribes from this query.
120122
* @param [options] The rest of the options are passed to the `prefetchQuery` function from TanStack Query, this library has no defaults for them.
@@ -150,7 +152,7 @@ export const createCollectionQueryPrefetch = <
150152
* - If a `create` action is received via the realtime subscription, the new record is added to the end of the query's data array before the `filterFunction` and `sortFunction` run.
151153
*
152154
* @param collection The collection from which to get all the records.
153-
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received.
155+
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
154156
* @param [options.sortFunction] `compareFn` from `Array.prototype.sort` that runs when an action is received via the realtime subscription. This is used since Pocketbase realtime subscriptions does not support `sort` in `queryParams`.
155157
* @param [options.filterFunction] `predicate` from `Array.prototype.filter` that runs when an action is received via the realtime subscription. This is used since Pocketbase realtime subscriptions does not support `filter` in `queryParams`.
156158
* @param [options.filterFunctionThisArg] `thisArg` from `Array.prototype.filter` that runs when an action is received via the realtime subscription. This is used since Pocketbase realtime subscriptions does not support `filter` in `queryParams`.

src/lib/queries/infinite-collection.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818

1919
import { produce, setAutoFreeze, type Draft } from 'immer';
2020

21-
import { realtimeStoreExpand } from '../internal';
21+
import { realtimeStoreExpand, reconcileOptionalFields } from '../internal';
2222
import { collectionKeys } from '../query-key-factory';
2323
import type { InfiniteCollectionStoreOptions, InfiniteQueryPrefetchOptions } from '../types';
2424

@@ -33,7 +33,7 @@ const infiniteCollectionStoreCallback = async <
3333
subscription: RecordSubscription<T>,
3434
collection: RecordService,
3535
perPage: number,
36-
queryParams: RecordListQueryParams | undefined = undefined,
36+
queryParams: Pick<RecordListQueryParams, 'expand'> | undefined = undefined,
3737
sortFunction?: (a: T, b: T) => number,
3838
filterFunction?: (value: T, index: number, array: T[]) => boolean,
3939
filterFunctionThisArg?: any
@@ -231,7 +231,7 @@ const infiniteCollectionStoreCallback = async <
231231
* @param collection The collection from which to get paginated records.
232232
* @param [options.page] The page that will be passed on to `getList`. By default, `1`.
233233
* @param [options.perPage] The per page that will be passed on to `getList`. By default, `20`.
234-
* @param [options.queryParams] The query params that will be passed on to `getList`.
234+
* @param [options.queryParams] The query params that will be passed on to `getList`. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
235235
* @returns The initial data required for a collection query, i.e. an array of Pocketbase records.
236236
*/
237237
export const infiniteCollectionQueryInitialData = async <
@@ -243,15 +243,17 @@ export const infiniteCollectionQueryInitialData = async <
243243
perPage = 20,
244244
queryParams = undefined
245245
}: { page?: number; perPage?: number; queryParams?: RecordListQueryParams } = {}
246-
): Promise<ListResult<T>> => ({ ...(await collection.getList<T>(page, perPage, queryParams)) });
246+
): Promise<ListResult<T>> => ({
247+
...(await collection.getList<T>(page, perPage, reconcileOptionalFields(queryParams)))
248+
});
247249

248250
/**
249251
* Meant for SSR use, allows for prefetching queries on the server so that data is already available in the cache, and no initial fetch occurs client-side. See [TanStack's documentation](https://tanstack.com/query/v4/docs/svelte/ssr#using-prefetchquery) and this project's README.md for some examples.
250252
*
251253
* @param collection The collection from which to get paginated records.
252254
* @param [options.page] The page that will be passed on to `getList`. By default, `1`.
253255
* @param [options.perPage] The per page that will be passed on to `getList`. By default, `20`.
254-
* @param [options.queryParams] The query params are simply passed on to `getList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received.
256+
* @param [options.queryParams] The query params are simply passed on to `getList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
255257
* @param [options.queryKey] Provides the query key option for TanStack Infinite Query. By default, uses the `collectionKeys` function from this package.
256258
* @param [options.staleTime] Provides the stale time option for TanStack Infinite Query. By default, `Infinity` since the query receives realtime updates. Note that the package will take care of automatically marking the query as stale when the last subscriber unsubscribes from this query.
257259
* @param [options] The rest of the options are passed to the `prefetchQuery` function from TanStack Infinite Query, this library has no defaults for them.
@@ -294,7 +296,7 @@ export const infiniteCollectionQueryPrefetch = <
294296
* @param collection The collection from which to get paginated records.
295297
* @param [options.page] The page that will be passed on to `getList`. By default, `1`.
296298
* @param [options.perPage] The per page that will be passed on to `getList`. By default, `20`.
297-
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received.
299+
* @param [options.queryParams] The query params are simply passed on to `getFullList` for the initial data fetch, and `getOne` for realtime updates. If the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
298300
* @param [options.keepCurrentPageOnly] Only keeps data from the current page of the infinite query, and discards the rest of the data when a page is changed.
299301
* @param [options.sortFunction] `compareFn` from `Array.prototype.sort` that runs when an action is received via the realtime subscription. This is used since Pocketbase realtime subscriptions does not support `sort` in `queryParams`.
300302
* @param [options.filterFunction] `predicate` from `Array.prototype.filter` that runs when an action is received via the realtime subscription. This is used since Pocketbase realtime subscriptions does not support `filter` in `queryParams`.

src/lib/queries/record.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
RecordSubscription
1616
} from 'pocketbase';
1717

18-
import { realtimeStoreExpand } from '../internal';
18+
import { realtimeStoreExpand, reconcileOptionalFields } from '../internal';
1919
import { collectionKeys } from '../query-key-factory';
2020
import type { QueryPrefetchOptions, RecordStoreOptions } from '../types';
2121

@@ -27,7 +27,7 @@ const createRecordQueryCallback = async <
2727
queryKey: TQueryKey,
2828
subscription: RecordSubscription<T>,
2929
collection: RecordService,
30-
queryParams: RecordQueryParams | undefined = undefined
30+
queryParams: Pick<RecordQueryParams, 'expand'> | undefined = undefined
3131
) => {
3232
let data = queryClient.getQueryData<T | null>(queryKey);
3333

@@ -63,7 +63,7 @@ const createRecordQueryCallback = async <
6363
*
6464
* @param collection The collection from which to get the record.
6565
* @param id The `id` of the record to get from the collection.
66-
* @param [options.queryParams] The query params that will be passed on to `getOne`.
66+
* @param [options.queryParams] The query params that will be passed on to `getOne`. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
6767
* @returns The initial data required for a record query, i.e. the Pocketbase record.
6868
*/
6969
export const createRecordQueryInitialData = <
@@ -72,14 +72,14 @@ export const createRecordQueryInitialData = <
7272
collection: RecordService,
7373
id: string,
7474
{ queryParams = undefined }: { queryParams?: RecordQueryParams }
75-
): Promise<T> => collection.getOne<T>(id, queryParams);
75+
): Promise<T> => collection.getOne<T>(id, reconcileOptionalFields(queryParams));
7676

7777
/**
7878
* Meant for SSR use, allows for prefetching queries on the server so that data is already available in the cache, and no initial fetch occurs client-side. See [TanStack's documentation](https://tanstack.com/query/v4/docs/svelte/ssr#using-prefetchquery) and this project's README.md for some examples.
7979
*
8080
* @param collection The collection from which to get the record.
8181
* @param id The `id` of the record to get from the collection.
82-
* @param [options.queryParams] The query params are simply passed on to `getOne` and if the `expand` key is provided, the record is expanded every time a realtime update is received.
82+
* @param [options.queryParams] The query params are simply passed on to `getOne` and if the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
8383
* @param [options.queryKey] Provides the query key option for TanStack Query. By default, uses the `collectionKeys` function from this package.
8484
* @param [options.staleTime] Provides the stale time option for TanStack Query. By default, `Infinity` since the query receives realtime updates. Note that the package will take care of automatically marking the query as stale when the last subscriber unsubscribes from this query.
8585
* @param [options] The rest of the options are passed to the `prefetchQuery` function from TanStack Query, this library has no defaults for them.
@@ -117,7 +117,7 @@ export const createRecordQueryPrefetch = <
117117
*
118118
* @param collection The collection from which to get the record.
119119
* @param id The `id` of the record to get from the collection.
120-
* @param [options.queryParams] The query params are simply passed on to `getOne` and if the `expand` key is provided, the record is expanded every time a realtime update is received.
120+
* @param [options.queryParams] The query params are simply passed on to `getOne` and if the `expand` key is provided, the record is expanded every time a realtime update is received. If you use `fields` to specify optional fields, note that `id` and `updated` will be added to it since the library uses them internally.
121121
* @param [options.disableRealtime] Provides an option to disable realtime updates to the Pocketbase record. By default, `false` since we want the Pocketbase record to be updated in realtime. If set to `true`, a realtime subscription to the Pocketbase server is never sent. Don't forget to set `options.staleTime` to a more appropriate value than `Infinity` you disable realtime updates.
122122
* @param [options.invalidateQueryOnRealtimeError] Provides an option to invalidate the query if a realtime error occurs. By default, `true` since if a realtime error occurs, the query's data would be stale.
123123
* @param [options.onRealtimeUpdate] This function is called with the realtime action every time an realtime action is received.

0 commit comments

Comments
 (0)