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-fakery-cn

v0.0.1

Published

Fixtures replacement and random data generator for mongoose.

Downloads

2

Readme

mongoose-fakery

mongoose-fakery provides an easy fixture replacement method and random data generators.

Inspiration

Parts of mongoose-fakery where inspired by:

Thanks.

Contributing

  1. Clone the repo.
  2. Create a branch.
  3. Write awesome code.
  4. Add tests for your changes. Test dependencies are defined in package.json.
  5. Open a Pull Request.
  6. Receive a 'Thank you!' and possibly a digital beer from me.

License

MIT.

TODO

  1. Associations with other models. This is my first priority.
  2. Browser compatibility.
  3. Add more data generators.

Documentation

Installing

npm install mongoose-fakery

Creating a fakery (factory)

In your models.js:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var UserSchema = new Schema({
    name: String,
    surname: String
});

mongoose.model('User', UserSchema);

In your tests or fixture files:

var fakery = require('mongoose-fakery');

fakery.fake('user', mongoose.model('User'), {
    name: 'john',
    surname: 'doe'
});

Getting a fakery

If the only argument you pass into fake() is the name of the factory then the method becomes a getter.

var userFakery = fakery.fake('user');

Lazy attributes

'Lazy' attributes are attributes that are resolved during a 'second' pass over the attributes of the fakery. Common usage are attributes that depend on other attributes for their value.

To create a 'lazy' attribute use fakery.lazy():

fakery.fake('user', mongoose.model('User'), {
    name: 'john',
    surname: 'doe',
    email: fakery.lazy(function(attrs) {
        // this will return [email protected]
        return attrs.name + '@example.com';
    });
});

Each lazy attribute receives all the resolved attributes of the first pass as the only parameter.

Using data generators

Data generators are functions that return data. That data can be random or follow specific patterns. mongoose-fakery comes with a number of pre-defined data generators which will probably suit most of your needs i.e:

  1. random strings (hex, alpha, alphanum)
  2. random numbers
  3. random booleans (true/false)
  4. lorem generator
  5. name, surname and gender
  6. picking random items from lists

Pre-defined data generators are exposed under the g attribute of the fakery object. Take a look in data_providers.js to see all the available generators and their APIs.

Some examples:

// using the user model defined above
fakery.fake('user', mongoose.model('User'), {
    name: fakery.g.name(),
    surname: fakery.g.surname()
});

Generators can also be used in arrays and nested attributes:

fakery.fake('post', mongoose.model('Post'), {
    name: fakery.g.name(),
    // this will create tags 'projects', <random string>, 'tech'
    tags: ['projects', fakery.g.str(5), 'tech']
});

Data generators can also be used when you just want to generate a bunch of random data for whatever purpose. They are not specific to test factories:

var fakery = require('mongoose-fakery');

// generate 10 random full names
var names = [], i;
for (i = 0; i < 10; i++) {
    names.push(fakery.g.fullname());
}

Creating custom data generators

mongoose-fakery gives you the option to create custom generators that attach themselves to the g attribute to the fakery object:

// declare like this
fakery.generator('custom', function() {
    return 'custom';
});

// use like this
var customGenerator = fakery.g.custom();
customGenerator(); // returns 'custom'

As you might have guessed, generators wrap 'data provider' methods in a function. You can thus do things like:

fakery.generator('timesTwo', function(n) {
    return n*2;
});

var timesTwo = fakery.g.timesTwo();
timesTwo(2); // returns 4

Making a fake model

To make a fake model, use the make() method. make() can also receive overrides.

var model = fakery.make('user');
var modelWithOverrides = fakery.make('user', {
    name: 'override'
});

Note that the model is not saved to the database.

Making & saving a fake model

To make and save a fake model, use the makeAndSave() method.

fakery.makeAndSave('user', function(err, user) {
    // `user` is saved to the database at this point
});

fakery.makeAndSave('user', {name: 'override'}, function(err, user) {
    // `user` is saved to the database and name is overriden to 'override'.
});