kromitj / mean-seed

Module version of node_seed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#This needs to be updated...

#MeanSeed ##The Easy Way to Seed MongoDB

MeanSeed is a Node.js module that allows you to create a schema, populate a JSON file with faker seed-data, and export it into a MondoDB collection.

###examples directory Inside is an example seedFile(exampleSeed.js) that shows the steps to create and seed a collection use MeanSeed

This file shows the steps to seed a database

example.json and exampleArticle.json are pre-populated with seeds that were generated by running exampleSeed.js __ ##How to use it

###1 Navigate the terminal to the project directory and enter: // Terminal inside root dir of app

npm install https://github.com/kromitj/mean-seed

###2 Create a seed.js file at the root of your application #####This will be where you create your seed-data

###3 Require MeanSeed & require moongoose then connect mongoose to your local mongoDB server(which needs to be running) // ./seed.js

var MeanSeed = require('meanSeed');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/heroes');

###4 Create an instance of Meanseed: #####MeanSeed#init(mongooseObj:Obj, collectionName:String)

init takes in the mongoose object you just made and the name of the collection your seeding

           MeanSeed.init(mongooseObj, collectionName)
    var user = MeanSeed.init(mongoose,"User");

###5 Define the schema of the collection: #####MeanSeed#defineSchema( { schemaTemplate } ) Each key defines a property in your schema, and each assiated value represents the dataType of the property. For example, the f_name property has a datatType of String.

User.defineSchema({"f_name": String, "l_name": String, "username": String, "password_digest": String})

###6 Generate SeedData From the schema: #####MeanSeed.generateSeedData(seedCount:Int, [ fakerValueTypes ], JSONFileLocation:String(Optional) ); First argument states how many records you create. Each element in the second argument is assossiated with a property from the schema, assignment is done by the order in which you entered them, for example, the first element of the following array would be assosiated with the "f_name" of the schema. The values of each element corespond with the property names of a fakerList module that in turn return a related value using faker.js.The third arugument is optional and specifies where you want to generated JSON seeddata to populate, it defaults to "./seed.js"

	user.generateSeedData(10, ["fName", "lName", "username", "passwordDefault"]);

###Faker Data: ####MeanSeed uses Faker.js to generate fake seeddata. Inside the modules directory there's fakerList.js, and it contains properties that represent faker data-types; like fName returns a random first name, paragraph returns larem ipsum text and articleTitle returns a goofy title. It is also very easy to customize and add more fakertypes, you simply look at the faker.js docs, select a fakertype you want and add it was a property of the fakerList, for example if you wanted to add "random number" you would add this to fakerList:

//  ./module/fakerList.js

var fakerList = {
  randNum: function() {     // randNum can be named anything you want
    return faker.name.firstName();  // this is the function call inside faker.js
  },	
}

###Take a Look at What #generateSeedData created: ####Before you export to mongoDB check to see it the seed-data is right

//  ./seed.js 	- or where ever you specified:

[
  {
"title": "If we copy the port, we can get to the PCI capacitor through the cross-platform JSON card!",
"content": "Nihil animi dignissimos nisi aut repellat dolor ut. Asperiores ut amet odit quaerat et occaecati maxime quia. Sed natus eum blanditiis voluptas ex beatae inventore. Expedita maiores sit velit at pariatur. Eligendi voluptatem et laudantium eligendi quis quasi."
  },
  {
    "title": "The EXE panel is down, reboot the cross-platform program so we can bypass the PNG protocol!",
"content": "Rerum quia iusto quo itaque autem velit ea. Aspernatur sint rem. Numquam id consectetur sequi non corrupti voluptatem. Veniam nihil perferendis quia. Fuga rem culpa fuga voluptatem id sit exercitationem eligendi alias."
  }, 
  .... continue
]

###Now we Seed the DB! ####MeanSeed#exportToDB() Takes no arguments, just call and you're done

User.exportToDB();

###Thats it, look inside a mongoDB shell to see if it seeded correctly: // connect to the database your app is linked to: use ex: use blog

// show all the collections inside the database(you should see the one you just seeded):
show collections

// you can all the seeds/records listed out for you, like this:
db.<collectionName>.find()
db.User.find()

About

Module version of node_seed


Languages

Language:JavaScript 80.0%Language:Makefile 20.0%