Vuex ORM is a plugin for Vuex to enable Object-Relational Mapping access to the Vuex Store. But why do we need ORM at the front-end?
Many applications deal with data that is nested or relational in nature. For example, a blog editor could have many Posts, each Post could have many Comments, and both Posts and Comments would be written by a User. Using data in such kind of "nested or relational" structure can be very difficult for JavaScript applications, especially those using the single tree state management system such as Vuex or Redux .
To nicely handle such data, one approach is to split the nested data into separate modules and decouple them from each other. Simply put, it's kind of like treating a portion of your store as if it were a database, and keep that data in a normalized form.
This is an excellent article that describes the difficulty of nested data structures. It also explains how to design normalized state, and Vuex ORM is heavily inspired by it.
Note that in this documentation, we're borrowing many examples and texts from the article. I would like to credit Redux and the author of the section Mark Erikson for the beautiful piece of article.
# Issue with Nested Relational DataLet's say you fetch posts data from a backend server. In many cases, the response might look something like:
Notice that the structure of the data is a bit complex, and some of the data is repeated. It contains lots of author
fields, which is a User. Some of them are the exact same User. If you save this data to the store as is, there will be a concern for several reasons:
Because of this, the recommended approach to managing nested or relational data in a store is to treat a portion of your store as if it were a database, and keep that data in a normalized form.
# Normalizing DataThe basic concepts of normalizing data are:
As you may notice, it's pretty much the same as how ordinary relational database systems manage relations. We could do the same for our store.
An example of a normalized state structure for the blog posts example above might look like:
This state structure is much flatter overall. Compared to the original nested format, this is an improvement in several ways:
Note that a normalized state structure generally implies that more components are connected and each component is responsible for looking up its own data, as opposed to a few connected components looking up large amounts of data and passing all that data downwards. As it turns out, having connected parent components simply pass item ids to connected children is a good pattern for optimizing UI performance as well, so keeping state normalized plays a key role in improving performance.
However, it's still hard to actually organize such normalized data. You must write some kind of logic to "normalize" the input data, and also you must write logic to retrieve the data with also resolving any necessary relationships in mind. Here is where Vuex ORM comes in.
# How Vuex ORM Handles DataVuex ORM will manage both creating (normalizing) and also retrieving data through fluent and sufficient API.
Let's say we want to store above blog posts data. You'll first create a representing "Model" for Post, Comment, and User.
The Model would look like:
Then you may simply call Model's insert
method to insert data.
With this simple method, Vuex ORM will automatically normalize the given data and save them inside Vuex Store State as following structure.
Notice that Vuex ORM will even generate any missing foreign keys (in this case user_id
) during the normalization process.
Now, you can fetch these data using Model's fluent Query Builder just like any ordinary ORM library.
Cool, isn't it? Are you ready to start using Vuex ORM? Let's get started.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4