Skip to content

Releases: vuex-orm/vuex-orm

v0.20.2

24 Feb 04:25
Compare
Choose a tag to compare

Fixes

  • #96 Fix auto relation field generation failing.

v0.20.1

23 Feb 14:49
Compare
Choose a tag to compare

Fixes

Fixed model instantiation failing when passing object containing length as property.

v0.20.0

19 Feb 17:01
Compare
Choose a tag to compare

New Features

Polymorphic relationship

Issue: #50

The polymorphic relationship is now available. There is also the many-to-many polymorphic relationship Now you may define;

  • Morph One
  • Morph Many
  • Morph To
  • Morph To Many
  • Morphed By Many

Please see the documentation for more detail.

Update returning updated data

PR: #79

As same as insert or create, the update action now returns updated data as a Promise.

store.dispatch('entities/users/update', { id: 1, age: 24 })
  .then((user) => {
    console.log(user)
  })

// User { id: 1, name: 'John Doe', age: 24 }

When updating many records by specifying closure to the where property, the returned data will always be an array containing all updated data.

store.dispatch('entities/users/update', {
  where: record => record.age === 30,
  data { age: 24 }
).then((users) => {
  console.log(users)
})

/*
  [
    User { id: 1, name: 'John Doe', age: 24 },
    User { id: 2, name: 'Jane Doe', age: 24 }
  ]
*/

Composite primary key support for update

Now update method supports model with the compsite primary key defined.

Thanks to @iNaD for this great PR!

Fixes

  • #87 Many to Many Relationship insert data without relational data breaks

v0.19.0

14 Feb 15:25
Compare
Choose a tag to compare

New Features

Add insert or update action

Issue: #73

Sometimes you might want to insert a set of records which includes already existing and new records. When using the insert action you would replace the dataset of an already existing record. This can cause unexpected side effects.

For example if an API supports dynamic embedding of relationships and doesn't always return all relationships, the relationships would be emptied when missing on insert.

For those cases you can now use the insertOrUpdate action:

// Initial State.
{
  entities: {
    users: {
      data: {
        '1': { id: 1, name: 'John', roles: [3] }
      }
    }
  }
}

// `insertOrUpdate` is going to add new records and update existing
// records (see `update`). Also accepts a single item as data.
store.dispatch('entities/users/insertOrUpdate', {
  data: [
    { id: 1, name: 'Peter' }, 
    { id: 2, name: 'Hank' }
  ]
})

// State after `insertOrUpdate`. Roles for Peter won't be set to empty array
// The new record is inserted with an empty relationship.
{
  entities: {
    users: {
      data: {
        '1': { id: 1, name: 'Peter', roles: [3] }, 
        '2': { id: 2, name: 'Hank', roles: [] }
      }
    }
  }
}

Thanks to @iNaD for this wonderful PR!

v0.18.0

09 Feb 17:47
Compare
Choose a tag to compare

New Features

Get newly created object as a return from actions

Issue: #62

Both create and insert will return the created data as Promise so that you can get them as a return value.

store.dispatch('entities/users/create', {
  data: { id: 1, name: 'John Doe' }
}).then((user) => {
  console.log(user)
})

// User { id: 1, name: 'John Doe' }

Add last method

Issue: #62

As oppose to first method, the last method returns the last matching data.

const user = store.getters['entities/users/query']().last()

Interact With Store From Model

Alternative for calling store methods directly, you may access store instance from a model as well to dispatch actions or call getters.

const user = User.dispatch('create', { data: ... })

// The above code is exactly same as below.

const user = store.dispatch('entities/users/create', { data: ... })

Accessing store from a model does nothing special compared to accessing the store directly. It's just for the convenience. Hence you may choose whichever way that fits your preference.

Learn more at the doc.

v0.17.0

04 Feb 08:44
Compare
Choose a tag to compare

New Features

Many To Many Relationship

Issue: #50 #61

At last, many to many relationship is here! Find out more in the documentation.

Granularly define which properties to insert or to create

PR: #67

Thanks to @timoschwarzer, now you can granularly choose how data will be inserted into the store when you pass data containing multiple relationship records to the create or insert action.

// `create` users but `insert` posts.
store.dispatch('entities/users/create', {
  data: [{ ... }],
  insert: ['posts']
})

// `insert` users but `create` posts and `comments`.
store.dispatch('entities/users/create', {
  data: [{ ... }],
  create: ['posts', 'comments']
})

Define Local Key For The Relationship

You can now add an extra argument to the relationship attributes to define the local key to lookup.

class User extends Model {
  static entity = 'users'

  static fields () {
    return {
      id: this.attr(null),
      local_id: this.attr(null),
      name: this.attr(''),
      profile: this.hasOne(Profile, 'user_id', 'local_id') // <- Local key at 3rg arg.
    }
  }
}

You can find out more at Defining Relationships page in the doc.

Fixes

  • #68 Return empty array on empty to-many relations

    Again, thanks @timoschwarzer for the fix!

v0.16.3

31 Jan 17:22
Compare
Choose a tag to compare

Expose modules to the public and for plugin

Now you can extend following modules when creating plugin!

  • rootGetters
  • subGetters
  • rootActions
  • subActions
  • mutations
const plugin = {
  install (components) {
    components.mutations.setName = (state, name) => {
      state.name = name
    }
  }
}

Add lifecycle hooks to the query

Issue: #41

Now you can subscribe to Query class to hook into its fetching lifecycle! Learn more.

v0.16.2

26 Jan 11:55
Compare
Choose a tag to compare

Model instance in where closure

When closure is used at 1st argument of where clause, the model instance is passed as a 3rd argument for the closure. It's useful when you want to use a model method or mutated property for the condition.

class User extends Model {
  static entity = 'entity'

  static fields () {
    return {
      id: this.attr(null),
      name: this.attr(''),
      role: this.attr('')
    }
  }

  isAdmin () {
    return this.role === 'admin'
  }
}

const user = store.getters['entities/users/query']()
  .where((_record, _query, model) => {
    return model.isAdmin()
  })
  .get()

v0.16.1

23 Jan 16:32
Compare
Choose a tag to compare

Fix

  • Fix custom primary key not working with hasMany and hasOne.

v0.16.0

23 Jan 14:47
Compare
Choose a tag to compare

max and min aggregates

Issue: #45

Now it's able to use max and min aggregates.

const mostLiked = store.getters['entities/posts/query']().max('like')

const cheapest = store.getters['entities/orders/query']().min('price')

Auto increment attribute

Issue: #38

You can now define custom auto increment attribute. this.increment() method will generate field type which will be auto incremented. Autoincrement field must be a number and should not have arguments. The value of this field gets incremented when you create a new record.

class User extends Model {
  static fields () {
    return {
      id: this.increment(),
      name: this.attr('')
    }
  }
}

Composite primary key

You can now also define a composite primary key by passing array of keys.

class Vote extends Model {
  static entity = 'votes'

  static primaryKey = ['user_id', 'vote_id']

  static fields () {
    return {
      user_id: this.attr(''),
      vote_id: this.attr('')
    }
  }
}