createRelated
Category
Tags
Export size
min 2.75 kB · gzip 1.20 kB
See also
Creates related records in other services after a successful create call. For each result item, a data function produces the record to create in the target service. They are created in a single multi-create if the related service allows it, otherwise with one call per item.
ts
import { createRelated } from 'feathers-utils/hooks';Example
ts
import { createRelated } from 'feathers-utils/hooks'
app.service('users').hooks({
after: {
create: [createRelated({ service: 'profiles', data: (user) => ({ userId: user.id }) })]
}
})Type declaration
Show Type Declarations
ts
export interface CreateRelatedOptions<
H extends HookContext = HookContext,
Services extends H["app"]["services"] = H["app"]["services"],
S extends keyof Services = keyof Services,
> {
service: S
/**
* Is relevant when the current context result is an array.
*
* Whether the related service allows creating multiple items in a single
* call. Defaults to the `multi` option of the related service. If multi
* creating is not allowed, the related records are created with one call
* per item.
*
* @default service.options.multi
*/
multi?: Multi
/**
* A function that returns the data to be created in the related service.
*
* Receives the current item from the context result and the full hook context as arguments.
* Can return a single data object, an array of data objects, or a promise that resolves to either.
*
* If the function returns undefined, no related record will be created for that item.
*/
data: (
item: ResultSingleHookContext<H>,
context: H,
) => Promisable<MaybeArray<InferCreateDataSingle<Services[S]>> | undefined>
}
/**
* Creates related records in other services after a successful `create` call.
* For each result item, a `data` function produces the record to create in the target service.
* They are created in a single multi-create if the related service allows it,
* otherwise with one call per item.
*
* @example
* ```ts
*
*
* app.service('users').hooks({
* after: {
* create: [createRelated({ service: 'profiles', data: (user) => ({ userId: user.id }) })]
* }
* })
* ```
*
* @see https://utils.feathersjs.com/hooks/create-related.html
*/
export declare function createRelated<H extends HookContext = HookContext>(
options: MaybeArray<CreateRelatedOptions<H>>,
): (context: H, next?: NextFunction) => Promise<void>| Argument | Type | Description |
|---|---|---|
| options | MaybeArray<CreateRelatedOptions<H>> |
| type | methods | multi |
|---|---|---|
| before, around | create | yes |
