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

feat: add transformer to ViewColumnOptions #8717

Merged

Conversation

tomwelsh11
Copy link
Contributor

Description of change

Resolves #4339

This pull request adds a transformer field to ViewColumnOptions, to allow a view column to be unmarshalled by a custom ValueTransformer. This is the same functionality that currently exists for ColumnOptions and regular table entities. Note that you can already get the same result as this PR using @ts-ignore:

export const uppercase: ValueTransformer = {
    to: (entityValue: string) => {},
    from: (databaseValue: string) => databaseValue.toLocaleUpperCase()
};

@ViewEntity({ expression: `
    SELECT "post"."id" "id", "post"."name" AS "name", "category"."name" AS "categoryName"
    FROM "post" "post"
    LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"
`})
export class PostCategory {
    @ViewColumn()
    id: number;

    @ViewColumn({ name: "name" })
    postName: string;

    //@ts-ignore
    @ViewColumn({ transformer: uppercase })
    categoryName: string;
}

However, it would be nice not to have to use @ts-ignore when needing this functionality.
I updated some of the existing tests to verify that this works correctly. Also added some documentation on the view column options.
This seemed like a bit of low hanging fruit that I could do - let me know if there's something I missed!

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

Added `transformer` field into ViewColumnOptions
Allow view column to unmarshal by custom ValueTransformer
docs/view-entities.md Outdated Show resolved Hide resolved
@pleerock pleerock merged commit 96ac8f7 into typeorm:master Mar 2, 2022
@pleerock
Copy link
Member

pleerock commented Mar 2, 2022

Thank you for contribution! 🎉

@tomwelsh11 tomwelsh11 deleted the feature/addTransformerOptionToViewColumn branch March 2, 2022 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ViewColumn does not take a transformer input
2 participants