Sailsjs 与 Sails-SqlServer 和 Waterline ORM

Sailsjs with Sails-SqlServer and Waterline ORM

在正确理解 Waterline ORM 文档时遇到一些问题。我正在寻求关于在我的 Sails API.

中没有定义模型的情况下查询 MSSQL 数据库的正确方法的建议

到目前为止,我已经成功创建了必要的连接并下载了 sails-sqlserver npm 包。

我有一个控制器,代码如下:

    module.exports = {

       /**
       * @name: all
       * @description: return a list of items
       **/

        all: function(req, res){

           /** as per Waterline documentation, use a use an arbitary model to create a raw SQL query **/

           Items.query("select * from table", function(err, resultSet){

              if(err) return res.send(err);

              return res.ok(resultSet);

           });          


        }
    }

Items.js

module.exports = {

  attributes: {

  }
};

使用 Postman

调用控制器 GET /api/items 的路由

API 没有 return 响应,它只是挂起,最终超时。

我在 Items.query 上面有 console.log() 以确保正在执行控制器。

我发现 SQL 服务器拒绝访问服务器。

The prelogin packet used to open the connection is structurally invalid; the connection has been closed. Please contact the vendor of the client library.

我是 sails-sqlserver 适配器的维护者。

确保:

  1. 您可以通过 SQL 服务器 Workbench 使用相同的凭据连接到数据库。

  2. 您不在任何可能干扰您的请求的限制性防火墙或代理后面

  3. 您使用的是受支持的 SQL 服务器版本(2008 及更高版本)

我觉得你倒过来了,你得用你的DB的属性来定义你的模型,例如:

items.js

module.exports = {
  schema: true,
  connection: 'yourConnection',
  tableName: 'yourTableName',
  attributes: {
      id:{
        type:"number",
        primaryKey: true
      },
      name:{
        type:"string"
      },
      address:{
        type:"string"
      }
  }
};

现在,当您调用路由 GET localhost:1337/items 时,它将 return 您所有的项目,而无需在您的控制器中使用任何功能。

注意:假设您使用的是本地主机。