A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/robsontenorio/vue-api-query/commit/db3951155c8b3965194457410ed7e5d9979dc182 below:

add methods `with`, `all` and `$all` (#147) · robsontenorio/vue-api-query@db39511 · GitHub

File tree Expand file treeCollapse file tree 6 files changed

+68

-5

lines changed

Filter options

Expand file treeCollapse file tree 6 files changed

+68

-5

lines changed Original file line number Diff line number Diff line change

@@ -23,6 +23,8 @@ await Model.include('user', 'category')

23 23

await Model.include(['user', 'category'])

24 24

```

25 25 26 +

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

27 + 26 28

## `append`

27 29

- Arguments: `(...args)`

28 30

- Returns: `self`

@@ -225,6 +227,8 @@ Execute the query as a "select" statement.

225 227

await Model.get()

226 228

```

227 229 230 +

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

231 + 228 232

## `first`

229 233

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

230 234

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

249 253 250 254

Execute the query as a "select" statement.

251 255 252 -

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

253 - 254 256

```js

255 257

await Model.$get()

256 258

```

257 259 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.

259 261

They handle and unwrap responses within "data".</alert>

260 262 263 +

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

264 + 261 265

## `$first`

262 266

- Returns: `Model`

263 267 Original file line number Diff line number Diff line change

@@ -14,7 +14,7 @@ With our models already set up, it's time to start using them!

14 14

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

15 15 16 16

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`.

18 18 19 19

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

20 20

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

407 407 408 408

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

409 409 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`.

411 411

The arguments are the names of the relationships we want to include. We can pass as many arguments as we want.

412 412 413 413

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

Original file line number Diff line number Diff line change

@@ -213,6 +213,10 @@ export default class Model extends StaticModel {

213 213

return this

214 214

}

215 215 216 +

with(...args) {

217 +

return this.include(...args)

218 +

}

219 + 216 220

append(...args) {

217 221

this._builder.append(...args)

218 222

@@ -428,6 +432,14 @@ export default class Model extends StaticModel {

428 432

.then(response => response.data || response)

429 433

}

430 434 435 +

all() {

436 +

return this.get()

437 +

}

438 + 439 +

$all() {

440 +

return this.$get()

441 +

}

442 + 431 443

/**

432 444

* Common CRUD operations

433 445

*/

Original file line number Diff line number Diff line change

@@ -18,6 +18,13 @@ export default class StaticModel {

18 18

return self

19 19

}

20 20 21 +

static with(...args) {

22 +

let self = this.instance()

23 +

self.with(...args)

24 + 25 +

return self

26 +

}

27 + 21 28

static append(...args) {

22 29

let self = this.instance()

23 30

self.append(...args)

@@ -111,9 +118,21 @@ export default class StaticModel {

111 118

return self.get()

112 119

}

113 120 121 +

static all() {

122 +

let self = this.instance()

123 + 124 +

return self.all()

125 +

}

126 + 114 127

static $get() {

115 128

let self = this.instance()

116 129 117 130

return self.$get()

118 131

}

132 + 133 +

static $all() {

134 +

let self = this.instance()

135 + 136 +

return self.$all()

137 +

}

119 138

}

Original file line number Diff line number Diff line change

@@ -70,6 +70,16 @@ describe('Query builder', () => {

70 70

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

71 71

})

72 72 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 + 73 83

test('append() sets properly the builder', () => {

74 84

let post = Post.append('likes')

75 85 Original file line number Diff line number Diff line change

@@ -265,6 +265,24 @@ describe('Model methods', () => {

265 265

})

266 266

})

267 267 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 + 268 286

test('save() method makes a POST request when ID of object does not exists', async () => {

269 287

let post

270 288

const _postResponse = {

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4