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

create-dataset

v1.0.2

Published

[![Build Status](https://travis-ci.org/AnyFetch/create-dataset.png?branch=master)](https://travis-ci.org/AnyFetch/create-dataset) [![Dependency Status](https://gemnasium.com/AnyFetch/create-dataset.png)](https://gemnasium.com/AnyFetch/create-dataset) [![N

Downloads

37

Readme

Create dataset

Build Status Dependency Status NPM version

When writing tests with Node, we often need to create complex objects interdependencies.

Some patterns helps : factories, fixtures... but at the end of the day we still need to spend time managing our dependencies.

This library tries to bring the best of both world: a simple JS object to define the structure, and nothing more. It is framework agnostic and will work for Mongo or SQL objects.

Installation

npm install create-dataset --save-dev

Example use

For instance, to create a company, a user and a profile, one would simply write:

var createDataset = require('create-dataset');
// See next section
require('./create-configuration');

// We define our dataset here
var rawDataset = {
    // Leave an empty object, to inherit all default values from some factory
    company: {},

    // Create a user, 
    user1: {
        // We can also override properties from the default values in the factory
        name: "Some name",
        // For the company, we'll use the id from the company we just created
        company: createDataset.defer("company")
    },
    // Create another user with default values (and in another company, 
    user2: {
    },

    profile: {
        user: createDataset.defer("user")
    }
};

createDataset(rawDataset, function(err, dataset) {
    // At this point, we're all set, one can do
    console.log(dataset.user.id);
    dataset.company.save();
    // etc.
});

Configuration

Before using this, we need to set the config for objects creation. You only need to call this once.

var createDataset = require('create-dataset');
createDataset.config = {
    company: {
        generator: function(data, cb) {
            cb(null, CompanyFactory.create(data));
        }
    },
    user: {
        dependencies: ['company'],
        generator: function(data, cb) {
            cb(null, UserFactory.create(data));
        }
    },
    profile: {
        dependencies: ['user'],
        generator: function(data, cb) {
            cb(null, ProfileFactory.create(data));
        }
    }
};

There is only one mandatory key, generator, which must indicate how to create an instance from raw data. This can be a call to your factory builder, your ORM or your own custom function. It must return a new item (or an error following node convention).

Other keys:

  • dependencies, an array of models to build before building this object. When unspecified, no dependencies are implied. Be careful not to introduce deadlocks here (A needs B and B needs A)

How does it works? And potential caveats

For the sake of simplicity, the key name in your dataset is loosely matched with a model name. For instance, mainCompany will be matched to company.

This can be problematic when the name is ambiguous, for instance myUserCompany. In such a case, the first key to match in your config will be used (in this case, Company). If this is not enough, you can add a _model property to your dataset to force the use of a model:

var rawDataset = {
    // Will be matched with 'company' config
    startup: {
        _model: 'company'
    },
    // !! Will be matched with `user`, as `user` is defined before `profile` in createDataset.config
    userProfile: {

    }
}

Advanced use

Build over seed object

When calling createDataset, you may want to build over a pre-existing object.

You may then add a second parameter to createDataset, specifying the "seed object" on which to build:

var dataset = {
    hello: 'lol'
};
var rawDataset = {
    company: {}
    /* ... */
};

createDataset(rawDataset, dataset, function(err, dataset) {
    console.log(dataset.hello); // "lol"
    console.log(dataset.company); // [object Object]
});

Wrap with apply

You may also want to wrap the whole function in a simple function(err){}, for use with async or Mongoose's before. You can simply use .apply:

var dataset = {
    hello: 'lol'
};
var rawDataset = { /* ... */ };

before(createDataset.apply(rawDataset, dataset));

// which is equivalent to...

before(function(done) {
    createDataset(rawDataset, dataset, done);
});