【问题标题】:Loopback-4 Add timestamp createdAt & updatedAt through modelLoopback-4 通过模型添加时间戳 createdAt 和 updatedAt
【发布时间】:2020-10-08 14:03:23
【问题描述】:

我已经查看了为较低版本提供的挂钩,但我想知道如何通过模型添加时间戳 createdAtupdatedAt 而无需与控制器中的时间戳进行交互。

我曾在Rails 工作,但Loopback-4 对我来说是新事物,不如Rails 灵活。 我仅对少数特定型号需要此时间戳

【问题讨论】:

    标签: loopback4


    【解决方案1】:

    您可以通过以下方式执行此操作:-

    在模型文件中:

    @property({
      type: 'date',
      default: () => new Date()
    })
    created ? : string;
    
    @property({
      type: 'date',
      default: () => new Date()
    })
    modified ? : string;
    

    在存储库文件中:

    constructor(
      @inject('datasources.db') dataSource: DbDataSource,  
    ) {
      super(User, dataSource);
    
      (this.modelClass as any).observe('persist', async (ctx: any) => {
        ctx.data.modified = new Date();
      });
    }
    

    【讨论】:

    • 会试一试
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2015-10-11
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多