X

Generating fake data in Node.js with Faker.js

fakerjs

Last time I did a sample app to generate large amount of data using GenFu in C# so that I can be in .NET land but I wanted to also try out what it would be like to generate fake data in node.js using VSCode.

My investigation lead me to Faker.js, which would allow me to generate fake data in Node.js with Faker.js

Installation

To install faker.js you just need to use npm package manager

npm install faker --save-dev

Usage

I will use a similar kind of example I had used previously to generate logs, below is the sample code I used to generate, it is a bit different but the idea is the same.

var faker = require('faker');
var uuidV4 = require('uuid/v4');

var log = {
  "v": "1.0",
  "category": "ERRORLOG",
  "level": "INFO",
  "timestamp": "2016-11-04T22:02:02.365Z",
  "application": {
    "name": "My Fav application",
    "version": "1.0.0-SNAPSHOT",
    "thread": "default task-16",
    "component": "com.AuditLogInterceptor"
  },
  "context": {
    "tenantId": "saml-demo",
    "principalId": "griduser",
    "userGuid" : "c92c7642-777a-41a2-87d6-79a8acec1d7f",
    "sessionId": "123456789",  
    "globalAccessId": "593b740b-d53e-490a-b5d1-fb3fa2fc1c4c",
    "policyId": "c92c7642-777a-41a2-87d6-79a8acec1d7f",
    "policyName": "Default",
    "scenarioId": "0011c00d-b48b-b82c-4a66-1d394b64ff67",
    "scenarioName": "trusted network",
    "applicationId" : "c92c7642-777a-41a2-87d6-79a8acec1d7f",
    "applicationName" : "Office 365",
  },
  "details": {
    "type": "ACCESS REQUEST",
    "state": "",
    "action" : "",
    "credentials" : [
        {
            type: "OTP",
            state: "" 
        },
        {
            type: "Password",
            state: ""
        }
    ]
  }
}

var state = ["Initiated", "Accepted", "Denied" ,"Waiting"];
var action = ["auth", "change password", "push", "restart"];
var cred_state = ["Pending", "VerifiedInSession","Verified" ];

function  genlog() {
  log.timestamp = faker.date.recent();
  log.context.tenantId = faker.company.companyName().replace(/\s+/g, ''); //remove space in name
  log.context.principalId = faker.internet.userName();
  log.context.userGuid = uuidV4(); //generate guid
  log.context.sessionId = uuidV4();
  log.context.globalAccessId = uuidV4();
  log.context.policyId = uuidV4();
  log.context.policyName = faker.commerce.productName();
  log.context.scenarioId = uuidV4();
  log.context.scenarioName = faker.commerce.productName();
  log.context.applicationId = uuidV4();
  log.context.applicationName = faker.commerce.productName();
  var state_randon = faker.random.number({min:0, max:state.length});
  var action_random = faker.random.number({min:0, max:action.length});
  var cred_random = faker.random.number({min:0, max:cred_state.length});
  var cred_random2 = faker.random.number({min:0, max:cred_state.length});

  log.details.state = state[state_randon];
  log.details.action = action[action_random];
  log.details.credentials[0].state = cred_state[cred_random];
  log.details.credentials[1].state = cred_state[cred_random2];
  
  console.log("%j", log);
}

genlog();

As you can see faker.js is also quite easy to use and generate fake data, although in GenFu we were able to generate a List of objects, I was not able to find such functionality in faker.js but one nice thing faker.js had was localization where it can generate data in different languages.

Faker.js also has quite a few api methods to generate data take a look at their github page for all the methods https://github.com/marak/Faker.js/.

Check out faker.js if you are using node.js and need to generate large amount of fake data.

Categories: Node node.js
Taswar Bhatti:
Related Post