Skip to content

Commit db3fd67

Browse files
feat(content): Optional search parameter (#733)
* docs: updated FR & EN docs with optional search * feat: optional search * fix: lint * fix: empty or falsey values instead of boolean false
1 parent a6274e4 commit db3fd67

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

docs/content/en/fetching.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,15 @@ Performs a full-text search on a field. `value` is optional, in this case `field
148148

149149
The fields you want to search on must be defined in options in order to be indexed, see [configuration](/configuration#fulltextsearchfields).
150150

151+
Using an empty string as parameter will skip the search.
152+
151153
```js
152154
// Search on field title
153155
const articles = await this.$content('articles').search('title', 'welcome').fetch()
154156
// Search on all pre-defined fields
155157
const articles = await this.$content('articles').search('welcome').fetch()
158+
// Search will be skipped if the search string is empty
159+
const articles = await this.$content('articles').search('').fetch()
156160
```
157161

158162
<alert type="info">

docs/content/fr/fetching.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ Effectue une recherche plein texte sur un champ. Le paramètre `value` est optio
141141

142142
Le champ sur lequel vous voulez effectuer la recherche doit être défini dans les options afin d'être indexé, voir [configuration](/fr/configuration#fulltextsearchfields).
143143

144+
Utiliser une chaîne de caractères vide comme paramètre `value` n'exécutera pas la recherche.
145+
144146
```js
145147
// Search on field title
146148
const articles = await this.$content('articles').search('titre', 'bienvenue').fetch()
147149
// Search on all pre-defined fields
148150
const articles = await this.$content('articles').search('bievenue').fetch()
151+
// Search will be skipped if the search string is empty
152+
const articles = await this.$content('articles').search('').fetch()
149153
```
150154

151155
### surround(slug, options)

packages/content/lib/query-builder.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class QueryBuilder {
8181
* @returns {QueryBuilder} Returns current instance to be chained
8282
*/
8383
search (query, value) {
84+
// Passing an empty or falsey value as query will avoid triggering a search to allow optional chaining
85+
if (!query?.length) { return this }
86+
8487
let $fts
8588

8689
if (typeof query === 'object') {

0 commit comments

Comments
 (0)