typeorm-实体管理器日期

发布于 2025-02-10 06:48:20 字数 775 浏览 2 评论 0原文

我遇到了与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-02-17 06:48:20

您正在运行原始查询,因此将转换为date实例而不是Postgres驱动程序的不是Typeorm。

我不确定是否可以配置驱动程序不执行转换,但是相对简单的解决方法可能是将与时间相关的字段强加于字符串:

SELECT date::text AS dateString, dueDate::text AS dueDateString, * FROM invoice

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:

SELECT date::text AS dateString, dueDate::text AS dueDateString, * FROM invoice
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文