/How-to-create-fake-data

This is a Repository or tutorial on how to create a fake data using fakerjs by writing typescript code and adding the data to the .json file

Generating Fake Data with FakerJS and Writing to a JSON File

This guide demonstrates how to generate fake data using FakerJS and write it to a JSON file.

Prerequisites

  • Node.js installed on your machine
  • FakerJS library installed (npm install @faker-js/faker)
  • File system (fs) module installed (npm install fs)

Code

const { faker } = require('@faker-js/faker');
import * as fs from 'fs';

// Define possible roles and teams
const roles = [
    'Product Designer',
    'Product Manager',
    'Frontend Developer',
    'Backend Developer'
];

const teams = [
    'Design',
    'Product',
    'Marketing',
    'Technology'
];

// Function to generate a single entry
const generateEntry = () => ({
    image: faker.image.avatar(), // Generates a random avatar image
    name: faker.name.fullName(), // Generates a random full name
    status: faker.helpers.arrayElement(['Active', 'Inactive']), // Randomly selects between 'Active' and 'Inactive'
    role: faker.helpers.arrayElement(roles), // Randomly selects a role from the roles array
    email: faker.internet.email(), // Generates a random email address
    team: faker.helpers.arrayElement(teams) // Randomly selects a team from the teams array
});

// Function to generate multiple entries
const generateEntries = (count = 100) => {
    const entries = [];
    for (let i = 0; i < count; i++) {
        entries.push(generateEntry());
    }
    return entries;
};

// Generate 100 entries
const entries = generateEntries(100);

// Write the data to data.json
fs.writeFile('data.json', JSON.stringify(entries, null, 2), (err) => {
    if (err) {
        console.error('Error writing to data.json:', err);
    } else {
        console.log('Data written to data.json successfully');
    }
});

Explanation

This code uses FakerJS to generate fake data for 100 entries. Each entry includes:

  • A random avatar image
  • A random full name
  • A random status (Active or Inactive)
  • A random role from the roles array
  • A random email address
  • A random team from the teams array

The generateEntry function generates a single entry, while the generateEntries function generates multiple entries. The fs.writeFile function writes the generated data to a JSON file named data.json.

Example Use Cases

  • Generating fake data for testing purposes
  • Creating sample data for a new application
  • Populating a database with initial data

Tips and Variations

  • Adjust the count parameter in the generateEntries function to change the number of generated entries.
  • Modify the roles and teams arrays to change the available roles and teams.
  • Use other FakerJS methods to generate different types of data (e.g., addresses, phone numbers, etc.).