v2.0.0-pre.18
associateGet
The associateGet
utility allows you to define one-to-one
relationships on your Model functions. The getId
property allows you to specify the id to use to get the related data.
ts
import type { Messages } from 'my-feathers-api'
import { type ModelInstance, useInstanceDefaults, associateGet } from 'feathers-pinia'
import { User } from './user'
const modelFn = (data: ModelInstance<Messages>) => {
const withDefaults = useInstanceDefaults({ text: '', userId: null }, data)
const withUser = associateFind(withDefaults, 'user', {
Model: User,
getId: (data) => data.messageId
})
return withUser
}
associateGet(data, prop, options)
data {Object}
is the record that will contain the association.prop {string}
is the name of the property that will hold the related data.options {Object}
Model
is the Model Function for the related record. RequiredgetId
is a function that receives thedata
and must return the related idField's value. RequiredmakeParams
is an optional function that receives thedata
and must return a params object containing thequery
used to create the association. OptionalhandleSetInstance
is a function that receives the related object and can use it to update properties ondata
or the related object in order to establish a relationship when assigning data to theprop
. Optional In the above example, if you assign an array of messages touser.messages
, the message'suserId
prop will be set automatically.