带有postgreSQL的KnexJS以字符串形式返回所有数据,不尊重来自数据库的数据类型

时间:2019-01-13 13:41:51

标签: node.js postgresql orm knex.js

我在与postgreSQL一起使用的KnexJs中有一个问题,但是每次选择时,knex都会将表中的所有数据作为字符串来回答,但事实并非如此,因为有些字段是数字和knex把我像字符串一样传递给我,有人会知道如何帮助我,我感到绝望。

选择knex

function sum(n, total = 0) {
  console.log(n, total);
  if (n !== 1) {
    console.log("insie IF");
    total += n;
    sum(n - 1, total);
  } else {
    console.log("inside ELSE");
    total += 1;
  }
  return total;
}

响应knexjs代码

function getDeadlines(){
    let query = knex(prazo.getTableName())
                    .select(prazo.properties.idDeadline.getDbProperty(), prazo.properties.deadline.getDbProperty())
                    .orderBy(prazo.properties.deadline.getDbProperty(), 'desc')
    return query
}

cd_prazo_pk_36不是字符串,而是数字

连接

{
    deadlines: [
      {
        cd_prazo_pk_36: "1", //(Is a numeric in database table)
        ds_prazo_36: "A Vista"
      },
      {
        cd_prazo_pk_36: "2",  //(Is a numeric in database table)
        ds_prazo_36: "7 Dias"
      },
      {
        cd_prazo_pk_36: "4", //(Is a numeric in database table)
        ds_prazo_36: "21 Dias"
      },
      {
        cd_prazo_pk_36: "3", //(Is a numeric in database table)
        ds_prazo_36: "14 Dias"
      }
   ]
}

我已经谢谢你

1 个答案:

答案 0 :(得分:0)

  如果

node-postgres没有针对数据库类型的注册类型解析器,它将把数据库类型转换为JavaScript字符串。此外,您可以将任何类型作为字符串发送到PostgreSQL服务器,并且node-postgres将通过它而不进行任何修改。

https://node-postgres.com/features/types

Knex使用node-postgres数据库驱动程序来访问postgresql。

您可以使用https://github.com/brianc/node-pg-types覆盖每种数据类型的默认解析器。