# sails-hook-sqlize

> create sequelize model counterparts for sails models

Latest version **0.3.3** (published 2015-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install sails-hook-sqlize
pnpm add sails-hook-sqlize
yarn add sails-hook-sqlize
bun add sails-hook-sqlize
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2015-11-04 |
| First published | 2015-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+17 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | chris ludden |
| Maintainers | cludden |
| Keywords | sails, hook, sequelize, sails-hook-sqlize |

## Links

- npm: https://www.npmjs.com/package/sails-hook-sqlize
- Repository: https://github.com/cludden/sails-hook-sqlize
- Homepage: https://github.com/cludden/sails-hook-sqlize#readme
- Issues: https://github.com/cludden/sails-hook-sqlize/issues
- npm.io page: https://npm.io/package/sails-hook-sqlize

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^1.4.2
- [sails](https://npm.io/package/sails.md) ^0.11.0
- [lodash](https://npm.io/package/lodash.md) ^3.10.1
- [sequelize](https://npm.io/package/sequelize.md) ^3.5.1

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 0.3.3 (latest) — 2015-11-04
- 0.3.2 — 2015-11-03
- 0.2.0 — 2015-10-14
- 0.1.0 — 2015-08-26
- 0.0.5 — 2015-08-26
- 0.0.4 — 2015-08-26
- 0.0.3 — 2015-08-21
- 0.0.2 — 2015-08-21
- 0.0.1 — 2015-08-21

## 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).

---
_Source: https://npm.io/package/sails-hook-sqlize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
