Releases: vuex-orm/vuex-orm
v0.24.1
v0.24.0
Breaking Change
There is a breaking change to the return value from the persist methods create
, insert
, update
and insertOrUpdate
. Now, all of this method will return all data that has been persisted in following structure.
{
users: [{ id: 1, name: 'John Doe' }],
posts: [{ id: 2, user_id: 1, title: '...' }]
}
This makes the API more consistent overall. Please see more details in the docs at Inserting And Updating Data section.
New Features
LIfecycle hook
Vuex ORM fires several lifecycle hooks while dispatching the actions, allowing you to hook into the particular points in a query lifecycle. Lifecycle hook allows you to easily execute code each time a specific record is saved or updated in the database.
Supported lifecycle hooks are as follows.
beforeCreate
afterCreate
beforeUpdate
afterUpdate
beforeDelete
afterDelete
See how to use lifecycle hooks at here.
Persist options for all persist methods.
When persisting the relation data, now all of the persist methods, you can choose all methods which are create
, insert
, update
, and insertOrUpdate
by passing those options to any of the methods.
// `create` users but `update` posts.
store.dispatch('entities/users/create', {
data: [{ ... }],
update: ['posts']
})
// `insert` users but `insertOrUpdate` posts and `comments`.
store.dispatch('entities/users/insert', {
data: [{ ... }],
insertOrUpdate: ['posts', 'comments']
})
Get deleted records as a return value
delete
method will now return deleted records.
const item = store.dispatch('entities/users/delete', 1)
// User { id: 1, name: 'John Doe' }
Fixes
- #131 HasManyThrough only fetches the last item in the list.
Thanks to @kkyouhei for this wonderful fix!
v0.23.4
v0.23.3
Enhancement
Add new parameter to Query on
method to allow automatic removal of registered hook after next process()
. Also added Query.off()
method. Please see the doc.
Thanks to @cjcrawford for the wonderful PR!
v0.23.2
v0.23.1
v0.23.0
Breaking Change
This version introduces some breaking changes on has
and hasNot
constraint.
Because there was a huge change in relation loading method, I had to separate has
method for counting the record, and filtering the record.
In short, when a user wants to check relationship existence with the relationship count, a user must use has
method. On the other hand, when a user wants to check relation existence with the value of the related record, a user must use whereHas
method (the new method).
store.getters['entities/users/query']().has('posts', 3).get()
store.getters['entities/users/query']().whereHas('posts', (post) => {
post.where('published', true)
}).get()
Fixes
v0.22.0
New Features
Relationship loading improvement
You can now use withAll
, withRecursive
, and special syntax to load multiple relationships. Please see the docs for each detail.
Thanks to @timoschwarzer for this awesome improvement 🎉
Update data with closure
Now you may pass a closure to the data
to update record.
store.dispatch('entities/users/update', {
where: 1,
data (user) {
user.name = 'Jane Doe'
user.arrayField.push(1)
}
})
Fixes
- #101 Fixed where update method not being able to update Array and Object properly.
v0.21.1
New Features
Exposing attribute classes
Now Vuex ORM exposes attribute classes such as Attr
or HasOne
.
import { Attr, HasOne } from '@vuex-orm/core'
Noe plugin author can extend these Attribute as well! See documentation on what kind of classes are available.
Fixes
- Fix where update fails when having falsy primary id such as
0
.
v0.21.0
New Features
Has Many Through Relationship
The "has-many-through" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country might have many Post through an intermediate User. In this example, you could easily gather all posts for a given country. Let's look at the models required to define this relationship:
class Country extends Model {
static entity = 'countries'
static fields () {
return {
id: this.attr(null),
posts: this.hasManyThrough(Post, User, 'country_id', 'user_id')
}
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
country_id: this.attr(null)
}
}
}
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
user_id: this.attr(null)
}
}
}
Though posts do not contain a country_id column, the hasManyThrough
relation provides access to a country's posts. To perform this query, Vuex ORM inspects the country_id
on the intermediate User model. After finding the matching user IDs, they are used to query the Post model.
The first argument passed to the hasManyThrough
method is the final model we wish to access, while the second argument is the intermediate model. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model.
If you would like to customize the local key for the models, you could also pass the fifth argument which is the local key, while the sixth argument is the local key of the intermediate model.
this.hasManyThrough(
Post, // Final model we wish to access.
User, // Intermediate model.
'country_id', // Foreign key on User model.
'user_id', // Foreign key on Post model.
'id', // Local key on Country model.
'id' // Local key on User model.
)