Query Builder Methods
include
- Arguments:
(...args)
- Returns:
self
Eager load relationships.
await Model.include('user', 'category')
Array
await Model.include(['user', 'category'])
with
is an alias of this method.
append
- Arguments:
(...args)
- Returns:
self
Append attributes.
await Model.append('likes', 'shares')
Array
await Model.append(['likes', 'shares'])
select
- Arguments:
(...fields)
- Returns:
self
Set the columns to be selected.
Single entity
await Model.select(['title', 'content'])
Related entities
await Post.select({
posts: ['title', 'content'],
user: ['age', 'firstName']
})
where
- Arguments:
(field, value)
- Returns:
self
Add a basic where clause to the query.
await Model.where('status', 'active')
Nested
await Model.where(['user', 'status'], 'active')
Object
await Model.where({ user: { status: 'active' } })
whereIn
- Arguments:
(field, array)
- Returns:
self
Add a "where in" clause to the query.
await Model.whereIn('id', [1, 2, 3])
Nested
await Model.whereIn(['user', 'id'], [1, 2, 3])
Object
await Model.where({ user: { id: [1, 2, 3] } })
orderBy
- Arguments:
(...args)
- Returns:
self
Add an "order by" clause to the query.
await Model.orderBy('-created_at', 'category_id')
Array
await Model.orderBy(['-created_at', 'category_id'])
page
- Arguments:
(value)
- Returns:
self
Set the current page.
await Model.page(1)
limit
- Arguments:
(value)
- Returns:
self
Set the page limit.
await Model.limit(20)
params
- Arguments:
(payload)
- Returns:
self
Add custom parameters to the query.
await Model.params({
foo: 'bar',
baz: true
})
GET /resource?foo=bar&baz=true
when
- Arguments:
(value, callback)
- Returns:
self
Add a conditional clause to the query.
const search = 'foo'
await Model.when(search, (query, value) => query.where('search', value))
wrappedBy
- Arguments:
(value)
- Returns:
self
Change the wrap()
data wrapper for this request.
await Model.wrappedBy('somewrapper').get()
nowrap
- Returns:
self
Remove the wrap()
data wrapper for this request to return the raw response.
await Model.nowrap().get()
custom
- Arguments:
(...args)
- Returns:
self
Build custom endpoints.
await Post.custom('posts/latest')
GET /posts/latest
const user = new User({ id: 1 })
const post = new Post()
await Post.custom(user, post, 'latest')
GET /users/1/posts/latest
config
- Arguments:
(config)
- Returns:
self
Configuration of HTTP Instance.
await Model.config({
method: 'PATCH',
header: {
/* ... */
},
data: { foo: 'bar' }
}).save()
get
- Returns:
Collection | { data: Collection }
Execute the query and get all results.
await Model.get()
all
is an alias of this method.
first
- Returns:
Model | { data: Model }
Execute the query and get the first result.
await Model.first()
find
- Arguments:
(identifier)
- Returns:
Model | { data: Model }
Find a model by its primary key.
await Model.find(1)
$get
- Returns:
Collection
Execute the query and get all results.
await Model.$get()
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".
$all
is an alias of this method.
$first
- Returns:
Model
Execute the query and get the first result.
await Model.$first()
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".
$find
- Arguments:
(identifier)
- Returns:
Model
Find a model by its primary key.
await Model.$find(1)
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".
file
- Returns:
Binary
Execute the query with $http.responseType as blob
and returns a binary
// get the blob
const data = await Model.file()
// force file download
const url = window.URL.createObjectURL(new Blob([data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', 'model.xlsx') //or any other extension
document.body.appendChild(link)
link.click()