learn groovy
learn groovy

Reputation: 527

Generate millions of mock data using nodejs

  1. List item

I'm trying to create millions of mock data using mocker-data-generator, I've installed this node module npm install mocker-data-generator

I've written below script to generate 100000 records, when I try to execute this file I'm getting below error:

mockData.js

var mocker = require('mocker-data-generator').default
var fs = require('fs');
var cat = {
    id: {
        chance: 'guid'
    },
    name: {
        faker: 'name.firstName'
    },
    lname: {
        faker: 'name.lastName'
    }

};

var json = JSON.stringify(mocker().schema('users', cat, 20000));

fs.writeFile('myjsonfile.json', json, 'utf8', fileWritten);

function fileWritten() { console.log('json file saved'); }

Execute command: node mockData.js

Error:

 (node:13420) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.

I would like to write these 100000 in a file, how can I achieve it in this script. I've followed the example from here

I'm new to Node.js - just started exploring - it would be really helpful if someone can help me to fix this. Thanks in advance.

Upvotes: 0

Views: 988

Answers (2)

Dalton Whyte
Dalton Whyte

Reputation: 717

I think you have two problems first one your import statement is failing, as an alternative have you tried to use:

var mocker = require('mocker-data-generator').default

Secondly, you intend to write the generated mocked data to file, you will need to use the 'fs' node package for this:

var fs = require('fs');
fs.writeFile('myjsonfile.json', mockedJSONData, 'utf8', callback);

Change 'callback' to a function you wish to call after it writes the file.

Upvotes: 1

Shams Nahid
Shams Nahid

Reputation: 6559

Updated: This will generate and save the data in a file.

Here we first generate 10k mock data and then save these data in a file named myjsonfile.json.

var mocker = require('mocker-data-generator').default;
const util = require('util');
const fs = require('fs');

var cat = {
    id: {
        chance: 'guid'
    },
    name: {
        faker: 'name.firstName'
    },
    lname: {
        faker: 'name.lastName'
    }

};

mocker()
    .schema('cat', cat, 10000)
    .build((err, data) => {
        if (err) {
            console.log(err);
        } else {
            // console.log(util.inspect(data, { depth: 10 }))
            fs.writeFile('myjsonfile.json', JSON.stringify(data), 'utf8', (err) => {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Done');
                }
            });
        }
    })

Upvotes: 1

Related Questions