casts()

Define casts for fields


string

User.js
import { Model } from 'pinia-orm'class User extends Model {  static entity = 'users'  static fields () {    return {      id: this.attr(null),      firstName: this.string('')    }  }    static casts () {      return {          firstName: 'string'      }  }}

number

User.js
import { Model } from 'pinia-orm'class User extends Model {  static entity = 'users'  static fields () {    return {      id: this.attr(null),      age: this.number(0)    }  }    static casts () {      return {          age: 'number'      }  }}

boolean

User.js
import { Model } from 'pinia-orm'class User extends Model {  static entity = 'users'  static fields () {    return {      id: this.attr(null),      registered: this.boolean(false)    }  }    static casts () {      return {          registered: 'boolean'      }  }}

array

User.js
import { Model } from 'pinia-orm'class User extends Model {  static entity = 'users'  static fields () {    return {      id: this.attr(null),      meta: this.attr({})    }  }    static casts () {      return {          meta: 'array'      }  }}

With Decorator

User.ts
import { Model, Attr, Cast } from 'pinia-orm'class User extends Model {  static entity = 'users'  @Cast('array')  @Attr('{}')    meta!: Record<string, any>}

Typescript Definition

export interface Casts {  [name: string]: typeof CastAttribute | string}function casts(): Casts