@videsk/mongoose-dummy v1.4.5
π² Mongoose Dummy
Create realistic test data for your Mongoose models with zero hassle!
Mongoose Dummy is a powerful random data generator built specifically for Mongoose schemas. Generate realistic test data with support for complex relationships, nested objects, and custom generators. Perfect for testing, development, and seeding your MongoDB databases.
β¨ Features
- π Seamless integration with Mongoose models
- π Smart population of referenced models
- π Random selection from enum values
- π― Customizable field filters
- π§ Flexible array length control
- π¨ Works with Faker.js and other data generation libraries
- π¦ Support for nested objects and arrays
- π§ͺ Perfect for testing and development
π¦ Installation
npm install @videsk/mongoose-dummy
π Quick Start
import mongoose from 'mongoose';
import MongooseDummy from '@videsk/mongoose-dummy';
import { faker } from '@faker-js/faker';
// Initialize with mongoose
const dummy = new MongooseDummy(mongoose);
// Add faker.js support
dummy.generators = { faker };
// Generate fake data
const fakeUser = dummy.model('User').generate();
π Usage Guide
ποΈ Defining Schemas
Add the dummy
property to any field you want to generate data for:
const userSchema = new mongoose.Schema({
name: {
type: String,
dummy: ({ faker }) => faker.person.fullName()
},
email: {
type: String,
dummy: ({ faker }) => faker.internet.email()
},
address: {
street: {
type: String,
dummy: ({ faker }) => faker.location.streetAddress()
},
city: {
type: String,
dummy: ({ faker }) => faker.location.city()
}
},
createdAt: {
type: Date,
dummy: ({ faker }) => faker.date.past()
}
});
π Working with References
Automatically populate referenced models:
const orderSchema = new mongoose.Schema({
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
populate: true, // π Will generate full user data
dummy: true
},
products: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Product',
populate: true,
dummy: true
}],
total: {
type: Number,
dummy: ({ faker }) => faker.number.float({ min: 10, max: 1000 })
}
});
π Smart Enum Handling
const taskSchema = new mongoose.Schema({
status: {
type: String,
enum: ['pending', 'in-progress', 'completed'],
dummy: true // π Will randomly select from enum values
},
priority: {
type: String,
enum: ['low', 'medium', 'high'],
dummy: true
}
});
π― Custom Field Filters
Generate data only for specific fields:
// Only generate required fields
const requiredOnly = dummy.model('User').generate(
options => options.required === true
);
// Only generate fields with specific validators
const validatedFields = dummy.model('User').generate(
options => options.validate !== undefined
);
π Array Configuration
Control the length of generated arrays:
// Global array length setting
const dummy = new MongooseDummy(mongoose);
dummy.setup({ arrayLength: 5 });
// Generate data with custom array length
const data = dummy.model('Order').generate();
// All arrays will have 5 items
π Complex Relationships
Generate data with nested relationships and dependencies:
const companySchema = new mongoose.Schema({
name: {
type: String,
dummy: ({ faker }) => faker.company.name()
},
employees: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
populate: true,
dummy: true
}],
departments: [{
name: {
type: String,
dummy: ({ faker }) => faker.commerce.department()
},
manager: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
populate: true,
dummy: true
},
budget: {
type: Number,
dummy: ({ faker }) => faker.number.int({ min: 10000, max: 1000000 })
}
}]
});
π¨ Custom Data Generation
Use values from other fields in your generators:
const productSchema = new mongoose.Schema({
name: {
type: String,
dummy: ({ faker }) => faker.commerce.productName()
},
basePrice: {
type: Number,
dummy: ({ faker }) => faker.number.float({ min: 10, max: 1000 })
},
discountedPrice: {
type: Number,
dummy() {
return this.basePrice * 0.8; // Access other generated fields
}
}
});
β οΈ Limitations
- π Populate feature is limited to one level deep to prevent circular dependencies
- π·οΈ Fields without a
dummy
key are ignored in generation - π Some Mongoose features like virtual fields are not supported
π€ Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
π§ͺ Running Tests
npm test
π License
LGPL-2.1 License - Created with β€οΈ by Videskβ’
π Acknowledgments
Special thanks to all contributors and the Mongoose community for making this project possible!