A comprehensive mock data generator that creates realistic test data based on field definitions and types. Supports multiple output formats including JSON, CSV, JavaScript, and TypeScript.
- Type-based generation: Define field types and get realistic data
- Multiple output formats: JSON, CSV, JS, TS
- Customizable: Support for custom field types and parameters
- Seeded generation: Reproducible data with seed values
- Business-specific: Pre-built generators for common business data
- Auto-save: Automatically save to files with directory creation
import { generateData } from './index.mjs';
const fields = {
id: 'id',
name: 'name',
email: 'email',
amount: 'amount',
status: 'status'
};
const data = generateData(10, fields, {
outputPath: './output.json',
format: 'json',
seed: 12345
});'string'- Random alphanumeric string'number'- Random integer (1-10000)'boolean'- Random boolean value'date'- Recent date in ISO format
'terminalId'- Terminal ID (T + 8 chars)'merchantId'- Merchant ID (M + 8 chars)'card_pan'- Credit card number'rrn'- Retrieval Reference Number'transactionId'- Transaction ID (TXN + 10 chars)
'amount'- Float amount (100-100000)'totalValue'- Large float value (1000-1000000)'settledValue'- Settlement amount (500-500000)'charge'- Fee amount (10-5000)
'status'- Transaction status (pending, completed, failed, processing)'settlementStatus'- Settlement status (settled, pending, failed, processing)'type'- Transaction type (credit, debit, transfer, withdrawal)
'name'- Full name'firstName'- First name'lastName'- Last name'email'- Email address'phone'- Phone number'address'- Street address'city'- City name'country'- Country name'zipCode'- ZIP/Postal code
'companyName'- Company name'jobTitle'- Job title'department'- Department name
'productName'- Product name'productDescription'- Product description'price'- Product price (10-1000)
'url'- Website URL'imageUrl'- Image URL'avatar'- Avatar image URL
'description'- Sentence description'title'- Short title (3 words)'text'- Paragraph text
'id'- Sequential integer ID'uuid'- UUID string
{
status: { type: 'enum', options: ['active', 'inactive', 'suspended'] }
}{
quantity: { type: 'numberRange', min: 1, max: 100 }
}{
price: { type: 'floatRange', min: 10.50, max: 999.99, precision: 0.01 }
}{
eventDate: { type: 'dateRange', startDate: '2024-01-01', endDate: '2024-12-31' }
}{
code: { type: 'stringLength', length: 8 }
}{
status: {
name: 'statusName',
description: 'statusDescription',
context: 'statusContext'
}
}This will generate:
{
"status": {
"name": "processing",
"description": "Transaction is being processed",
"context": "warning"
}
}Generates mock data based on field definitions.
count(number): Number of records to generatefields(object): Field definitions with typesoptions(object): Optional configuration
seed(number): Seed for reproducible generationformat(string): Output format ('json', 'csv', 'js', 'ts')outputPath(string): File path to save outputadditionalData(object): Additional data to merge with each record
Array of generated records
Generates a single record.
fields(object): Field definitionsindex(number): Record index (for sequential IDs)
Single generated record object
const transactionFields = {
id: 'id',
transactionId: 'transactionId',
terminalId: 'terminalId',
merchantId: 'merchantId',
amount: 'amount',
status: 'status',
type: 'type',
card_pan: 'card_pan',
rrn: 'rrn',
createdAt: 'createdAt'
};
const data = generateData(50, transactionFields, {
outputPath: './transactions.json',
format: 'json',
seed: 12345
});const merchantFields = {
id: 'id',
name: 'companyName',
email: 'email',
phone: 'phone',
address: 'address',
city: 'city',
country: 'country',
zipCode: 'zipCode',
status: { type: 'enum', options: ['active', 'inactive', 'suspended', 'pending'] },
registrationDate: 'registrationDate',
totalTransactions: { type: 'numberRange', min: 0, max: 10000 },
averageAmount: { type: 'floatRange', min: 100, max: 50000, precision: 0.01 }
};
const data = generateData(25, merchantFields, {
outputPath: './merchants.json',
format: 'json',
seed: 67890
});const csvFields = {
id: 'id',
name: 'name',
email: 'email',
amount: 'amount',
status: 'status',
date: 'date'
};
const data = generateData(15, csvFields, {
outputPath: './data.csv',
format: 'csv',
seed: 44444
});const tsFields = {
id: 'id',
title: 'title',
description: 'description',
status: { type: 'enum', options: ['draft', 'published', 'archived'] },
createdAt: 'createdAt'
};
const data = generateData(10, tsFields, {
outputPath: './data.ts',
format: 'ts',
seed: 55555
});const disputeFields = {
id: 'id',
product: 'product',
transaction_reference: 'transactionId',
reason: 'reason',
merchantId: 'merchantId',
amount: 'amount',
status: {
name: 'statusName',
description: 'statusDescription',
context: 'statusContext'
}
};
const data = generateData(20, disputeFields, {
outputPath: './disputes.json',
format: 'json',
seed: 88888
});This generates data like:
{
"id": 1,
"product": "commissionDispute",
"transaction_reference": "TXNJBCZAXEKEK",
"reason": "Transaction dispute raised by merchant",
"merchantId": "MSKRJZFNA",
"amount": 2782.24,
"status": {
"name": "processing",
"description": "Dispute is being reviewed",
"context": "warning"
}
}[
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"amount": 1250.50,
"status": "completed"
}
]id,name,email,amount,status
1,John Doe,[email protected],1250.50,completed
2,Jane Smith,[email protected],890.25,pending
export const mockData = [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"amount": 1250.50,
"status": "completed"
}
];export const mockData = [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"amount": 1250.50,
"status": "completed"
}
] as const;To run the example file:
node data/mock/examples.mjsThis will generate multiple sample files in the data/mock/generated/ directory.
You can extend the FIELD_GENERATORS object to add custom field types:
const FIELD_GENERATORS = {
// ... existing generators
// Custom generator
customField: () => 'custom value',
// Generator with parameters
customRange: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
};- Use seeds for reproducible data during development
- Define field types that match your actual data structure
- Use business-specific types when available for more realistic data
- Test with small counts first before generating large datasets
- Validate generated data to ensure it meets your requirements
@faker-js/faker: For generating realistic fake datafs: For file system operationspath: For path manipulation