Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thebumpaster/mstdog
mstdog is a simple and efficient tool to generate mock data based on your Mongoose schemas.
https://github.com/thebumpaster/mstdog
faker generator mongoose
Last synced: 17 days ago
JSON representation
mstdog is a simple and efficient tool to generate mock data based on your Mongoose schemas.
- Host: GitHub
- URL: https://github.com/thebumpaster/mstdog
- Owner: TheBumpaster
- License: mit
- Created: 2023-09-16T17:54:04.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-22T17:58:51.000Z (over 1 year ago)
- Last Synced: 2024-03-26T07:05:35.118Z (10 months ago)
- Topics: faker, generator, mongoose
- Language: TypeScript
- Homepage:
- Size: 73.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Mongoose Schema to Dummy Object Generator
Generate realistic mock data for your Mongoose schemas with ease.
## Table of Contents
- [Description](#description)
- [Installation](#installation)
- [Usage](#usage)
- [Basic Usage](#basic-usage)
- [Handling Different Field Types](#handling-different-field-types)
- [Custom Field Generators](#custom-field-generators)
- [Type Generators](#type-generators)
- [Handling Dependencies](#handling-dependencies)
- [Options](#options)
- [Contributing](#contributing)
- [License](#license)## Description
`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.
## Installation
Install the package using npm:
```bash
npm install mstdog --save-dev
```## Usage
### Basic Usage
```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);
```### Handling Different Field Types
```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);
```### Custom Field Generators
```javascript
const options = {
customFieldGenerators: {
name: () => 'Custom Name',
age: () => 25,
}
};const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
```### Type Generators
```javascript
const options = {
typeGenerators: {
string: () => 'Custom String',
number: () => 42,
}
};const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
```### Handling Dependencies
```javascript
const options = {
typeGenerators: {
string: (mockData, fieldOptions) => {
if (fieldOptions?.fieldName === 'email') {
if (mockData.username) {
return `${mockData.username}@example.com`;
} else {
return "[email protected]"
}
}
if (fieldOptions?.fieldName === 'username') {
return "generatedUsername";
}return "randomString";
}
},
typeGeneratorDependencies: {
"root": {
"email": ["username"]
}
}
};const mockData = mstdog(userSchema.paths, options);
console.log(mockData);
```## Options
`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);
```## Contributing
Feedback, bug reports, and pull requests are welcome. Feel free to improve and suggest any changes.
## License
[MIT](./LICENSE)