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-hook-fireline

v5.1.1

Published

Sails.js hook to use sequelize ORM

Downloads

74

Readme

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