mongoose-seed-2
v0.1.0
Published
Simple tool to clear & and populate your mongo db written in Typescript.
Downloads
75
Readme
mongoose-seeder-2
Simple tool to clear & and populate your mongo db written in Typescript.
Installation
yarn add -D mongoose-seed-2
Usage
// seed.ts
import mongoose from 'mongoose';
import { Seeder } from 'mongoose-seeder-2';
// 1. import models, so they register in mongoose
import { User } from './src/app/models';
// model example:
// mongoose.model('User', new mongoose.Schema({ email: String, unique: true }));
async function seed() {
// 2. connect seeder
const seeder = new Seeder(
'mongodb://<username>:<password>@<server>.mlab.com:<port>/<project>'
);
// 3. Pass names of models to be cleared
await seeder.clearModels(['User']);
// 4. Pass data to initialize db where key is model, and value is list of documents
await seeder.populateModels({
User: [{ email: '[email protected]' }, { email: '[email protected]' }],
});
await seeder.disconnect();
}
seed();
License
MIT