Quick Intro to Vuex ORM

Quick Intro to Vuex ORM

If you're looking to make a scalable Vue or Nuxt app, you might consider using Vuex ORM. I've recently used it in a project, and in this article, I'll share with you how it works and why I think you'll like it too.

If you're looking to make a scalable Vue or Nuxt app, you might consider using Vuex ORM. I've recently used it in a project, and in this article, I'll share with you how it works and why I think you'll like it too.

What is Vuex ORM

Vuex introduces some powerful concepts for managing your application state including the store, mutations, actions, and so on.

Vuex ORM is an abstraction of Vuex that allows you to think about your application state in terms of models e.g. Posts, Users, Orders, etc, and CRUD operations, e.g. create, update, delete, etc.

ORM tools (object-relational mapping) transform data between incompatible system using objects. ORMs are very popular for databases.
This allows for a signficant simplification of your code. For example, rather than this.$store.state.commit("UPDATE_USER", { ... }), you can use User.update({ ... }), making your Vue code much easier to reason about.

The other advantages of Vuex ORM are that it reduces boilerplate code by setting up the mutations and getter you'll need automatically, and also makes it easy to work with nested data structures in your application state.

From Vuex to Vuex ORM

As a way of demonstrating the advantages, let's refactor some raw Vuex code using Vuex ORM.

We'll use a classic to-do list example where we can mark a to-do as "done". Here's the store which will represent that:


store: {
  state: { todos: [] },
  mutations: {
    MARK_DONE(state, id) {
      const todo = state.todos.find(todo => todo.id === id);
      todo.done = true;

Let's say we display our to-do items on the home page of the app. We'll use a computed property todos and a v-for to link the to-do items to the template.

When a to-do is clicked, we'll mark it as "done" by making a commit to the MARK_DONE mutation.


    v-for="todo in todos"
  export default {
    computed: {
      todos() {
        return this.$store.state.todos;
    methods: {
      markDone(id) {
        this.$store.state.commit(MARK_DONE, id);

The Vuex ORM way

As I said, Vuex ORM represents data as models. So we'll first create a Todo model and define the fields we'd need like id, title, and done.

Unlike most Vue software, Vuex ORM uses ES6 classes for configuration.

import { Model } from "@vuex-orm/core";

export default class Todo extends Model {
  static entity = "todos";

  static fields () {
    return {
      id: this.string(""),      
      title: this.string(""),
      done: this.boolean(false),

Now it's time to register the model to the Vuex ORM "database", which allows you to use the model.

While we're at it, we can register the Vuex ORM plugin with Vuex.


import VuexORM from "@vuex-orm/core";
import Todo from "./models/Todo";

const database = new VuexORM.Database();
database.register(Todo, {});

const plugin = VuexORM.install(database);

export default {
  plugins: [plugin]

With our Vuex ORM store set up, we can start using it in our components. First, import the model into a component file. Now, rather than using the "weird" syntax of Vuex, we can use standard CRUD methods to query our store:


import Todo from "../store/models/todo";
export default {
  computed: {
    // todos() {
    //   return this.$store.state.todos;
    // }
    todos: () => Todo.all();
  methods: {
    markDone(id) {
      // this.$store.state.commit(MARK_DONE, id);
        where: id,
        data: { done: true }

I don't know about you, but I find that much more readable!

Store config

But hang on, where is the store configuration for the Todo model? Unless you want to customize it, you don't need any! Vuex ORM will automatically create state, mutations, and getters that are aliased to the model API e.g. read, update, find.


Even better, you can add some really handy plugins to Vuex ORM (that's right, a plugin for a plugin for a plugin), include ones to abstract your server communication.

For example, there is an Axios plugin that is almost zero config so long as your model endpoints fit the RESTful pattern.

Let's say when our app loads, it retrieves all the to-do items from the server and pushes them to the store:

created() {
  try {
    let { data } = await this.$http.get("/todos");
    data.forEach(todo => this.$store.state.commit(ADD_TODO, todo));
  } catch (err) {
    // handle error

The Vuex ORM Axios plugin adds additional model methods like fetch which allows you to replace the above code with:

created() {

How easy is that?


There's plenty more to know about Vuex ORM so check out the docs:

Thanks for reading

Top Vue.js Developers in USA

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.

Vue.js is an open-source JavaScript framework that is incredibly progressive and adoptive and majorly used to build a breathtaking user interface. Vue.js is efficient to create advanced web page applications.

Vue.js gets its strength from the flexible JavaScript library to build an enthralling user interface. As the core of Vue.js is concentrated which provides a variety of interactive components for the web and gives real-time implementation. It gives freedom to developers by giving fluidity and eases the integration process with existing projects and other libraries that enables to structure of a highly customizable application.

Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.

Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.

We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.

Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:

  • A team of Vue.js developers of your choice
  • 100% guaranteed client satisfaction
  • Integrity and Transparency
  • Free no-obligation quote
  • Portal development solutions
  • Interactive Dashboards over a wide array of devices
  • Vue.js music and video streaming apps
  • Flexible engagement model
  • A free project manager with your team
  • 24*7 communication with your preferred means

If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.