Postgres的StrongLoop查询/存储过程?

时间:2015-11-23 07:50:27

标签: postgresql loopbackjs strongloop

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

如果有人认为你可以通过简单的连接构建企业应用程序超出我的范围,但我确实发现这篇帖子说你可以做到: Execute raw query on 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}
    }
);
};

对于[Number(byId)]部分,我也尝试了[byId]和byId。什么都行不通。

有什么想法吗?到目前为止,我真的很喜欢StrongLoop,但看起来Postgresql连接器还没有准备好生产。如果这不起作用,我将使用Sails做原型。 : - (

1 个答案:

答案 0 :(得分:5)

这是arg类型为'整数'的东西,它不是有效的Loopback Type。使用`数字代替。检查下面的更正代码:

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'.
        }
    );
};

注意: MySQL 的预处理语句本身使用?作为参数占位符,但 PostgreSQL 使用{{1} },$1等。

希望这适合你。否则,请根据docs尝试使用$2代替[byId]

相关问题