使用Postgres的StrongLoop查询/存储过程?

use*_*058 1 postgresql strongloop loopbackjs

根据文档,StrongLoop不支持运行自定义sql语句。 https://docs.strongloop.com/display/public/LB/Executing+native+SQL

任何人都认为您可以通过简单的连接就可以构建企业应用程序,这超出了我,但是我确实找到了这篇文章,说明您可以做到: 在MySQL Loopback Connector上执行原始查询

但这是针对MySql的。当我用Postgres尝试它时,出现错误:“类型'object'的参数'byId'的值无效:0。收到的类型已转换为数字。” 而且它不返回任何数据。这是我的代码:

module.exports = function(account) {

account.byId = function(byId, cb){
    var ds=account.dataSource;
    var sql = "SELECT * FROM account where id > ?";
    ds.connector.execute(sql, [Number(byId)], function(err, accounts)    {
        if (err) console.error(err);
        console.info(accounts);
        cb(err, accounts);
    });
};
account.remoteMethod(
    'byId',
    {
        http: {verb: 'get'},
        description: "Get accounts greater than id",
        accepts: {arg: 'byId', type: 'integer'},
        returns: {arg: 'data', type: ['account'], root: true}
    }
);
};
Run Code Online (Sandbox Code Playgroud)

对于[Number(byId)]部分,我也尝试过[byId]和仅byId。没用。

有任何想法吗?到目前为止,我真的很喜欢StrongLoop,但是看来Postgresql连接器尚未准备好投入生产。如果不行,我将在下一个Sails中制作原型。:-(

Vis*_*mar 5

arg是'integer'类型的,这不是有效的Loopback Type。使用`Number代替。检查以下更正的代码:

module.exports = function(account) {
    account.byId = function(byId, cb){
        var ds = account.dataSource;
        var sql = "SELECT * FROM account WHERE id > $1";
        ds.connector.execute(sql, byId, function(err, accounts) {
            if (err) console.error(err);
            console.info(accounts);
            cb(err, accounts);
        });
    };
    account.remoteMethod(
        'byId',
        {
            http: {verb: 'get'},
            description: "Get accounts greater than id",
            accepts: {arg: 'byId', type: 'Number'},
            returns: {arg: 'data', type: ['account'], root: true}    //here 'account' will be treated as 'Object'.
        }
    );
};
Run Code Online (Sandbox Code Playgroud)

注: MySQL的的准备语句本身使用?作为参数占位符,但是PostgreSQL的用途$1$2等等。

希望这对您有用。否则尝试使用,[byId]而不是byId按照docs进行尝试。