2

我正在尝试以微秒精度保存时间戳,如下所示:

2016-10-18T13:36:38.431555

我已经单独测试了 PostgreSQL,它工作正常:

CREATE TABLE test(mytime timestamptz);
INSERT INTO test('2016-10-18T13:36:38.431555');
SELECT * FROM test;

选择返回正确的精度:

2016-10-18T13:36:38.431555

然后我使用相同的 sql 查询对 nodejs 和 pg 连接器进行了测试。

将时间戳作为字符串传递是可行的,但如果我想传递一个 Date 对象,那么我会失去微秒精度。这是一个 JavaScript 限制,因为 Date 对象的最大分辨率为毫秒。为了绕过它,我使用此函数将时间戳转换为 bigint:

function convertToUnixTS(timestamp) {
    return new Date(timestamp).getTime() * 1000 + parseInt(timestamp.substr(timestamp.length - 3));
}

bigint 对 postgres 是透明的,并且选择查询返回正确的时间戳

2016-10-18T13:36:38.431555

现在到 Waterline,我使用 postgres 适配器创建了一个集合,并将我的时间戳定义为日期时间

Persistence.prototype.collections.Value = Waterline.Collection.extend({
    identity: 'value',
    connection: 'default',
    attributes: {
        mytimestamptz: {
            type: 'datetime',
            required: true
        },
        myvalue: {
            type: 'float',
            required: true
        }
    }
});

这会将我的时间戳保存到秒,在水线 github 上提出了可能的补丁来支持毫秒(我已经测试过并且它正在工作)但我仍然缺少微秒精度的 3 个额外数字。正如我所展示的,在 JavaScript 中处理此问题的唯一方法是将其转换为 bigint。将 bigint 传递给我当前的模型会引发异常,所以我尝试像这样更改我的模型

mytimestamptz: {
    type: 'integer',
    size: 64,
    required: true
}

mytimestamptz: {
    type: 'bigint',
    required: true
}

但是现在水线框架抛出了一个无效的属性错误。

(node:12964) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): [Error (E_VALIDATION) 1 attribute is invalid] Invalid attributes sent to event:
 • di_timestamptz
   • [object Object]

而作为一个字符串,我看到:

1477488110007650

另外,我真的更喜欢将它作为时间戳。

可能吗?

谢谢

4

0 回答 0