Sequelize postgres过程返回字符串而不是对象

时间:2017-11-15 16:07:40

标签: node.js postgresql stored-procedures sequelize.js

我有存储过程,最后插入:

CREATE OR REPLACE FUNCTION procedure_name(id_ VARCHAR(128), fname_ VARCHAR(128), lname_ VARCHAR(128)) RETURNS RECORD AS $$
DECLARE
      result RECORD;
BEGIN
    --some logic here
    INSERT INTO user (id, fname, lname)
       VALUES (id_, fname_, lname_)
    RETURNING * into result;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

当我运行普通INSERT(来自代码的纯SQL)时:

var query = "INSERT INTO user (id, fname, lname) VALUES (2, Jack, Doe) RETURNING *;"
sequelizeConnection.query(query, {raw : true, plain: true, type:sequelizeConnection.QueryTypes.INSERT})

结果已映射到​​对象:

{
    id: 2,
    fname: Jack,
    lname: Doe
}

但是当我调用这样的程序时:

    function insertUser(data) {
        var options = {replacements: data, raw:true, plain:true,  model:sequelize.models['user'], type: sequelize.QueryTypes.INSERT};
        return sequelize.query("SELECT procedure_name(:id, :fname, :lname)", options);
    }

我得到以下结果:

{ procedure_name: '(1,John,Doe)'}

如何从程序中获取对象?

1 个答案:

答案 0 :(得分:1)

在Abdel P.的帮助下,完成了答案:

sequelize.query("Select * from procedure_name(:id, :fname, :lname) as (id VARCHAR(128), fname VARCHAR(128), lname VARCHAR(128))", options)