Search code examples
node.jstypescriptpostgresqlloopbackloopback4

Autogenerate timestamp postgres loopback 4


how can I autogenerate a timestamp in Postgres via Loopback 4? I tried this but it didn't work:

@property({ type: 'Date', generated: true, autoGenerated: true, postgresql: { dataType: 'timestamp with time zone', extension: 'now()' }, }) timestamp?: Date;


Solution

  • There is two ways to achieve this.

    Solution 1: Specify it directly in your database, for example

    created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL
    

    Solution 2: Add it in your model

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