Skip to content

Commit 296f7d5

Browse files
ky0615farnabaz
authored andcommitted
fix(query): surround and only cannot be used at the same time (#1238)
1 parent 35df339 commit 296f7d5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/runtime/query/match/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export const detectProperties = (keys: string[]) => {
4040
}
4141

4242
export const withoutKeys = (keys: string[] = []) => (obj: any) => {
43-
if (keys.length === 0) {
43+
if (keys.length === 0 || !obj) {
4444
return obj
4545
}
4646
const { prefixes, properties } = detectProperties(keys)
4747
return _pick(obj, key => !properties.includes(key) && !prefixes.includes(key[0]))
4848
}
4949

5050
export const withKeys = (keys: string[] = []) => (obj: any) => {
51-
if (keys.length === 0) {
51+
if (keys.length === 0 || !obj) {
5252
return obj
5353
}
5454
const { prefixes, properties } = detectProperties(keys)

test/features/query/query.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,30 @@ describe('Database Provider', () => {
205205
assert(result[2] === null)
206206
})
207207

208+
test('Surround and using only method', async () => {
209+
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
210+
const result = await createQuery(fetcher)
211+
.only(['_path'])
212+
.findSurround({ id: 3 }, { before: 2, after: 1 })
213+
214+
assert((result as Array<any>).length === 3)
215+
assert(result[0]._path === '/a')
216+
assert(result[1]._path === '/b')
217+
assert(result[2] === null)
218+
})
219+
220+
test('Surround and using without method', async () => {
221+
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
222+
const result = await createQuery(fetcher)
223+
.without('id')
224+
.findSurround({ id: 3 }, { before: 2, after: 1 })
225+
226+
assert((result as Array<any>).length === 3)
227+
assert(result[0]._path === '/a')
228+
assert(result[1]._path === '/b')
229+
assert(result[2] === null)
230+
})
231+
208232
test('Chain multiple where conditions', async () => {
209233
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, path: '/a' }, { id: 2, path: '/b' }, { id: 3, path: '/c' }] as any[]))
210234
const query = createQuery(fetcher).where({ id: { $in: [1, 2] } })

0 commit comments

Comments
 (0)