Giter Site home page Giter Site logo

sails-factory's Introduction

sails-factory

Sails Factory is a simple model factory for Sails.js. Inspired by factory_girl and rosie.

Installation

npm install sails-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 if model name is not provided.

var Factory = require('sails-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

Factory.build('active_user', function(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(user) {
  // user: non-persistent 'user' instance
  // {
  //    first_name: 'Hello',
  //    last_name: 'World',
  //    random_id: <number>
  // }
});

Factory.create('active_user', function(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>
  // }
});

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. By default, the model name will be set to factory file name if not provided on define parameters.

// api/models/User.js
module.exports = {
  attributes: {
    first_name: 'string',
    last_name: 'string',
    random_id: 'integer',
    active: 'boolean'
  }
};

// test/factories/User.js
module.exports = function(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);
};

// test/bootstrap.js
before(function(done) {
  require('sails').lift({
    log: {
      level: 'error'
    }
  }, function(err, sails) {
    if (sails) {
      //-- load factory definition files from test/factories
      require('sails-factory').load();
    }
    done(err);
  });
});

To load factory files from different folder:

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

To get the total number of loaded factory files:

Factory.load(function(count) {
  // count is the total number of loaded files
});

sails-factory's People

Contributors

hectorups avatar zand3rs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.