Node.js 在 typeorm 的实体中添加 created_at 和 updated_at

标签 node.js typeorm node.js-typeorm

我有 task像这样的实体:

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

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

    @Column()
    title:string

   @Column()
    description:string

}
我想向这个实体添加 created_at 和 updated_at 字段,并用 Node.js 自动填充它喜欢 Laravel框架。我的数据库是 postgres

最佳答案

我们先导入

import { CreateDateColumn,UpdateDateColumn } from "typeorm";
在实体中添加此字段和 decarators
@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;

关于Node.js 在 typeorm 的实体中添加 created_at 和 updated_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64941148/

相关文章:

typescript - 如何在 TypeORM postgres 中使用 leftJoinAndSelect 查询?

node.js - 错误: Cannot query across one-to-many for property TypeORM

Javascript es6 - 它是重新分配还是参数?

node.js - 在 mongodb 中聚合撤消展开

node.js - typeORM: "message": "Data type\"对象\"in\".. ."is not supported by\"postgres\"database."

javascript - 实体元数据未找到 : No metadata for "User" was found

node.js - NestJS > TypeORM 将复杂实体映射到复杂 DTO

postgresql - 将 Date.now() 保存到时间戳列但获取超出范围的日期/时间字段值

node.js - Node 中的角色和锁定端点

javascript - 使用 multer 上传 Angular 5 图像不上传文件