Skip to content

Commit

Permalink
fix(query): surround and only cannot be used at the same time (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
ky0615 committed Jun 10, 2022
1 parent ede65e8 commit 56c5228
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/runtime/query/match/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export const detectProperties = (keys: string[]) => {
}

export const withoutKeys = (keys: string[] = []) => (obj: any) => {
if (keys.length === 0) {
if (keys.length === 0 || !obj) {
return obj
}
const { prefixes, properties } = detectProperties(keys)
return _pick(obj, key => !properties.includes(key) && !prefixes.includes(key[0]))
}

export const withKeys = (keys: string[] = []) => (obj: any) => {
if (keys.length === 0) {
if (keys.length === 0 || !obj) {
return obj
}
const { prefixes, properties } = detectProperties(keys)
Expand Down
24 changes: 24 additions & 0 deletions test/features/query/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ describe('Database Provider', () => {
assert(result[2] === null)
})

test('Surround and using only method', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.only(['_path'])
.findSurround({ id: 3 }, { before: 2, after: 1 })

assert((result as Array<any>).length === 3)
assert(result[0]._path === '/a')
assert(result[1]._path === '/b')
assert(result[2] === null)
})

test('Surround and using without method', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[]))
const result = await createQuery(fetcher)
.without('id')
.findSurround({ id: 3 }, { before: 2, after: 1 })

assert((result as Array<any>).length === 3)
assert(result[0]._path === '/a')
assert(result[1]._path === '/b')
assert(result[2] === null)
})

test('Chain multiple where conditions', async () => {
const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, path: '/a' }, { id: 2, path: '/b' }, { id: 3, path: '/c' }] as any[]))
const query = createQuery(fetcher).where({ id: { $in: [1, 2] } })
Expand Down

0 comments on commit 56c5228

Please sign in to comment.