v2.0.0-pre.18
associateFind
The associateFind utility uses the Feathers Query syntax to establish one-to-many relationships with associated data.
ts
import type { Users } from 'my-feathers-api'
import { type ModelInstance, useInstanceDefaults, associateFind } from 'feathers-pinia'
import { Message } from './message'
const modelFn = (data: ModelInstance<Users>) => {
const withDefaults = useInstanceDefaults({ email: '', password: '' }, data)
const withMessages = associateFind(withDefaults, 'messages', {
Model: Message,
makeParams: (data) => ({ query: { userId: data.id } }),
handleSetInstance(message) {
message.userId = data.id
},
})
return withMessages
}
associateFind(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. RequiredmakeParamsis a function that receives thedataand must return a params object containing thequeryused to create the association. RequiredhandleSetInstanceis 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.