typeorm-实体管理器日期
我遇到了与Nestjs Typeorm的问题。所使用的数据库是PostgreSQL。我正在运行的Typeorm版本为0.2.45。
当我使用Entity Manager查询数据库时,请查询一些复杂的查询日期输出的日期输出,它为我提供了一个ISO格式的JavaScript Date对象,而不是“ Yyyy-MM-DD”,如果我使用查找数据库查询数据库,它将在其中。
运行
const q = await this.invoicesRepository.find();
return q;
将返回,
[
Invoice {
...
date: '2022-04-01',
dueDate: '2022-06-01',
...
}
]
其中运行
const em = getManager();
const q: Invoice[] = await em.query('SELECT * FROM invoice');
return q;
将导致一个看起来像
[
{
...
date: 2022-03-31T13:00:00.000Z,
dueDate: 2022-05-31T14:00:00.000Z,
...
}
]
数据库中的输出,该值仅存储为日期仅粘附于yyyy-mm-dd格式,但看起来TypeOmm正在尝试解释日期值。有没有办法使typeorm不触摸日期格式?
I'm running into an issue with TypeORM with NestJS. The database being used is PostgreSQL. The TypeORM version I'm running is 0.2.45.
When I'm querying the database with Entity Manager with some complex queries the date output it's giving me a javascript date object in ISO format instead of "YYYY-MM-DD" in which it would if I was to query the database using find.
Running
const q = await this.invoicesRepository.find();
return q;
will return
[
Invoice {
...
date: '2022-04-01',
dueDate: '2022-06-01',
...
}
]
Where as running
const em = getManager();
const q: Invoice[] = await em.query('SELECT * FROM invoice');
return q;
will result in an output that looks like
[
{
...
date: 2022-03-31T13:00:00.000Z,
dueDate: 2022-05-31T14:00:00.000Z,
...
}
]
In the database the value is stored as date only adhering to YYYY-MM-DD format but it looks like TypeORM is trying to interpret the date value. Is there a way to make TypeORM not touch the formatting of the date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在运行原始查询,因此将转换为
date
实例而不是Postgres驱动程序的不是Typeorm。我不确定是否可以配置驱动程序不执行转换,但是相对简单的解决方法可能是将与时间相关的字段强加于字符串:
You're running a raw query, so it's not TypeORM that's doing the conversion to a
Date
instance, but rather the Postgres driver.I'm not sure if it's possible to configure the driver not to perform the conversion, but a relatively simple workaround could be to force your time-related fields to a string: