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-sequelize-multi

v1.0.4

Published

Sails.js hook to use sequelize ORM, multiple connection version

Downloads

4

Readme

sails-hook-sequelize-multi

This is a fork of the sails-hook-sequelize project, expanded to support multiple databases and connections. It's a quick and dirty implementation.

Install

Install this hook with:

$ npm install sails-hook-sequelize-multi --save

Configuration

.sailsrc

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

## Connections files

In the `app/config/connections.js` file, list all the connections that you're going to use.

```javascript
module.exports.connections = {
  staging: {
    user: 'stagingUser'
    password: 'abcd'
    database: 'staging_db'
    options: {
      host: 'staging.example.com'
      dialect: 'mssql'
      pool: {
        max: 5,
        min: 0,
        idle: 1000
      },
      isolationLevel: 'READ UNCOMMITTED'
    }
  },
  production: {
    user: 'prodUser'
    password: 'abcd'
    database: 'production_db'
    options: {
      host: 'prod.example.com'
      dialect: 'mssql'
      pool: {
        max: 5,
        min: 0,
        idle: 1000
      },
      isolationLevel: 'READ UNCOMMITTED'
    }
  }
};
```

## Models config

In the `app/config/models.js` file, declare the model that you'd like to use as the default.
For example to use the `staging` database as the default for the connections file above:

```javascript
module.exports.models = {
  connection: 'staging'
};
```

Or, to use the `production` database as the default:

```javascript
module.exports.models = {
  connection: 'production'
};
```

## Models

No changes to the model code are necessary -- declare the models as you normally do.

# How to use

Initially, all your existing code will work the same as before.
By default, your Sequelize code will use the default database that you've declared in
the `app/config/models.js` file (see above).

For instance, the example below should look very similar to your existing code:

```javascript
UserTable.find(option)
.then(function(res) {
  console(res);
}).catch(function(err) {
  console(err);
});
```

However, if you'd like to target a specific database, you can declare that right after the table name:

```javascript
UserTable.staging.find(option)
.then(function(res) {
  console(res);
}).catch(function(err) {
  console(err);
});
```

or

```javascript
var dbname = 'staging';    // db name is set elsewhere in the code

UserTable[dbname].find(option)
.then(function(res) {
  console(res);
}).catch(function(err) {
  console(err);
});
```

This also works if you're using the `sequelize` object to execute raw queries

```javascript
var dbname = 'production';    // db name is set elsewhere in the code
var sqlString = 'SELECT getdate()';

sequelize[dbname].query(sqlString)
.then(function(res) {
  console(res);
}).catch(function(err) {
  console(err);
});
```

# Caveats

At the moment, associations do not work for the "extra" databases. In the example above,
the "production" tables would have associations, but the "staging" tables are not because
they are not the default. This will be addressed in a future version.

#License
[MIT](./LICENSE)