|
| 1 | +import { ColumnType } from "../../driver/types/ColumnTypes" |
| 2 | +import { ColumnTypeUndefinedError } from "../../error" |
| 3 | +import { getMetadataArgsStorage } from "../../globals" |
| 4 | +import { ColumnMetadataArgs } from "../../metadata-args/ColumnMetadataArgs" |
| 5 | +import { VirtualColumnOptions } from "../options/VirtualColumnOptions" |
| 6 | +/** |
| 7 | + * VirtualColumn decorator is used to mark a specific class property as a Virtual column. |
| 8 | + */ |
| 9 | +export function VirtualColumn(options: VirtualColumnOptions): PropertyDecorator |
| 10 | + |
| 11 | +/** |
| 12 | + * VirtualColumn decorator is used to mark a specific class property as a Virtual column. |
| 13 | + */ |
| 14 | +export function VirtualColumn( |
| 15 | + typeOrOptions: ColumnType, |
| 16 | + options: VirtualColumnOptions, |
| 17 | +): PropertyDecorator |
| 18 | + |
| 19 | +/** |
| 20 | + * VirtualColumn decorator is used to mark a specific class property as a Virtual column. |
| 21 | + */ |
| 22 | +export function VirtualColumn( |
| 23 | + typeOrOptions?: ColumnType | VirtualColumnOptions, |
| 24 | + options?: VirtualColumnOptions, |
| 25 | +): PropertyDecorator { |
| 26 | + return function (object: Object, propertyName: string) { |
| 27 | + // normalize parameters |
| 28 | + let type: ColumnType | undefined |
| 29 | + if (typeof typeOrOptions === "string") { |
| 30 | + type = <ColumnType>typeOrOptions |
| 31 | + } else { |
| 32 | + options = <VirtualColumnOptions>typeOrOptions |
| 33 | + type = options.type |
| 34 | + } |
| 35 | + |
| 36 | + if (!options?.query) { |
| 37 | + throw new Error( |
| 38 | + "Column options must be defined for calculated columns.", |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + // if type is not given explicitly then try to guess it |
| 43 | + const reflectMetadataType = |
| 44 | + Reflect && (Reflect as any).getMetadata |
| 45 | + ? (Reflect as any).getMetadata( |
| 46 | + "design:type", |
| 47 | + object, |
| 48 | + propertyName, |
| 49 | + ) |
| 50 | + : undefined |
| 51 | + if (!type && reflectMetadataType) |
| 52 | + // if type is not given explicitly then try to guess it |
| 53 | + type = reflectMetadataType |
| 54 | + |
| 55 | + // check if there is no type in column options then set type from first function argument, or guessed one |
| 56 | + if (type) options.type = type |
| 57 | + |
| 58 | + // specify HSTORE type if column is HSTORE |
| 59 | + if (options.type === "hstore" && !options.hstoreType) |
| 60 | + options.hstoreType = |
| 61 | + reflectMetadataType === Object ? "object" : "string" |
| 62 | + |
| 63 | + // if we still don't have a type then we need to give error to user that type is required |
| 64 | + if (!options.type) |
| 65 | + throw new ColumnTypeUndefinedError(object, propertyName) |
| 66 | + |
| 67 | + getMetadataArgsStorage().columns.push({ |
| 68 | + target: object.constructor, |
| 69 | + propertyName: propertyName, |
| 70 | + mode: "virtual-property", |
| 71 | + options: options || {}, |
| 72 | + } as ColumnMetadataArgs) |
| 73 | + } |
| 74 | +} |
0 commit comments