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

quick-mongo-seed

v2.0.0

Published

A module to help you create a seed database for your test environments

Downloads

53

Readme

Quick Mongo Seed

Quick Mongo Seed is an npm module designed to simplify the process of seeding a MongoDB database for your Node.js applications, particularly in a testing environment. It allows you to easily populate your MongoDB collections with data from seed files.

Installation

npm i quick-mongo-seed

Usage

Use the function in your test files as shown below. It will automatically seed the database before tests and close the connection clearing the database after all tests have run.

setUpDb("databaseName",true,"mongodb://127.0.0.1","/path/to/seedFiles")

Below is an example using supertest.

import {app} from '../index.js'
import supertest from 'supertest'
import { setupDB } from "quick-mongo-seed"

const request =supertest(app)
setUpDb("databaseName",true,"mongodb://127.0.0.1","/path/to/seedFiles")
it("should return all products that belong to a particular user", async ()=>{
  
const res= await request.post("/products/list").send({
    email:"[email protected]"
})
expect(res.status).toBe(200)
expect(res.body[0].publisher).toBe("[email protected]")


})

The implementation of the endpoints you want to test depends on you. In the example above, we seed our database with products and users from files, product.seed.js and user.seed.js. The controllers for this endpoints use models named Product and User in their logic. Read along to see how this works.

Parameters:

  • databaseName (Type: string): The name of the MongoDB database to be used for seeding.

  • runSaveMiddleware (Type: boolean): Determines the seeding method. Set to true for create() and false for insertMany().

  • connection_url (Type: string): The connection string to the MongoDB server.

  • seedDir (Type: string): The path to the folder containing the seed files.

Seed Files Format

Seed files should follow a specific naming convention and export data in a particular format:

  1. File Naming Convention: Seed files should be named in the following format: <model name>.seed.js, where <model name> corresponds to the name of the MongoDB model or collection to which the data should be seeded. For example:

    • If you want to populate the "Users" collection, name the seed file as user.seed.js.
    • For the "Products" collection, use the name product.seed.js.
  2. Data Export Format: Each seed file should export data in the following format using the export default syntax:

    export default [
        {
            // Data for the first document
            key1: value1,
            key2: value2,
            // ...
        },
        {
            // Data for the second document
            key1: value1,
            key2: value2,
            // ...
        },
        // Additional documents as needed
    ]

Using the runSaveMiddleware Parameter

The runSaveMiddleware parameter in the setupDB function controls whether to use create() or insertMany() when populating the database. This parameter affects the behavior of schema validations and middleware in your MongoDB models.

  • When runSaveMiddleware is set to true:

    • The create() method is used to insert documents into the database.
    • This method invokes schema validations and triggers any associated middleware in your MongoDB models.
    • Schema validations ensure that data adheres to the defined schema, enforcing constraints such as required fields, data types, and more.
  • When runSaveMiddleware is set to false:

    • The insertMany() method is used to insert documents into the database.
    • This method does not trigger schema validations or middleware in your MongoDB models.
    • Data is inserted directly into the database without schema checks or custom logic defined in your models.

Example:

Suppose you have a MongoDB model for users defined as follows:

import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
    email: {
        type: String,
        required: [true, "An email must be provided"],
    },
    password: {
        type: String,
        required: [true, "A password must be provided"],
    },
});

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

export default User;

If you call setupDB with runSaveMiddleware set to true:

  • It will use User.create() to insert data into the "User" collection.
  • Schema validations will be enforced, ensuring that data meets the defined schema requirements.

If you call setupDB with runSaveMiddleware set to false:

  • It will use User.insertMany() to insert data into the "User" collection.
  • Schema validations and middleware will be bypassed, allowing data to be inserted without validation checks.

Choose the appropriate value for runSaveMiddleware based on your testing and seeding needs, considering whether you want schema validations and middleware to be applied during data insertion.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.