Node.js add created_at and updated_at in entity of typeorm

Let’s import first import { CreateDateColumn,UpdateDateColumn } from “typeorm”; Add this fields and decarators in Entity @CreateDateColumn({ type: “timestamp”, default: () => “CURRENT_TIMESTAMP(6)” }) public created_at: Date; @UpdateDateColumn({ type: “timestamp”, default: () => “CURRENT_TIMESTAMP(6)”, onUpdate: “CURRENT_TIMESTAMP(6)” }) public updated_at: Date;

How can I create columns with type Date and type DateTime in nestjs with typeORM?

You can see the docs here that explains the @Column decorator. In @Column there is an option called type -> here is where you specify which type of date you want to store for that specific column. More on column types here. For example (using PostgreSQL): @Column({ type: ‘date’ }) date_only: string; @Column({ type: ‘timestamptz’ … Read more

How to exclude entity field from returned by controller JSON. NestJS + Typeorm

I’d suggest creating an interceptor that takes advantage of the class-transformer library: @Injectable() export class TransformInterceptor implements NestInterceptor { intercept( context: ExecutionContext, call$: Observable<any>, ): Observable<any> { return call$.pipe(map(data => classToPlain(data))); } } Then, simply exclude properties using @Exclude() decorator, for example: import { Exclude } from ‘class-transformer’; export class User { id: number; email: … Read more

No metadata for “User” was found using TypeOrm

Adding answer bit late but its very common error. There are two main reasons for above error. In OrmConfig, You have used *.ts instead of *.js. For example, entities: [__dirname + ‘/../**/*.entity.ts’] <– Wrong It should be entities: [__dirname + ‘/../**/*.entity.js’] entities path is wrong. Make sure, entities path is defined according to dist folder … Read more

TypeORM: How to order by a relation field

As of TypeORM version 0.3.0 you can do the following: repository.find({ order: { singer: { name: “ASC” } } }) Before version 0.3.0 (original response) I don’t think it is currently supported by typeorm without the query builder, there is currently a feature request open With the QueryBuilder it is quite simple though: connection.createQueryBuilder(Song, ‘songs’) … Read more

Typeorm how to get relations of relations

We can load sub-relations by using ‘relation.subrelation’ within the relations array itself like this: relations: [‘relation1’, ‘relation2’, ‘relation2.subrelation1’] So for your case, instead of using join you can simply do something like this: this.entityManager.findOne(ChatRoomEntity, { where: {id: roomToJoin.id}, relations: [‘activeUsers’, ‘messages’, ‘messages.fromUser’], }).then(roomEntity => { … This is specified here: https://github.com/typeorm/typeorm/blob/master/docs/find-options.md#basic-options