ViewColumn does not take a transformer input
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?
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 :/
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.
Came across this issue today. Seems like there isn't much difference in hydrating data between
@ViewColumnand@Column. There is just no types assigned in theViewColumnOptions. Probably going to make a PR for this very soon.Quick and dirty workaround: Simply add the
transformerproperty as you would in@Columnand// @ts-ignorethe 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;
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.