sails.js-postgresql为bigint字段返回字符串值而不是整数

sails.js-postgresql为bigint字段返回字符串值而不是整数,postgresql,sails.js,waterline,sails-postgresql,Postgresql,Sails.js,Waterline,Sails Postgresql,我们正在使用Sails.js作为后端框架,将一个项目从PHP迁移到Node.js。我们无法修改我们的数据库,并且必须为此项目使用现有数据库 如果我为新创建的模型保留migrate:“alter”,默认情况下,Sails会将id字段保留为整数 module.exports = { tableName: "timeslots", autoCreatedAt: false, autoUpdatedAt: false, attributes: { starttime: { typ

我们正在使用Sails.js作为后端框架,将一个项目从PHP迁移到Node.js。我们无法修改我们的数据库,并且必须为此项目使用现有数据库

如果我为新创建的模型保留
migrate:“alter”
,默认情况下,Sails会将
id
字段保留为整数

module.exports = {
  tableName: "timeslots",
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    starttime: { type: "string", required: true },
    endtime: { type: "string", required: true },

    toJSON: function(){
      var obj = this.toObject();
      obj.id = parseInt(obj.id);
      return obj;
    }

  }
};
然而,对于我们现有的数据库,
id
字段大多是
bigint
。因此,我定义了
migrate:“safe”
,并继续创建模型

现在我面临的问题是,当blueprint routes返回结果时,id列值(应以数字形式返回)将以字符串形式返回。以下是一个例子:

[
  {
    "starttime": "07:00:00",
    "endtime": "14:00:00",
    "id": "1"
  },
  {
    "starttime": "14:00:00",
    "endtime": "22:00:00",
    "id": "2"
  },
  {
    "starttime": "22:00:00",
    "endtime": "07:00:00",
    "id": "3"
  }
]
如何解决此问题

这是我的模型:

module.exports = {
  tableName: "timeslots",
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    starttime: { type: "string", required: true },
    endtime: { type: "string", required: true }
  }
};
下面是postgresql表定义

                                              Table "public.timeslots"
  Column   |  Type  |                       Modifiers                        | Storage  | Stats target | Description 
-----------+--------+--------------------------------------------------------+----------+--------------+-------------
 id        | bigint | not null default nextval('timeslots_id_seq'::regclass) | plain    |              | 
 starttime | text   | not null                                               | extended |              | 
 endtime   | text   | not null                                               | extended |              | 
Indexes:
    "idx_43504_primary" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "doctortimeslot" CONSTRAINT "doctortimeslot_ibfk_2" FOREIGN KEY (timeslot_id) REFERENCES timeslots(id) ON UPDATE CASCADE ON DELETE CASCADE

Waterline使用它没有内置的数据类型变得很奇怪。我认为当它不确定该做什么时,它默认为字符串。这并不重要,因为JS会在前端自动将这些值强制转换为数字

然而,如果您需要它是一个数字,最简单的解决方案可能是重写模型中的toJSON方法,并让它强制该值为整数

module.exports = {
  tableName: "timeslots",
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    starttime: { type: "string", required: true },
    endtime: { type: "string", required: true },

    toJSON: function(){
      var obj = this.toObject();
      obj.id = parseInt(obj.id);
      return obj;
    }

  }
};

作为一种替代方法,您可以使用它通过将不安全的处理委托给您(当数字不符合2^53 javascript限制时)来精确处理此问题,您可以返回解析值、字符串、null、抛出错误或执行其他操作


在许多情况下,您可以使用库提供的自动解析,在不安全的处理程序中只返回原始字符串值。然后,在使用2^53以上的数字(即随机大数字)的代码中,始终将其转换为字符串,您将很好。

完美!在我的例子中,重写toJSON方法将是一个理想的解决方案,因为该API将由本地android和iOS应用程序使用。谢谢通过任何方式都可以覆盖所有模型的此方法,而不必在每个模型中显式指定?找到了。在model.js文件
属性中指定:{toJSON:function(){var obj=this.toObject();obj.id=parseInt(obj.id);return obj;}}}
我不确定。。。如果是这样,您可以在
config/models.js
中执行。我不知道是否可以在其中定义属性。