🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

sails-hook-fireline

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sails-hook-fireline

Sails.js hook to use sequelize ORM

5.1.1
latest
Source
npm
Version published
Weekly downloads
30
Maintainers
1
Weekly downloads
 
Created
Source

forthebadge

npm version Stories in progress Build Status Dependencies DevDependencies GitHub license

sails-hook-fireline

Sails.js hook to use sequelize ORM. It's like Waterline but so much hotter. (Also, exclusive to SQL DB's)

Install

Install this hook with:

$ npm install sails-hook-fireline bluebird sequelize --save

As you can see, you have to install bluebird and sequelize independently and add them as dependencies. This allows you to better control you ORM versions, as this hook is somewhat version agnostic.

Bluebird is needed for transactions to work correctly. Please, refer to the official Sequelize docs linked above for more info about patching Bluebird (done inside this hook).

This hook supports versions 4 and 5.0-beta currently.

Configuration

Modify .sailsrc to resemble the next file:

{
  "hooks": {
    "orm": false,
    "pubsub": false
  }
}

Connections

To make a connection, configure sails like so

//sails.config.connections or sails.config.datastores
module.exports.connections = {
  somePostgresqlServer: {
    //You can pass an URI connection string. Be aware that this takes precedence
    //uri: 'postgresql://user:password@host:port/database',
    user: 'postgres',
    password: '',
    database: 'sequelize',
    //Thisi s a typical Sequelize constructor `options` object
    options: {
      dialect: 'postgres',
      host   : 'localhost',
      port   : 5432,
      logging: console.log //Why not? Just ship it like this to production. No biggie.
    }
  }
}
module.exports.models = {
  //You can also use sails.config.models.datastore
  connection: "somePostgresqlServer",
  migrate: "drop", // 'alter' and 'safe' are also options
}

Models

An example of model configuration on models/user.js

module.exports = {
  attributes: {
    name: {
      type: Sequelize.STRING,
      allowNull: false
    },
    age: {
      type: Sequelize.INTEGER
    }
  },
  associations: function() {
    user.hasMany(image, {
      foreignKey: {
        name: 'owner',
        allowNull: false
      }
    });
  },
  options: {
    tableName: 'user',
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
};

Available variables

  • Sequelize or sails.Sequelize as the constructor
  • sequelize or sails.Sequelize as the instance
  • Op as a shortcut to Sequelize.Op

Sequelize Extensions

To avoid a potential loss of an unique index when using deletedAt (for unicity and softDelete), an option can be passed to the model, making its default zero, avoiding such SQL halting experience.

module.exports = {
  attributes: {},
  associations: () => {},
  options: {
    classMethods: {},
    instanceMethods: {},
    hooks: {},
    paranoidSchizophrenia: true,
  }
};

It work just the same, except for some minor changes:

  • Default value for deletedAt is Date(0)
  • When deletedAt is accessed, if date is Date(0), it will return null (custom getter)
  • Queries including the aforementioned model, will default to inner-joins BEWARE
  • When making queries in paranoid: false, a non-default scope must be used. This package provides the drugged scope that has no effect over queries whatsoever for this specific purpose.
    //All three are equivalent
    let query = {
      where: {},
      paranoid: false,
    };
    User.scope(null).findAll(query);
    User.scope('drugged').findAll(query);
    User.unscoped().findAll(query);
    
    

Fireline Hooks

When fireline is preparing your modelrs, you can run your own functions to alter the default behaviour.

somePostgresqlServer: {
  user: 'postgres',
  password: '',
  database: 'sequelize',
  options: {
    dialect: 'postgres',
    host   : 'localhost',
    port   : 5432,
    logging: console.log
  },
  hooks: {
    myFirstHook: {
      beforeDefinition: (modelDef, modelList) => {},
      afterDefinition: (modelDef, modelList)  => {},
      beforeLoad: (modelDef, modelList) => {},
      afterLoad: (modelList)  => {},
      beforeAssociation: (model, modelList) => {},
      afterAssociation: (model, modelList) => {},
      beforeDefaultScope: (model, modelList) => {},
      afterDefaultScope: (model, modelList) => {},
    }
  }
}

The hooks to be run are:

  • beforeDefinition
  • afterDefinition

(Refering to model loading from files, just an object is loaded)

  • beforeLoad
  • afterLoad

(Sequelize define)

  • beforeAssociation
  • afterAssociation

(Associations are created with the association() methods of each model)

  • beforeDefaultScope
  • afterDefaultScope

(Default scope is added)

Special Thanks

A big shoutout to Gergely Munkacsy for starting the base of this fork at sails-hook-sequelize.

Also, special a special mention goes to Susana Hahn for using this hook to make awsome-factory-associator, a module that provides a syntax for easily creating factories when unit testing.

License

MIT

Keywords

sails

FAQs

Package last updated on 15 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts