Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/content/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ content: {
}
```

### `nestedProperties`

- Type `Array`
- Default: `[]`
- Version: **v2.0.0**

Register nested properties to handle dot-notation and deep filtering.

```js{}[nuxt.config.js]
content: {
nestedProperties: ['categories.slug']
}
```

### `markdown`

This module uses [remark](https://github.com/remarkjs/remark) under the hood to compile markdown files into JSON AST that will be stored into the `body` variable.
Expand Down Expand Up @@ -161,8 +175,12 @@ export default {
apiPrefix: '_content',
dir: 'content',
fullTextSearchFields: ['title', 'description', 'slug', 'text'],
nestedProperties: [],
markdown: {
externalLinks: {},
footnotes: {
inlineNotes: true
},
basePlugins: ['remark-squeeze-paragraphs', 'remark-slug', 'remark-autolink-headings', 'remark-external-links', 'remark-footnotes'],
plugins: [],
prism: {
Expand Down
8 changes: 8 additions & 0 deletions docs/content/en/fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const articles = await this.$content('articles').where({ age: { $gt: 18 } }).fet
const articles = await this.$content('articles').where({ name: { $in: ['odin', 'thor'] } }).fetch()
```

In order to filter in objects and array you need to enable nestedProperties, see [configuration](/configuration#nestedproperties).

```js
const products = await this.$content('products').where({ 'categories.slug': { $contains: 'top' } }).fetch()

const products = await this.$content('products').where({ 'categories.slug': { $contains: ['top', 'woman'] } }).fetch()
```

This module uses LokiJS under the hood, you can check for [query examples](https://github.com/techfort/LokiJS/wiki/Query-Examples#find-queries).

### sortBy(key, direction)
Expand Down
14 changes: 14 additions & 0 deletions example/content/products/jean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Jean",
"categories": [
{
"slug": "man"
},
{
"slug": "woman"
},
{
"slug": "bottom"
}
]
}
11 changes: 11 additions & 0 deletions example/content/products/shirt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"title": "Shirt",
"categories": [
{
"slug": "woman"
},
{
"slug": "top"
}
]
}
11 changes: 11 additions & 0 deletions example/content/products/short.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"title": "Short",
"categories": [
{
"slug": "man"
},
{
"slug": "bottom"
}
]
}
14 changes: 14 additions & 0 deletions example/content/products/tshirt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "T-Shirt",
"categories": [
{
"slug": "man"
},
{
"slug": "woman"
},
{
"slug": "top"
}
]
}
7 changes: 6 additions & 1 deletion example/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ module.exports = {
],
plugins: [
'~/plugins/components'
]
],
content: {
nestedProperties: [
'categories.slug'
]
}
}
28 changes: 28 additions & 0 deletions example/pages/products/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div>
<nuxt-link to="/">Home</nuxt-link>
<h2>Nuxt.js Shop</h2>

<ul>
<li
v-for="product in products"
:key="product.slug"
>{{ product.title }} - {{ product.categories.map(category => category.slug).join(', ') }}</li>
</ul>
</div>
</template>

<script>
export default {
watchQuery: true,
async asyncData ({ $content, route }) {
const products = await $content('products')
.where({ 'categories.slug': { $contains: ['top', 'man'] } })
.fetch()

return {
products
}
}
}
</script>
5 changes: 4 additions & 1 deletion lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class Database extends Hookable {
// Create Loki database
this.db = new Loki('content.db')
// Init collection
this.items = this.db.addCollection('items', { fullTextSearch: options.fullTextSearchFields.map(field => ({ field })) })
this.items = this.db.addCollection('items', {
fullTextSearch: options.fullTextSearchFields.map(field => ({ field })),
nestedProperties: options.nestedProperties
})
// Call chokidar watch if option if provided (dev only)
options.watch && this.watch()
this.options = options
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaults = {
apiPrefix: '_content',
dir: 'content',
fullTextSearchFields: ['title', 'description', 'slug', 'text'],
nestedProperties: [],
markdown: {
basePlugins: [
'remark-squeeze-paragraphs',
Expand Down