Skip to content

Commit 8f08c8f

Browse files
committedJul 2, 2022
feat(builder): add when method
Closes #219
1 parent 6098689 commit 8f08c8f

File tree

7 files changed

+260
-1
lines changed

7 files changed

+260
-1
lines changed
 

‎docs/content/en/api/query-builder-methods.md

+14
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ Add custom parameters to the query.
162162
</code-block>
163163
</code-group>
164164

165+
## `when`
166+
<alert type="success">Available in version >= v1.10.0</alert>
167+
168+
- Arguments: `(value, callback)`
169+
- Returns: `self`
170+
171+
Add a conditional clause to the query.
172+
173+
```js
174+
const search = 'foo'
175+
176+
await Model.when(search, (query, value) => query.where('search', value))
177+
```
178+
165179
## `custom`
166180
- Arguments: `(...args)`
167181
- Returns: `self`

‎docs/content/en/building-the-query.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,38 @@ Let's say we are at page 1, and we want 20 **Posts** per page:
597597
</code-block>
598598
</code-group>
599599

600+
## Conditional
601+
602+
<alert type="success">Available in version >= v1.10.0</alert>
603+
604+
We may need to add a clause based on a condition, and we can do so by using the `when` method.
605+
The first argument is the flag, and the second argument is the callback with the clause we want.
606+
607+
608+
<code-group>
609+
<code-block label="Query" active>
610+
611+
```js
612+
const search = 'foo'
613+
const posts = await Post.when(search, (query, value) => query.where('title', value)).get()
614+
```
615+
616+
</code-block>
617+
<code-block label="Request">
618+
619+
```http request
620+
GET /posts?filter[title]=foo
621+
```
622+
623+
</code-block>
624+
</code-group>
625+
626+
600627
## Applying Custom Parameters
601628

