Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamps persisted incorrectly to Postgres despite DB timezone is set to UTC #3409

Open
gabriellbui opened this issue Mar 11, 2025 · 0 comments
Labels
type: bug 🐛 Something isn't working

Comments

@gabriellbui
Copy link

gabriellbui commented Mar 11, 2025

Describe the bug
This is basically a duplicate of typeorm/typeorm#9627. It's mainly here to track the timestamp issue within Vendure as well.

In TypeORM columns with timestamp or timestamp without time zone type is read as local date, not UTC.

This happens with automatically created values as well (e.g., @CreateDateColumn()). In Vendure, the base VendureEntity applies the default @CreateDateColumn() to createdAt and @UpdateDateColumn() to updatedAt, causing them to persist with incorrect timestamps.

To Reproduce
Follow the "To Reproduce" section in this issue: TypeORM Issue #9627

Expected behavior
Entities extending the base VendureEntity should have the correct createdAt and updatedAt timestamps

Environment (please complete the following information):

Dependency Version
Operating System macOS sequoia 15.3.1
Node.js version 22.14.0
Typescript version 5.2.2
TypeORM version 0.3.20
PostgreSQL version 15.4

Related issues
typeorm/typeorm#9627
typeorm/typeorm#2220
typeorm/typeorm#8296
typeorm/typeorm#5841
typeorm/typeorm#7184
#1375

Suggested solution
Use the timestamptz type which is an abbreviation for type timestamp with time zone. It should internally store them in UTC, but converted to the session's time zone when queried.

  @CreateDateColumn({ type: "timestamptz" })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamptz' })
  updatedAt: Date;

An alternative is to configure the driver to interpret timestamps as UTC inside your project

pg.defaults.parseInputDatesAsUTC = true

const dateParser = pg.types.getTypeParser(pg.types.builtins.TIMESTAMP)
pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, (val: string) =>
  dateParser(`${val}Z`),
)
@gabriellbui gabriellbui added the type: bug 🐛 Something isn't working label Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant