typeorm: UpdateDateColumn fails with transformer

Issue Description

Both the UpdateDateColumn and CreateDateColumn decorators are broken in some way for Postgres.

CreateDateColumn has no value being set on save, UpdateDateColumn does not work if the column has a transformer set on it.

Expected Behavior

On save, I’d expect TypeORM to set a createdAt date.

When I have a transformer, I’d expect TypeORM to run a new date through the transformer – or just work?

Actual Behavior

No dates are set automatically for non-nullable columns.

When I have a transformer, TypeORM makes no attempt to update the updatedAt date.

Steps to Reproduce

Both bugs are outlined here:

https://github.com/mothership/typeorm-bug

My Environment

Dependency Version
Operating System OSX
Node.js version v12.18.3
Typescript version v4.2.1
TypeORM version v0.2.29

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 16 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Hello, Passing to: () => undefined seems to do the job too:

/** A timestamptz column which results an ISO string date. */
export const DateTimeColumn = (options?: ColumnOptions) =>
  Column({
    transformer: dateTimeTransformer,
    type: options?.type ?? 'timestamptz',
    ...options,
  });

/** A timestamptz column of a creation date which results an ISO string date. */
export const CreateDateTimeColumn = (options?: ColumnOptions) =>
  CreateDateColumn({
    transformer: {
      from: dateTimeTransformer.from,
      to: () => undefined,
    },
    type: options?.type ?? 'timestamptz',
    ...options,
  });

/** A timestamptz column of an update date which results an ISO string date. */
export const UpdateDateTimeColumn = (options?: ColumnOptions) =>
  UpdateDateColumn({
    transformer: {
      from: dateTimeTransformer.from,
      to: () => undefined,
    },
    type: options?.type ?? 'timestamptz',
    ...options,
  });