Sails.js and SQL Server database provider

wljmcqd8  于 2023-10-15  发布在  SQL Server
关注(0)|答案(3)|浏览(98)

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?

chy5wohz

chy5wohz1#

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.

yebdmbv4

yebdmbv42#

I have similar problem with Sql server express as I added following to connections.js

sqlserver: {
  adapter: 'sails-sqlserver',
  host: localhost'+ '\u005C' + 'sqlexpress',
  user: 'sa',
  password: 'gogators',
  database: 'users'

},

the host definition is wierd but came from a post on Git

my sql express has tcp/ip enabled

but I get this sails startup error: ** the hook 'orm' takes to long to load**

now this database i can reach from asp.net MVC with following connection string

<add name="UsersContext" connectionString="Data Source=.\sqlexpress; Initial Catalog=Users; Integrated Security=False; user=sa;password=gogators "
  providerName="System.Data.SqlClient" />
8dtrkrch

8dtrkrch3#

If you use latest version of Sails.js (currently it is 1.5.8) you will need to:

  1. Install sails-mssql package ( https://www.npmjs.com/package/sails-mssql ):
npm install sails-mssql --save
  1. Configure your config/datastores.js in proper way:
module.exports.datastores = {
  sqlserver: {
    adapter: 'sails-mssql',
    url: 'mssql://user:[email protected]:port/database',
    options: {
      encrypt: true   // use this for Azure databases 
    },
  },
};

Mentioned below configuration may not work due to waterline package used by sails-hook-orm as it require url property to be defined.

module.exports.datastores = {
  sqlserver: {
    adapter: 'sails-mssql',
    user: 'user',
    password: 'password',
    host: 'name-dbserver.database.windows.net', // azure database 
    database: 'database',
    options: {
      encrypt: true   // use this for Azure databases 
    },
  }
}

and modify your config/models.js

module.exports.models = {
  datastore: "sqlserver",
  migrate: 'safe',
};

相关问题