Categories: Software Development

Loopback automatic creation & modification timestamp

In certain cases, it’s convenience to have automatic creation & modification timestamp where system automatically records when and who create/update a record. So far, I find that the most efficient way to do this in Loopback is by utilizing hook feature.

Based on the docs, there are 3 types of hooks provided by loopback based on different layers in API server application:

  1. Remote hook: triggered on remote method invocation
  2. Operation hook: triggered on model operations (CRUD)
  3. Connector hook: triggered on connector level operations (connect/disconnect)

To implement automatic creation & modification timestamp, we can use remote and operation hook by adding code below in the model js, in my case my model js is common/models/product.js

module.exports = function(Product) {
    var app = require('../../server/server')

    // Operation hook to handle the date info
    // Update createdAt on model creation and updatedAt on model update
    Product.observe('before save', function filterProperties(ctx, next) {
        if (ctx.isNewInstance) {
            ctx.instance.createdAt = new Date() 
        } else {
            ctx.data.updatedAt = new Date()
        } next()
    }) 

    // Remote hooks to handle the user info
    // Capture username and put it in createdBy when create API called
    Product.afterRemote('create', function( ctx, modelInstance, next) {
        app.models.Customer.findById(ctx.req.accessToken.userId, function(err, user) {          
            modelInstance.createdBy = user.username next()
        })
    }) 

    // Capture username and put it in updatedBy when update API called 
    Product.afterRemote('upsert', function( ctx, modelInstance, next) { 
        app.models.Customer.findById(ctx.req.accessToken.userId, function(err, user) { 
            modelInstance.updatedBy = user.username next()
        })
    })
}

This implementation of course assumes that authentication is enabled for creation and modification API so it can capture the user ID.

0 0 votes
Article Rating
yohanes.gultom@gmail.com

Share
Published by
yohanes.gultom@gmail.com

Recent Posts

Get Unverified SSL Certificate Expiry Date with Python

Getting verified SSL information with Python (3.x) is very easy. Code examples for it are…

3 years ago

Spring Data Couchbase 4 Multibuckets in Spring Boot 2

By default, Spring Data Couchbase implements single-bucket configuration. In this default implementation, all POJO (Plain…

3 years ago

Firebase Auth Emulator with Python

Last year, Google released Firebase Auth Emulator as a new component in Firebase Emulator. In…

3 years ago

Google OIDC token generation/validation

One of the authentication protocol that is supported by most of Google Cloud services is…

4 years ago

Fast geolocation query with PostGIS

If you need to to add a spatial information querying in your application, PostGIS is…

4 years ago

Auto speech-to-text (Indonesian) with AWS Transcribe and Python

Amazon Web Service Transcribe provides API to automatically convert an audio speech file (mp3/wav) into…

4 years ago