The Vuex plugin to enable Object-Relational Mapping access to the Vuex Store.
Issue: #527
You may now access the intermediate model through customizable pivot
attribute on the relationships.
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
podcasts: this.belongsToMany(
Podcast,
Subscription,
'user_id',
'podcast_id'
).as('subscription')
}
}
}
const user = User.query().with('podcasts').first()
user.podcasts.forEach((podcast) => {
// Access to pivot model!
console.log(podcast.subscription)
})
Please refer to the documentation for more details.
exists
method available in query chain.Isseu: #486
The exists
method allows you to check wether a query chain would return any records. The method will return either true
or false
.
// Check whether the user store contains any data.
const resultExists = User.exists()
// Check whether an user with id 5 exists.
const resultExists = User.query().where('id', 5).exists()
Issue: #557
Now Database object is also passed to the plugin component options.
It adds as method to many-to-many relationship attribute to let users customize pivot key name.
You can get model objects through store instance. Please refer to issue #514 for the reason why we needed this.
Now you can access models like this.
// String base access.
const User = store.$db.model('users')
// Type base access.
import User from '@/models/User'
const UserModel = store.$db.model(User)
To be able to add this feature, there're few internal breaking changes. It shouldn't affect most of the users.
getModel
are now an instance method.Please refer to the documentation for more details.
Issue #187
Now you can register a new model after you've installed Vuex ORM to Vuex. All you need to do is to call database.register
.
Thanks to @SzNagyMisu for this wonderful PR! 🎉
Model.fields().attribute.value
correctly typed.$id
from toJson()
method.Now you can pass a function as the 1st argument of orderBy
. The function will accept a record that is being sorted, and it should return value to be sorted by.
// Sort user name by its 3rd character.
const users = User.query().orderBy(user => user.name[2]).get()
/*
[
{ id: 4, name: 'Andy' },
{ id: 2, name: 'Roger' },
{ id: 1, name: 'John' }
]
*/
Thanks to @leearaneta for this wonderful PR! 🎉
first
and last
method was returning undefined
instead of null
when data is empty.Now you can use the new this.uid
attribute for the model fields. This attribute will generate a unique ID if the field is not present when inserting a record.
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.uid(),
name: this.string('')
}
}
}
This attribute will generate a unique string value that looks like this.
const user = new User()
user.id // <- '$uid32'
The default UID generation is a really simple one. It will have an incremented number with $uid
prefix. It's going to be unique for single client usage, but it's not a Universally Unique ID (UUID).
If you need stronger ID generation (like UUID or CID), you can pass your desired function that returns the ID.
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.uid(() => uuid())
}
}
}
The increment attribute is now deprecated and will be removed at v2.0.0. Also, the Increment field will now work as an alias to the new UID attribute. Sorry for the inconcinience, though UID is a much simpler approach, and we thought having increment ID is really not that useful in front-end anyway.
whereHas
constraint argument was not assignable to type parameter of type 'Constraint'. Thanks to @Emily-RoseSteyn for the PR!