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

ViewColumn does not take a transformer input #4339

Closed
rhlsthrm opened this issue Jun 21, 2019 · 4 comments · Fixed by #8717
Closed

ViewColumn does not take a transformer input #4339

rhlsthrm opened this issue Jun 21, 2019 · 4 comments · Fixed by #8717

Comments

@rhlsthrm
Copy link

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

import { BigNumber } from "bignumber.js";

@Entity()
export class Number {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column("text", {
    transformer: {
      from: (value: string): BigNumber => new BigNumber(value),
      to: (value: BigNumber): string => value.toString(),
    },
  })
  big!: BigNumber;
}

@ViewEntity(`SELECT "big" from "number"`)
export class NumberView {
  @ViewColumn()
  // does not return as BigNumber type
  big: BigNumber;
}

I'd like to be able to have my view entity transform the value before returning it to my JS. Is there a way to do this?

@NestorPerez13
Copy link

Same issue here i have a simple-array stored in the DB i want to get this from a view in the array form, instead i get a string :/

@MikeMatrix
Copy link

Came across this issue today. Seems like there isn't much difference in hydrating data between @ViewColumn and @Column. There is just no types assigned in the ViewColumnOptions.
Probably going to make a PR for this very soon.

Quick and dirty workaround:
Simply add the transformer property as you would in @Column and // @ts-ignore the type error for now.
Obviously not a recommended approach, but if you really need it (as I did), this would fix it for now.

@pcnova
Copy link

pcnova commented Nov 28, 2023

Came across this issue today. Seems like there isn't much difference in hydrating data between @ViewColumn and @Column. There is just no types assigned in the ViewColumnOptions. Probably going to make a PR for this very soon.

Quick and dirty workaround: Simply add the transformer property as you would in @Column and // @ts-ignore the type error for now. Obviously not a recommended approach, but if you really need it (as I did), this would fix it for now.

To avoid the unsightly // @ts-ignore (which, if you're using ESLint, will force you to add another // eslint-disable-next-line comment on top), I found out you can also do this:

@ViewColumn({
    transformer: { from: val => new BigNumber(val) },
  } as ColumnOptions)
big: BigNumber;

@IanGYan
Copy link

IanGYan commented Mar 12, 2024

transformer should be an instance of a class which implements ValueTransformer or instances array.

import { ViewColumnOptions } from "../options/ViewColumnOptions";
/**
 * ViewColumn decorator is used to mark a specific class property as a view column.
 */
export declare function ViewColumn(options?: ViewColumnOptions): PropertyDecorator;
import { ValueTransformer } from "./ValueTransformer";
/**
 * Describes all view column's options.
 */
export interface ViewColumnOptions {
    /**
     * Column name in the database.
     */
    name?: string;
    /**
     * Specifies a value transformer(s) that is to be used to unmarshal
     * this column when reading from the database.
     */
    transformer?: ValueTransformer | ValueTransformer[];
}
/**
 * Interface for objects that deal with (un)marshalling data.
 */
export interface ValueTransformer {
    /**
     * Used to marshal data when writing to the database.
     */
    to(value: any): any;
    /**
     * Used to unmarshal data when reading from the database.
     */
    from(value: any): any;
}

Write a transformer such as export class NumToBig implements ValueTransformer { ... }, then pass the instance to transformer.

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

Successfully merging a pull request may close this issue.

6 participants