Generate realistic mock data for your Mongoose schemas with ease.
mstdog
is a simple and efficient tool to generate mock data based on your Mongoose schemas. It supports various field types, embedded subdocuments, arrays, and more. Integrated with the faker
and randexp
library, it ensures that you get realistic mock data for each field type.
Install the package using npm:
npm install mstdog --save-dev
import mstdog from 'mstdog';
import { Schema } from 'mongoose';
const userSchema = new Schema({
name: String,
age: Number,
isActive: Boolean,
birthdate: Date,
});
const mockData = mstdog(userSchema.paths);
console.log(mockData);
const addressSchema = new Schema({
street: String,
city: String,
zipcode: String,
});
const educationSchema = new Schema({
degree: String,
institution: String,
year: Number,
});
const userSchema = new Schema({
name: String,
age: Number,
isActive: Boolean,
birthdate: Date,
bio: {
type: String,
text: true,
},
address: addressSchema,
education: [educationSchema],
hobbies: [String],
scores: [Number],
});
const mockData = mstdog(userSchema.paths);
console.log(mockData);
const options = {
customFieldGenerators: {
name: () => 'Custom Name',
age: () => 25,
}
};
const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
const options = {
typeGenerators: {
string: () => 'Custom String',
number: () => 42,
}
};
const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
const options = {
typeGenerators: {
string: (mockData, fieldOptions) => {
if (fieldOptions?.fieldName === 'email') {
if (mockData.username) {
return `${mockData.username}@example.com`;
} else {
return "random@email.com"
}
}
if (fieldOptions?.fieldName === 'username') {
return "generatedUsername";
}
return "randomString";
}
},
typeGeneratorDependencies: {
"root": {
"email": ["username"]
}
}
};
const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
mstdog
supports several options to customize data generation:
- arrayLength: Specify the length of arrays to generate (default:
3
). - maxDepth: Limit the depth of nested objects generated (default:
5
). - customFieldGenerators: Provide custom functions to generate data for specific fields.
- typeGenerators: Provide custom functions to generate data for specific types.
- typeGeneratorDependencies: Define dependencies between fields to ensure correct generation order.
Example with options:
const options = {
arrayLength: 5, // Generate arrays with 5 elements
maxDepth: 3, // Limit nested objects to a depth of 3
customFieldGenerators: {
name: () => 'Custom Name', // Override data generation for 'name' field
},
typeGenerators: {
string: () => 'Custom String',
number: () => 42,
},
typeGeneratorDependencies: {
"root": {
"email": ["username"]
}
}
};
const mockDataWithOptions = mstdog(userSchema.paths, options);
console.log(mockDataWithOptions);
Feedback, bug reports, and pull requests are welcome. Feel free to improve and suggest any changes.