Skip to content

Commit 0d7ac00

Browse files
feat(builder): accept array for include, append and orderBy (#148)
Accept array of strings for `include`, `append` and `orderBy`.
1 parent a93cf5e commit 0d7ac00

File tree

4 files changed

+127
-24
lines changed

4 files changed

+127
-24
lines changed

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

+29-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,30 @@ Eager load relationships.
1515
await Model.include('user', 'category')
1616
```
1717

18+
#### Array
19+
20+
<alert type="success">Available in version >= v1.8.0</alert>
21+
22+
```js
23+
await Model.include(['user', 'category'])
24+
```
25+
1826
## `append`
1927
- Arguments: `(...args)`
2028
- Returns: `self`
2129

2230
Append attributes.
2331

2432
```js
25-
await Model.append('likes')
33+
await Model.append('likes', 'shares')
34+
```
35+
36+
#### Array
37+
38+
<alert type="success">Available in version >= v1.8.0</alert>
39+
40+
```js
41+
await Model.append(['likes', 'shares'])
2642
```
2743

2844
## `select`
@@ -31,12 +47,12 @@ await Model.append('likes')
3147

3248
Set the columns to be selected.
3349

34-
**Single entity:**
50+
#### Single entity
3551
```js
3652
await Model.select(['title', 'content'])
3753
```
3854

39-
**Related entities:**
55+
#### Related entities
4056
```js
4157
await Post.select({
4258
posts: ['title', 'content'],
@@ -50,13 +66,11 @@ await Post.select({
5066

5167
Add a basic where clause to the query.
5268

53-
**Simple:**
54-
5569
```js
5670
await Model.where('status', 'active')
5771
```
5872

59-
**Nested:**
73+
#### Nested
6074

6175
<alert type="success">Available in version >= v1.8.0</alert>
6276

@@ -70,13 +84,11 @@ await Model.where(['user', 'status'], 'active')
7084

7185
Add a "where in" clause to the query.
7286

73-
**Simple:**
74-
7587
```js
7688
await Model.whereIn('id', [1, 2, 3])
7789
```
7890

79-
**Nested:**
91+
#### Nested
8092

8193
<alert type="success">Available in version >= v1.8.0</alert>
8294

@@ -94,6 +106,14 @@ Add an "order by" clause to the query.
94106
await Model.orderBy('-created_at', 'category_id')
95107
```
96108

109+
#### Array
110+
111+
<alert type="success">Available in version >= v1.8.0</alert>
112+
113+
```js
114+
await Model.orderBy(['-created_at', 'category_id'])
115+
```
116+
97117
## `page`
98118
- Arguments: `(value)`
99119
- Returns: `self`

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

+77-8
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ We also need to sort our queries, so let's do this now!
334334
The method we want to use now is `orderBy`. The arguments are the names of the properties we want to sort.
335335
We can pass as many arguments as we want.
336336

337-
**Single Sort**
337+
#### Single Sort
338338

339339
We can sort our **Posts** by the `created_at` date:
340340

@@ -355,7 +355,7 @@ We can sort our **Posts** by the `created_at` date:
355355
</code-block>
356356
</code-group>
357357

358-
**Multiple Sort**
358+
#### Multiple Sort
359359

360360
And we can sort by their `title` too:
361361

@@ -380,27 +380,73 @@ And we can sort by their `title` too:
380380
Sorting is ascending by default and can be reversed by adding a hyphen (-) to the start of the property name.
381381
</alert>
382382

383+
#### Using an Array
384+
385+
<alert type="success">Available in version >= v1.8.0</alert>
386+
387+
The first argument of `orderBy` also accepts an array of string.
388+
389+
<code-group>
390+
<code-block label="Query" active>
391+
392+
```js
393+
const posts = await Post.orderBy(['-created_at', 'title']).get()
394+
```
395+
396+
</code-block>
397+
<code-block label="Request">
398+
399+
```http request
400+
GET /posts?sort=-created_at
401+
```
402+
403+
</code-block>
404+
</code-group>
405+
383406
## Including Relationships
384407

385408
See the [API reference](/api/query-builder-methods#include)
386409

387410
Sometimes, we will want to eager load a relationship, and to do so, we can use the `include` method.
388411
The arguments are the names of the relationships we want to include. We can pass as many arguments as we want.
389412

390-
Let's eager load the `category` relationship of our **Post**:
413+
Let's eager load the relationships `category` and `tags` of our **Post**:
414+
415+
<code-group>
416+
<code-block label="Query" active>
417+
418+
```js
419+
const posts = await Post.include('category', 'tags').get()
420+
```
421+
422+
</code-block>
423+
<code-block label="Request">
424+
425+
```http request
426+
GET /posts?include=category,tags
427+
```
428+
429+
</code-block>
430+
</code-group>
431+
432+
#### Using an Array
433+
434+
<alert type="success">Available in version >= v1.8.0</alert>
435+
436+
The first argument of `include` also accepts an array of string.
391437

392438
<code-group>
393439
<code-block label="Query" active>
394440

395441
```js
396-
const posts = await Post.include('category').get()
442+
const posts = await Post.include(['category', 'tags']).get()
397443
```
398444

399445
</code-block>
400446
<code-block label="Request">
401447

402448
```http request
403-
GET /posts?include=category
449+
GET /posts?include=category,tags
404450
```
405451

406452
</code-block>
@@ -413,20 +459,43 @@ See the [API reference](/api/query-builder-methods#append)
413459
We can also append attributes to our queries using the `append` method.
414460
The arguments are the names of the attributes we want to append. We can pass as many arguments as we want.
415461

416-
Let's append the `likes` attribute of our **Post**:
462+
Let's append the attribute `likes` and `shares` of our **Post**:
463+
464+
<code-group>
465+
<code-block label="Query" active>
466+
467+
```js
468+
const posts = await Post.append('likes', 'shares').get()
469+
```
470+
471+
</code-block>
472+
<code-block label="Request">
473+
474+
```http request
475+
GET /posts?append=likes,shares
476+
```
477+
478+
</code-block>
479+
</code-group>
480+
481+
#### Using an Array
482+
483+
<alert type="success">Available in version >= v1.8.0</alert>
484+
485+
The first argument of `append` also accepts an array of string.
417486

418487
<code-group>
419488
<code-block label="Query" active>
420489

421490
```js
422-
const posts = await Post.append('likes').get()
491+
const posts = await Post.append(['likes', 'shares']).get()
423492
```
424493

425494
</code-block>
426495
<code-block label="Request">
427496

428497
```http request
429-
GET /posts?append=likes
498+
GET /posts?append=likes,shares
430499
```
431500

432501
</code-block>

src/Builder.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ export default class Builder {
6363
* Query builder
6464
*/
6565

66-
include(...args) {
67-
this.includes = args
66+
include(...relationships) {
67+
relationships = Array.isArray(relationships[0]) ? relationships[0] : relationships
68+
this.includes = relationships
6869

6970
return this
7071
}
7172

72-
append(...args) {
73-
this.appends = args
73+
append(...attributes) {
74+
attributes = Array.isArray(attributes[0]) ? attributes[0] : attributes
75+
this.appends = attributes
7476

7577
return this
7678
}
@@ -131,8 +133,9 @@ export default class Builder {
131133
return this
132134
}
133135

134-
orderBy(...args) {
135-
this.sorts = args
136+
orderBy(...fields) {
137+
fields = Array.isArray(fields[0]) ? fields[0] : fields
138+
this.sorts = fields
136139

137140
return this
138141
}

tests/builder.test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ describe('Query builder', () => {
6464
post = Post.include('user', 'category')
6565

6666
expect(post._builder.includes).toEqual(['user', 'category'])
67+
68+
post = Post.include(['user', 'category'])
69+
70+
expect(post._builder.includes).toEqual(['user', 'category'])
6771
})
6872

6973
test('append() sets properly the builder', () => {
@@ -74,6 +78,10 @@ describe('Query builder', () => {
7478
post = Post.append('likes', 'visits')
7579

7680
expect(post._builder.appends).toEqual(['likes', 'visits'])
81+
82+
post = Post.append(['likes', 'visits'])
83+
84+
expect(post._builder.appends).toEqual(['likes', 'visits'])
7785
})
7886

7987
test('orderBy() sets properly the builder', () => {
@@ -84,6 +92,10 @@ describe('Query builder', () => {
8492
post = Post.orderBy('created_at', '-visits')
8593

8694
expect(post._builder.sorts).toEqual(['created_at', '-visits'])
95+
96+
post = Post.orderBy(['created_at', '-visits'])
97+
98+
expect(post._builder.sorts).toEqual(['created_at', '-visits'])
8799
})
88100

89101
test('where() sets properly the builder', () => {
@@ -141,7 +153,6 @@ describe('Query builder', () => {
141153
expect(errorModel).toThrow('The second argument on whereIn() method must be an array.')
142154
})
143155

144-
145156
test('page() sets properly the builder', () => {
146157
let post = Post.page(3)
147158

0 commit comments

Comments
 (0)