npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-data-seeder

v1.0.3

Published

Seed your MongoDB database with a set of collections including referential relationship using mongoose

Downloads

74

Readme

mongoose-data-seeder

This package is refactored from origianl Sam Verschueren's mongoose-seeder package, I only did minor code change to make it as ES6 class and working with mongoose 5.x.

I need do this refactoring due to an internal MERN project, I publish this package hoping it might be useful for someone who also need seed a MongoDB for his or her project using Node and MongoDB with Mongoose.

Install

$ npm install mongoose-data-seeder

How to use

Start a local mongo instance using docker and create a 'test' db

$ docker run -d --name local-mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=welcome -e MONGO_INITDB_DATABASE=test mongo:3.6

Create seed data project

$ npm init

Install required packages

$ npm install --save mongo mongoose mongoose-data-seeder commander

Prepare seed data "data.json" including document referencing another document

{
  "teams": {
    "_model": "teams",
    "teamA": {
      "name": "Team A",
      "company": "Startup"
    }
  },
  "users": {
    "_model": "users",
    "user1": {
      "firstName": "Joe",
      "name": "User One",
      "email": "[email protected]",
      "teamId": "->teams.teamA._id"
    }
  }
}

If you want to know more about loading documents that have a reference to another document, please see the original Sam Verschueren's mongoose-seeder project

Create test-seeder.js using 'mongoose-data-seeder' to load seed data into Mongo database

const command = require('commander')
const mongoose = require('mongoose')

const MongooseDataSeeder = require('mongoose-data-seeder')

const dbUrl = 'mongodb://mongoadmin:welcome@localhost:27017/?authMechanism=DEFAULT&authSource=admin'

// create or import mongoose schema
const TeamSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  name: {type: String, required: true},
  company: {type: String},
})

mongoose.model('teams', TeamSchema)

const UserSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  firstName: {type: String, required: true},
  name: {type: String, required: true},
  fullName: {type: String},
  email: {type: String, required: true},
  birthday: {type: Date},
  teamId: {type: mongoose.Schema.Types.ObjectId, ref: 'teams'}
})

mongoose.model('users', UserSchema)

command
  .version('1.0.0')
  .option('-d --data [jsonfile]', 'seed data in json file')
  .parse(process.argv)

const jsonDataFile = command.data ? command.data : './data.json'

// connect to MongoDB
mongoose.connect(dbUrl, {dbName: 'test', autoIndex: false}, function(err){
  if (err) {
    console.log(`Failed to connect to MongoDB at ${dbUrl}`)
    console.log(err)
    process.exit(1)
  } else {
    console.log(`Connected to MongoDB, Prepare to load seed data from ${jsonDataFile} ...`)

    const seedData = require(jsonDataFile)

    const mongoSeeder = new MongooseDataSeeder({dropCollection: true})

    mongoSeeder
      .load(seedData)
      .then(function(dbData) {
        console.log("seedData is loaded into test database!")
        console.log(dbData)

        process.exit(0)
      })
      .catch(function(err) {
        console.log("Failed to load seedData into test database")
        console.log(err)

        process.exit(1)
      })
  }
})

Run "test-seeder.js" script loading seed data into MongoDB

$ node test-seeder.js --data ./data.json
Connected to MongoDB, Prepare to load seed data from ./data.json ...
seedData is loaded into test database!
{ teams:
  { teamA:
    { name: 'Team A',
      company: 'Startup',
      _id: 5c36f4ea400d5ab55a981a98,
      __v: 0
    }
  },
  users:
  { user1:
    { firstName: 'Joe',
      name: 'User One',
      email: '[email protected]',
      teamId: 5c36f4ea400d5ab55a981a98,
      _id: 5c36f4ea400d5ab55a981a99,
      __v: 0
    }
  }
}

License

MIT © Jian Wu