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

@coorpacademy/mongo-fixme

v1.0.2

Published

Easy JSON fixture loading for MongoDB. Makes managing document relationships easier. This is a forked version of pow-mongodb-fixtures

Downloads

4

Readme

mongodb-fixtures

Build Status

This is a forked version of pow-mongodb-fixtures

Simple fixture loader for MongoDB on NodeJS. Makes managing relationships between documents easier.

Fixtures can be in one file, or divided up into separate files for organisation (e.g. one file per model)

The fixture files must export objects which are keyed by the MongoDB collection name, each containing the data for documents within that.

FOR EXAMPLE: With the file below, 3 documents will be inserted into the 'users' collection and 2 into the 'businesses' collection:

//fixtures.js
exports.users = [
    { name: 'Gob' },
    { name: 'Buster' },
    { name: 'Steve Holt' }
];

exports.businesses = [
    { name: 'The Banana Stand' },
    { name: 'Bluth Homes' }
];

You can also load fixtures as an object where each document is keyed, in case you want to reference another document. This example uses the included createObjectId helper:

//users.js
var id = require('mongo-fixme').createObjectId;

var users = exports.users = {
    user1: {
        _id: id(),
        name: 'Michael'
    },
    user2: {
        _id: id(),
        name: 'George Michael',
        father: users.user1._id
    },
    user3: {
        _id: id('4ed2b809d7446b9a0e000014'),
        name: 'Tobias'
    }
}

CLI usage

A CLI program is included for quickly loading fixture files. To use it install the module globally:

npm install mongo-fixme -g

Then use the program to install a file or directory:

mongofixtures <dbname> <fixture file>

mongofixtures appdb fixtures/users.js

API

connect(dbname, options)

Returns a new Loader instance, configured to interact with a certain database.

Options:

  • host (Default: localhost)
  • port (Default: 27017)
  • user
  • pass
  • safe (Default: false)

Usage:

var fixtures = require('mongo-fixme').connect('dbname');

var fixtures2 = require('mongo-fixme').connect('dbname', {
  host: 'http://dbhost.com/',
  port: 1234
});

load(data, callback)

Adds documents to the relevant collection. If the collection doesn't exist it will be created first.

var fixtures = require('mongo-fixme').connect('mydb');

//Objects
fixtures.load({
    users: [
        { name: 'Maeby' },
        { name: 'George Michael' }
    ]
}, callback);

//Files
fixtures.load(__dirname + '/fixtures/users.js', cb);

//Directories (loads all files in the directory)
fixtures.load(__dirname + '/fixtures', callback);

clear(callback)

Clears existing data.

fixtures.clear(function(err) {
    //Drops the database
});

fixtures.clear('foo', function(err) {
    //Clears the 'foo' collection
});

fixtures.clear(['foo', 'bar'], function(err) {
    //Clears the 'foo' and 'bar' collections
});

clearAllAndLoad(data, callback)

Drops the database (clear all collections) and loads data.

clearAndLoad(data, callback)

Clears the collections that have documents in the data that is passed in, and then loads data.

var data = { users: [...] };

fixtures.clearAndLoad(data, function(err) {
    //Clears only the 'users' collection then loads data
});

addModifier(callback)

Adds a modifier (function) which gets called for each document that is to be inserted. The signature of this function should be:

(collectionName, document, callback)
  • collectionName - name of collection
  • document - the document which is to be inserted
  • callback - function with signature (err, modifiedDocument). This should be called with the modified document.

Modifiers are chained in the order in which they're added. For example:

var data = { users: [...] };

// this modifier will get called first
fixtures.addModifier(function(collectionName, doc, cb) {
  doc.createdAt = new Date();

  cb(null, doc);
});

// this modifier will get called second with the result from the first modifier call
fixtures.addModifier(function(collectionName, doc, cb) {
  doc.updatedAt = new Date();

  cb(null, doc);
});

fixtures.load(data, function(err) {
    // each loaded data item will have the createdAt and updatedAt keys set.
});

Installation

npm install mongo-fixme

Changelog

###1.0.2

  • fix npm deploy key

###1.0.1

  • fix tag

###1.0.0

  • breaking change doesn't support node 0.10 & 0.12
  • support node 5 and node 6
  • async: 2.0.0-rc.6
  • bson: 0.4.23
  • lodash: 4.13.1
  • mongodb: 2.1.18

###0.10.1

  • Update mongodb driver to 1.4.31
  • fix mongo db minor version
  • replace underscore to lodash
  • rename project to mongodb-fixtures
  • add npm test support

###0.10.0

  • Update mongodb driver to 1.3.x
  • Add ability to connect with URI
  • Make safe mode the default

###0.8.1

  • Add mongofixtures CLI program

###0.7.1

  • Add 'safe' option (donnut)

###0.7.0

  • Add user and password options for connecting to authenticated/remote DBs

###0.6.4

  • Add username and password connect options

###0.6.3

  • Make clear be safe

###0.6.2

  • Windows fixes (samitny)

###0.6.1

  • Ignore subdirectories (hiddentao)