I made a simple script:
我做了一个简单的脚本:
this.db
.query(
`SELECT * FROM message WHERE id='5960ac15-34d6-472a-abe4-aec164a7bc90'`,
)
.then(async ({ rows }) => {
console.log(rows);
const expected = rows[0].creation_date;
const dateTime = DateTime.fromSQL(expected);
const result = dateTime.toSQL();
console.log(expected === result);
console.log(expected, typeof expected);
console.log(result);
const { rows: test } = await this.db.query(
`SELECT * FROM message WHERE creation_date=$1`,
[result],
);
console.log(test);
});
This.db correspond to a Pool object.
So I select a particular object in my db using his ID.
Next to it I try to get back the same object but this time using the creation_date field.
This.db对应于Pool对象。因此,我使用他的ID在我的数据库中选择了一个特定的对象。在它旁边,我试图取回相同的对象,但这次使用的是Creation_Date字段。
I've tried many result variable changes. Like toIso(), fromISO()... But none seems to work. I never get my object back in my second query.
我尝试了许多结果变量更改。像toIso(),来自ISO()...但似乎都没有奏效。在我的第二个查询中,我再也没有得到我的对象。
I know that node-pg parse dates to JS dates. So I overrided the parsers:
我知道Node-PG会将日期解析为JS日期。所以我改写了解析器:
types.setTypeParser(1114, function (value) {
return value;
});
types.setTypeParser(1082, (value) => value);
Which is working because now, if I pass expected to the second query instead of result, I know get back my object.
My creation_date field is timestamp in my db.
这是有效的,因为现在,如果我将Expect传递给第二个查询而不是Result,我知道会得到我的对象。我的Creation_Date字段在我的数据库中是时间戳。
This script is a simulation of my frontend/backend communication. My frontend need to transform the original field to luxon DateTime and I send back this date. I can use less than or greater than operators but not equal.
这个脚本模拟了我的前端/后端通信。我的前端需要将原始字段转换为Luxon Date Time,然后我将此日期发回。我可以使用小于或大于运算符,但不能使用等于。
Can someone help me?
有人能帮我吗?
The expected format is: '2023-09-04 01:34:44.522983'
预期格式为:‘2023-09-04 01:34:44.522983’
更多回答
优秀答案推荐
Ok, so I finaly managed to found why.
The precision of my timestamp in my db was in microseconds but luxon keep only milliseconds in my case.
好了,我终于找到了原因。我的数据库中时间戳的精度是以微秒为单位的,但Luxon在我的数据库中只保留毫秒。
I updated my db script from:
我从以下位置更新了我的数据库脚本:
creation_date timestamp NOT NULL DEFAULT NOW()
To
至
creation_date timestamp(3) NOT NULL DEFAULT NOW()
更多回答
我是一名优秀的程序员,十分优秀!