Skip to content

Commit

Permalink
fix(queryContent): use path argument as prefix if there is another …
Browse files Browse the repository at this point in the history
…condition (#1612)
  • Loading branch information
farnabaz committed Oct 19, 2022
1 parent 4a79197 commit 8c7668f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/runtime/composables/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import { addPrerenderPath, shouldUseClientDB, withContentBase } from './utils'
*/
export const createQueryFetch = <T = ParsedContent>(path?: string) => async (query: QueryBuilder<T>) => {
if (path) {
if (query.params().first) {
if (query.params().first && (query.params().where || []).length === 0) {
// If query contains `path` and does not contain any `where` condition
// Then can use `path` as `where` condition to find exact match
query.where({ _path: withoutTrailingSlash(path) })
} else {
query.where({ _path: new RegExp(`^${path.replace(/[-[\]{}()*+.,^$\s/]/g, '\\$&')}`) })
Expand Down
43 changes: 39 additions & 4 deletions test/features/content-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,68 @@ export const testContentQuery = () => {
})

test('exact match foo not found', async () => {
const content = await $fetch('/features/query-content?path=/foo&findOne=1')
const content = await $fetch('/features/query-content?path=/prefix/foo&findOne=1')

// empty
expect(content).includes('$$$$')
})

// test `queryContent( PREFIX ).findOne()`
test('exact match foo/bar found', async () => {
const content = await $fetch('/features/query-content?path=/foo/bar&findOne=1')
const content = await $fetch('/features/query-content?path=/prefix/foo/bar&findOne=1')

// empty
expect(content).includes('prefix:foo:bar.md$$')
})

// test `queryContent( PREFIX ).find()`
test('prefix queries', async () => {
const content = await $fetch('/features/query-content?path=/foo')
const content = await $fetch('/features/query-content?path=/prefix/foo')

expect(content).includes('prefix:foo:bar.md')
expect(content).includes('prefix:foo:baz.md')
expect(content).includes('prefix:foobarbaz.md')
})

// test `queryContent( PREFIX ).find()` with trailing slash
test('directory listing', async () => {
const content = await $fetch('/features/query-content?path=/foo/')
const content = await $fetch('/features/query-content?path=/prefix/foo/')

expect(content).includes('prefix:foo:bar.md')
expect(content).includes('prefix:foo:baz.md')
expect(content).not.includes('prefix:foobarbaz.md')
})

// test `queryContent( PREFIX ).where( CONDITION ).find()`
test('list contents with tag', async () => {
const content = await $fetch('/features/query-content?where={"tags": { "$contains": "mdc" } }')

expect(content).includes(':mdc-props-inline.md')
expect(content).includes(':mdc-props.md')
})

// test `queryContent( PREFIX ).where( CONDITION ).findOne()`
test('find contents with tag', async () => {
const content = await $fetch('/features/query-content?where={"tags": { "$contains": "mdc" } }&findOne=1')

expect(content).includes(':mdc-props-inline.md')
expect(content).not.includes(':mdc-props.md')
})

// test `queryContent().where( CONDITION ).find()`
test('find contents with tag', async () => {
const content = await $fetch('/features/query-content?prefix=&where={"tags": { "$contains": "mdc" } }')

expect(content).includes(':mdc-props-inline.md')
expect(content).includes(':mdc-props.md')
})

// test `queryContent().where( CONDITION ).findOne()`
test('find contents with tag', async () => {
const content = await $fetch('/features/query-content?prefix=&where={"tags": { "$contains": "mdc" } }&findOne=1')

expect(content).includes(':mdc-props-inline.md')
expect(content).not.includes(':mdc-props.md')
})
})
}
2 changes: 2 additions & 0 deletions test/fixtures/basic/content/_partial/mdc-props-inline.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
tags:
- mdc
categories:
- Art
- History
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/basic/content/_partial/mdc-props.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
tags:
- mdc
categories:
- Art
- History
Expand Down
20 changes: 13 additions & 7 deletions test/fixtures/basic/pages/features/query-content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

<script setup>
const route = useRoute()
const path = route.query.path
const findOne = route.query.findOne
const prefix = route.query.prefix === undefined ? '/_partial' : ''
const path = route.query.path || ''
const findOne = route.query.findOne || false
const where = route.query.where ? JSON.parse(route.query.where) : false
const { data } = await useAsyncData('foo', () => {
const q = queryContent('/_partial/prefix' + path)
return findOne ? q.findOne() : q.find()
}, {
transform: (data) => {
return findOne ? data._id : data.map(d => d._id)
let q = queryContent(prefix + path || undefined)
if (where) {
q = q.where(where)
}
return findOne ? q.findOne() : q.find()
},
{
transform: data => findOne ? data._id : data.map(d => d._id)
})
</script>

0 comments on commit 8c7668f

Please sign in to comment.