@floogulinc/cypress-mongo-seeder
Version:
A cypress plugin to populate a mongo db from json files
85 lines (61 loc) • 1.79 kB
Markdown
> A tool to quickly populate your mongo db from a set of .json files. The package prepared for seed database before each test on Cypress. You can use as a Cypress plugin or independently.
```sh
$ npm install --save-dev cypress-mongo-seeder
or
$ yarn add --dev cypress-mongo-seeder
```
Read all json files in specified directory and insert them to mongodb. (File names are collection names)
```javascript
seedAll: (
mongoUri: string,
dataFolder: string,
dropCollection?: boolean
) => Promise<any>;
```
cypress/plugins/index.js
```javascript
const path = require('path');
const seeder = require('cypress-mongo-seeder');
const mongouri = 'mongodb://localhost:27017/dummy-db';
const folder = './data';
const dropCollections = true;
module.exports = on => {
on('task', {
'seed:database': () => {
return seeder.seedAll(mongouri, folder, dropCollections);
},
});
};
```
Reads a json file and inserts content to mongo. (File name is collection name )
```javascript
seedSingleCollection: (
mongoUri: string,
fileAbsolutePath: string,
dropCollection?: boolean
) => Promise<any>;
```
```javascript
const path = require('path');
const seeder = require('../build');
const mongouri = 'mongodb://localhost:27017/dummy-main';
const fileFullPath = path.resolve(__dirname + '/data/users.json');
const dropCollection = true;
seeder
.seedSingleCollection(mongouri, fileFullPath, dropCollection)
.then(seeded => {
console.log('Seeded -> ', seeded);
});
```
1. Fork the project
2. Add feature(s)
3. Add tests for it
4. Submit pull request
Enjoy!