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-seeder-plugin

v1.0.3

Published

Seed DB by adding options to schema

Downloads

9

Readme

mongoose-seeder-plugin

Generates seed data for mongodb database through mongoose plugin

Installation

npm install --save mongoose-seeder-plugin

Requiring

Export your schema(s) instead of the model(s).

const mongoose = require('mongoose');

const clientSchema = new mongoose.Schema(
  {
    firstName: { type: String, lowercase: true, seed: 'firstName' },
    lastName: { type: String, lowercase: true, seed: 'lastName' },
    email: { type: String, lowercase: true, seed: 'email' },
  },
  { timestamps: true }
);

module.exports = clientSchema;

Setup

Add the "seed" option to each path you wish to seed.

const clientSchema = new mongoose.Schema({
  firstName: { type: String, seed: 'firstName' },
});

Seed Options

The seed option accepts the following values:

  • "firstName" - Random male or female first name.
  • "lastName" - Random last name.
  • "firstMen" - Random male first name.
  • "firstWomen" - Random female first name.
  • "fullName" - Random male or female full name.
  • "phone" - Random 10 digit phone number.
  • "email" - Random email address.
  • "password" - Random password.
  • "fileName" - Random file name.
  • "pickOne" - Select random item from array. MUST PASS 'enum' option in path.
    fileType: {
        type: String,
        enum: ['application/pdf', 'application/json'],
        default: 'application/pdf',
        seed: "pickOne"
    },

Seeding Refs for population

Schema type must contain 'ref' option with the value of the model you wish to populate from. The type option must equal mongoose.Schema.Types.ObjectId.

  • For single document population pass 1 as the value of the 'refseed' option.
  client: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Client",
        refseed: 1,
    },
  • For multipul documents pass an array with two numbers. This will return a random count of Object Ids ranging between them. The example below will seed between 2 to six Object Ids from the model "Client"
  client: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Client",
        refseed: [2, 6],
    },

Example Seeder

// index.js

require('dotenv').config();
const mongoose = require('mongoose');
const uri = process.env.MONGODB_URI;
const opt = require('./options');

// Create mongoose connection & add seeder plugin
const conn = mongoose.createConnection(uri, opt);
conn.plugin(require('mongoose-seeder-plugin'));

// Create models
const User = conn.model('User', require('./schema/user.schema'));
const Client = conn.model('Client', require('./schema/client.schema'));
const Upload = conn.model('Upload', require('./schema/upload.schema'));

(async () => {
  // Seed 10 Users
  const users = await User.seed(10);
  console.log(users.length, 'Users added');

  // Seed 5 Clients
  const clients = await Client.seed(5);
  console.log(clients.length, 'Clients added');

  // Seed 10 Uploads
  const uploads = await Upload.seed(10);
  console.log(uploads.length, 'Uploads added');

  console.log('Seeding complete');
})();
// user.schema.js
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      trim: true,
      lowercase: true,
      seed: 'fullName',
    },
    email: {
      type: String,
      trim: true,
      lowercase: true,
      seed: 'email',
    },
    password: {
      type: String,
      seed: 'password',
    },
    role: {
      type: String,
      enum: ['admin', 'user'],
      default: 'user',
      seed: 'pickOne',
    },
  },
  {
    timestamps: true,
  }
);

module.exports = userSchema;
// client.schema.js
const mongoose = require('mongoose');

const clientSchema = new mongoose.Schema(
  {
    firstName: { type: String, lowercase: true, seed: 'firstName' },
    lastName: { type: String, lowercase: true, seed: 'lastName' },
    email: { type: String, lowercase: true, seed: 'email' },
  },
  { timestamps: true }
);

module.exports = clientSchema;
// upload.schema.js
const mongoose = require('mongoose');

const uploadSchema = new mongoose.Schema(
  {
    _client: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Client',
      private: true,
      privateRef: true,
      refseed: 1,
    },
    _users: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        private: true,
        privateRef: true,
        refseed: [1, 5],
      },
    ],
    fileName: { type: String, trim: true, lowercase: true, seed: 'fileName' },
    fileType: {
      type: String,
      enum: ['application/pdf', 'application/json'],
      default: 'application/pdf',
      seed: 'pickOne',
    },
    fileSize: Number,
  },
  { timestamps: true }
);

module.exports = uploadSchema;

Mongoose Connection Options

Example

// options.js
module.exports = {
  /**
   * Connection Settings
   */
  //bufferTimeoutMS: 500,   // default 30000
  autoIndex: true, // true for dev - (false = Don't build indexes)
  dbName: 'mydb', // db to connect to
  /**
   * Tunning Settings
   */
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  poolSize: 5, // Default 5 - MAX 10?
  socketTimeoutMS: 45000, // 30000 by Default (30 seconds), you should set this to 2-3x your longest running operation
  family: 4, // Use IP4 instead of trying IP6 first
  //socketTimeoutMS: 0,
  //keepAlive: true,
  //reconnectTries: 30

  /**
   * useUnifiedTopology: true - Options
   */
  useUnifiedTopology: true,
  //
  //
  // Raise this if you get a timeout error
  serverSelectionTimeoutMS: 30000, // Keep trying to send operations for 30 sec - 30000 by Default (30 seconds) -
  heartbeatFrequencyMS: 30000, //A heartbeat is subject to serverSelectionTimeoutMS
};