Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tnsengimana/mongoose-faker
A small library to generate test data automagically for mongoose models
https://github.com/tnsengimana/mongoose-faker
faker mongoose mongoosejs
Last synced: 25 days ago
JSON representation
A small library to generate test data automagically for mongoose models
- Host: GitHub
- URL: https://github.com/tnsengimana/mongoose-faker
- Owner: tnsengimana
- License: mit
- Created: 2018-03-20T21:24:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-20T22:28:39.000Z (almost 7 years ago)
- Last Synced: 2024-12-15T08:53:34.078Z (about 2 months ago)
- Topics: faker, mongoose, mongoosejs
- Language: JavaScript
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-faker
`mongoose-faker` is a small library to generate dump data using mongoose models.
## Installation
`npm install --save mongoose-faker`
## Usage
### Quick usage
```javascript
const faker = require('mongoose-faker');// Creata a document and save it to the db
const student = await faker.generateObject(StudentModel, { save: true});// You can also pass in custom fields to your model
const course = await faker.generateObject(CourseModel, {save: true, custom: { students: [ student ] }});
```### Using sessions
Sometimes you may create lots of data and desire to clean up the db right after you are done. You can use session to accomplish that.
```javascript
const faker = require('mongoose-faker');describe('Magic', () => {
before(() => {
// Tells mongoose-faker that for all objects committed to the db, keep a reference to each one of them.
faker.newSession();
});after(async () => {
// Clean up db
await faker.destroySession();
});it('should pop magic', async () => {
const student = await faker.generateObject(StudentModel);// Pop magic here
});
})
```## TODO
- [ ] Add unit tests
- [ ] Improve documentation## Credits
Most of the code came from [This repository](https://github.com/thedgmbh/mongoose-dummy). Thanks to [Ahmed Agiza](https://github.com/thedgmbh) for making it available in the first place.