Skip to content

Commit

Permalink
feat(model): add methods with, all and $all (#147)
Browse files Browse the repository at this point in the history
`with` is alias for `include`, `all` is alias for `get` and `$all` is alias for `$get`.
  • Loading branch information
JoaoPedroAS51 authored Nov 10, 2020
1 parent 0d7ac00 commit db39511
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
10 changes: 7 additions & 3 deletions docs/content/en/api/query-builder-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ await Model.include('user', 'category')
await Model.include(['user', 'category'])
```

<alert type="info">`with` is an alias of this method.</alert>

## `append`
- Arguments: `(...args)`
- Returns: `self`
Expand Down Expand Up @@ -225,6 +227,8 @@ Execute the query as a "select" statement.
await Model.get()
```

<alert type="info">`all` is an alias of this method.</alert>

## `first`
- Returns: `Model | { data: Model }`

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

Execute the query as a "select" statement.

These `$`-prefixed convenience methods always return the requested content as [`JSON`](https://developer.mozilla.org/en-US/docs/Web/API/Body/json).

```js
await Model.$get()
```

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

<alert type="info">`$all` is an alias of this method.</alert>

## `$first`
- Returns: `Model`

Expand Down
4 changes: 2 additions & 2 deletions docs/content/en/building-the-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ With our models already set up, it's time to start using them!
See the [API reference](/api/query-builder-methods#get)

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

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

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

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

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

Let's eager load the relationships `category` and `tags` of our **Post**:
Expand Down
12 changes: 12 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export default class Model extends StaticModel {
return this
}

with(...args) {
return this.include(...args)
}

append(...args) {
this._builder.append(...args)

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

all() {
return this.get()
}

$all() {
return this.$get()
}

/**
* Common CRUD operations
*/
Expand Down
19 changes: 19 additions & 0 deletions src/StaticModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default class StaticModel {
return self
}

static with(...args) {
let self = this.instance()
self.with(...args)

return self
}

static append(...args) {
let self = this.instance()
self.append(...args)
Expand Down Expand Up @@ -111,9 +118,21 @@ export default class StaticModel {
return self.get()
}

static all() {
let self = this.instance()

return self.all()
}

static $get() {
let self = this.instance()

return self.$get()
}

static $all() {
let self = this.instance()

return self.$all()
}
}
10 changes: 10 additions & 0 deletions tests/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ describe('Query builder', () => {
expect(post._builder.includes).toEqual(['user', 'category'])
})

test('with() sets properly the builder', () => {
let post = Post.with('user')

expect(post._builder.includes).toEqual(['user'])

post = Post.with('user', 'category')

expect(post._builder.includes).toEqual(['user', 'category'])
})

test('append() sets properly the builder', () => {
let post = Post.append('likes')

Expand Down
18 changes: 18 additions & 0 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ describe('Model methods', () => {
})
})

test('all() method should be an alias of get() method', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsResponse)

const postsAll = await Post.all()
const postsGet = await Post.get()

expect(postsAll).toStrictEqual(postsGet)
})

test('$all() method should be an alias of $get() method', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)

const postsAll = await Post.$all()
const postsGet = await Post.$get()

expect(postsAll).toStrictEqual(postsGet)
})

test('save() method makes a POST request when ID of object does not exists', async () => {
let post
const _postResponse = {
Expand Down

0 comments on commit db39511

Please sign in to comment.