3

I have been going round in circles trying to get this to work how I think it should in sails v 0.11.0

I have my development.js as follows

module.exports = {

connections: {
    conn1: {
        adapter: 'sails-sqlserver',
        user: 'user',
        password: 'pass',
        server: 'server1',
        database: 'db1',

        options: {
            encrypt: true
        }
    },


    conn2: {
        adapter: 'sails-sqlserver',
        user: 'user',
        password: 'pass',
        server: 'server1',
        database: 'db2',

        options: {
            encrypt: true
        }
    }
},

    models:
    {
        firstconn: 'conn1',
        secondconn: 'conn2'
    }

};

My connection.js is empty

In my controller I am trying to echo the connection and user it to get a list of users:

UsersController:

var sql = require('mssql');

    module.exports = {
        hi: function (req, res) {

            console.log(sails.config.firstconn);

            var connection = new sql.Connection(sails.config.firstconn, function (err) {
                var request = new sql.Request(connection);
                request.query('SELECT * FROM [dbo].[Perms_Users]', function (err, recordsets) {
                    res.send(recordsets)
                });
            });
        }
    };

However my connection is always null?

I am running in development, can someone point out what I have missed?

2 Answers 2

1

You should be able to add connection: 'dbname' to your model to access the database. The models block in development should be the default db connection and and configuration to use.

For model settings http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html

In depth models: http://sailsjs.org/#!/documentation/concepts/ORM/Models.html

Sign up to request clarification or add additional context in comments.

Comments

1

Actually it was very easy!

sails.config.connections.conn1 gives access to the modal

2 Comments

You should validate your answer then.
I have another 20 hours before I can :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.