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

Transform.from does nothing when column is NULL #3395

Closed
rrgoncalves opened this issue Jan 9, 2019 · 1 comment · Fixed by #3581
Closed

Transform.from does nothing when column is NULL #3395

rrgoncalves opened this issue Jan 9, 2019 · 1 comment · Fixed by #3581
Labels

Comments

@rrgoncalves
Copy link

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[x] mssql
[x] mysql / mariadb
[x] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.11 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Have a NULL value in a column like user.name and try to transform it with the following code:

// user.ts

@Entity("usuario", { schema: "myschema" })
export class user { 
      @Column({
        name: "name",
        type: 'text',
        transformer: {
          to(n) { 
            return n === undefined ? null : n 
          },
          from(n) { 
            return n === null ? undefined : n
          },
        }
    })
}

So, I was checking the code from PostgresDriver.js (/driver/postgres/PostgresDriver.js) and found out that there's no check if there's a transformer function set in the @Column decorator, and the code always checks first if the value if either null or undefined and returns it. As shown below:

PostgresDriver.prototype.prepareHydratedValue = function (value, columnMetadata) {
        if (columnMetadata.type === Boolean) {
            value = value ? true : false;

        // [...]
}

The fix should be something as simple as checking if the transformer exists and if it does, execute it:

PostgresDriver.prototype.prepareHydratedValue = function (value, columnMetadata) {
        if (columnMetadata.transformer) 
           return columnMetadata.transformer.from(value);

        if (columnMetadata.type === Boolean) {
            value = value ? true : false;

        // [...]
}

Some other people (##2777) and I believe the transformer should be executed nonetheless.

I'm willing to send a PR and add tests to fix such issue.

PS: MongoDriver got it right:

MongoDriver.prototype.prepareHydratedValue = function (value, columnMetadata) {
  if (columnMetadata.transformer)
    value = columnMetadata.transformer.from(value);
  return value;
};

Thanks!

@sroze
Copy link

sroze commented Feb 4, 2019

Definitely, agree. Got this issue as of now :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants