Skip to content

Commit db39511

Browse files
feat(model): add methods with, all and $all (#147)
`with` is alias for `include`, `all` is alias for `get` and `$all` is alias for `$get`.
1 parent 0d7ac00 commit db39511

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ await Model.include('user', 'category')
2323
await Model.include(['user', 'category'])
2424
```
2525

26+
<alert type="info">`with` is an alias of this method.</alert>
27+
2628
## `append`
2729
- Arguments: `(...args)`
2830
- Returns: `self`
@@ -225,6 +227,8 @@ Execute the query as a "select" statement.
225227
await Model.get()
226228
```
227229

230+
<alert type="info">`all` is an alias of this method.</alert>
231+
228232
## `first`
229233
- Returns: `Model | { data: Model }`
230234

@@ -249,15 +253,15 @@ await Model.find(1)
249253

250254
Execute the query as a "select" statement.
251255

252-
These `$`-prefixed convenience methods always return the requested content as [`JSON`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).
253-
254256
```js
255257
await Model.$get()
256258
```
257259

258-
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
260+
<alert type="info">These `$`-prefixed convenience methods always return the requested content.
259261
They handle and unwrap responses within "data".</alert>
260262

263+
<alert type="info">`$all` is an alias of this method.</alert>
264+
261265
## `$first`
262266
- Returns: `Model`
263267

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ With our models already set up, it's time to start using them!
1414
See the [API reference](/api/query-builder-methods#get)
1515

1616
Let's start initializing a model and building a simple query that gets all records from the database.
17-
To achieve this, we can use the `get` method.
17+
To achieve this, we can use the `get` method or its alias `all`.
1818

1919
We can get a list of posts using the **Post** model:
2020

@@ -407,7 +407,7 @@ The first argument of `orderBy` also accepts an array of string.
407407

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

410-
Sometimes, we will want to eager load a relationship, and to do so, we can use the `include` method.
410+
Sometimes, we will want to eager load a relationship, and to do so, we can use the `include` method or its alias `with`.
411411
The arguments are the names of the relationships we want to include. We can pass as many arguments as we want.
412412

413413
Let's eager load the relationships `category` and `tags` of our **Post**:

src/Model.js

+12
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ export default class Model extends StaticModel {
213213
return this
214214
}
215215

216+
with(...args) {
217+
return this.include(...args)
218+
}
219+
216220
append(...args) {
217221
this._builder.append(...args)
218222

@@ -428,6 +432,14 @@ export default class Model extends StaticModel {
428432
.then(response => response.data || response)
429433
}
430434

435+
all() {
436+
return this.get()
437+
}
438+
439+
$all() {
440+
return this.$get()
441+
}
442+
431443
/**
432444
* Common CRUD operations
433445
*/

src/StaticModel.js

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export default class StaticModel {
1818
return self
1919
}
2020

21+
static with(...args) {
22+
let self = this.instance()
23+
self.with(...args)
24+
25+
return self
26+
}
27+
2128
static append(...args) {
2229
let self = this.instance()
2330
self.append(...args)
@@ -111,9 +118,21 @@ export default class StaticModel {
111118
return self.get()
112119
}
113120

121+
static all() {
122+
let self = this.instance()
123+
124+
return self.all()
125+
}
126+
114127
static $get() {
115128
let self = this.instance()
116129

117130
return self.$get()
118131
}
132+
133+
static $all() {
134+
let self = this.instance()
135+
136+
return self.$all()
137+
}
119138
}

tests/builder.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ describe('Query builder', () => {
7070
expect(post._builder.includes).toEqual(['user', 'category'])
7171
})
7272

73+
test('with() sets properly the builder', () => {
74+
let post = Post.with('user')
75+
76+
expect(post._builder.includes).toEqual(['user'])
77+
78+
post = Post.with('user', 'category')
79+
80+
expect(post._builder.includes).toEqual(['user', 'category'])
81+
})
82+
7383
test('append() sets properly the builder', () => {
7484
let post = Post.append('likes')
7585

tests/model.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ describe('Model methods', () => {
265265
})
266266
})
267267

268+
test('all() method should be an alias of get() method', async () => {
269+
axiosMock.onGet('http://localhost/posts').reply(200, postsResponse)
270+
271+
const postsAll = await Post.all()
272+
const postsGet = await Post.get()
273+
274+
expect(postsAll).toStrictEqual(postsGet)
275+
})
276+
277+
test('$all() method should be an alias of $get() method', async () => {
278+
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
279+
280+
const postsAll = await Post.$all()
281+
const postsGet = await Post.$get()
282+
283+
expect(postsAll).toStrictEqual(postsGet)
284+
})
285+
268286
test('save() method makes a POST request when ID of object does not exists', async () => {
269287
let post
270288
const _postResponse = {

0 commit comments

Comments
 (0)