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

mango-seed

v0.1.1

Published

A small seeding library for mongoose, used primarily to make writing unit tests easier

Downloads

2

Readme

Mango-Seed

Build Status

Seeder is a simple utility for mongoose based applications that allows for quick an easy seeding when writing unit test!

Setup:

initialize a new Instance of Seeder and set the mongoose models.

const mongoose = require('mongoose');
const Seeder = require('Mango-Seed');

const url = 'mongodb://localhost:27017/SeederTest';
mongoose.connect(url);
const db = mongoose.connection;

db.on('error', console.error.bind(console, 'Connection Error'));


const userSchema = mongoose.Schema({
  name: String,
  email: {
    type: String,
    required: true,
    default: true,
  },
  password: {
    type: String,
    required: true,
  },
  admin: {
    type: Boolean,
    default: false,
  },
});

const User = mongoose.model('User', userSchema);

const seeder = new Seeder();
seeder.addModel('user', User);
/*
  To add many models at once
*/

const models = {
  { name: 'user', model: User },
  { name: 'post', model: Post },
  { name: 'project', model: Project },
};

seeder.addManyModels(models)

Here we passed the string 'user' to the seeder instance so we can access it like so:

seeder.user.create('admin', {admin: true})

API

Templates:

In order to simplify the process of creating unique users

/*
 * Hard-coding Values:
 * Hard-coding fields and values is the easiest way to set a teamplate.
 
 * However if you have constrains in your schema like unique emails, this could throw a validation error if documents are created without changing the email. See create for that.
*/
const userTemplate = {
  name: 'Mango',
  email: '[email protected]',
  password: 'chamoy',
}

/* Recommended way using Faker.js*/
/*
 * This ensures that every time you create a new document it's unique and won't throw a validation error. In addition, this would make using the utility's .createMany() method a lot seasier to use.
*/
const userTemplate = {
  name: faker.name.firstName,
  email: faker.internet.email,
  password: faker.internet.password,
}

//Set the template
seeder.user.setTemplate(userTemplate)

Create Document:

/**
 * creates - Persists a document to the database and saves it to this.documents
 * @param {string} name - Used to keep track of this document
 * @param {object} data - Properties you'd like to override from the model
 * @param {object} options - Needs work
 */
const tajin = await seeder.user.create()
//Returns { name: 'created document id', doc: {'Newly created document'}}

//With name and data passed
/*
 * The name passed in is set to the models documents object and makes it easy to recieve the document via seeder.user.documents.name
 
 * The second argument overiddes the properties set on the template
*/
const tajin = await seeder.user.create('admin', {admin: true, name: 'add-man'})
//Returns { name: 'admin', doc:{ admin: true, name: 'add-man' }}

/*
 * With options passed
 * with the cache option set to false, the method won't cache the created document to the seeder.user.documents object.
*/
const tajin = await seeder.user.create('admin', {admin: true, name: 'add-man'}, {cache: false});
//returns {'created document'}

Create Many Documents:

/**
   * createMany - Creates a number of documents
   * @param {number} num - Number of documents to create
   * @param {string} name - Name of the property to track the array
   * @param {object} data - Properties you'd like to override from the model object
   * @param {object} options - Needs work
   */
  const userArr = await seeder.user.createMany(2, 'admins', {admin: true});
  // Returns {name: 'admins', docs:[{documents}]}

Find Document:

/**
 * find - Query for a document from the database
 * @param {object} data - Parameters to query the db with
 */
  const foundMango = seeder.user.find({ email: '[email protected]' })
  //returns {'document form the database'}

Remove Documents:

/**
 * remove - removes domcument from this.documents and the database
 * @param {string} name - name of the documents you wan't to get rid of
 */
  seeder.user.remove(admins);
  //Deletes the documets and returns them
  seeder.user.removeAll();
  //Deletes all documents returns nothing

Thanks!!

Any questions or feedback could be sent to my email [email protected].

TODOS

  • Write examples.
  • expand to make it useful for sequelize.