Generate random fake data with your mongoose schema
MIT License
Generate random fake data with your mongoose schema. Demo application is available here.
# Using npm
npm i mongoose-data-faker
# Using yarn
yarn add mongoose-data-faker
const { generate } = require('mongoose-data-faker');
const { Schema } = require('mongoose');
// This is just an example schema, feel free to use something more complicated
const sampleSchema = new Schema({
name: String,
age: Number
});
generate(sampleSchema, 50);
/* RESULT:
[
{
name: 'gbemisola', // This can be any random string
age: 40 // This can be any random number
}
// ...49 more objects in this array
]
*/
Mongoose-data-faker would try as much as possible to respect the standard mongoose schemaType options that makes sense for data generation. This is a list of some schema type options and how mongoose-data-faker would use them for data generation:
maxlength
min
max
min
max
For eample:
const schema = new Schema({
// name would always be one of 'olaide', 'oyindamola' or 'gbemisola'
name: {
type: String,
enum: ['olaide', 'oyindamola', 'gbemisola']
},
// age would always be greater than or equal to 18
age: {
type: Number,
min: 18,
},
// createdAt would always be greated than or equal to
// 1598308338428(unix time stamp)
createdAt: {
type: Date,
min: 1598308338428,
}
})
Mongoose-data-faker supports using Faker for generating specific types of string values. You can specify a faker property in the schemaTypeOption to use a specific faker API, for example:
const user = {
name: {
type: String,
// Generate a random firstName with faker
faker: 'name.firstName',
},
email: {
type: String,
// Generate a random email with faker
faker: 'internet.email',
},
phoneNumber: {
type: String,
// Generate a random phone number with faker
faker: 'phone.phoneNumber',
},
zipCode: {
type: String,
// Generate a random zipcode with faker
faker: 'address.zipCode'
}
}
const schema = new Schema(user);
// Generate 10 random user object
generate(schema, 10);
Mongoose-data-faker currently does not support generation for custom schema type and this is simply because it does not recognize any custom schema type and consequently can't tell what data to generate for them. In the future, I hope to be able to add a way with which you can register your custom data types as well as custom generators. For now, the package would throw an error if it comes across a schema type it does not recognize.
I answer questions about MongoDB and Mongoose on StackOverflow. More than often when people ask questions, they simply post their mongoose schema, and in the process of trying to answer their questions, I need to test out some queries on actual data. I find myself manually composing sample data and this consumes a lot of valuable time.
I researched existing tools but everything I came across requires me to learn some form of syntax for data generation, I did not want to do that, I want something I can just point my schema to and flexibly generate as much data as I want... No stress... No hassle.
While the inspiration for this project is from answering questions on StackOverflow, the usage is not limited to that, you can use the project for any other use-case you see fit.
Contributions are highly welcomed. To contribute to this project: