oeCloud.io Example Audit Field

is a app which tries to demonstrate how Audit fields are automatically maintained by oeCloud.

What are Audit Fields?

Audit fields are set of Properties which are use to monitor who created/modified a record and when it was created/modified. These properties are auto injected to each record inorder to maintain there audit.

Properties List

Property Type Value
_createdOn Date Used to store TimeStamp of when a new record is created.
_createdBy String Used to store User name of who created the record.
_modifiedOn Date Used to store TimeStamp of when a record was updated, in case of new record it is same as of _createdOn.
_modifiedBy String Used to store User name of who modified the record, in case of new record it is same as _createdBy
_type String Model Name.

What you’ll build

A oeCloud based app to demonstrate Audit fields feature, in order to do that we will create a Product model and perform different CURD operation, and see how audit fields are automatically maintained.

Scenario which will be covered are :

  1. Create a record.
  2. Update a record.

for the above scenarios, we’ll see how audit fields are maintained.

What you’ll need:

  • You will need oecloud.io based example running
  • You should know about swagger explorer and should have access to mongodb.

How to complete this guide

Download and install example app from here

Note: We are using swagger* explorer, for exploring model api’s, alternatively user can choose to use any other explorer tool (Ex: Postman).

Once you have started the server, open a browser link: http://localhost:3000/explorer/.

You should create Product model if it is not available. Please refer to ‘how to create model guide’ here. You should able to see model as below.

Schema of Product Model:

{
    "name": "Product",
    "base": "BaseEntity",
    "strict": false,
    "plural": "Products",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties": {
        "code": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "category": {
            "type": "string"
        },
        "price": {
            "type": "number"
        },
        "offeredSince": {
            "type": "date"
        },
        "active": {
            "type": "boolean"
        },
        "description": {
            "type": "string"
        }
    }
}

Create a new record in product model

To create a record, use api - POST /Products

sample data:

{
    "code": "HC1001",
    "name": "Caffe Latte",
    "description": "Strong Blend Espresso filled up with...",
    "category": "",
    "price": 1.23,
    "offeredSince": "01-01-2012",
    "flavourable": true
}

Output

{
  "code": "HC1001",
  "name": "Caffe Latte",
  "category": "",
  "price": 1.23,
  "offeredSince": "2011-12-31T18:30:00.000Z",
  "flavourable": true,
  "description": "Strong Blend Espresso filled up with...",
  "id": "592534ba500f730c0b4fd483",
  "_type": "Product",
  "_createdBy": "admin",
  "_modifiedBy": "admin",
  "_createdOn": "2017-05-24T07:22:34.950Z",
  "_modifiedOn": "2017-05-24T07:22:34.950Z",
  "_version": "6d899b4d-303a-4011-bffb-7ffeb7c033ed",
  "_isDeleted": false
}

Observation: The response data will have an additional fields named _type, _createdBy, _modifiedBy, _createdOn, _modifiedOn these are Audit Fields added to maintain audit of the record.

Update a record

To update a record, use api - PUT /Products/{id}

Note: Set id as 592534ba500f730c0b4fd483. Set version number 6d899b4d-303a-4011-bffb-7ffeb7c033ed in posted data. This is based on what is your response. it will be different.

Sample data:

{
    "category": "Latte",
    "price": 100.23,
    "_version":"6d899b4d-303a-4011-bffb-7ffeb7c033ed"
}

Output:

{
  "code": "HC1001",
  "name": "Caffe Latte",
  "category": "Latte",
  "price": 100.23,
  "offeredSince": "2011-12-31T18:30:00.000Z",
  "flavourable": true,
  "description": "Strong Blend Espresso filled up with...",
  "id": "592534ba500f730c0b4fd483",
  "_type": "Product",
  "_createdBy": "admin",
  "_modifiedBy": "admin",
  "_createdOn": "2017-05-24T07:22:34.950Z",
  "_modifiedOn": "2017-05-24T07:25:12.803Z",
  "_oldVersion": "6d899b4d-303a-4011-bffb-7ffeb7c033ed",
  "_version": "a0a48d45-9d6b-4491-8c72-b1a78da5da46",
  "_isDeleted": false
}

Observation: See that _modifiedBy and _modifiedOn are automatically updated. _modifiedBy is set to user nmae who has modified the record and _modifiedOn is set to timestamp as to when record was updated.

Note: Six records were created during the startup of the app. That is why _createdBy is set to upload.

Observation made:

Sr.No Scenario Observation
1 create a record When a record is created, audit fields are automatically injected.
2 update a record When a record is updates, _modifiedBy and _modifiedOn are updates to which user had modified and when.

Internals

  • Audit functionality is implemented as mixin. You can disable (by default it is enabled) this functionality by setting mixin value to false in your model definition.
 "mixins": {
        "AuditFieldsMixin": false
    }

Summary

  • You should observe that all audit fields (createdBy, createdOn etc) are updated when record is getting created or modified.
  • You should also check same in mongodb database.