602629
See the [API reference](/api/query-builder-methods#params)
603630

604-
We may need to use parameters that are not provided by [vue-api-query](https://github.com/robsontenorio/vue-api-query),
631+
We may also need to use parameters that are not provided by [vue-api-query](https://github.com/robsontenorio/vue-api-query),
605632
and that's when the `params` method comes in to help.
606633

607634
The argument is an object of the parameters to add to the query.

‎index.d.ts

+169
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ declare class StaticModel {
202202
*/
203203
static params<M extends typeof Model> (this: M, payload: Record<string, string | number | boolean>): InstanceType<M>
204204

205+
/**
206+
* Add a conditional clause to the query.
207+
*
208+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#when|API Reference}
209+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#conditional|Building the Query}
210+
*/
211+
static when<M extends typeof Model, T = any> (this: M, value: T, callback: (query: Builder, value: T) => any): InstanceType<M>
212+
205213
/**
206214
* Build custom endpoints.
207215
*
@@ -559,6 +567,14 @@ export class Model extends StaticModel {
559567
*/
560568
params (payload: Record<string, string | number | boolean>): this
561569

570+
/**
571+
* Add a conditional clause to the query.
572+
*
573+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#when|API Reference}
574+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#conditional|Building the Query}
575+
*/
576+
when<T = any>(value: T, callback: (query: Builder, value: T) => any): this
577+
562578
/**
563579
* Build custom endpoints.
564580
*
@@ -705,3 +721,156 @@ export class Model extends StaticModel {
705721
*/
706722
sync (params: Record<string, any>): Promise<any>
707723
}
724+
725+
declare class Builder {
726+
/**
727+
* Query
728+
*/
729+
730+
/**
731+
* Eager load relationships.
732+
*
733+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#include|API Reference}
734+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#including-relationships|Building the Query}
735+
*/
736+
include (...relationships: string[]): this
737+
738+
/**
739+
* Eager load relationships.
740+
*
741+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#include|API Reference}
742+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#including-relationships|Building the Query}
743+
*/
744+
include (relationships: string[]): this
745+
746+
/**
747+
* Eager load relationships.
748+
*
749+
* Alias for the [include()]{@link include} method.
750+
*
751+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#include|API Reference}
752+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#including-relationships|Building the Query}
753+
*/
754+
with (...relationships: string[]): this
755+
756+
/**
757+
* Eager load relationships.
758+
*
759+
* Alias for the [include()]{@link include} method.
760+
*
761+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#include|API Reference}
762+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#including-relationships|Building the Query}
763+
*/
764+
with (relationships: string[]): this
765+
766+
/**
767+
* Append attributes.
768+
*
769+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#append|API Reference}
770+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#appending-attributes|Building the Query}
771+
*/
772+
append (...attributes: string[]): this
773+
774+
/**
775+
* Append attributes.
776+
*
777+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#append|API Reference}
778+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#appending-attributes|Building the Query}
779+
*/
780+
append (attributes: string[]): this
781+
782+
/**
783+
* Set the columns to be selected.
784+
*
785+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#select|API Reference}
786+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#selecting-fields|Building the Query}
787+
*/
788+
select (...columns: string[]): this
789+
790+
/**
791+
* Set the columns to be selected.
792+
*
793+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#select|API Reference}
794+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#selecting-fields|Building the Query}
795+
*/
796+
select (columns: string[]): this
797+
798+
/**
799+
* Set the columns to be selected.
800+
*
801+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#select|API Reference}
802+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#selecting-fields|Building the Query}
803+
*/
804+
select (columns: {
805+
[related: string]: string[]
806+
}): this
807+
808+
/**
809+
* Add a basic where clause to the query.
810+
*
811+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#where|API Reference}
812+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#evaluating-a-single-value|Building the Query}
813+
*/
814+
where (field: string | string[], value: string | number | boolean): this
815+
816+
/**
817+
* Add a "where in" clause to the query.
818+
*
819+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#wherein|API Reference}
820+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#evaluating-multiple-values|Building the Query}
821+
*/
822+
whereIn (field: string | string[], array: (string | number | boolean)[]): this
823+
824+
/**
825+
* Add an "order by" clause to the query.
826+
*
827+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#orderby|API Reference}
828+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#sorting|Building the Query}
829+
*/
830+
orderBy (...columns: string[]): this
831+
832+
/**
833+
* Add an "order by" clause to the query.
834+
*
835+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#orderby|API Reference}
836+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#sorting|Building the Query}
837+
*/
838+
orderBy (columns: string[]): this
839+
840+
/**
841+
* Set the current page.
842+
*
843+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#page|API Reference}
844+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#paginating|Building the Query}
845+
*/
846+
page (number: number): this
847+
848+
/**
849+
* Set the page limit.
850+
*
851+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#limit|API Reference}
852+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#paginating|Building the Query}
853+
*/
854+
limit (number: number): this
855+
856+
/**
857+
* Add custom parameters to the query.
858+
*
859+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#params|API Reference}
860+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#applying-custom-parameters|Building the Query}
861+
*/
862+
params (payload: Record<string, string | number | boolean>): this
863+
864+
/**
865+
* Add a conditional clause to the query.
866+
*
867+
* @see {@link https://robsontenorio.github.io/vue-api-query/api/query-builder-methods#when|API Reference}
868+
* @see {@link https://robsontenorio.github.io/vue-api-query/building-the-query#conditional|Building the Query}
869+
*/
870+
when<T = any>(value: T, callback: (query: this, value: T) => any): this
871+
872+
/**
873+
* Return parsed query string.
874+
*/
875+
query(): string
876+
}

‎src/Builder.js

+14
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,18 @@ export default class Builder {
173173

174174
return this
175175
}
176+
177+
when(value, callback) {
178+
if (typeof callback !== 'function') {
179+
throw new Error(
180+
'The CALLBACK is required and must be a function on when() method.'
181+
)
182+
}
183+
184+
if (value) {
185+
callback(this, value)
186+
}
187+
188+
return this
189+
}
176190
}

‎src/Model.js

+6
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ export default class Model extends StaticModel {
250250
return this
251251
}
252252

253+
when(value, callback) {
254+
this._builder.when(value, callback)
255+
256+
return this
257+
}
258+
253259
/**
254260
* Result
255261
*/

‎src/StaticModel.js

+7
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ export default class StaticModel {
8181
return self
8282
}
8383

84+
static when(value, callback) {
85+
let self = this.instance()
86+
self.when(value, callback)
87+
88+
return self
89+
}
90+
8491
static custom(...args) {
8592
let self = this.instance()
8693
self.custom(...args)

‎tests/builder.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,28 @@ describe('Query builder', () => {
276276
expect(errorModel).toThrow('You must pass a payload/object as param.')
277277
})
278278

279+
test('when() sets properly the builder', () => {
280+
let search = ''
281+
let post = Post.when(search, (query, value) => query.where('title', value))
282+
283+
expect(post._builder.filters).toEqual({})
284+
285+
search = 'foo'
286+
post = Post.when(search, (query, value) => query.where('title', value))
287+
288+
expect(post._builder.filters).toEqual({ title: 'foo' })
289+
})
290+
291+
test('when() throws a exception when callback is not a function', () => {
292+
errorModel = () => {
293+
Post.when()
294+
}
295+
296+
expect(errorModel).toThrow(
297+
'The CALLBACK is required and must be a function on when() method.'
298+
)
299+
})
300+
279301
test('it resets the uri upon query generation when the query is regenerated a second time', () => {
280302
const post = Post.where('title', 'Cool').page(4)
281303

0 commit comments

Comments
 (0)
Please sign in to comment.