API Reference
Composables
useRepo()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
import PostRepository from './repositories/Post'
const userRepo = useRepo(User) // Repository<User>
const postRepo = useRepo(PostRepo) // PostRepository
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
import PostRepository from './repositories/Post'
const userRepo = useRepo(User) // Repository<User>
const postRepo = useRepo(PostRepo) // PostRepository
Type Declaration
export function useRepo<M extends Model>(
model: Constructor<M>,
pinia?: Pinia,
): Repository<M>
export function useRepo<R extends Repository>(
repository: Constructor<R>,
pinia?: Pinia,
): R
export function useRepo<M extends Model>(
model: Constructor<M>,
pinia?: Pinia,
): Repository<M>
export function useRepo<R extends Repository>(
repository: Constructor<R>,
pinia?: Pinia,
): R
Helpers
useCollect()
For Details what each function can do, look at the separate composable for it e.g.
min->useMin
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useCollect } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// order a collection by 'name' attributes
useCollect(users).orderBy('name')
// get the min of the 'age' attribute
useCollect(users).min('age')
// get the max of the 'age' attribute
useCollect(users).max('age')
// get the sum of the 'age' attribute
useCollect(users).sum('age')
// sort by 'age' attribute
useCollect(users).sortBy('age')
// get all values in 'age' attribute
useCollect(users).pluck('age')
// get all primary keys
useCollect(users).keys()
import { useRepo } from 'pinia-plugin-orm'
import { useCollect } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// order a collection by 'name' attributes
useCollect(users).orderBy('name')
// get the min of the 'age' attribute
useCollect(users).min('age')
// get the max of the 'age' attribute
useCollect(users).max('age')
// get the sum of the 'age' attribute
useCollect(users).sum('age')
// sort by 'age' attribute
useCollect(users).sortBy('age')
// get all values in 'age' attribute
useCollect(users).pluck('age')
// get all primary keys
useCollect(users).keys()
Type Declaration
export interface UseCollect<M extends Model = Model> {
sum: (field: string) => number
min: (field: string) => number
max: (field: string) => number
pluck: (field: string) => any[]
groupBy: (fields: string[] | string) => Record<string, Collection<M>>
sortBy: (sort: sorting<M>, flags?: SortFlags) => M[]
keys: () => string[]
}
export function useCollect<M extends Model = Model>(models: Collection<M>): UseCollect<M>
export interface UseCollect<M extends Model = Model> {
sum: (field: string) => number
min: (field: string) => number
max: (field: string) => number
pluck: (field: string) => any[]
groupBy: (fields: string[] | string) => Record<string, Collection<M>>
sortBy: (sort: sorting<M>, flags?: SortFlags) => M[]
keys: () => string[]
}
export function useCollect<M extends Model = Model>(models: Collection<M>): UseCollect<M>
useGroupBy()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useGroupBy } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// group by the 'name' attribute
useGroupBy(users, 'name')
// group by the 'name' and 'age' attribute
useGroupBy(users, ['name', 'age'])
import { useRepo } from 'pinia-plugin-orm'
import { useGroupBy } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// group by the 'name' attribute
useGroupBy(users, 'name')
// group by the 'name' and 'age' attribute
useGroupBy(users, ['name', 'age'])
Type Declaration
export function useGroupBy<T>(models: T[], fields: string[] | string): Record<string, T[]>
export function useGroupBy<T>(models: T[], fields: string[] | string): Record<string, T[]>
useKeys()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useKeys } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// retrieve all primary keys
useKeys(users)
import { useRepo } from 'pinia-plugin-orm'
import { useKeys } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// retrieve all primary keys
useKeys(users)
Type Declaration
export function useKeys(models: Collection): string[]
export function useKeys(models: Collection): string[]
useMax()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useMax } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the max of the 'age' attribute
useMax(users, 'age')
// get the max of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useMax(users, 'role.title')
import { useRepo } from 'pinia-plugin-orm'
import { useMax } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the max of the 'age' attribute
useMax(users, 'age')
// get the max of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useMax(users, 'role.title')
Type Declaration
export function useMax(models: Collection, field: string): number
export function useMax(models: Collection, field: string): number
useMin()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useMin } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the min of the 'age' attribute
useMin(users, 'age')
// get the min of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useMin(users, 'role.title')
import { useRepo } from 'pinia-plugin-orm'
import { useMin } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the min of the 'age' attribute
useMin(users, 'age')
// get the min of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useMin(users, 'role.title')
Type Declaration
export function useMin(models: Collection, field: string): number
export function useMin(models: Collection, field: string): number
usePluck()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { usePluck } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// retrieve all values of the 'age' attribute
usePluck(users, 'age')
// retrieve all values of the 'role.title' attribute. The dot notation works only for 1n1 Relations
usePluck(users, 'role.title')
import { useRepo } from 'pinia-plugin-orm'
import { usePluck } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// retrieve all values of the 'age' attribute
usePluck(users, 'age')
// retrieve all values of the 'role.title' attribute. The dot notation works only for 1n1 Relations
usePluck(users, 'role.title')
Type Declaration
export function usePluck(models: Collection, field: string): any[]
export function usePluck(models: Collection, field: string): any[]
useSortBy()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useSortBy } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// sort by the 'name' attribute
useSortBy(users, 'name')
// sort by the `name` attribute case insensitive
useSortBy(users, 'name', 'SORT_FLAG_CASE')
// sorts the collection by 'name' descending and then by 'lastname' ascending
useSortBy(users, [
['name', 'desc'],
['lastname', 'asc'],
])
// sort by the 'age' attribute
useSortBy(users, (model) => model.age)
import { useRepo } from 'pinia-plugin-orm'
import { useSortBy } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// sort by the 'name' attribute
useSortBy(users, 'name')
// sort by the `name` attribute case insensitive
useSortBy(users, 'name', 'SORT_FLAG_CASE')
// sorts the collection by 'name' descending and then by 'lastname' ascending
useSortBy(users, [
['name', 'desc'],
['lastname', 'asc'],
])
// sort by the 'age' attribute
useSortBy(users, (model) => model.age)
Type Declaration
export type sorting<T> = ((record: T) => any) | string | [string, 'asc' | 'desc'][]
export type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE'
export function useSortBy<T>(collection: T[], sort: sorting<T>, flags?: SortFlags): T[]
export type sorting<T> = ((record: T) => any) | string | [string, 'asc' | 'desc'][]
export type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE'
export function useSortBy<T>(collection: T[], sort: sorting<T>, flags?: SortFlags): T[]
useSum()
Usage
import { useRepo } from 'pinia-plugin-orm'
import { useSum } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the sum of the 'age' attribute
useSum(users, 'age')
// get the sum of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useSum(users, 'role.title')
import { useRepo } from 'pinia-plugin-orm'
import { useSum } from 'pinia-plugin-orm/dist/helpers'
import User from './models/User'
const users = useRepo(User).all()
// get the sum of the 'age' attribute
useSum(users, 'age')
// get the sum of the 'role.title' attribute. The dot notation works only for 1n1 Relations
useSum(users, 'role.title')
Type Declaration
export function useSum(models: Collection, field: string): number
export function useSum(models: Collection, field: string): number
Model - fields(Attr)
attr
Usage
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
name: this.attr('Elone Hoo'),
address: this.attr(() => 'Address'),
}
}
}
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
name: this.attr('Elone Hoo'),
address: this.attr(() => 'Address'),
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Attr('') declare name: string
@Attr(() => 'street') declare address: string
}
import { Model } from 'pinia-plugin-orm'
import { Attr } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Attr('') declare name: string
@Attr(() => 'street') declare address: string
}
Typescript Declarations
function attr(value: any | (() => any)): Attr
function attr(value: any | (() => any)): Attr
boolean
Usage
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
published: this.boolean(false),
released: this.boolean(() => false),
}
}
}
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
published: this.boolean(false),
released: this.boolean(() => false),
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Bool, Num } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Bool(false) declare published: boolean
@Bool(() => false) declare released: boolean
}
import { Model } from 'pinia-plugin-orm'
import { Bool, Num } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Bool(false) declare published: boolean
@Bool(() => false) declare released: boolean
}
Typescript Declarations
function boolean(value: boolean | null | (() => boolean | null)): Bool
function boolean(value: boolean | null | (() => boolean | null)): Bool
number
Usage
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0)
extraId: this.number(() => 0)
}
}
}
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0)
extraId: this.number(() => 0)
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Num } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Num(() => 0) declare extraId: number
}
import { Model } from 'pinia-plugin-orm'
import { Num } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Num(() => 0) declare extraId: number
}
Typescript Declarations
function number(value: number | null | (() => number | null)): Number
function number(value: number | null | (() => number | null)): Number
number
Usage
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string('')
address: this.string(() => 'street')
}
}
}
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string('')
address: this.string(() => 'street')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Num, Str } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Str('') declare name: string
@Str(() => 'street') declare address: string
}
import { Model } from 'pinia-plugin-orm'
import { Num, Str } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Str('') declare name: string
@Str(() => 'street') declare address: string
}
Typescript Declarations
function string(value: string | null | (() => string | null)): String
function string(value: string | null | (() => string | null)): String
uid
Usage
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.uid()
}
}
}
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.uid()
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Uid } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Uid() declare id: string
}
import { Model } from 'pinia-plugin-orm'
import { Uid } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Uid() declare id: string
}
Typescript Declarations
function uid(): Uid
function uid(): Uid
Model - fields(Rel)
belongsToMany
Usage
import { Model } from 'pinia-plugin-orm'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, BelongsToMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id') declare roles: Role[]
}
import { Model } from 'pinia-plugin-orm'
import { Attr, BelongsToMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Role from './Role'
import RoleUser from './RoleUser'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@BelongsToMany(() => Role, () => RoleUser, 'user_id', 'role_id') declare roles: Role[]
}
Typescript Declarations
function belongsToMany(
related: typeof Model,
pivot: typeof Model,
foreignPivotKey: string,
relatedPivotKey: string,
parentKey?: string,
relatedKey?: string,
): BelongsToMany
function belongsToMany(
related: typeof Model,
pivot: typeof Model,
foreignPivotKey: string,
relatedPivotKey: string,
parentKey?: string,
relatedKey?: string,
): BelongsToMany
belongsTo
Usage
import { Model } from 'pinia-plugin-orm'
import User from './User'
class Phone extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
userId: this.attr(null),
number: this.string(''),
user: this.belongsTo(User, 'userId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import User from './User'
class Phone extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
userId: this.attr(null),
number: this.string(''),
user: this.belongsTo(User, 'userId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, BelongsTo, Str } from 'pinia-plugin-orm/dist/decorators'
import User from './User'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Attr(null) declare userId: number | null
@Str('') declare number: string
@BelongsTo(() => User, 'userId') declare user: User
}
import { Model } from 'pinia-plugin-orm'
import { Attr, BelongsTo, Str } from 'pinia-plugin-orm/dist/decorators'
import User from './User'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Attr(null) declare userId: number | null
@Str('') declare number: string
@BelongsTo(() => User, 'userId') declare user: User
}
Typescript Declarations
function belongsTo(
related: typeof Model,
foreignKey: string | string[],
ownerKey?: string | string[],
): BelongsTo
function belongsTo(
related: typeof Model,
foreignKey: string | string[],
ownerKey?: string | string[],
): BelongsTo
hasManyBy
Usage
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
postIds: this.attr([]),
title: this.string(''),
comments: this.hasManyBy(Comment, 'postId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
postIds: this.attr([]),
title: this.string(''),
comments: this.hasManyBy(Comment, 'postId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasManyBy, Str } from 'pinia-plugin-orm/dist/decorators'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
@Attr(null) declare id: number | null
@Attr([]) declare postIds: number[]
@Str('') declare title: string
@HasManyBy(() => Comment, 'postIds') declare comments: Comment[]
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasManyBy, Str } from 'pinia-plugin-orm/dist/decorators'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
@Attr(null) declare id: number | null
@Attr([]) declare postIds: number[]
@Str('') declare title: string
@HasManyBy(() => Comment, 'postIds') declare comments: Comment[]
}
Typescript Declarations
function hasManyBy(
related: typeof Model,
foreignKey: string,
ownerKey?: string,
): HasManyBy
function hasManyBy(
related: typeof Model,
foreignKey: string,
ownerKey?: string,
): HasManyBy
hasManyThrough
Usage
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Country extends Model {
static entity = 'countries'
static fields () {
return {
id: this.attr(null),
posts: this.hasManyThrough(Post, User, 'country_id', 'user_id')
}
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
country_id: this.attr(null)
}
}
}
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
user_id: this.attr(null)
}
}
}
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Country extends Model {
static entity = 'countries'
static fields () {
return {
id: this.attr(null),
posts: this.hasManyThrough(Post, User, 'country_id', 'user_id')
}
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
country_id: this.attr(null)
}
}
}
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
user_id: this.attr(null)
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasManyThrough, Str } from 'pinia-plugin-orm/dist/decorators'
class Country extends Model {
static entity = 'countries'
@Attr() declare id: number
@HasManyThrough(() => Post, () => User, 'countryId', 'userId')
declare posts: Post[]
}
class Post extends Model {
static entity = 'posts'
@Attr() declare id: number
@Attr() declare userId: number
@Str('') declare title: string
}
class User extends Model {
static entity = 'users'
@Attr() declare id: number
@Attr() declare countryId: number
@Str('') declare name: string
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasManyThrough, Str } from 'pinia-plugin-orm/dist/decorators'
class Country extends Model {
static entity = 'countries'
@Attr() declare id: number
@HasManyThrough(() => Post, () => User, 'countryId', 'userId')
declare posts: Post[]
}
class Post extends Model {
static entity = 'posts'
@Attr() declare id: number
@Attr() declare userId: number
@Str('') declare title: string
}
class User extends Model {
static entity = 'users'
@Attr() declare id: number
@Attr() declare countryId: number
@Str('') declare name: string
}
Typescript Declarations
function hasManyThrough(
related: typeof Model,
through: typeof Model,
firstKey: string,
secondKey: string,
localKey?: string,
secondLocalKey?: string,
): HasManyThrough
function hasManyThrough(
related: typeof Model,
through: typeof Model,
firstKey: string,
secondKey: string,
localKey?: string,
secondLocalKey?: string,
): HasManyThrough
hasMany
Usage
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
title: this.string(''),
comments: this.hasMany(Comment, 'postId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
static fields () {
return {
id: this.attr(null),
title: this.string(''),
comments: this.hasMany(Comment, 'postId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
@Attr(null) declare id: number | null
@Str('') declare title: string
@HasMany(() => Comment, 'postId') declare comments: Comment[]
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Comment from './Comment'
class Post extends Model {
static entity = 'posts'
@Attr(null) declare id: number | null
@Str('') declare title: string
@HasMany(() => Comment, 'postId') declare comments: Comment[]
}
Typescript Declarations
function hasMany(
related: typeof Model,
foreignKey: string | string[],
localKey?: string | string[],
): HasMany
function hasMany(
related: typeof Model,
foreignKey: string | string[],
localKey?: string | string[],
): HasMany
hasOne
Usage
import { Model } from 'pinia-plugin-orm'
import Phone from './Phone'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
name: this.string(''),
phone: this.hasOne(Phone, 'userId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Phone from './Phone'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.attr(null),
name: this.string(''),
phone: this.hasOne(Phone, 'userId')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasOne, Str } from 'pinia-plugin-orm/dist/decorators'
import Phone from './Phone'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@HasOne(() => Phone, 'userId') declare phone: Phone
}
import { Model } from 'pinia-plugin-orm'
import { Attr, HasOne, Str } from 'pinia-plugin-orm/dist/decorators'
import Phone from './Phone'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@HasOne(() => Phone, 'userId') declare phone: Phone
}
Typescript Declarations
function hasOne(
related: typeof Model,
foreignKey: string | string[],
localKey?: string | string[],
): HasOne
function hasOne(
related: typeof Model,
foreignKey: string | string[],
localKey?: string | string[],
): HasOne
morphMany
Usage
import { Model } from 'pinia-plugin-orm'
import Image from './Image'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string(''),
images: this.morphMany(Image, 'imageableId', 'imageableType')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Image from './Image'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string(''),
images: this.morphMany(Image, 'imageableId', 'imageableType')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Image from './Image'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@MorphMany(() => Image, 'imageableId', 'imageableType') declare images: Image[]
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphMany, Str } from 'pinia-plugin-orm/dist/decorators'
import Image from './Image'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@MorphMany(() => Image, 'imageableId', 'imageableType') declare images: Image[]
}
Typescript Declarations
function morphMany(
related: typeof Model,
id: string,
type: string,
localKey?: string,
): MorphMany
function morphMany(
related: typeof Model,
id: string,
type: string,
localKey?: string,
): MorphMany
morphOne
Usage
import { Model } from 'pinia-plugin-orm'
import Image from './Image'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string(''),
image: this.morphOne(Image, 'imageableId', 'imageableType')
}
}
}
import { Model } from 'pinia-plugin-orm'
import Image from './Image'
class User extends Model {
static entity = 'users'
static fields () {
return {
id: this.number(0),
name: this.string(''),
image: this.morphOne(Image, 'imageableId', 'imageableType')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphOne, Str } from 'pinia-plugin-orm/dist/decorators'
import Image from './Image'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@MorphOne(() => Image, 'imageableId', 'imageableType') declare image: Image
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphOne, Str } from 'pinia-plugin-orm/dist/decorators'
import Image from './Image'
class User extends Model {
static entity = 'users'
@Attr(null) declare id: number | null
@Str('') declare name: string
@MorphOne(() => Image, 'imageableId', 'imageableType') declare image: Image
}
Typescript Declarations
function morphOne(
related: typeof Model,
id: string,
type: string,
localKey?: string,
): MorphOne
function morphOne(
related: typeof Model,
id: string,
type: string,
localKey?: string,
): MorphOne
morphTo
Usage
import { Model } from 'pinia-plugin-orm'
import User from './Image'
import Post from './Post'
class Image extends Model {
static entity = 'images'
static fields () {
return {
id: this.number(0),
url: this.string(''),
imageableId: this.number(0),
imageableType: this.string(''),
imageable: this.morphTo(
[User, Post],
'imageableId',
'imageableType'
)
}
}
}
import { Model } from 'pinia-plugin-orm'
import User from './Image'
import Post from './Post'
class Image extends Model {
static entity = 'images'
static fields () {
return {
id: this.number(0),
url: this.string(''),
imageableId: this.number(0),
imageableType: this.string(''),
imageable: this.morphTo(
[User, Post],
'imageableId',
'imageableType'
)
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphTo, Str } from 'pinia-plugin-orm/dist/decorators'
import User from './Image'
import Post from './Post'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Str('') declare url: string
@Num(0) declare imageableId: number
@Str('') declare imageableType: string
@MorphTo(() => [User, Post], 'imageableId', 'imageableType') declare imageable: Post[] | User[]
}
import { Model } from 'pinia-plugin-orm'
import { Attr, MorphTo, Str } from 'pinia-plugin-orm/dist/decorators'
import User from './Image'
import Post from './Post'
class User extends Model {
static entity = 'users'
@Num(0) declare id: number
@Str('') declare url: string
@Num(0) declare imageableId: number
@Str('') declare imageableType: string
@MorphTo(() => [User, Post], 'imageableId', 'imageableType') declare imageable: Post[] | User[]
}
Typescript Declarations
function morphTo(
related: typeof Model[],
id: string,
type: string,
ownerKey = '',
): MorphTo
function morphTo(
related: typeof Model[],
id: string,
type: string,
ownerKey = '',
): MorphTo
Model - Options
baseEntity
WARNING
This field is required for a class which is inheriting one of the classes defined
Look at Single Table Inheritance for more detail
Usage
class Adult extends Person {
static entity = 'adult'
static baseEntity = 'person'
static fields () {
return {
...super.fields(),
job: this.attr('')
}
}
}
Copy to clipboard
class Adult extends Person {
static entity = 'adult'
static baseEntity = 'person'
static fields () {
return {
...super.fields(),
job: this.attr('')
}
}
}
Copy to clipboard
:::
Typescript Declarations
const baseEntity: string = undefined
const baseEntity: string = undefined
casts
Usage
import { Model } from 'pinia-plugin-orm'
import { StringCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
firstName: this.string('')
}
}
static casts() {
return {
firstName: StringCast
}
}
import { Model } from 'pinia-plugin-orm'
import { StringCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
firstName: this.string('')
}
}
static casts() {
return {
firstName: StringCast
}
}
import { Model } from 'pinia-plugin-orm'
import { NumberCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
age: this.number(0)
}
}
static casts() {
return {
age: NumberCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { NumberCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
age: this.number(0)
}
}
static casts() {
return {
age: NumberCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { BooleanCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
registered: this.boolean(false)
}
}
static casts() {
return {
registered: BooleanCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { BooleanCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
registered: this.boolean(false)
}
}
static casts() {
return {
registered: BooleanCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { ArrayCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
meta: this.attr({})
}
}
static casts() {
return {
meta: ArrayCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { ArrayCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
id: this.attr(null),
meta: this.attr({})
}
}
static casts() {
return {
meta: ArrayCast
}
}
}
import { Model } from 'pinia-plugin-orm'
import { StringCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
updated: this.attr(''),
}
}
static casts() {
return {
updated: DateCast,
}
}
}
import { Model } from 'pinia-plugin-orm'
import { StringCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
static fields() {
return {
updated: this.attr(''),
}
}
static casts() {
return {
updated: DateCast,
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Cast } from 'pinia-plugin-orm/dist/decorators'
import { ArrayCast } from 'pinia-plugin-orm/dist/casts'
class User extends Model {
static entity = 'users'
@Cast(() => ArrayCast) @Attr('{}') declare meta: Record<string, any>
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Cast } from 'pinia-plugin-orm/dist/decorators'
import { ArrayCast } from 'pinia-plugin-orm/dist/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
export interface Casts {
[name: string]: typeof CastAttribute
}
function casts(): Casts
config
Usage
class User extends Model {
static entity = 'users'
// activate meta data to be saved with this model
static config = {
withMeta: true
}
static fields () {
return {
id: this.uid()
}
}
}
class User extends Model {
static entity = 'users'
// activate meta data to be saved with this model
static config = {
withMeta: true
}
static fields () {
return {
id: this.uid()
}
}
}
Typescript Definition
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
visible?: string[]
}
const config: ModelConfigOptions = undefined
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
visible?: string[]
}
const config: ModelConfigOptions = undefined
entity
WARNING
This field is required and must be set in every model
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null)
}
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null)
}
}
}
Typescript Declarations
const entity: string = undefined
const entity: string = undefined
hidden
WARNING
Don't hide the id or you won't find any record 😉
Usage
class User extends Model {
static entity = 'users'
// only return fields "name" and "phone" for this model by default
static hidden = ['secret']
static fields () {
return {
id: this.uid(),
name: this.string(''),
phone: this.number(0),
secret: this.string('')
}
}
}
class User extends Model {
static entity = 'users'
// only return fields "name" and "phone" for this model by default
static hidden = ['secret']
static fields () {
return {
id: this.uid(),
name: this.string(''),
phone: this.number(0),
secret: this.string('')
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Hidden, Uid } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Uid() declare id: string
@Attr('{}') declare name: string
@Hidden() @Attr('{}') declare secret: string
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Hidden, Uid } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Uid() declare id: string
@Attr('{}') declare name: string
@Hidden() @Attr('{}') declare secret: string
}
Typescript Declarations
const visible: hidden[] = []
const visible: hidden[] = []
mutators
Usage
import { Model } from 'pinia-plugin-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()
}
}
}
import { Model } from 'pinia-plugin-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()
}
}
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Mutate } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Mutate((value: any) => value.toUpperCase()) @Str('') declare name: string
}
import { Model } from 'pinia-plugin-orm'
import { Attr, Mutate } from 'pinia-plugin-orm/dist/decorators'
class User extends Model {
static entity = 'users'
@Mutate((value: any) => value.toUpperCase()) @Str('') declare name: string
}
Typescript Declarations
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
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
piniaOptions
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null)
}
}
static piniaOptions = {
persist: true
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null)
}
}
static piniaOptions = {
persist: true
}
}
Typescript Declarations
const piniaOptions: DefineStoreOptionsBase<DataStoreState, DataStore> = {}
const piniaOptions: DefineStoreOptionsBase<DataStoreState, DataStore> = {}
primaryKey
NOTE
By default, the field id is used as primary key.
Usage
class User extends Model {
static entity = 'users'
static primaryKey = 'userId'
static fields () {
return {
userId: this.attr(null)
}
}
}
class User extends Model {
static entity = 'users'
static primaryKey = 'userId'
static fields () {
return {
userId: this.attr(null)
}
}
}
class RoleUser extends Model {
static entity = 'roleUser'
static primaryKey = ['role_id', 'user_id']
static fields () {
return {
role_id: this.attr(null),
user_id: this.attr(null)
}
}
}
class RoleUser extends Model {
static entity = 'roleUser'
static primaryKey = ['role_id', 'user_id']
static fields () {
return {
role_id: this.attr(null),
user_id: this.attr(null)
}
}
}
Typescript Declarations
const primaryKey: string | string[] = 'id'
const primaryKey: string | string[] = 'id'
typeKey
Look at Single Table Inheritance for more detail
Usage
import Person from './models/Person'
import Adult from './models/Adult'
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static typeKey = 'person_type'
static types () {
return {
PERSON: Person,
ADULT: Adult
}
}
static fields () {
return {
id: this.uid(),
name: this.string()
}
}
}
import Person from './models/Person'
import Adult from './models/Adult'
import { Model } from 'pinia-plugin-orm'
class User extends Model {
static entity = 'users'
static typeKey = 'person_type'
static types () {
return {
PERSON: Person,
ADULT: Adult
}
}
static fields () {
return {
id: this.uid(),
name: this.string()
}
}
}
Typescript Declarations
const typeKey: string = 'type'
const typeKey: string = 'type'
types
NOTE
Look at Single Table Inheritance for more detail
Usage
class Person extends Model {
static entity = 'person'
static types () {
return {
PERSON: Person,
ADULT: Adult
}
}
static fields () {
return {
id: this.attr(null),
name: this.attr('')
}
}
}
class Person extends Model {
static entity = 'person'
static types () {
return {
PERSON: Person,
ADULT: Adult
}
}
static fields () {
return {
id: this.attr(null),
name: this.attr('')
}
}
}
Typescript Declarations
export interface InheritanceTypes {
[key: string]: typeof Model
}
function $types(): InheritanceTypes
export interface InheritanceTypes {
[key: string]: typeof Model
}
function $types(): InheritanceTypes
visible
NOTE
The primary key of the model will always be added to the visible list so the record can still be find.
Usage
class User extends Model {
static entity = 'users'
// only return fields "name" and "phone" for this model by default
static visible = ['name', 'phone']
static fields () {
return {
id: this.uid(),
name: this.string(''),
phone: this.number(0),
secret: this.string('')
}
}
}
class User extends Model {
static entity = 'users'
// only return fields "name" and "phone" for this model by default
static visible = ['name', 'phone']
static fields () {
return {
id: this.uid(),
name: this.string(''),
phone: this.number(0),
secret: this.string('')
}
}
}
Typescript Declarations
const visible: string[] = []
const visible: string[] = []
Model - Hooks
created
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static created (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static created (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
Typescript Declarations
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const created: AfterHook = () => {}
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const created: AfterHook = () => {}
creating
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static creating (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static creating (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
Typescript Declarations
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const creating: BeforeHook = () => {}
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const creating: BeforeHook = () => {}
deleted
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static deleted (model) {
// check value saved
console.log(model.published)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static deleted (model) {
// check value saved
console.log(model.published)
}
}
Typescript Declarations
export interface AfterHook<M extends Model = Model> {
(model: M): void
}
const deleted: AfterHook = () => {}
export interface AfterHook<M extends Model = Model> {
(model: M): void
}
const deleted: AfterHook = () => {}
deleting
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static deleting (model) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static deleting (model) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
}
}
Typescript Declarations
export interface BeforeHook<M extends Model = Model> {
(model: M): void | boolean
}
const deleting: BeforeHook = () => {}
export interface BeforeHook<M extends Model = Model> {
(model: M): void | boolean
}
const deleting: BeforeHook = () => {}
retrieved
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static retrieved (model) {
// check value saved
console.log(model.published)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static retrieved (model) {
// check value saved
console.log(model.published)
}
}
Typescript Declarations
export interface AfterHook<M extends Model = Model> {
(model: M): void
}
const retrieved: AfterHook = () => {}
export interface AfterHook<M extends Model = Model> {
(model: M): void
}
const retrieved: AfterHook = () => {}
saved
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static saved (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static saved (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
Typescript Declarations
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const saved: AfterHook = () => {}
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const saved: AfterHook = () => {}
saveing
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static saving (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static saving (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
Typescript Declarations
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const saving: BeforeHook = () => {}
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const saving: BeforeHook = () => {}
updated
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static updated (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static updated (model, record) {
// check value saved
console.log(model.published)
// check original data
console.log(record)
}
}
Typescript Declarations
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const updated: AfterHook = () => {}
export interface AfterHook<M extends Model = Model> {
(model: M, record?: Element): void
}
const updated: AfterHook = () => {}
updating
Usage
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static updating (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
class User extends Model {
static entity = 'users'
static fields () {
return {
userId: this.attr(null),
published: this.attr(false)
}
}
static updating (model, record) {
// change values before saving
model.published = true
if (model.userId === 2) {
// prevent model with userId 2 being saved in the store
return false
}
// check original data
console.log(record)
}
}
Typescript Declarations
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const updating: BeforeHook = () => {}
export interface BeforeHook<M extends Model = Model> {
(model: M, record?: Element): void | boolean
}
const updating: BeforeHook = () => {}
Model - Function
$getAttributes()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
@HasMany(() => Post, 'userId') declare posts: Post[]
}
const user = new User({ id: 1, name: 'Elone Hoo', posts: [{ id: 1, title: 'Merry Christmas' }] })
user.$getAttributes()
// {
// id: 1,
// name: 'Elone Hoo',
// }
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
@HasMany(() => Post, 'userId') declare posts: Post[]
}
const user = new User({ id: 1, name: 'Elone Hoo', posts: [{ id: 1, title: 'Merry Christmas' }] })
user.$getAttributes()
// {
// id: 1,
// name: 'Elone Hoo',
// }
Typescript Declarations
function $getAttributes(): Element
function $getAttributes(): Element
$getIndexId()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getIndexId()
// -> '1'
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getIndexId()
// -> '1'
Typescript Declarations
function $getIndexId(record?: Element): string
function $getIndexId(record?: Element): string
$getKeyName()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getKeyName()
// -> 'id'
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getKeyName()
// -> 'id'
Typescript Declarations
function $getKeyName(): string | string[]
function $getKeyName(): string | string[]
$getKey()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getKey()
// -> 1
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getKey()
// -> 1
Typescript Declarations
function $getKey(record?: Element): string | number | (string | number)[] | null
function $getKey(record?: Element): string | number | (string | number)[] | null
$getLocalKey()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getLocalKey()
// -> 'id'
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$getLocalKey()
// -> 'id'
Typescript Declarations
function $getLocalKey(): string
function $getLocalKey(): string
$getOriginal()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.name = 'Vue JS Amsterdam'
user.$getOriginal()
// {
// id: 1,
// name: 'Elone Hoo',
// }
console.log(user.name)
// 'Vue JS Amsterdam'
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.name = 'Vue JS Amsterdam'
user.$getOriginal()
// {
// id: 1,
// name: 'Elone Hoo',
// }
console.log(user.name)
// 'Vue JS Amsterdam'
Typescript Declarations
function $getOriginal(): Element
function $getOriginal(): Element
$hasCompositeKey()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$hasCompositeKey()
// -> false
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.$hasCompositeKey()
// -> false
Typescript Declarations
function $hasCompositeKey(): boolean
function $hasCompositeKey(): boolean
$isDirty()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
// Returns false
user.$isDirty()
user.name = 'HuChengYe'
// Returns true
user.$isDirty()
// Returns true
user.$isDirty('name')
// Returns false
user.$isDirty('id')
// Throws an error because you are checking a not existing attribute of `User`
user.$isDirty('lastName')
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
// Returns false
user.$isDirty()
user.name = 'HuChengYe'
// Returns true
user.$isDirty()
// Returns true
user.$isDirty('name')
// Returns false
user.$isDirty('id')
// Throws an error because you are checking a not existing attribute of `User`
user.$isDirty('lastName')
Typescript Declarations
function $isDirty($attribute?: keyof ModelFields): Boolean
function $isDirty($attribute?: keyof ModelFields): Boolean
$refresh()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.name = 'Vue JS Amsterdam'
console.log(user.name)
// 'Vue JS Amsterdam'
user.$refresh()
console.log(user.name)
// 'Elone Hoo'
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
}
const user = new User({ id: 1, name: 'Elone Hoo' })
user.name = 'Vue JS Amsterdam'
console.log(user.name)
// 'Vue JS Amsterdam'
user.$refresh()
console.log(user.name)
// 'Elone Hoo'
Typescript Declarations
function $refresh(): Model
function $refresh(): Model
$toJson()
Usage
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
@HasMany(() => Post, 'userId') declare posts: Post[]
}
const user = new User({ id: 1, name: 'Elone Hoo', posts: [{ id: 1, title: 'Merry Christmas' }] })
user.$toJson()
// {
// id: 1,
// name: 'Elone Hoo',
// posts: [{ id: 1, title: 'Merry Christmas' }],
// }
class User extends Model {
static entity = 'users'
@Attr('') declare id: number
@Str('') declare name: string
@HasMany(() => Post, 'userId') declare posts: Post[]
}
const user = new User({ id: 1, name: 'Elone Hoo', posts: [{ id: 1, title: 'Merry Christmas' }] })
user.$toJson()
// {
// id: 1,
// name: 'Elone Hoo',
// posts: [{ id: 1, title: 'Merry Christmas' }],
// }
Typescript Declarations
function $toJson(model?: Model, options: ModelOptions = {}): Element
function $toJson(model?: Model, options: ModelOptions = {}): Element
Query
all()
WARNING
The difference with the get is that this method will not process any query chain. It'll always retrieve all models.
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.query().all() // User[] - all
const usersPreName = userRepo.where('prename', 'John').all() // User[] - still all User !
Copy to clipboard
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.query().all() // User[] - all
const usersPreName = userRepo.where('prename', 'John').all() // User[] - still all User !
Copy to clipboard
Typescript Declarations
function all(): Collection<M>
function all(): Collection<M>
delete()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('name', 'Elone').delete()) // User
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('name', 'Elone').delete()) // User
Typescript Declarations
function delete(): Model[]
function delete(): Model[]
destroy()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.destroy(1)) // User
console.log(userRepo.destroy([1,2,5])) // User[]
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.destroy(1)) // User
console.log(userRepo.destroy([1,2,5])) // User[]
TIP
If you want also relations to be deleted with the deleted record look at Deleting Relationships
Typescript Declarations
function destroy(id: string | number): Item<M>
function destroy(ids: (string | number)[]): Collection<M>
function destroy(id: string | number): Item<M>
function destroy(ids: (string | number)[]): Collection<M>
doesntHave()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have no comments
useRepo(User).doesntHave('comments').get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have no comments
useRepo(User).doesntHave('comments').get()
Typescript Declarations
function doesntHave(relation: string): Query
function doesntHave(relation: string): Query
find()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.find(1)) // User
console.log(userRepo.find([1,2,5])) // User[]
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.find(1)) // User
console.log(userRepo.find([1,2,5])) // User[]
Typescript Declarations
function find(id: string | number): Item<M>
function find(ids: (string | number)[]): Collection<M>
function find(id: string | number): Item<M>
function find(ids: (string | number)[]): Collection<M>
first()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('prename', 'Elone').first()) // User - with prename 'Elone'
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('prename', 'Elone').first()) // User - with prename 'Elone'
Typescript Declarations
function first(): Item<M>
function first(): Item<M>
get()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.query().get() // User[] - all
const usersPreName = userRepo.where('prename', 'Elone').get() // User[] - with prename 'Elone'
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.query().get() // User[] - all
const usersPreName = userRepo.where('prename', 'Elone').get() // User[] - with prename 'Elone'
Typescript Declarations
function get(): Collection<M>
function get(): Collection<M>
groupBy()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Group users by name.
useRepo(User).groupBy('name').get()
// You may also pass multiple columns
useRepo(User).groupBy('name', 'age').get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Group users by name.
useRepo(User).groupBy('name').get()
// You may also pass multiple columns
useRepo(User).groupBy('name', 'age').get()
Typescript Declarations
function groupBy(...fields: GroupByFields): Query
function groupBy(...fields: GroupByFields): Query
has()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have at least one comment.
useRepo(User).has('comments').get()
// Retrieve all posts that have at least 2 comments.
useRepo(User).has('comments', 2).get()
// Retrieve all posts that have more than 2 comments.
useRepo(User).has('comments', '>', 2).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have at least one comment.
useRepo(User).has('comments').get()
// Retrieve all posts that have at least 2 comments.
useRepo(User).has('comments', 2).get()
// Retrieve all posts that have more than 2 comments.
useRepo(User).has('comments', '>', 2).get()
Typescript Declarations
function has(relation: string, operator?: string | number, count?: number): Query
function has(relation: string, operator?: string | number, count?: number): Query
limit()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Return only 30 users
useRepo(User).limit(30).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Return only 30 users
useRepo(User).limit(30).get()
Typescript Declarations
function limit(value: number): Query
function limit(value: number): Query
load()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.all()
// eager load "coomments" relation for all users
userRepo.with('comments').load(users)
// eager load "coomments" relation with closure for all users
userRepo.with('comments', (query) => {
query.where('active', true)
}).load(users)
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.all()
// eager load "coomments" relation for all users
userRepo.with('comments').load(users)
// eager load "coomments" relation with closure for all users
userRepo.with('comments', (query) => {
query.where('active', true)
}).load(users)
Typescript Declarations
function load(models: Collection<M>): void
function load(models: Collection<M>): void
makeHidden()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Returns User without the field 'phone'
userRepo.makeHidden(['phone']).first()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Returns User without the field 'phone'
userRepo.makeHidden(['phone']).first()
Typescript Declarations
function makeHidden(fields: string[]): Query
function makeHidden(fields: string[]): Query
makeVisible()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Returns User with hidden field 'secret'
userRepo.makeVisible(['secret']).first()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Returns User with hidden field 'secret'
userRepo.makeVisible(['secret']).first()
Typescript Declarations
function makeVisible(fields: string[]): Query
function makeVisible(fields: string[]): Query
offset()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Remove the last 30 users
useRepo(User).offset(30).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Remove the last 30 users
useRepo(User).offset(30).get()
Typescript Declarations
function offset(value: number): Query
function offset(value: number): Query
orDoesntHave()
Usage
Same as doesntHave usage just with "or" condition
Typescript Declarations
function orDoesntHave(relation: string): Query
function orDoesntHave(relation: string): Query
orHas()
Usage
Same as has usage just with "or" condition
Typescript Declarations
function orHas(relation: string, operator?: string | number, count?: number): Query
function orHas(relation: string, operator?: string | number, count?: number): Query
orWhereDoesntHave()
Usage
Same as whereDoesntHave usage just with "or" condition
Typescript Declarations
function orWhereDoesntHave(
relation: string,
callback: EagerLoadConstraint = () => {}
): Query
function orWhereDoesntHave(
relation: string,
callback: EagerLoadConstraint = () => {}
): Query
orWhereHas()
Usage
Same as whereHas usage just with "or" condition
Typescript Declarations
function orWhereHas(
relation: string,
callback: EagerLoadConstraint = () => {},
operator?: string | number,
count?: number
): Query
function orWhereHas(
relation: string,
callback: EagerLoadConstraint = () => {},
operator?: string | number,
count?: number
): Query
orWhere()
Usage
Same as where usage just with "or" condition
Typescript Declarations
function orWhere(
field: WherePrimaryClosure | string,
value?: WhereSecondaryClosure | any,
): Query
function orWhere(
field: WherePrimaryClosure | string,
value?: WhereSecondaryClosure | any,
): Query
orderBy()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Order users by name.
useRepo(User).orderBy('name').get()
// You may also chain orderBy.
useRepo(User)
.orderBy('name')
.orderBy('age', 'desc')
.get()
// Sort user name by its third character.
useRepo(User).orderBy(user => user.name[2]).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Order users by name.
useRepo(User).orderBy('name').get()
// You may also chain orderBy.
useRepo(User)
.orderBy('name')
.orderBy('age', 'desc')
.get()
// Sort user name by its third character.
useRepo(User).orderBy(user => user.name[2]).get()
Typescript Declarations
function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query
function orderBy(field: OrderBy, direction: OrderDirection = 'asc'): Query
useCache()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Generate a cache with a auto generated key.
useRepo(User).useCache().get()
// Generate a cache with a manual key (recommanded).
useRepo(User).useCache('key').get()
// Generate a cache with a manual key and dynamic params (recommanded).
useRepo(User).useCache('key', { id: idProperty }).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Generate a cache with a auto generated key.
useRepo(User).useCache().get()
// Generate a cache with a manual key (recommanded).
useRepo(User).useCache('key').get()
// Generate a cache with a manual key and dynamic params (recommanded).
useRepo(User).useCache('key', { id: idProperty }).get()
Typescript Declarations
function useCache(key?: string, params?: Record<string, any>): Query
function useCache(key?: string, params?: Record<string, any>): Query
whereDoesntHave()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that doesnt have comment from userId 1.
useRepo(Post).whereDoesntHave('comments', (query) => {
query.where('userId', 1)
}).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that doesnt have comment from userId 1.
useRepo(Post).whereDoesntHave('comments', (query) => {
query.where('userId', 1)
}).get()
Typescript Declarations
function whereDoesntHave(
relation: string,
callback: EagerLoadConstraint = () => {}
): Query
function whereDoesntHave(
relation: string,
callback: EagerLoadConstraint = () => {}
): Query
whereHas()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have comment from userId 1.
useRepo(Post).whereHas('comments', (query) => {
query.where('userId', 1)
}).get()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Retrieve all posts that have comment from userId 1.
useRepo(Post).whereHas('comments', (query) => {
query.where('userId', 1)
}).get()
Typescript Declarations
function whereHas(
relation: string,
callback: EagerLoadConstraint = () => {},
operator?: string | number,
count?: number
): Query
function whereHas(
relation: string,
callback: EagerLoadConstraint = () => {},
operator?: string | number,
count?: number
): Query
whereId()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.whereId(1).get()) // User[]
console.log(userRepo.whereId([1,2,5]).get()) // User[]
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.whereId(1).get()) // User[]
console.log(userRepo.whereId([1,2,5]).get()) // User[]
Typescript Declarations
function whereId(ids: string | number | (string | number)[]): Query
function whereId(ids: string | number | (string | number)[]): Query
whereIn()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Filter by array
console.log(userRepo.query().whereIn('commentIds', [1,2,5]).get())
// Filter by Set
console.log(userRepo.query().whereIn('commentIds', new Set([1,2,5])).get())
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Filter by array
console.log(userRepo.query().whereIn('commentIds', [1,2,5]).get())
// Filter by Set
console.log(userRepo.query().whereIn('commentIds', new Set([1,2,5])).get())
Typescript Declarations
function whereIn(field: string, values: any[]|Set): Query
function whereIn(field: string, values: any[]|Set): Query
where()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('prename', 'Elone').get()) // User[] - with prename 'Elone'
// with value closure
console.log(useRepo(User).where('votes', (value) => {
return value >= 100
}).get())
// with where closure
console.log(userRepo.where((user: User) => {
return user.votes >= 100 && user.active
}).get()) // User[]
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
console.log(userRepo.where('prename', 'Elone').get()) // User[] - with prename 'Elone'
// with value closure
console.log(useRepo(User).where('votes', (value) => {
return value >= 100
}).get())
// with where closure
console.log(userRepo.where((user: User) => {
return user.votes >= 100 && user.active
}).get()) // User[]
withAllRecursive()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.withAllRecursive().get() // User[] with all its nested relations 3 levels deep
const usersWithRelations = userRepo.withAllRecursive(2).get() // User[] with all its nested relations 2 levels deep
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const users = userRepo.withAllRecursive().get() // User[] with all its nested relations 3 levels deep
const usersWithRelations = userRepo.withAllRecursive(2).get() // User[] with all its nested relations 2 levels deep
Typescript Declarations
function withAllRecursive(depth = 3): Query
function withAllRecursive(depth = 3): Query
withAll()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const usersWithComments = userRepo.withAll().get() // User[] with all its relations loaded
// with closure
const usersWithCommentsOnlyActive = userRepo.withAll((query) => {
query.where('active', true)
}).get() // User[] with comments which are active. Don't forget that you still get all Users just with less relations
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const usersWithComments = userRepo.withAll().get() // User[] with all its relations loaded
// with closure
const usersWithCommentsOnlyActive = userRepo.withAll((query) => {
query.where('active', true)
}).get() // User[] with comments which are active. Don't forget that you still get all Users just with less relations
Typescript Declarations
function withAll(callback: EagerLoadConstraint = () => {}): Query
function withAll(callback: EagerLoadConstraint = () => {}): Query
withMeta()
_meta is only filled if you have defined in your model static config = { model: { withMeta = true } } or globally set by configuration
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// gives you access to the default hidden prop '_meta'
userRepo.withMeta().first()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// gives you access to the default hidden prop '_meta'
userRepo.withMeta().first()
Typescript Declarations
function withMeta(): Query
function withMeta(): Query
with()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const usersWithComments = userRepo.with('comments').get() // User[] with comments relations
// with closure
const usersWithCommentsOnlyActive = userRepo.with('comments', (query) => {
query.where('active', true)
}).get() // User[] with comments which are active. Don't forget that you still get all Users just with less comments
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
const usersWithComments = userRepo.with('comments').get() // User[] with comments relations
// with closure
const usersWithCommentsOnlyActive = userRepo.with('comments', (query) => {
query.where('active', true)
}).get() // User[] with comments which are active. Don't forget that you still get all Users just with less comments
Typescript Declarations
function with(name: string, callback: EagerLoadConstraint = () => {}): Query
function with(name: string, callback: EagerLoadConstraint = () => {}): Query
Repository
cache()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Returns the cache instance
useRepo(User).cache()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Returns the cache instance
useRepo(User).cache()
Typescript Declarations
function cache(): WeakCache
function cache(): WeakCache
make()
TIP
This method will not save the model to the store. It's pretty much the alternative to new Model(), but it injects the store instance to support model instance methods in SSR environment.
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Make a model with default values
userRepo.make()
// Make a model with values
userRepo.make({
id: 1,
name: 'Elone Hoo',
})
// Make many models with values
userRepo.make([
{
id: 1,
name: 'Elone Hoo',
},
{
id: 2,
name: 'elonehoo',
},
])
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Make a model with default values
userRepo.make()
// Make a model with values
userRepo.make({
id: 1,
name: 'Elone Hoo',
})
// Make many models with values
userRepo.make([
{
id: 1,
name: 'Elone Hoo',
},
{
id: 2,
name: 'elonehoo',
},
])
Typescript Declarations
function make(records?: Element | Element[]): M | M[]
function make(records?: Element | Element[]): M | M[]
new()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Make a model with default values
userRepo.new()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
const userRepo = useRepo(User)
// Make a model with default values
userRepo.new()
Typescript Declarations
function new(): Model | null
function new(): Model | null
piniaStore()
Usage
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Return the complete pinia Store instance
useRepo(User).piniaStore()
import { useRepo } from 'pinia-plugin-orm'
import User from './models/User'
// Return the complete pinia Store instance
useRepo(User).piniaStore()
Typescript Declarations
export interface DataStoreState {
data: Record<string, any>
[s: string]: any
}
function piniaStore<S extends DataStoreState = DataStoreState>(): Store
export interface DataStoreState {
data: Record<string, any>
[s: string]: any
}
function piniaStore<S extends DataStoreState = DataStoreState>(): Store
Configuration
model
| Option | Default | Description |
|---|---|---|
| withMeta | false | Activates the _meta field to be saved for every model |
| visible | [*] | Sets default visible fields for every model |
| hidden | [ ] | Sets default hidden fields for every model |
cache
| Option | Default | Description |
|---|---|---|
| provider | Weakcache | Defines which cache provider should be used |
| shared | true | Activates the cache to be shared between all repositories |
Typescript Declarations
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
visible?: string[]
}
export interface CacheConfigOptions {
shared?: boolean
provider?: typeof WeakCache<string, Model[]>
}
export interface InstallOptions {
model?: ModelConfigOptions
cache?: CacheConfigOptions | boolean
}
const options: InstallOptions
export interface ModelConfigOptions {
withMeta?: boolean
hidden?: string[]
visible?: string[]
}
export interface CacheConfigOptions {
shared?: boolean
provider?: typeof WeakCache<string, Model[]>
}
export interface InstallOptions {
model?: ModelConfigOptions
cache?: CacheConfigOptions | boolean
}
const options: InstallOptions