@ApiFunctionCreate
Decorates a service method so it creates a new entity instance, executes before/after function subscribers, and propagates typed HTTP errors. It uses the repository on this or an optional EntityManager for transactional work.
Signature
@ApiFunctionCreate<E extends IApiBaseEntity>(
properties: IApiFunctionProperties<E>
): (target, propertyKey, descriptor) => PropertyDescriptorDecorated methods receive the shape create(dto: TApiFunctionCreateProperties<E>, eventManager?: EntityManager): Promise<E>.
Behavior
- Instantiates the entity to collect metadata and subscriber context.
- Executes
onBeforeCreatesubscribers; if a handler returns a DTO, it replaces the incoming body. - Persists the entity using
repository.saveoreventManager.getRepository(entity).save. - Executes
onAfterCreatesubscribers with the persisted entity. - On errors, runs
onBeforeErrorCreateoronAfterErrorCreatesubscribers and rethrowsHttpExceptioninstances or wraps unknown errors inInternalServerErrorException.
Example
export class PostService {
constructor(public repository: Repository<PostEntity>) {}
@ApiFunctionCreate<PostEntity>({ entity: PostEntity })
async createPost(properties: TApiFunctionCreateProperties<PostEntity>) {
// Implementation replaced by decorator
return {} as PostEntity;
}
}Related resources
Last updated on