Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js add created_at and updated_at in entity of typeorm

I have task entity like this:

import {BaseEntity, Column, Entity, PrimaryGeneratedColumn} from "typeorm";

@Entity()
export class Task extends BaseEntity{
    @PrimaryGeneratedColumn()
    id:number;

    @Column()
    title:string

   @Column()
    description:string

}

I want to add created_at and updated_at field to this entity and populate it automatically with Node.js like Laravel framework. My database is postgres

like image 281
paranoid Avatar asked Nov 21 '20 08:11

paranoid


People also ask

How to add created_at column in typeorm?

In TypeORM, you can add a created_at column and a updated_at column by making use of the CreateDateColumn and UpdateDateColumn decorators, respectively. These columns will be automatically initialized and updated to the current date and time (that is, the current timestamp).

What is typeorm and how does it work?

To follow the DRY ( Don’t-Repeat-Yourself) principle, TypeORM gives us the luxury of creating entities that inherit the columns of a base entity. TypeORM also comes with a defined BaseEntity that we can extend to have access to the various attributes and methods we can use to perform CRUD operations.

What is type Orm in PostgreSQL?

TypeORM – an ORM (Object Relational Mapping) for popular Database services like PostgreSQL, MySQL, MongoDB, and many more. We’ll build a REST API where we can create, read, update and delete a post in the database. To test the API endpoints, am going to use HTTP Client VS Code extension .


4 Answers

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;
like image 96
Penava Avatar answered Oct 24 '22 04:10

Penava


import {BaseEntity, Column, Entity, PrimaryGeneratedColumn, CreateDateColumn} from "typeorm";

@Entity()
export class Task extends BaseEntity{
    @PrimaryGeneratedColumn()
    id:number;

    @Column()
    title:string

   @Column()
    description:string
   
   @CreateDateColumn()
   created_at: Date;

   @UpdateDateColumn()
   updated_at: Date;


}
like image 17
Welisson Moura Avatar answered Oct 24 '22 03:10

Welisson Moura


import { CreateDateColumn, UpdateDateColumn } from 'typeorm';

@CreateDateColumn()
createdAt: Date;

@UpdateDateColumn()
updatedAt: Date;

like image 6
Ahmed Afifi Avatar answered Oct 24 '22 03:10

Ahmed Afifi


I tried all the previous ways you informed, but the only way that worked was this.

import moment from "moment";

@CreateDateColumn()
  created_at: Date;

@UpdateDateColumn()
  updated_at: Date;

@BeforeInsert()
insertCreated() {
    this.created_at = new Date(
      moment().tz("America/Sao_Paulo").format("YYYY-MM-DD HH:mm:ss")
    );
    this.updated_at = new Date(
      moment().tz("America/Sao_Paulo").format("YYYY-MM-DD HH:mm:ss")
    );
  }

@BeforeUpdate()
insertUpdated() {
    this.updated_at = new Date(
      moment().tz("America/Sao_Paulo").format("YYYY-MM-DD HH:mm:ss")
    );
  }
like image 2
Weverton Icaro Avatar answered Oct 24 '22 04:10

Weverton Icaro