Access @Ctx in @AfterInsert/@AfterUpdate #1144
-
Hello, I have the following BaseEntity class: import { Field, ID, ObjectType } from 'type-graphql';
import { validateOrReject } from 'class-validator';
import {
BaseEntity,
PrimaryGeneratedColumn,
CreateDateColumn,
BeforeInsert,
BeforeUpdate,
} from 'typeorm';
@ObjectType()
export abstract class BasicEntity extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
readonly id!: string;
@Field()
@CreateDateColumn()
createdAt!: Date;
@Field()
@CreateDateColumn()
updatedAt!: Date;
@BeforeInsert()
@BeforeUpdate()
async validate() {
await validateOrReject(this, { validationError: { target: false } });
}
} and was wondering If it was possible to access the @ctx decorator to possibly assign which user updated/created a given record. Something like this: import { Field, ID, ObjectType } from 'type-graphql';
import { validateOrReject } from 'class-validator';
import {
BaseEntity,
PrimaryGeneratedColumn,
CreateDateColumn,
BeforeInsert,
BeforeUpdate,
} from 'typeorm';
@ObjectType()
export abstract class BasicEntity extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
readonly id!: string;
@Field()
@CreateDateColumn()
createdAt!: Date;
@Field()
@CreateDateColumn()
updatedAt!: Date;
@Field()
updatedBy?: User;
@Field()
createdBy?: User;
@BeforeInsert()
@BeforeUpdate()
async validate() {
await validateOrReject(this, { validationError: { target: false } });
}
@AfterInsert()
setUser(@Ctx() {user} : Context) {
this.createdBy = user;
}
} Is this functionality currently achievable? Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Dec 16, 2021
Replies: 1 comment 1 reply
-
No, it's not possible. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No, it's not possible.