ArsenyYankovsky / typeorm-aurora-data-api-driver

A bridge between TypeORM and Aurora Data API
MIT License
175 stars 38 forks source link

generatedMaps for timestamps are being returned as string not Date #141

Open calvin-cdev opened 2 years ago

calvin-cdev commented 2 years ago

Package versions and database engine type (please complete the following information):

Describe the bug When I do an insert for an entity with a generated date (timestamp) field like createdAt or updatedAt, the value in the resultant generatedMaps is a string, not a Date. I would expect a Date object, just like I would get in the result of a find operation, but I end up getting a string. Worse, the string is missing the UTC Z at the end, so there is even more work to do to get it back to UTC time as a Date object.

Not only is the value on the generatedMaps incorrectly formatted, but also the mutated entity object passed into the insert call has the incorrectly formatted date:

const entity = { someField:  true }
await getRepository(Entity).insert(entity)

/** insert call mutated the object passed in.
*   now entity.createdAt exists and is not a Date, but a string.
*   more complete example in repro section. **/

I'd be happy to fix this, but I need guidance I'm not sure where the fix should go. It could go into this driver package. It could go into typeorm. It could also go into the data-api-client. I think it should go into the data-api-client, and here is why: There was a PR to address this issue but the relevant part of the fix was dropped by the owner while addressing merge conflicts before merging the PR. From the requester's findings and my own, it looks like aws sends the typeName from executeStatement commands back in lowercase for postgres :grimacing: . The code in the data-api-client is written only to compare against uppercase typeName values. I monkey-patched the package and found that this issue was indeed fixed. However, I'm not sure if this is actually the correct layer for the fix. What do you think?

To Reproduce

be sure to use formatOptions.deserializeDate=true

entity class

@Entity()
export default class Event {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @CreateDateColumn()
  createdAt: Date;

  @Column({ type: 'boolean' })
  someField: boolean
}

use it

const entity = { someField:  true }
const result = await getRepository(Event).insert(entity)

view that the result has generatedMaps with createdAt as a timezone-less string. It should be a UTC Date.

calvin-cdev commented 2 years ago

opened a pr in the data-api-client repo to fix https://github.com/jeremydaly/data-api-client/pull/117 worth noting that at the request of the author, this pr targets their aws sdk v3 migration branch, not main. so the fix won't be available until they release that version.

ArsenyYankovsky commented 2 years ago

Thanks for your contribution! Reading about your findings it looks like the best would be to fix it in the data-api-client. Let's keep this open until the PR is merged so we can upgrade to the fixed data-api-client version when it's released.