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

fakegoose

v0.0.3

Published

Faker + Mongoose

Downloads

20

Readme

Fakegoose

Fakegoose is a plugin for simulating queries and seeding collections in Mongoose using the Faker contextual data generation tool. Fakegoose takes Mongoose schemas and generates the proper dummy data based on the types and defaults described for on-the-fly queries or seeding collections for testing.

npm install fakegoose

Command-line Usage

Seeding the database

fakegoose examples/chat-message.js --count 42 --seed mongodb://localhost:27017/test

Quick JSON documents

fakegoose examples/chat-message.js --count 42

Without the --seed [mongo_url] argument, generated documents will be printed with console.log().

Note: Fakegoose must be installed globally install --global to be used from the command-line.

Programmatic Usage

Fakegoose works like other Mongoose plugins and only effects the models of schemas it is applied to. Inside the schema, the fake property instructs Fakegoose how to generate fake data. See Faker for a list of all methods. If Faker does not have the generator you need, fake can also be a function that takes no arguments.

// models/chat-message.js
var mongoose = require('mongoose');
var fakegoose = require('fakegoose');

var chatMessageSchema = new mongoose.Schema({
  first: {
    type: String,
    fake: 'name.firstName'  // calls faker.name.firstName()
  },
  last: {
    type: String,
    fake: 'name.lastName'   // calls faker.name.lastName()
  },
  text: {
    type: String,
    fake: 'lorem.paragraph' // calls faker.lorem.paragraph()
  },
  date: {
    type: Date,
    fake: 'date.past'       // you get the pattern
  }
});

chatMessageSchema.plugin(fakegoose);
module.exports = mongoose.model('ChatMessage', chatMessageSchema);

Fakegoose adds static methods fake (find variant) and fakeOne (findOne variant) for querying, and seed for database population.

Querying

The fake and fakeOne methods are drop-in replacements for Mongoose's find and findOne accepting the same arguments and using the same chaining interface.

Model.fake

  • fake([conditions]) Query
  • fake(conditions[, options]) Query
  • fake(conditions[, options], callback:(error, results)) Query

Model.fakeOne

  • fakeOne([conditions]) Query
  • fakeOne(conditions[, options]) Query
  • fakeOne(conditions[, options], callback:(error, results)) Query
// elsewhere
var assert = require('assert');
var mongoose = require('mongoose');
var ChatMessage = mongoose.model('ChatMessage');

ChatMessage.fakeOne({first: 'Chris'}, function(error, message) {
  if(error) {
    // this won't be called ever but is good
    // to include if #fakeOne is ever going
    // to be changed to #findOne.
  }
  assert.equal(message.first, 'Chris');
});

Fakegoose queries will conform to simple conditions, but don't yet interpret $[g|l]t[e], $in, or other expressions, but can with help from viewers like you. Options like select, limit, skip, and lean work properly, however complicated features such as aggregation and populate do not..yet.

Seeding

Model.seed

  • Model.seed(count:number[, forceAppend=false], callback:(error))

Model.seed adds count documents to the model's collection, passing an error to the completion callback if there was a Mongoose error. By default if you specify a count Fakegoose will only seed at a maximum the number of documents necessary to reach the count. So if your collection has 42 records and you call Model.seed(69, ...) only 27 documents will be added to the collection. This is done because seeding generally is safe to perform multiple times without overfilling the database. To add exactly count documents, use Model.seed(420, true, myCallback).

Contributing

Contributions are incredibly welcome as long as they are standardly applicable and pass the tests (or break bad ones). Tests are written in Mocha and assertions are done with the Node.js core assert module.

# running tests
npm run test

Follow me on Twitter for updates or just for the lolz and please check out my other repositories if I have earned it. I thank you for reading.