Skip to content

Commit ed8e3d0

Browse files
committed
feat: allow passing meta to find options
1 parent 42865ff commit ed8e3d0

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

packages/core/src/query/findFirst.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ async function _findFirst<
6363
key: keyOrOptions,
6464
}
6565
: keyOrOptions
66+
67+
if (findOptions.meta) {
68+
Object.assign(meta, findOptions.meta)
69+
}
70+
6671
const fetchPolicy = store.$getFetchPolicy(findOptions?.fetchPolicy)
6772

6873
let result: any

packages/core/src/query/findMany.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ async function _findMany<
5959
meta ??= {}
6060
findOptions ??= {}
6161

62+
if (findOptions.meta) {
63+
Object.assign(meta, findOptions.meta)
64+
}
65+
6266
const fetchPolicy = store.$getFetchPolicy(findOptions.fetchPolicy)
6367

6468
let result: any[] | undefined

packages/core/src/query/peekFirst.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ export function peekFirst<
3535
key: keyOrOptions,
3636
}
3737
: keyOrOptions
38+
3839
const key = findOptions?.key
40+
41+
if (findOptions.meta) {
42+
Object.assign(meta, findOptions.meta)
43+
}
44+
3945
const fetchPolicy = store.$getFetchPolicy(findOptions?.fetchPolicy)
4046

4147
if (force || shouldReadCacheFromFetchPolicy(fetchPolicy)) {

packages/core/src/query/peekMany.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function peekMany<
3030
}: PeekManyOptions<TCollection, TCollectionDefaults, TSchema>): QueryResult<Array<WrappedItem<TCollection, TCollectionDefaults, TSchema>>> {
3131
meta ??= {}
3232

33+
if (findOptions?.meta) {
34+
Object.assign(meta, findOptions.meta)
35+
}
36+
3337
const fetchPolicy = store.$getFetchPolicy(findOptions?.fetchPolicy)
3438
if (force || shouldReadCacheFromFetchPolicy(fetchPolicy)) {
3539
let marker = defaultMarker(collection, findOptions)

packages/shared/src/types/query.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable unused-imports/no-unused-vars */
22

33
import type { Collection, CollectionByName, CollectionDefaults, CollectionRelation, RelationsByName, ResolvedCollectionItem, StoreSchema } from './collection'
4+
import type { CustomHookMeta } from './hooks'
45

56
export interface CustomParams<
67
TCollection extends Collection,
@@ -131,6 +132,8 @@ export interface FindOptionsBase<
131132
* Experimental: Enable garbage collection for items that are not referenced by any query or other item.
132133
*/
133134
experimentalGarbageCollection?: boolean
135+
136+
meta?: CustomHookMeta
134137
}
135138

136139
export type FindFirstOptions<

packages/vue/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ export function createCollectionApi<
333333
return createQuery<TCollection, TCollectionDefaults, TSchema, any, WrappedItem<TCollection, TCollectionDefaults, TSchema> | null | Array<WrappedItem<TCollection, TCollectionDefaults, TSchema>>>({
334334
store,
335335
fetchMethod: (options, meta) => toValue(type) === 'first'
336-
? findFirst({ store, collection, findOptions: options!, meta }).then(r => r.result)
336+
? (options ? findFirst({ store, collection, findOptions: options, meta }).then(r => r.result) : Promise.resolve(null))
337337
: findMany({ store, collection, findOptions: options, meta }).then(r => r.result),
338338
cacheMethod: (options, meta) => toValue(type) === 'first'
339-
? peekFirst({ store, collection, findOptions: options!, meta, force: true }).result
339+
? (options ? peekFirst({ store, collection, findOptions: options, meta, force: true }).result : null)
340340
: peekMany({ store, collection, findOptions: options, meta, force: true }).result,
341341
defaultValue: () => toValue(type) === 'first' ? null : [],
342342
options: boundOptionsGetter,

0 commit comments

Comments
 (0)