Repository: robsontenorio/vue-api-query
Pull request #147 (2020-11-10 01:46:11)

feat(model): add methods `with`, `all` and `$all`
`with` is alias for `include`, `all` is alias for `get` and `$all` is alias for `$get`.


Patch:
diff --git a/docs/content/en/api/query-builder-methods.md b/docs/content/en/api/query-builder-methods.md
index 502aac9..93bc51a 100644
--- a/docs/content/en/api/query-builder-methods.md
+++ b/docs/content/en/api/query-builder-methods.md
@@ -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`
@@ -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 }`
 
@@ -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`
 
diff --git a/docs/content/en/building-the-query.md b/docs/content/en/building-the-query.md
index cea56a5..9eae5ab 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:
 
@@ -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**:
diff --git a/src/Model.js b/src/Model.js
index 12150b6..3464292 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
    */
diff --git a/src/StaticModel.js b/src/StaticModel.js
index b07c7e9..0c9cc18 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()
+  }
 }


Test patch:
diff --git a/tests/builder.test.js b/tests/builder.test.js
index 3676b56..c100662 100644
--- a/tests/builder.test.js
+++ b/tests/builder.test.js
@@ -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')
 
diff --git a/tests/model.test.js b/tests/model.test.js
index eae156d..385970f 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 = {