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

m-seeds

v1.3.1

Published

a mongoose seeding module

Downloads

26

Readme

m-seeds

M-seeds is a npm module for seeding collections for MongoDB databases, using Mongoose and the Faker contextual data generation tool. M-seeds take the defined schemas and generate the proper dummy data based on the functions of the faker tool indicated on the model schema definition file, as you can see below.

To install you can use npm or yarn

npm install m-seeds or yarn add m-seeds

How to use it

M-seeds only effects the models of schemas it is applied to. Inside the schema, the fake property instructs m-seeds how to generate the proper dummy data. See Faker for a list of all methods.

var mongoose = require("mongoose");
const Schema = mongoose.Schema;
const SubDoc = require("./subDoc");
var UserSchema = new Schema({
  firstName: {
    type: String,
    fake: "name.firstName" // calls faker.name.firstName()
  },
  lastName: {
    type: String,
    fake: "name.lastName" // calls faker.name.lastName()
  },
  userName: {
    type: String,
    fake: "internet.userName" // calls faker.internet.userName()
  },
  someOptionalParam: { type:String, fake:"optional:70%" }, // if you want some optional param, that 70% of times get filled and sometimes not
  someArrayOfStrings: [{ type: String, fake: "random.word" }], // this fills an array with random words
  someSubDoc: SubDoc // this sub document is filled too, with its fake options
  refToAnotherModel: { type: Schema.Types.ObjectId, ref: "AnotherModel" } // if you fill m-seeds with a list of models (mSeeds.setModels())
                                                                          // it can fill ref data with random id of any documents
                                                                          // if the collections have no documents
                                                                          // it create new one and store it and then
                                                                          // add it ref
});

module.exports = mongoose.model("User", UserSchema);

Run the Seeding

To run the seeding you can make a file just importing the module (const mSeeds = require("m-seeds")), the models, and stablish the database connection and then just simply call mSeeds.seed(someModel,count) or mSeeds.seedAll(count), where count is the number of documents we want to make on our collections.

Note: To call mSeeds.seedAll(count) you need to call before mSeeds.setModels(models), where models is a list of all models.

const mSeeds = require("m-seeds");
const models = require("./models");

doDatabaseConnection();
mSeeds.setModels(models);
//then you can simply make
mSeeds.seed(models["User"], 5); // this will fill the User collection with 5 documents with fake data.
// or
mSeeds.seedAll(5); // this will fill all the models collections passed on setModels, with 5 documents with fake data.

Seed with some Default Values

If you want to seed but you want to fill some properties with default values, you can use mSeeds.seedWithDefaults(someModel,count, defaults), where defaults is an object like key and value, to indicate the value for the right property, as you can see in this example

const mSeeds = require("m-seeds");
const models = require("./models");

doDatabaseConnection();

// this will fill the User collection with 5 documents, with firstName and lastName the same
// as indicate the 3th param
mSeeds.seedWithDefaults(models["User"], 5, {
  firstName: "Jesus",
  lastName: "Mota"
});

Generate fake docs not for seeding purposes

To generate a fake docs instances of a model you just need to call

mSeeds.makeFakeDoc(model);
// or if you want to generate it with with default Values
mSeeds.makeFakeDoc(model, defaults);

Test it

If you clone or fork this repo, after you do npm install or yarn install you can test this module as you can see below

# running tests
npm example or yarn example