mongoose-faker

1.0.0 • Public • Published

mongoose-faker

mongoose-faker is a small library to generate dump data using mongoose models.

Installation

npm install --save mongoose-faker

Usage

Quick usage

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.

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
    });
})

Credits

Most of the code came from This repository. Thanks to The D. GmbH for making it available in the first place.

Package Sidebar

Install

npm i mongoose-faker

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

16.7 kB

Total Files

7

Last publish

Collaborators

  • tnsengimana