typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.46k stars 6.21k forks source link

ES2022 Repository create method include undefined field that breaks DB update #9118

Closed mbagsik00 closed 1 week ago

mbagsik00 commented 1 year ago

Issue Description

repository.create() includes the undefined field which breaks the DB update query when using ES2022 as a target in tsconfig

Expected Behavior

Previously we're using es6 as a target for tsconfig and the field that we didn't pass in the repository.create() will not be included in the response.

// Entity
class TestEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('varchar')
  name: string;
}

// Repository Create
const result = Repository.create({
   name: 'test name'
})

// Result when using ES6
{
 name: 'test name'
}

Actual Behavior

When using ES2022 as a target in tsconfig it returns all the fields that are declared in the entity and sets the value to undefined for the fields that don't exist in the object when creating the repository. This causes some issues when doing an update since it sets other fields to null if we don't include the field in the repository creation. I know we can easily fix this by adding all the fields when creating the repository but didn't expect this to break.

// Entity
class TestEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('varchar')
  name: string;
}

// Repository Create
const result = Repository.create({
   name: 'test name'
})

// Result when using ES2022
{
 id: undefined,
 name: 'test name'
}

Steps to Reproduce

  1. Create a repository using only some of the fields from the entity.
  2. Result returns the other fields from the entity that haven't been declared when creating the repository with an undefined value.

My Environment

Dependency Version
Operating System MacOS
Node.js version 16.15.0
Typescript version 4.6.2
TypeORM version 0.2.25

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

Ginden commented 1 year ago

This is related to useDefineForClassFields setting of TypeScript.

Default: true if target is ES2022 or higher, including ESNext, false otherwise.

I'm marking it as "accepting PRs".

rvitaliy commented 1 year ago

same problem on my project with nodejs: 18.10.0

andrewnester commented 1 year ago

I've published a PR to address this issue, can someone take a look? Thank you!

MiqueiasGFernandes commented 5 months ago

Hi @Ginden , I think the @andrewnester fix was merged successfully. Could you please mark this Issue as closed? Thank's!

erfanium commented 2 weeks ago

I still have this issue using typeorm@0.3.20. is the fix has been published?

Ginden commented 1 week ago

@MiqueiasGFernandes @erfanium As you can see, this PR was rejected.

I'm closing it as "won't fix", unfortunately.