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-sqlize

v0.3.3

Published

create sequelize model counterparts for sails models

Downloads

7

Readme

sails-hook-sqlize

Sequelize hook for Sails.js v0.11

Install

npm install --save sails-hook-sqlize

Purpose

create Sequelize models using your sails model definitions, and make them available via sails.sequelize['modelName'], giving you access to both sails and sequelize models for the same table

Background

Sails is pretty great. Even Waterline, Sails' default ORM is pretty great and works fine for most heavily CRUD based apps. It even gives you some pretty cool things right out of the box like pubsub support over websockets. But when you start to get into some more complex querying, the limitations become apparent fairly quickly. My biggest complaint is Waterline's lack of support for proper joins.

Take a data structure like the following:

  • Users belong to many Groups
  • Groups belong to many Abilities and Abilities belong to many Groups
  • Abilities belong to one Resource

Going from Users to Resources in Waterline would involve 6 separate queries behind the scenes

  • a query to users to find the user
  • a query to the group_users__user_groups join table
  • a query to groups to find the actual groups
  • a query to the ability_groups__group_abilities join table
  • a query to abilities to find the actual abilities
  • a query to resources to find the resources

Not very efficient when we're using a relational database that could handle our "complex" join with a single query. And the actual deep/nested population needed to get you there is painful to write (trust me).

Sequelize on the other hand, will get you there pretty easily, and with one query:

sails.sequelize['user'].findOne({
    where: {
        id: 1
    },
    include: [{
        model: sails.sequelize['groups'],
        as: 'groups',
        include: [{
            model: sails.sequelize['abilities'],
            as: 'abilities',
            include: [{
                model: sails.sequelize['resource'],
                as: 'resource'
            }]
        }]
    }]
}).nodeify(function(err, user) {});

This hook aims to give you the best of both worlds, native Waterline models for your day-to-day CRUD stuff (blueprints, pubsub, etc), and more robust Sequelize models that will allow you to sleep soundly every night knowing that you have a model capable of handling your complex SQL needs. This module also does not require you to disable the native Sails hooks (orm, pubsub, etc) that some other hooks require.

Getting started

Check out the /api folder for a sample model setup. Create a new file called sequelize.js in /config containing your config options (more to come on this, but for now, see /test/bootstrap.test.js for sample config)

Configuration

// in config/sqlize.js

module.exports.sqlize = {
    /**
     * Specify Sequelize connection options for each sails connection you wish to create
     * Sequelize models for 
     */
    connections: {
        mysql: {
            dialect: 'mysql',
            pool: {
                max: 5,
                min: 0,
                idle: 1000
            },
            define: {
                timestamps: false
            },
            logging: 'silly' // the sails logging level to use
        }
    },
    
    /**
     * Specify default options for each Sequelize instance
     */
    options: {
        define: {
            timestamps: false,
            freezeTableName: true
        }
    }
}

Testing

  • install a compatible sequelize database (mysql, postgresql, ms sql server, etc)
  • edit the connection info in /test/bootstrap.test.js
  • npm test

To Do

Improve docs. Improve feature suite. ADD MORE TESTS.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2015 Chris Ludden Licensed under the MIT license.