我正在尝试以微秒精度保存时间戳,如下所示:
2016-10-18T13:36:38.431555
我已经单独测试了PostgreSQL,它运行良好:
CREATE TABLE test(mytime timestamptz);
INSERT INTO test('2016-10-18T13:36:38.431555');
SELECT * FROM test;
select返回正确的精度:
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是透明的,select查询返回正确的时间戳
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
另外,我更愿意把它作为时间戳。
有可能吗?
由于