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-control

v1.0.11

Published

Migrations and seed/fixtures framework for node (v16+) and mongoose (v7+).

Downloads

65

Readme

mongoose-control

npm npm GitHub license npm

Migrations (pending) and seed/fixtures (already done) framework and cli for node (v16+) and mongoose (v7+).

This project is based in:

Both projects use the MIT license. Please check its authors files for more information. Both projects seem to be discontinued, so I decided to merge some of their features and create this new project.

Installation

npm i --save-dev mongoose-control to use from your repository.

npm i -g mongoose-control to use from the command line (see CLI usage).

Usage

import { fixtures } from 'mongoose-control';

await fixtures({
    <collection name>: [
      <record>,
      <record>
    ],
    <collection name>: [
      <record>,
      <record>
    ]
});

Example

import mongoose from 'mongoose';
import { fixtures } from 'mongoose-control';

// User
const userSchema = new mongoose.Schema({
    username: String,
    password: String
});
mongoose.model('Users', userSchema);

// Book
const bookSchema = new mongoose.Schema({
    title: String
});
mongoose.model('Books', bookSchema);

// Create dataset immediately
//   - data is an array of all the documents created
let data = await fixtures({
    Users: [
        { username: 'one', password: 'pass' },
        { username: 'two', password: 'pass' }
    ],
    Books: [
        { title: 'Enders Game' },
        { title: 'Speaker of the Dead' }
    ]
});

// Name a dataset for future use
fixtures.save('Users', {
    users: [
        { username: 'one', password: 'pass' },
        { username: 'two', password: 'pass' }
    ]
});

// Use the named dataset
//  - data is an array of all documents created
data = await fixtures('Users');

Using files for fixtures and/or seeds

Create files for the data, i.e. userFixtures.js:

import { ObjectId } from 'mongodb';

// you can have relations using the same ObjectIds in different collections
const userId1 = new ObjectId();
const bookId1 = new ObjectId();

const Users = [
  {
    _id: userId1,
    name: 'Awesome User 1',
    books: [bookId1],
    ...
  },
  {
    ...
  },
];

const Books = [
  {
    _id: bookId1,
    title: 'Awesome Book 1',
    ...
  },
  {
    ...
  },
];

export default {
  Users,
  Books,
};

Now you can use your fixtures in your tests or any other place:

import { fixtures } from 'mongoose-control';
import fixturesData from 'userFixtures.js';

await fixtures(fixturesData);

Remember Mongoose have to be initialized before using fixtures(), and the models we are going to use have to be registered in Mongoose.

CLI

You can use some of the features of this package from the command line:

# install the package globally:
npm i -g mongoose-control

# to see the available options:
mongoose-control --help

# use as seeder, with a seed file (you can use the test files):
mongoose-control --url mongodb://localhost:27017/testing --models test/models/ seed test/example.data.js

API

Create a dataset

async fixtures(dataset, <mongoose instance>);

Immediately creates the documents from the dataset through the mongoose connection. Returns a flat array with every inserted fixture.

  • dataset can be a hash or a name of a named fixture.
  • mongoose instance is optional and is a singular instance of mongoose.

Save a named fixture

fixtures.save(name, dataset);

Save a fixture to be used for later. Returns previous existent fixture with the same name, if it existed.

  • name is the name of your new named fixture.
  • dataset is the hash of the dataset you want to save.

Retrieve a named fixture's dataset

fixtures.get(name);

Retrieves a named fixture's dataset.

  • name is the name of the named fixture you wish to retrieve.

Clear named fixture

fixtures.clear(<name>);

Clears named fixtures.

  • name is optional. It's the name of the named fixture. If omitted, all named fixtures will be cleared.

Reset database collection(s)

async fixtures.reset(<model name>, <mongoose instance>);

Deletes all documents within a collection. Returns the result of the delete operations.

  • model name is optional. It's the name of the collection. If omitted, all collections will be purged.
  • mongoose instance is optional and is a singular instance of mongoose.