【问题标题】:sails.js -postgresql returning string value instead of integer for bigint fieldssails.js -postgresql 返回字符串值而不是 bigint 字段的整数
【发布时间】:2015-10-03 08:05:10
【问题描述】:

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

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

但是,对于我们现有的数据库,id 字段大多是bigint。所以我定义了migrate: "safe" 并继续创建模型。

现在我面临的问题是,当蓝图路由返回结果时,应该作为数字返回的 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

【问题讨论】:

    标签: postgresql sails.js waterline sails-postgresql


    【解决方案1】:

    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;
        }
    
      }
    };
    

    【讨论】:

    • 完美!在我的情况下,覆盖 toJSON 方法将是一个理想的解决方案,因为 API 将由原生 android 和 iOS 应用程序使用。谢谢!
    • 通过任何方式是否可以为所有模型覆盖此方法而无需在每个模型中明确指定?
    • 找到了。在model.js文件中指定attributes: { toJSON: function(){ var obj = this.toObject(); obj.id = parseInt(obj.id); return obj; } }
    • 我不确定...如果是这样,您可以在 config/models.js 中进行操作。我不知道你是否可以在那里定义属性。
    【解决方案2】:

    作为替代方案,您可以使用https://github.com/mirek/node-pg-safe-numbers,它通过将不安全处理委托给您(当数字不适合 2^53 javascript 限制时)来准确处理此问题 - 您可以在其中返回解析值、字符串、null、抛出出错或执行其他操作。

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

    【讨论】:

      猜你喜欢
      • 2018-11-16
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      相关资源
      最近更新 更多