mstdog
Version:
Mongoose Schema To Dummy Object Generator
185 lines (145 loc) • 4.3 kB
Markdown
Generate realistic mock data for your Mongoose schemas with ease.
- [Description](
- [Installation](
- [Usage](
- [Basic Usage](
- [Handling Different Field Types](
- [Custom Field Generators](
- [Type Generators](
- [Handling Dependencies](
- [Options](
- [Contributing](
- [License](
`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:
```bash
npm install mstdog --save-dev
```
```javascript
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);
```
```javascript
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);
```
```javascript
const options = {
customFieldGenerators: {
name: () => 'Custom Name',
age: () => 25,
}
};
const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
```
```javascript
const options = {
typeGenerators: {
string: () => 'Custom String',
number: () => 42,
}
};
const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
```
```javascript
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:
```javascript
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.
[](./LICENSE)