From 7d9d56e5b598afcaeb2e0018f46bfc5e53544171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 9 Nov 2020 13:15:08 -0300 Subject: [PATCH 1/5] feat(model): add methods `with`, `all` and `$all` `with` is alias for `include`, `all` is alias for `get` and `$all` is alias for `$get` --- src/Model.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Model.js b/src/Model.js index 12150b61..3464292d 100644 --- a/src/Model.js +++ b/src/Model.js @@ -213,6 +213,10 @@ export default class Model extends StaticModel { return this } + with(...args) { + return this.include(...args) + } + append(...args) { this._builder.append(...args) @@ -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 */ From dd6b23bae2b07b69b91e436e12a1be41dbb5f936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 9 Nov 2020 22:23:18 -0300 Subject: [PATCH 2/5] feat(model): add static methods `with`, `all` and `$all` `with` is alias for `include`, `all` is alias for `get` and `$all` is alias for `$get` --- src/StaticModel.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/StaticModel.js b/src/StaticModel.js index b07c7e9f..0c9cc180 100644 --- a/src/StaticModel.js +++ b/src/StaticModel.js @@ -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) @@ -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() + } } From d4969ea2e799ce6dd18d9bf1fd6d059e371bab94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 9 Nov 2020 22:26:17 -0300 Subject: [PATCH 3/5] test: update tests --- tests/builder.test.js | 10 ++++++++++ tests/model.test.js | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tests/builder.test.js b/tests/builder.test.js index 5a83e556..1b483f50 100644 --- a/tests/builder.test.js +++ b/tests/builder.test.js @@ -66,6 +66,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') diff --git a/tests/model.test.js b/tests/model.test.js index eae156d5..385970f3 100644 --- a/tests/model.test.js +++ b/tests/model.test.js @@ -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 = { From e4529d4e0988dff5970f06fe9a7af95a81ab4b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 9 Nov 2020 22:43:13 -0300 Subject: [PATCH 4/5] docs: update docs --- docs/content/en/api/query-builder-methods.md | 39 +++++++++++++++++++- docs/content/en/building-the-query.md | 4 +- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/content/en/api/query-builder-methods.md b/docs/content/en/api/query-builder-methods.md index f2ed7ad1..5421ec50 100644 --- a/docs/content/en/api/query-builder-methods.md +++ b/docs/content/en/api/query-builder-methods.md @@ -15,6 +15,18 @@ Eager load relationships. await Model.include('user', 'category') ``` +## `with` +- Arguments: `(...args)` +- Returns: `self` + +Eager load relationships. + +```js +await Model.with('user', 'category') +``` + +This method is an alias of [include](/api/query-builder-methods#include) + ## `append` - Arguments: `(...args)` - Returns: `self` @@ -205,6 +217,17 @@ Execute the query as a "select" statement. await Model.get() ``` +## `all` +- Returns: `Collection | { data: Collection }` + +Execute the query as a "select" statement. + +```js +await Model.all() +``` + +This method is an alias of [get](/api/query-builder-methods#get) + ## `first` - Returns: `Model | { data: Model }` @@ -229,15 +252,27 @@ 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() ``` +These `$`-prefixed convenience methods always return the requested content. +They handle and unwrap responses within "data". + +## `$all` +- Returns: `Collection` + +Execute the query as a "select" statement. + +```js +await Model.$all() +``` + These `$`-prefixed convenience methods always return the requested content. They handle and unwrap responses within "data". +This method is an alias of [$get](/api/query-builder-methods#get-1) + ## `$first` - Returns: `Model` diff --git a/docs/content/en/building-the-query.md b/docs/content/en/building-the-query.md index ad6be300..6be408c0 100644 --- a/docs/content/en/building-the-query.md +++ b/docs/content/en/building-the-query.md @@ -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: @@ -384,7 +384,7 @@ And we can sort by their `title` too: 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 `category` relationship of our **Post**: From b932e9b82a02372be95332bc5485f0a0bdf6de27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Tue, 10 Nov 2020 11:56:24 -0300 Subject: [PATCH 5/5] docs: update docs --- docs/content/en/api/query-builder-methods.md | 37 ++------------------ 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/docs/content/en/api/query-builder-methods.md b/docs/content/en/api/query-builder-methods.md index 5421ec50..e8be6a5b 100644 --- a/docs/content/en/api/query-builder-methods.md +++ b/docs/content/en/api/query-builder-methods.md @@ -15,17 +15,7 @@ Eager load relationships. await Model.include('user', 'category') ``` -## `with` -- Arguments: `(...args)` -- Returns: `self` - -Eager load relationships. - -```js -await Model.with('user', 'category') -``` - -This method is an alias of [include](/api/query-builder-methods#include) +`with` is an alias of this method. ## `append` - Arguments: `(...args)` @@ -217,16 +207,7 @@ Execute the query as a "select" statement. await Model.get() ``` -## `all` -- Returns: `Collection | { data: Collection }` - -Execute the query as a "select" statement. - -```js -await Model.all() -``` - -This method is an alias of [get](/api/query-builder-methods#get) +`all` is an alias of this method. ## `first` - Returns: `Model | { data: Model }` @@ -259,19 +240,7 @@ await Model.$get() These `$`-prefixed convenience methods always return the requested content. They handle and unwrap responses within "data". -## `$all` -- Returns: `Collection` - -Execute the query as a "select" statement. - -```js -await Model.$all() -``` - -These `$`-prefixed convenience methods always return the requested content. -They handle and unwrap responses within "data". - -This method is an alias of [$get](/api/query-builder-methods#get-1) +`$all` is an alias of this method. ## `$first` - Returns: `Model`