Decorator


Pinia ORM provides property decorators to Typescript users for better type support when defining the schema for an entity. Simply put, decorators replace the need to define the fields method.

The following example defines a User model using decorators on properties that form the schema for the users entity:

import { Model, Attr, Str, HasMany } from 'pinia-orm'import Post from '@/models/Post'class User extends Model {  static entity = 'users'  @Attr(null)  id!: number | null  @Str('')  name!: string  @HasMany(() => Post, 'userId')  posts!: Post[]}

Of course, you can choose not to use decorators and continue to define the entity schema using the fields method. In such a case, you must explicitly define model class properties that correspond to the field definitions to get correct typings:

import { Model } from 'pinia-orm'import Post from '@/models/Post'class User extends Model {  static entity = 'users'  static fields () {    return {      id: this.attr(),      name: this.string('')      posts: this.hasMany(Post, 'userId')    }  }  id!: number | null  name!: string  posts!: Post[]}

Available Decorators

@Attr

Marks a property on the model as a generic attribute type. For example:

import { Model, Attr } from 'pinia-orm'export class User extends Model {  static entity = 'users'  @Attr(null)  id!: number | null  @Attr('John Doe')  name!: string  @Attr(false)  active!: boolean}

@Str

Marks a property on the model as a string attribute type. For example:

import { Model, Attr, Str } from 'pinia-orm'export class User extends Model {  static entity = 'users'  @Str('')  name!: string  @Str(null, { nullable: true })  address!: string | null}

@Num

Marks a property on the model as a number attribute type. For example:

import { Model, Num } from 'pinia-orm'export class User extends Model {  static entity = 'users'  @Num(0)  count!: number  @Num(null, { nullable: true })  roleId!: number | null}

@Bool

Marks a property on the model as a boolean attribute type. For example:

import { Model, Bool } from 'pinia-orm'export class User extends Model {  static entity = 'users'  @Bool(false)  active!: boolean  @Bool(null, { nullable: true })  visible!: boolean | null}

@Uid

Marks a property on the model as a Uid attribute type. For example:

import { Model, Uid } from 'pinia-orm'class User extends Model {  static entity = 'users'  @Uid()  id!: string}

Mutator

Adds a mutator to a property on the model. For example:

import { Attr, Mutate, Model } from 'pinia-orm'class User extends Model {  static entity = 'users'  @Mutate((value: any) => value.toUpperCase())  @Attr('')    name!: string}console.log(new User({ name: 'john doe' }).name) // 'JOHN DOE'

Relationships

Decorators on relation properties accept the same argument signature as their corresponding field attribute type with the exception that model references should be defined as a closure that return the model constructor (to avoid circular dependencies).

@HasOne

Marks a property on the model as a hasOne attribute type. For example:

import { Model, HasOne } from 'pinia-orm'import Phone from '@/models/Phone'class User extends Model {  static entity = 'users'  @HasOne(() => Phone, 'userId')  phone: Phone | null}

@BelongsTo

Marks a property on the model as a belongsTo attribute type. For example:

import { Model, Attr, BelongsTo } from 'pinia-orm'import User from '@/models/User'class Post extends Model {  static entity = 'posts'  @Attr(null)  userId!: number | null  @BelongsTo(() => User, 'userId')  user!: User | null}

@HasMany

Marks a property on the model as a hasMany attribute type. For example:

import { Model, HasMany } from 'pinia-orm'import Post from '@/models/Post'class User extends Model {  static entity = 'users'  @HasMany(() => Post, 'userId')  posts!: Post[]}

@HasManyBy

Marks a property on the model as a hasManyBy attribute type. For example:

import { Model, HasManyBy } from 'pinia-orm'import Node from '@/models/Node'class Cluster extends Model {  static entity = 'clusters'  @Attr(null)  nodeIds!: number[]  @HasManyBy(() => Node, 'nodesId')  nodes!: Node[]}

@BelongsToMany

Marks a property on the model as a belongsToMany attribute type. For example:

import { Model, HasManyBy } from 'pinia-orm'import Role from '@/models/Role'class User extends Model {  static entity = 'users'  @BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id')  roles!: Role[]}

@MorphOne

Marks a property on the model as a morphOne attribute type. For example:

import { Model, MorphOne } from 'pinia-orm'import Image from '@/models/Image'class User extends Model {  static entity = 'users'  @MorphOne(() => Image, 'imageableId', 'imageableType')  image!: Image | null}

@MorphTo

Marks a property on the model as a morphTo attribute type. For example:

import { Model, MorphTo } from 'pinia-orm'import User from '@/models/User'import Post from '@/models/Post'class Image extends Model {  static entity = 'images'  @Attr(null)  imageableId!: number | null  @Attr(null)  imageableType!: string | null  @MorphTo(() => [User, Post], 'imageableId', 'imageableType')  imageable!: User | Post | null}

@MorphMany

Marks a property on the model as a morphMany attribute type. For example:

import { Model, MorphMany } from 'pinia-orm'import Comment from '@/models/Comment'class Video extends Model {  static entity = 'videos'  @MorphMany(() => Comment, 'commentableId', 'commentableType')  comments!: Comment[]}