Releases: vuex-orm/vuex-orm
v0.7.2
v0.7.1
v0.7.0
More Powerful Where Query
Now you can use closure for the where statement, and you could have or
condition too.
Simple Where Clauses
You may use the where
method on a query chain to add where conditions. For example, here is a query that verifies the value of the "age" column is equal to 20.
const user = store.getters['entities/users/query']().where('age', 20).get()
// [User { id: 1, age: 20 }, User { id: 2, age: 20 }]
You may pass closure to the second argument if you need more powerful checking. The argument is the value of the field.
const user = store.getters['entities/users/query']().where('age', value => value > 20).get()
// [User { id: 1, age: 25 }, User { id: 2, age: 30 }]
Or you may pass closure to the first argument to get full control of the condition. The argument is the data it self.
const user = store.getters['entities/users/query']().where(record => record.age > 20).get()
// [User { id: 1, age: 25 }, User { id: 2, age: 30 }]
Or Statement
You may chain where constraints together as well as add or
condition to the query. The orWhere
method accepts the same arguments as the where method.
const user = store.getters['entities/users/query']()
.where('role', 'admin')
.orWhere('name', 'John')
.get()
// [User { id: 1, name: 'John', role: 'user' }, User { id: 2, name: 'Jane', role: 'admin' }]
v0.6.0
Has Many By relationship
In some case, the model it self has all the keys of the related model. Like below example.
{
nodes: {
'1': { id: 1 },
'2': { id: 1 }
},
clusters: {
'1': {
id: 1,
nodes: [1, 2]
}
}
}
As you can see, clusters wants to have has many relationship with nodes, but nodes doesn't have cluster_id
. So you can't use this.hasMany()
because there're no foreign key to look for. In such case you may use this.hasManyBy()
relationship.
import Model from 'vuex-orm/lib/Model'
class Node extends Model {
static entities = 'nodes'
static field () {
return {
id: this.attr(null),
name: this.attr(null)
}
}
}
class Cluster extends Model {
static entities = 'clusters'
static field () {
return {
id: this.attr(null),
nodes: this.hasManyBy(Node, 'nodes')
}
}
}
Now the cluster model will look for nodes by its own nodes
attributes.
Custom Primary Key
Now you can define a static primaryKey
property to override default (default is id
) primary key.
import Model from 'vuex-orm/lib/Model'
class User extends Model {
static entity = 'users'
static primaryKey = 'my_id'
static fields () {
return {
my_id: this.attr(null),
name: this.attr('')
}
}
}
v0.5.0
Actions and Getters from sub modules
Now you can use actions and getters from sub modules.
// Instead of this.
store.dispatch('entities/create', { entity: 'users', data: { id: 1, name: 'John' } })
store.getters['entities/find']('users', 1)
// Now you can do this.
store.dispatch('entities/users/create', { data: { id: 1, name: 'John' } })
store.getters['entities/users/find'](1)
Date mutator
Now you can define date attributes with this.date()
. With this attribute, the value will be converted to Moment.
import Model from 'vuex-orm/lib/Model'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
name: this.attr(''),
created_at: this.date(null)
}
}
}
const user = new User({ id: 1, name: 'John', created_at: '1985-10-10 00:00:00' })
user.created_at.format('MMM D, YYYY')
// Oct 10, 1985
To JSON serialization
Convert a model to JSON with $toJson
method. The $toJson
method is recursive, so all attributes and relations will be converted to JSON.
const json = user.$toJson()
/*
{
id: 1,
name: 'John Doe',
profile: {
id: 1,
user_id: 1,
age: 24,
sex: 'male'
}
}
*/