$ open source · GPL-3.0 — need a commercial license? imqueue.com →

CreatedBy() function

Stamps the decorated column with the acting user id on INSERT, taken from the in-flight IMQ request metadata (currentMetadata()?.userId) — so the id never travels through method arguments and cannot be spoofed by a caller.

A property decorator, reusable on any model field. The hook is a no-op when there is no acting user (system / unattributed writes) and never overwrites a value the application set explicitly.

Two hooks are registered, because rows are inserted in two ways: - instance / single create()beforeCreate (receives the instance) - static Model.bulkCreate(records, …)beforeBulkCreate (receives the built instances; a plain bulkCreate does not fire beforeCreate, so the per-instance hook alone would be bypassed). Sequelize filters the written columns down to options.fields when the caller supplies it, so an injected field is dropped unless also added there.

Mechanism: a property decorator receives the prototype, but Sequelize hook decorators must target a static method on the constructor, so generated static methods are attached to target.constructor and registered through the public @BeforeCreate / @BeforeBulkCreate. Hooks are installed by sequelize-typescript's installHooks during Sequelize#addModels.

Signature:

export declare function CreatedBy(target: any, propertyName: string): void;

Parameters

Parameter

Type

Description

target

any

the decorated model's prototype

propertyName

string

the decorated column property name

Returns:

void