Skip to content

tnsengimana/mongoose-faker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

TODO

  • Add unit tests
  • Improve documentation

Credits

Most of the code came from This repository. Thanks to Ahmed Agiza for making it available in the first place.

About

A small library to generate test data automagically for mongoose models

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published