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

sails-industrial-factory

v1.1.2

Published

Simple model factory for Sails.js with Sequelize inspired by Sails Factory

Downloads

1

Readme

forthebadge

Stories in Progress Build Status Codacy Badge bitHound Code bitHound Dependencies dependencies Status DevDependencies npm version GitHub license

sails-industrial-factory

Simple model factory for Sails.js with Sequelize inspired by Sails Factory

Installation

    npm install sails-industrial-factory

Usage

Defining factories

Define a factory by giving it a name and an optional model name. The factory name will be the default model name (parent model or the name itself) if model name is not provided.

var factory = require("sails-industrial-factory");

factory.define("user")
  .attr("first_name", "First Name")
  .attr("last_name", "Last Name")
  .attr("random_id", function() { return Math.random(); });

factory.define("active_user").parent("user")
  .attr("active", true);

factory.define("admin_user", Admin).parent("user");

Using factories

//Callback
factory.build("active_user", function(err, active_user) {
  // active_user: non-persistent "active_user" instance
  // {
  //    first_name: "First Name",
  //    last_name: "Last Name",
  //    random_id: <number>,
  //    active: true
  // }
});

factory.build("user", {first_name: "Hello", last_name: function() { return "World"; }}, function(err, user) {
  // user: non-persistent "user" instance
  // {
  //    first_name: "Hello",
  //    last_name: "World",
  //    random_id: <number>
  // }
});

factory.create("active_user", function(err, active_user) {
  // active_user: sails User model instance
  // {
  //    id: <id>,
  //    first_name: "First Name",
  //    last_name: "Last Name",
  //    random_id: <number>,
  //    active: true,
  //    createdAt: <date>,
  //    updatedAt: <date>
  // }
});

//Promise
factory.build("active_user")
  .then(()=> {
  // active_user: non-persistent "active_user" instance
  // {
  //    first_name: "First Name",
  //    last_name: "Last Name",
  //    random_id: <number>,
  //    active: true
  // }
  });

factory.build("user", {first_name: "Hello", last_name: function() { return "World"; }})
  .then(()=> {
  // user: non-persistent "user" instance
  // {
  //    first_name: "Hello",
  //    last_name: "World",
  //    random_id: <number>
  // }
  });

factory.create("active_user")
  .then(()=> {
  // active_user: sails User model instance
  // {
  //    id: <id>,
  //    first_name: "First Name",
  //    last_name: "Last Name",
  //    random_id: <number>,
  //    active: true,
  //    createdAt: <date>,
  //    updatedAt: <date>
  // }
  });

Auto increment attributes

Attributes can have an auto_increment option. By default, sequence will increment by 1, otherwise it will increment by whatever value the auto_increment option is set to. Counting starts at the initial value given. Sequence is shared among parent and children.

    factory.define("user")
      .attr("id", 0, {auto_increment: true})
      .attr("first_name", "First Name - ", {auto_increment: 5});

    factory.define("other_user").parent("user");

    factory.build("user", function(user) {
      // user:
      // {
      //    id: 1,
      //    first_name: "First Name - 5",
      //    ...
      // }
    });

    factory.create("user", function(user) {
      // user:
      // {
      //    id: 2,
      //    first_name: "First Name - 10",
      //    ...
      // }
    });

    factory.build("other_user", function(other_user) {
      // other_user:
      // {
      //    id: 3,
      //    first_name: "First Name - 15",
      //    ...
      // }
    });

Loading factories

Calling .load() without parameter will try to load factory definitions from test/factories folder.

// api/models/User.js
module.exports = {
  attributes: {
    first_name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    last_name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    last_name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    active: {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
  },
};


// test/factories/User.js
module.exports = function(factory) {
  factory.define("user")
    .attr("first_name", "First Name")
    .attr("last_name", "Last Name")

  factory.define("active_user").parent("user")
    .attr("active", true);
};

// test/bootstrap.js
before(function(done) {
  require("sails").lift({
    log: {
      level: "error"
    }
  }, function(err, sails) {
    if (sails) {
      // -- make factories global to your tests
      global.factory = require("sails-industrial-factory");
      //-- load factory definition files from test/factories
      factory.load();
    }
    done(err);
  });
});

To load factory files from different folder:

factory.load("/path/to/factories");

License

MIT

NPM