mutators

Usage

User.js
import { Model } from 'pinia-orm'
class User extends Model {
  static entity = 'users'
  static fields () {
    return {
      id: this.attr(null),
      firstName: this.attr(''),
      lastName: this.attr('')
    }
  }
  static mutators() {
    return {
      firstName: {
        get: (value: any) => value.toLowerCase(),
        set: (value: any) => value.toUpperCase(),
      },
      lastName(value: any) => value.toLowerCase()
    }
  }
}

With Decorator

User.ts
import { Model } from 'pinia-orm'
import { Attr, Mutate } from 'pinia-orm/decorators'
class User extends Model {
  static entity = 'users'
  @Mutate((value: any) => value.toUpperCase()) @Str('') declare name: string
}

Typescript Definition

export type Mutator<T> = (value: T) => T
export interface MutatorFunctions<T> {
  get?: Mutator<T>
  set?: Mutator<T>
}
export interface Mutators {
  [name: string]: MutatorFunctions<any> | Mutator<any>
}
function mutators(): Mutators