Quick start


Prologue

Note that in this documentation, we borrow many examples and texts from Vuex ORM Next. We would like to credit Vuex ORM Next and the author of the section Kia King Ishii for the beautiful work.

This is a quick starting guide to begin using Pinia ORM. It assumes you have a basic understanding of Pinia. If you are not familiar with Pinia, please visit the Pinia Documentation to learn more.

Setup

To setup Pinia ORM, you must:

  • Install Pinia ORM to Pinia.
  • Define Models.

Don't worry. It's much easier than you think.

Install Pinia ORM

Yarn
yarn add pinia-orm
NPM
npm install pinia-orm --save

Adding the plugin to your pinia store

Vue3
import { createPinia } from 'pinia'import PiniaORM from 'pinia-orm'const pinia = createPinia().use(PiniaORM.install())
Vue2
import { createPinia, PiniaVuePlugin } from 'pinia'import PiniaORM from 'pinia-orm'Vue.use(PiniaVuePlugin)const pinia = createPinia().use(PiniaORM.install())

Define models

Models represent a schema of data that will be stored in Pinia. The schema often follows a servers API response, but it could also be whatever you like it to be.

Models may have relationships with other models. For example, a post could belong to a user, or a post has many comments.

The following examples will demonstrate what these models may look like:

User Model

Fields Method
// User Modelimport { Model } from 'pinia-orm'export default class User extends Model {  // entity is a required property for all models.  static entity = 'users'  // List of all fields (schema) of the post model. `this.string()` declares  // a string field type with a default value as the first argument.  // `this.uid()` declares a unique id if none provided.  static fields () {    return {      id: this.uid(),      name: this.string(''),      email: this.string('')    }  }  // For typescript support of the field include also the next lines  id!: string  name!: string  email!: string}
Decorator Method
// User Modelimport { Model, Uid, Str } from 'pinia-orm'export default class User extends Model {  // entity is a required property for all models.  static entity = 'users'  @Uid() id!: string  @Str('') name!: string  @Str('') email!: string}

Post Model

Fields Method
// Post Modelimport { Model } from 'pinia-orm'import User from './User'export default class Post extends Model {  static entity = 'posts'  // `this.belongsTo(...)` declares this post belongs to a user. The first  // argument is the `User` model class. The second is the field name for  // the foreign key `userId`.  static fields () {    return {      id: this.uid(),      userId: this.attr(null),      title: this.string(''),      body: this.string(''),      published: this.boolean(false),      author: this.belongsTo(User, 'userId')    }  }    id!: string  userId!: string | null  title!: string  body!: string  published!: boolean  author!: User | null}
Decorator Method
// Post Modelimport { Model, Uid, Str, Bool, BelongsTo } from 'pinia-orm'import User from './User'export default class Post extends Model {  static entity = 'posts'  @Uid() id!: string  @Attr(null) userId!: string | null  @Str('') title!: string  @Str('') body!: string  @Bool(false) published!: boolean  @BelongsTo(() => User, 'userId') author!: User | null

All models are declared by extending the Pinia ORM base Model class.

These examples create a User model and a Post model. The Post model has a belongsTo relationship to User defined by the author key. It's now possible to create posts that are associated with users.

You can learn more about models at Model: Getting Started.

You're good to go!

Check out how to use Pinia ORM.