createRelated
hooks
Source Code | DocumentationCreates 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. Supports creating records one-by-one or in a single multi-create when multi: true.
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.
*
* If true, will create multiple related records in a single call to the related service's create method.
* If false or not provided, will create related records one by one.
*
* @default false
*/
multi?: boolean
/**
* 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.
* Supports creating records one-by-one or in a single multi-create when `multi: true`.
*
* @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 |
