sql-serverexpresssails.js

Sails.js and SQL Server database provider


As I have seen there is a very nice and solid SQL Server connector for Express.js

Can this be used for Sails.js as well?


Solution

  • Yes you can use Microsoft SQL Server with Sails.js! I've done it on a few projects. Really all you need to do is add it to your config/connections.js file:

    sqlserver: {
            adapter: 'sails-sqlserver',
            user: 'your_user',
            password: 'your_pw',
            host: 'host ip',
            database: 'your_db_name',
            // I've had to use this option in some cases
            // where the SQL Server refuses my user otherwise
            options: {
                encrypt: false
            }
    },
    

    Then use it the same way, by either defining it in your global config as default connection:

    connection: 'sqlserver'
    

    Or within a specific model:

    // Define an adapter to use
    adapter: 'sails-sqlserver'
    

    You also might want to consider disabling some of the fields in the models like:

    autoCreatedAt: false,
    autoUpdatedAt: false,
    

    And maybe enable migrate: 'safe' in the configs/models.js file.

    As a side note, when I've had to do stored procedures from Node.js with a MS SQL server, I usually just fall back to using the mssql library directly.