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}Modelis the Model Function for the related record. RequiredgetIdis a function that receives thedataand must return the related idField's value. RequiredmakeParamsis an optional function that receives thedataand must return a params object containing thequeryused to create the association. OptionalhandleSetInstanceis a function that receives the related object and can use it to update properties ondataor 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'suserIdprop will be set automatically.