Skip to content

Releases: vuex-orm/vuex-orm

v0.7.2

21 Oct 09:03
Compare
Choose a tag to compare

Fix

  • Fixes bug where circular schema definition leads to infinite loop.

v0.7.1

18 Oct 02:52
Compare
Choose a tag to compare

FIx

  • #7 Fix where clause to handle non string value.

v0.7.0

17 Oct 17:50
Compare
Choose a tag to compare

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

17 Oct 11:59
Compare
Choose a tag to compare

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

17 Oct 10:08
Compare
Choose a tag to compare

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'
    }
  }
*/

v0.4.0

16 Oct 11:30
Compare
Choose a tag to compare

Add hasMany relationship support.

v0.3.1

29 Sep 03:07
Compare
Choose a tag to compare

Make it able to resolve relation of record that has ID of 0.

v0.3.0

26 Sep 15:21
Compare
Choose a tag to compare

Add orderBy method to the getter.

v0.2.2

20 Sep 02:30
Compare
Choose a tag to compare

Fix

  • Update entity to be empty when empty data was passed to the create mutation.

v0.2.1

02 Sep 06:01
Compare
Choose a tag to compare

Added insert method and fixed few bugs.