casts

String

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

Number

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

Boolean

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

Array

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

Date

User.js
import { Model } from 'pinia-orm'
import { StringCast } from 'pinia-orm/casts'
class User extends Model {
  static entity = 'users'
  static fields() {
    return {
      updated: this.attr(''),
    }
  }
  static casts() {
    return {
      updated: DateCast,
    }
  }
}

With Decorator

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

Typescript Definition

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