Sukh
Sukh

Reputation: 361

Node JS Sails Two Different Databases

I'm building an application where I have one local database (mysql), but I need to connect to another database as well so I can pull some information from the second database (oracle), does anyone have any information or examples using Sails to manage multiple databases?

Thanks.

Upvotes: 1

Views: 5272

Answers (2)

Gabe H
Gabe H

Reputation: 1080

Sails uses Waterline as its ORM, and Waterline has a concept of adapters that allow the use of multiple datastores. For each model you create, you can define an adapter attribute. So for example if you wanted to create a User model that uses mysql as its datastore, it would look something like this.

In api/models/User.js

module.exports = {

    adapter: 'mysql',

    attributes: {
        name: 'string',
        email: 'string',
        phoneNumber: {
            type: 'string',
            defaultsTo: '555-555-5555'
        }
    }
};

You could also then have another model, such as an Office, be associated with a different datastore such as mongoDB.

In api/models/Office.js

module.exports = {

    adapter: 'mongo',

    attributes: {
        address: 'string',
        city: 'string',
        state: 'string'
    }
};

The only thing you would need to make sure is that you've installed both sails-mysql and sails-mongo adapters in your project and have them configured. You can install the adapters with npm.

npm install sails-mysql sails-mongo --save

You can find out more how to configure the databases at the sails docs for adapter configuration.

For your particular use case, there is no Oracle adapter as of now. Here is some docs and the boilerplate to start making a sails adapter if you were interested in starting that project. The list of current available adapters can be found here.

Upvotes: 12

xiraf
xiraf

Reputation: 21

If you're asking about Sails.js and its Waterline ORM, it doesn't appear that Oracle has a compatible adapter available as of yet. https://github.com/balderdashy/waterline

Upvotes: 2

Related Questions