I am implementing a multi-tenant application based on table column tenantId
.
@Entity()
export class User {
@PrimaryColumn()
id!: string
// other column ...
@Column()
tenantId!: string
}
And for insert, i can automatically set the tenantId value upon insertion by implementing a subscriber.
@EventSubscriber()
export class TenantSubscriber implements EntitySubscriberInterface {
async beforeInsert(event: InsertEvent<any>) {
for await (const item of event.metadata.primaryColumns) {
if (event.entity['tenantId'] === undefined) {
event.entity['tenantId'] = getCurrentTenantId()
}
}
return event
}
}
I would like to ask how to automatically add the tenant ID condition when performing queries, updates, and deletions as well.
This way, I won’t have to write a bunch of repetitive code.