https://github.com/cludden/sails-hook-sqlize
https://github.com/cludden/sails-hook-sqlize
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/cludden/sails-hook-sqlize
- Owner: cludden
- License: mit
- Created: 2015-08-01T19:41:08.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-04T16:39:00.000Z (about 10 years ago)
- Last Synced: 2025-08-11T14:59:29.443Z (4 months ago)
- Language: JavaScript
- Size: 258 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
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 `Resource`s 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:
```javascript
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
```javascript
// 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](https://github.com/cludden/sails-hook-sqlize/fork)
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](LICENSE.md).