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}
Model
is the Model Function for the related record. RequiredmakeParams
is a function that receives thedata
and must return a params object containing thequery
used to create the association. RequiredhandleSetInstance
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.