-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new virtual column decorator (#9339)
* feat: implement new calculated decorator This new feature change bahviour of typeorm to allow use new calculated decorator Closes #9323 * feat: implement new virtual decorator This new feature change bahviour of typeorm to allow use new virtual decorator Closes #9323 * feat: Implement new virtual decorator This new feature change bahviour of typeorm to allow use new virtual decorator Closes #9323 * feat: implement new virtual decorator This new feature change bahviour of typeorm to allow use new calculated decorator Closes #9323 * feat: implement new virtual decorator This new feature change behavior of typeorm to allow use of the new virtual column decorator Closes #9323
- Loading branch information
1 parent
8a837f9
commit d305e5f
Showing
24 changed files
with
658 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ColumnType } from "../../driver/types/ColumnTypes" | ||
import { ColumnTypeUndefinedError } from "../../error" | ||
import { getMetadataArgsStorage } from "../../globals" | ||
import { ColumnMetadataArgs } from "../../metadata-args/ColumnMetadataArgs" | ||
import { VirtualColumnOptions } from "../options/VirtualColumnOptions" | ||
/** | ||
* VirtualColumn decorator is used to mark a specific class property as a Virtual column. | ||
*/ | ||
export function VirtualColumn(options: VirtualColumnOptions): PropertyDecorator | ||
|
||
/** | ||
* VirtualColumn decorator is used to mark a specific class property as a Virtual column. | ||
*/ | ||
export function VirtualColumn( | ||
typeOrOptions: ColumnType, | ||
options: VirtualColumnOptions, | ||
): PropertyDecorator | ||
|
||
/** | ||
* VirtualColumn decorator is used to mark a specific class property as a Virtual column. | ||
*/ | ||
export function VirtualColumn( | ||
typeOrOptions?: ColumnType | VirtualColumnOptions, | ||
options?: VirtualColumnOptions, | ||
): PropertyDecorator { | ||
return function (object: Object, propertyName: string) { | ||
// normalize parameters | ||
let type: ColumnType | undefined | ||
if (typeof typeOrOptions === "string") { | ||
type = <ColumnType>typeOrOptions | ||
} else { | ||
options = <VirtualColumnOptions>typeOrOptions | ||
type = options.type | ||
} | ||
|
||
if (!options?.query) { | ||
throw new Error( | ||
"Column options must be defined for calculated columns.", | ||
) | ||
} | ||
|
||
// if type is not given explicitly then try to guess it | ||
const reflectMetadataType = | ||
Reflect && (Reflect as any).getMetadata | ||
? (Reflect as any).getMetadata( | ||
"design:type", | ||
object, | ||
propertyName, | ||
) | ||
: undefined | ||
if (!type && reflectMetadataType) | ||
// if type is not given explicitly then try to guess it | ||
type = reflectMetadataType | ||
|
||
// check if there is no type in column options then set type from first function argument, or guessed one | ||
if (type) options.type = type | ||
|
||
// specify HSTORE type if column is HSTORE | ||
if (options.type === "hstore" && !options.hstoreType) | ||
options.hstoreType = | ||
reflectMetadataType === Object ? "object" : "string" | ||
|
||
// if we still don't have a type then we need to give error to user that type is required | ||
if (!options.type) | ||
throw new ColumnTypeUndefinedError(object, propertyName) | ||
|
||
getMetadataArgsStorage().columns.push({ | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
mode: "virtual-property", | ||
options: options || {}, | ||
} as ColumnMetadataArgs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ColumnType } from "../../driver/types/ColumnTypes" | ||
import { ValueTransformer } from "./ValueTransformer" | ||
|
||
/** | ||
* Describes all calculated column's options. | ||
*/ | ||
export interface VirtualColumnOptions { | ||
/** | ||
* Column type. Must be one of the value from the ColumnTypes class. | ||
*/ | ||
type?: ColumnType | ||
|
||
/** | ||
* Return type of HSTORE column. | ||
* Returns value as string or as object. | ||
*/ | ||
hstoreType?: "object" | "string" | ||
|
||
/** | ||
* Query to be used to populate the column data. This query is used when generating the relational db script. | ||
* The query function is called with the current entities alias either defined by the Entity Decorator or automatically | ||
* @See https://typeorm.io/decorator-reference#virtualcolumn for more details. | ||
*/ | ||
query: (alias: string) => string | ||
|
||
/** | ||
* Specifies a value transformer(s) that is to be used to unmarshal | ||
* this column when reading from the database. | ||
*/ | ||
transformer?: ValueTransformer | ValueTransformer[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.