From 0d0c0a7f6461f237c6e14a8f78ddbc37e80176f1 Mon Sep 17 00:00:00 2001 From: belopash Date: Thu, 28 Mar 2024 17:04:43 +0500 Subject: [PATCH 1/5] re-export typeorm decorators --- .../typeorm-store/src/decorators/Entity.ts | 37 +++ .../typeorm-store/src/decorators/Index_.ts | 85 ++++++ .../src/decorators/columns/Column.ts | 44 ++++ .../src/decorators/columns/PrimaryColumn.ts | 16 ++ .../src/decorators/columns/common.ts | 245 ++++++++++++++++++ .../src/decorators/columns/index.ts | 3 + typeorm/typeorm-store/src/decorators/index.ts | 4 + .../src/decorators/relations/JoinColumn.ts | 46 ++++ .../src/decorators/relations/ManyToOne.ts | 36 +++ .../src/decorators/relations/OneToMany.ts | 15 ++ .../src/decorators/relations/OneToOne.ts | 33 +++ .../src/decorators/relations/common.ts | 19 ++ .../src/decorators/relations/index.ts | 5 + typeorm/typeorm-store/src/index.ts | 5 +- 14 files changed, 591 insertions(+), 2 deletions(-) create mode 100644 typeorm/typeorm-store/src/decorators/Entity.ts create mode 100644 typeorm/typeorm-store/src/decorators/Index_.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/Column.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/PrimaryColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/common.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/index.ts create mode 100644 typeorm/typeorm-store/src/decorators/index.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/JoinColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/ManyToOne.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/OneToMany.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/OneToOne.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/common.ts create mode 100644 typeorm/typeorm-store/src/decorators/relations/index.ts diff --git a/typeorm/typeorm-store/src/decorators/Entity.ts b/typeorm/typeorm-store/src/decorators/Entity.ts new file mode 100644 index 000000000..2aa6d7b85 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/Entity.ts @@ -0,0 +1,37 @@ +import {OrderByCondition, Entity as _Entity} from 'typeorm' + +export interface EntityOptions { + /** + * Table name. + * If not specified then naming strategy will generate table name from entity name. + */ + name?: string + /** + * Specifies a default order by used for queries from this table when no explicit order by is specified. + */ + orderBy?: OrderByCondition | ((object: any) => OrderByCondition | any) + /** + * Schema name. + */ + schema?: string +} + +/** + * This decorator is used to mark classes that will be an entity. + * Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it. + */ +export function Entity(options?: EntityOptions): ClassDecorator + +/** + * This decorator is used to mark classes that will be an entity (table or document depend on database type). + * Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it. + */ +export function Entity(name?: string, options?: EntityOptions): ClassDecorator + +/** + * This decorator is used to mark classes that will be an entity (table or document depend on database type). + * Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it. + */ +export function Entity(nameOrOptions?: string | EntityOptions, maybeOptions?: EntityOptions): ClassDecorator { + return _Entity(nameOrOptions as any, maybeOptions) +} diff --git a/typeorm/typeorm-store/src/decorators/Index_.ts b/typeorm/typeorm-store/src/decorators/Index_.ts new file mode 100644 index 000000000..930f89ded --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/Index_.ts @@ -0,0 +1,85 @@ +import {Index as _Index} from 'typeorm' + +export interface IndexOptions { + /** + * Indicates if this composite index must be unique or not. + */ + unique?: boolean + /** + * Index filter condition. + */ + where?: string +} + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index(options?: IndexOptions): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index(name: string, options?: IndexOptions): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index(name: string, options: {synchronize: false}): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index(name: string, fields: string[], options?: IndexOptions): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index(fields: string[], options?: IndexOptions): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index( + fields: (object?: any) => any[] | {[key: string]: number}, + options?: IndexOptions +): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index( + name: string, + fields: (object?: any) => any[] | {[key: string]: number}, + options?: IndexOptions +): ClassDecorator & PropertyDecorator + +/** + * Creates a database index. + * Can be used on entity property or on entity. + * Can create indices with composite columns when used on entity. + */ +export function Index( + nameOrFieldsOrOptions?: string | string[] | ((object: any) => any[] | {[key: string]: number}) | IndexOptions, + maybeFieldsOrOptions?: + | ((object?: any) => any[] | {[key: string]: number}) + | IndexOptions + | string[] + | {synchronize: false}, + maybeOptions?: IndexOptions +): ClassDecorator & PropertyDecorator { + return _Index(nameOrFieldsOrOptions as any, maybeFieldsOrOptions as any, maybeOptions) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/Column.ts b/typeorm/typeorm-store/src/decorators/columns/Column.ts new file mode 100644 index 000000000..2530c40b0 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/Column.ts @@ -0,0 +1,44 @@ +import {Column as _Column} from 'typeorm' +import {SimpleColumnType, WithLengthColumnType, WithPrecisionColumnType} from 'typeorm/driver/types/ColumnTypes' +import {ColumnCommonOptions, ColumnNumericOptions, ColumnOptions, ColumnType, ColumnWithLengthOptions} from './common' + +/** + * Column decorator is used to mark a specific class property as a table column. Only properties decorated with this + * decorator will be persisted to the database when entity be saved. + */ +export function Column(): PropertyDecorator + +/** + * Column decorator is used to mark a specific class property as a table column. + * Only properties decorated with this decorator will be persisted to the database when entity be saved. + */ +export function Column(type: SimpleColumnType, options?: ColumnCommonOptions): PropertyDecorator + +/** + * Column decorator is used to mark a specific class property as a table column. + * Only properties decorated with this decorator will be persisted to the database when entity be saved. + */ +export function Column( + type: WithLengthColumnType, + options?: ColumnCommonOptions & ColumnWithLengthOptions +): PropertyDecorator + +/** + * Column decorator is used to mark a specific class property as a table column. + * Only properties decorated with this decorator will be persisted to the database when entity be saved. + */ +export function Column( + type: WithPrecisionColumnType, + options?: ColumnCommonOptions & ColumnNumericOptions +): PropertyDecorator + +/** + * Column decorator is used to mark a specific class property as a table column. + * Only properties decorated with this decorator will be persisted to the database when entity be saved. + */ +export function Column( + typeOrOptions?: ((type?: any) => Function) | ColumnType | ColumnOptions, + options?: ColumnOptions +): PropertyDecorator { + return _Column(typeOrOptions as any, options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/PrimaryColumn.ts b/typeorm/typeorm-store/src/decorators/columns/PrimaryColumn.ts new file mode 100644 index 000000000..467775711 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/PrimaryColumn.ts @@ -0,0 +1,16 @@ +import assert from 'assert' +import {PrimaryColumn as _PrimaryColumn} from 'typeorm' + +/** + * Column decorator is used to mark a specific class property as a table column. + * Only properties decorated with this decorator will be persisted to the database when entity be saved. + * Primary columns also creates a PRIMARY KEY for this column in a db. + * + * Only `id` property can be used as a primary column + */ +export function PrimaryColumn(): PropertyDecorator { + return function (object: Object, propertyName: string | symbol) { + assert(propertyName === 'id', 'only "id" field can be used as a primary column') + _PrimaryColumn()(object, propertyName) + } +} diff --git a/typeorm/typeorm-store/src/decorators/columns/common.ts b/typeorm/typeorm-store/src/decorators/columns/common.ts new file mode 100644 index 000000000..6999da703 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/common.ts @@ -0,0 +1,245 @@ +import {ValueTransformer} from 'typeorm' + +/** + * Types are restricted only to types listed in + * https://orkhan.gitbook.io/typeorm/docs/entities#column-types-for-postgres + */ + +/** + * Column types used for @PrimaryGeneratedColumn() decorator. + */ +export type PrimaryGeneratedColumnType = + | 'int' + | 'int2' + | 'int4' + | 'int8' + | 'integer' + | 'smallint' + | 'bigint' + | 'decimal' + | 'numeric' +/** + * Column types where spatial properties are used. + */ +export type SpatialColumnType = 'geometry' | 'geography' +/** + * Column types where precision and scale properties are used. + */ +export type WithPrecisionColumnType = + | 'float' + | 'decimal' + | 'numeric' + | 'real' + | 'double precision' + | 'time' + | 'time with time zone' + | 'time without time zone' + | 'timestamp' + | 'timestamp without time zone' + | 'timestamp with time zone' +/** + * Column types where column length is used. + */ +export type WithLengthColumnType = 'character varying' | 'character' | 'varchar' | 'char' +export type WithWidthColumnType = 'smallint' | 'int' | 'bigint' +/** + * All other regular column types. + */ +export type SimpleColumnType = + | 'int2' + | 'integer' + | 'int4' + | 'int8' + | 'float' + | 'float4' + | 'float8' + | 'money' + | 'boolean' + | 'bool' + | 'text' + | 'citext' + | 'hstore' + | 'bytea' + | 'timetz' + | 'timestamptz' + | 'date' + | 'interval' + | 'point' + | 'line' + | 'lseg' + | 'box' + | 'circle' + | 'path' + | 'polygon' + | 'geography' + | 'geometry' + | 'int4range' + | 'int8range' + | 'numrange' + | 'tsrange' + | 'tstzrange' + | 'daterange' + | 'enum' + | 'cidr' + | 'inet' + | 'macaddr' + | 'bit' + | 'bit varying' + | 'varbit' + | 'tsvector' + | 'tsquery' + | 'uuid' + | 'xml' + | 'json' + | 'jsonb' + | 'cube' + | 'ltree' +/** + * Any column type column can be. + */ +export type ColumnType = + | WithPrecisionColumnType + | WithLengthColumnType + | WithWidthColumnType + | SpatialColumnType + | SimpleColumnType + | BooleanConstructor + | DateConstructor + | NumberConstructor + | StringConstructor + +export interface ColumnCommonOptions { + /** + * Column name in the database. + */ + name?: string + /** + * Specifies if column's value must be unique or not. + */ + unique?: boolean + /** + * Indicates if column's value can be set to NULL. + */ + nullable?: boolean + /** + * Default database value. + */ + default?: any + /** + * Column comment. Not supported by all database types. + */ + comment?: string + /** + * Indicates if this column is an array. + * Can be simply set to true or array length can be specified. + * Supported only by postgres. + */ + array?: boolean + /** + * Specifies a value transformer that is to be used to (un)marshal + * this column when reading or writing to the database. + */ + transformer?: ValueTransformer | ValueTransformer[] +} + +/** + * Options for numeric column types where user can specify scale and precision. + */ +export interface ColumnNumericOptions { + /** + * The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum + * number of digits that are stored for the values. + */ + precision?: number + /** + * The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number + * of digits to the right of the decimal point and must not be greater than precision. + */ + scale?: number +} + +/** + * Options for columns that can define a length of the column type. + */ +export interface ColumnWithLengthOptions { + /** + * Column type's length. + * For example type = "varchar" and length = "100" means ORM will create a column with type varchar(100). + */ + length?: string | number +} + +export interface ColumnOptions extends ColumnCommonOptions { + /** + * Column type. Must be one of the value from the ColumnTypes class. + */ + type?: ColumnType + /** + * Column name in the database. + */ + name?: string + /** + * Column type's length. Used only on some column types. + * For example type = "string" and length = "100" means that ORM will create a column with type varchar(100). + */ + length?: string | number + /** + * Indicates if column's value can be set to NULL. + * Default value is "false". + */ + nullable?: boolean + /** + * Default database value. + */ + default?: any + /** + * Indicates if this column is a primary key. + * Same can be achieved when @PrimaryColumn decorator is used. + */ + primary?: boolean + /** + * Specifies if column's value must be unique or not. + */ + unique?: boolean + /** + * Column comment. Not supported by all database types. + */ + comment?: string + /** + * The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum + * number of digits that are stored for the values. + */ + precision?: number | null + /** + * The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number + * of digits to the right of the decimal point and must not be greater than precision. + */ + scale?: number + /** + * Array of possible enumerated values. + */ + enum?: (string | number)[] | Object + /** + * Exact name of enum + */ + enumName?: string + /** + * If this column is primary key then this specifies the name for it. + */ + primaryKeyConstraintName?: string + /** + * If this column is foreign key then this specifies the name for it. + */ + foreignKeyConstraintName?: string + /** + * Indicates if this column is an array. + * Can be simply set to true or array length can be specified. + * Supported only by postgres. + */ + array?: boolean + /** + * Specifies a value transformer that is to be used to (un)marshal + * this column when reading or writing to the database. + */ + transformer?: ValueTransformer | ValueTransformer[] +} diff --git a/typeorm/typeorm-store/src/decorators/columns/index.ts b/typeorm/typeorm-store/src/decorators/columns/index.ts new file mode 100644 index 000000000..d7f6c9ca7 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/index.ts @@ -0,0 +1,3 @@ +export * from './common' +export * from './Column' +export * from './PrimaryColumn' diff --git a/typeorm/typeorm-store/src/decorators/index.ts b/typeorm/typeorm-store/src/decorators/index.ts new file mode 100644 index 000000000..f8ddd6b39 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/index.ts @@ -0,0 +1,4 @@ +export * from './Entity' +export * from './Index_' +export * from './columns' +export * from './relations' diff --git a/typeorm/typeorm-store/src/decorators/relations/JoinColumn.ts b/typeorm/typeorm-store/src/decorators/relations/JoinColumn.ts new file mode 100644 index 000000000..e9d036acb --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/JoinColumn.ts @@ -0,0 +1,46 @@ +import {JoinColumn as _JoinColumn} from 'typeorm' + +export interface JoinColumnOptions { + /** + * Name of the column. + */ + name?: string + /** + * Name of the column in the entity to which this column is referenced. + */ + referencedColumnName?: string + /** + * Name of the foreign key constraint. + */ + foreignKeyConstraintName?: string +} + +/** + * JoinColumn decorator used on one-to-one relations to specify owner side of relationship. + * It also can be used on both one-to-one and many-to-one relations to specify custom column name + * or custom referenced column. + */ +export function JoinColumn(): PropertyDecorator + +/** + * JoinColumn decorator used on one-to-one relations to specify owner side of relationship. + * It also can be used on both one-to-one and many-to-one relations to specify custom column name + * or custom referenced column. + */ +export function JoinColumn(options: JoinColumnOptions): PropertyDecorator + +/** + * JoinColumn decorator used on one-to-one relations to specify owner side of relationship. + * It also can be used on both one-to-one and many-to-one relations to specify custom column name + * or custom referenced column. + */ +export function JoinColumn(options: JoinColumnOptions[]): PropertyDecorator + +/** + * JoinColumn decorator used on one-to-one relations to specify owner side of relationship. + * It also can be used on both one-to-one and many-to-one relations to specify custom column name + * or custom referenced column. + */ +export function JoinColumn(optionsOrOptionsArray?: JoinColumnOptions | JoinColumnOptions[]): PropertyDecorator { + return _JoinColumn(optionsOrOptionsArray as any) +} diff --git a/typeorm/typeorm-store/src/decorators/relations/ManyToOne.ts b/typeorm/typeorm-store/src/decorators/relations/ManyToOne.ts new file mode 100644 index 000000000..41c5c7ba6 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/ManyToOne.ts @@ -0,0 +1,36 @@ +import {ObjectType, ManyToOne as ManyToOne_} from 'typeorm' +import {RelationOptions} from './common' + +/** + * A many-to-one relation allows creating the type of relation where Entity1 can have a single instance of Entity2, but + * Entity2 can have multiple instances of Entity1. Entity1 is the owner of the relationship, and stores the id of + * Entity2 on its side of the relation. + */ +export function ManyToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + options?: RelationOptions +): PropertyDecorator + +/** + * A many-to-one relation allows creating the type of relation where Entity1 can have a single instance of Entity2, but + * Entity2 can have multiple instances of Entity1. Entity1 is the owner of the relationship, and stores the id of + * Entity2 on its side of the relation. + */ +export function ManyToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + inverseSide?: string | ((object: T) => any), + options?: RelationOptions +): PropertyDecorator + +/** + * A many-to-one relation allows creating the type of relation where Entity1 can have a single instance of Entity2, but + * Entity2 can have multiple instances of Entity1. Entity1 is the owner of the relationship, and stores the id of + * Entity2 on its side of the relation. + */ +export function ManyToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + inverseSideOrOptions?: string | ((object: T) => any) | RelationOptions, + options?: RelationOptions +): PropertyDecorator { + return ManyToOne_(typeFunctionOrTarget, inverseSideOrOptions as any, options) +} diff --git a/typeorm/typeorm-store/src/decorators/relations/OneToMany.ts b/typeorm/typeorm-store/src/decorators/relations/OneToMany.ts new file mode 100644 index 000000000..f2478e701 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/OneToMany.ts @@ -0,0 +1,15 @@ +import {ObjectType, OneToMany as _OneToMany} from 'typeorm' +import {RelationOptions} from './common' + +/** + * A one-to-many relation allows creating the type of relation where Entity1 can have multiple instances of Entity2, + * but Entity2 has only one Entity1. Entity2 is the owner of the relationship, and stores the id of Entity1 on its + * side of the relation. + */ +export function OneToMany( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + inverseSide: string | ((object: T) => any), + options?: RelationOptions +): PropertyDecorator { + return _OneToMany(typeFunctionOrTarget, inverseSide, options) +} diff --git a/typeorm/typeorm-store/src/decorators/relations/OneToOne.ts b/typeorm/typeorm-store/src/decorators/relations/OneToOne.ts new file mode 100644 index 000000000..704648e15 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/OneToOne.ts @@ -0,0 +1,33 @@ +import {ObjectType, OneToOne as _OneToOne} from 'typeorm' +import {RelationOptions} from './common' + +/** + * One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2. + * Entity1 is an owner of the relationship, and storages Entity1 id on its own side. + */ +export function OneToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + options?: RelationOptions +): PropertyDecorator + +/** + * One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2. + * Entity1 is an owner of the relationship, and storages Entity1 id on its own side. + */ +export function OneToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + inverseSide?: string | ((object: T) => any), + options?: RelationOptions +): PropertyDecorator + +/** + * One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2. + * Entity1 is an owner of the relationship, and storages Entity1 id on its own side. + */ +export function OneToOne( + typeFunctionOrTarget: string | ((type?: any) => ObjectType), + inverseSideOrOptions?: string | ((object: T) => any) | RelationOptions, + options?: RelationOptions +): PropertyDecorator { + return _OneToOne(typeFunctionOrTarget, inverseSideOrOptions as any, options) +} diff --git a/typeorm/typeorm-store/src/decorators/relations/common.ts b/typeorm/typeorm-store/src/decorators/relations/common.ts new file mode 100644 index 000000000..48043714e --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/common.ts @@ -0,0 +1,19 @@ +export interface RelationOptions { + /** + * Indicates if relation column value can be nullable or not. + */ + nullable?: boolean + /** + * Indicates whether foreign key constraints will be created for join columns. + * Can be used only for many-to-one and owner one-to-one relations. + * Defaults to true. + */ + createForeignKeyConstraints?: boolean + /** + * Set this relation to be eager. + * Eager relations are always loaded automatically when relation's owner entity is loaded using find* methods. + * Only using QueryBuilder prevents loading eager relations. + * Eager flag cannot be set from both sides of relation - you can eager load only one side of the relationship. + */ + eager?: boolean +} diff --git a/typeorm/typeorm-store/src/decorators/relations/index.ts b/typeorm/typeorm-store/src/decorators/relations/index.ts new file mode 100644 index 000000000..8fd0ed645 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/relations/index.ts @@ -0,0 +1,5 @@ +export * from './common' +export * from './OneToOne' +export * from './OneToMany' +export * from './ManyToOne' +export * from './JoinColumn' diff --git a/typeorm/typeorm-store/src/index.ts b/typeorm/typeorm-store/src/index.ts index 162e43dfb..56529cb1b 100644 --- a/typeorm/typeorm-store/src/index.ts +++ b/typeorm/typeorm-store/src/index.ts @@ -1,2 +1,3 @@ -export * from "./database" -export {EntityClass, Entity, FindManyOptions, FindOneOptions, Store} from "./store" +export * from './database' +export {EntityClass, FindManyOptions, FindOneOptions, Store} from './store' +export * from './decorators' From 8269dcba9de0c91386ff7974a335c38c3681b91e Mon Sep 17 00:00:00 2001 From: belopash Date: Thu, 28 Mar 2024 23:58:50 +0500 Subject: [PATCH 2/5] use typeorm-store in codegen --- .../src/model/generated/transfer.model.ts | 2 +- typeorm/typeorm-codegen/src/codegen.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/erc20-transfers/src/model/generated/transfer.model.ts b/test/erc20-transfers/src/model/generated/transfer.model.ts index 9e12708b3..75a0c053f 100644 --- a/test/erc20-transfers/src/model/generated/transfer.model.ts +++ b/test/erc20-transfers/src/model/generated/transfer.model.ts @@ -1,4 +1,4 @@ -import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "typeorm" +import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "@subsquid/typeorm-store" import * as marshal from "./marshal" @Entity_() diff --git a/typeorm/typeorm-codegen/src/codegen.ts b/typeorm/typeorm-codegen/src/codegen.ts index 6b53b44d0..6604270d9 100644 --- a/typeorm/typeorm-codegen/src/codegen.ts +++ b/typeorm/typeorm-codegen/src/codegen.ts @@ -35,13 +35,13 @@ export function generateOrmModels(model: Model, dir: OutDir): void { index.line(`export * from "./${toCamelCase(name)}.model"`) const out = dir.file(`${toCamelCase(name)}.model.ts`) const imports = new ImportRegistry(name) - imports.useTypeorm('Entity', 'Column', 'PrimaryColumn') + imports.useTypeormStore('Entity', 'Column', 'PrimaryColumn') out.lazy(() => imports.render(model, out)) out.line() printComment(entity, out) entity.indexes?.forEach(index => { if (index.fields.length < 2) return - imports.useTypeorm('Index') + imports.useTypeormStore('Index') out.line(`@Index_([${index.fields.map(f => `"${f.name}"`).join(', ')}], {unique: ${!!index.unique}})`) }) out.line('@Entity_()') @@ -100,14 +100,14 @@ export function generateOrmModels(model: Model, dir: OutDir): void { break case 'fk': if (getFieldIndex(entity, key)?.unique) { - imports.useTypeorm('OneToOne', 'Index', 'JoinColumn') + imports.useTypeormStore('OneToOne', 'Index', 'JoinColumn') out.line(`@Index_({unique: true})`) out.line( `@OneToOne_(() => ${prop.type.entity}, {nullable: true})` ) out.line(`@JoinColumn_()`) } else { - imports.useTypeorm('ManyToOne', 'Index') + imports.useTypeormStore('ManyToOne', 'Index') if (!entity.indexes?.some(index => index.fields[0]?.name == key && index.fields.length > 1)) { out.line(`@Index_()`) } @@ -120,7 +120,7 @@ export function generateOrmModels(model: Model, dir: OutDir): void { case 'lookup': break case 'list-lookup': - imports.useTypeorm('OneToMany') + imports.useTypeormStore('OneToMany') out.line( `@OneToMany_(() => ${prop.type.entity}, e => e.${prop.type.field})` ) @@ -495,7 +495,7 @@ function collectVariants(model: Model): Set { function addIndexAnnotation(entity: Entity, field: string, imports: ImportRegistry, out: Output): void { let index = getFieldIndex(entity, field) if (index == null) return - imports.useTypeorm('Index') + imports.useTypeormStore('Index') if (index.unique) { out.line(`@Index_({unique: true})`) } else { @@ -531,7 +531,7 @@ class ImportRegistry { constructor(private owner: string) {} - useTypeorm(...names: string[]): void { + useTypeormStore(...names: string[]): void { names.forEach((name) => this.typeorm.add(name)) } @@ -565,7 +565,7 @@ class ImportRegistry { const importList = Array.from(this.typeorm).map( (name) => name + ' as ' + name + '_' ) - out.line(`import {${importList.join(', ')}} from "typeorm"`) + out.line(`import {${importList.join(', ')}} from "@subsquid/typeorm-store"`) } if (this.marshal) { out.line(`import * as marshal from "./marshal"`) From 9e26766da64f7a2d57ce5840444e8ed1d6297ffa Mon Sep 17 00:00:00 2001 From: belopash Date: Mon, 1 Apr 2024 23:42:07 +0500 Subject: [PATCH 3/5] add type-specific decorators --- .../src/model/generated/marshal.ts | 30 ---------- .../src/model/generated/transfer.model.ts | 15 +++-- typeorm/typeorm-codegen/src/codegen.ts | 56 ++++++------------- typeorm/typeorm-codegen/src/marshal.ts | 30 ---------- .../decorators/columns/BigDecimalColumn.ts | 9 +++ .../src/decorators/columns/BigIntColumn.ts | 9 +++ .../src/decorators/columns/BooleanColumn.ts | 11 ++++ .../src/decorators/columns/BytesColumn.ts | 11 ++++ .../src/decorators/columns/DateTimeColumn.ts | 11 ++++ .../src/decorators/columns/FloatColumn.ts | 9 +++ .../src/decorators/columns/IntColumn.ts | 11 ++++ .../src/decorators/columns/StringColumn.ts | 11 ++++ .../src/decorators/columns/index.ts | 8 +++ typeorm/typeorm-store/src/index.ts | 1 + typeorm/typeorm-store/src/transformers.ts | 40 +++++++++++++ 15 files changed, 156 insertions(+), 106 deletions(-) create mode 100644 typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/IntColumn.ts create mode 100644 typeorm/typeorm-store/src/decorators/columns/StringColumn.ts create mode 100644 typeorm/typeorm-store/src/transformers.ts diff --git a/test/erc20-transfers/src/model/generated/marshal.ts b/test/erc20-transfers/src/model/generated/marshal.ts index eaf8d36a8..2cf1c3a8e 100644 --- a/test/erc20-transfers/src/model/generated/marshal.ts +++ b/test/erc20-transfers/src/model/generated/marshal.ts @@ -127,36 +127,6 @@ export function nonNull(val: T | undefined | null): T { } -export const bigintTransformer = { - to(x?: bigint) { - return x?.toString() - }, - from(s?: string): bigint | undefined { - return s == null ? undefined : BigInt(s) - } -} - - -export const floatTransformer = { - to(x?: number) { - return x?.toString() - }, - from(s?: string): number | undefined { - return s == null ? undefined : Number(s) - } -} - - -export const bigdecimalTransformer = { - to(x?: any) { - return x?.toString() - }, - from(s?: any): any | undefined { - return s == null ? undefined : decimal.BigDecimal(s) - } -} - - export function enumFromJson(json: unknown, enumObject: E): E[keyof E] { assert(typeof json == 'string', 'invalid enum value') let val = (enumObject as any)[json] diff --git a/test/erc20-transfers/src/model/generated/transfer.model.ts b/test/erc20-transfers/src/model/generated/transfer.model.ts index 75a0c053f..9d5bb261f 100644 --- a/test/erc20-transfers/src/model/generated/transfer.model.ts +++ b/test/erc20-transfers/src/model/generated/transfer.model.ts @@ -1,5 +1,4 @@ -import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "@subsquid/typeorm-store" -import * as marshal from "./marshal" +import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, IntColumn as IntColumn_, DateTimeColumn as DateTimeColumn_, StringColumn as StringColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store" @Entity_() export class Transfer { @@ -10,21 +9,21 @@ export class Transfer { @PrimaryColumn_() id!: string - @Column_("int4", {nullable: false}) + @IntColumn_({nullable: false}) blockNumber!: number - @Column_("timestamp with time zone", {nullable: false}) + @DateTimeColumn_({nullable: false}) timestamp!: Date - @Column_("text", {nullable: false}) + @StringColumn_({nullable: false}) tx!: string - @Column_("text", {nullable: false}) + @StringColumn_({nullable: false}) from!: string - @Column_("text", {nullable: false}) + @StringColumn_({nullable: false}) to!: string - @Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) + @BigIntColumn_({nullable: false}) amount!: bigint } diff --git a/typeorm/typeorm-codegen/src/codegen.ts b/typeorm/typeorm-codegen/src/codegen.ts index 6604270d9..3a8887418 100644 --- a/typeorm/typeorm-codegen/src/codegen.ts +++ b/typeorm/typeorm-codegen/src/codegen.ts @@ -59,34 +59,11 @@ export function generateOrmModels(model: Model, dir: OutDir): void { if (key === 'id') { out.line('@PrimaryColumn_()') } else { + const decorator = getDecorator(prop.type.name) + imports.useTypeormStore(decorator) + addIndexAnnotation(entity, key, imports, out) - switch (prop.type.name) { - case 'BigInt': - imports.useMarshal() - out.line( - `@Column_("${getDbType(prop.type.name)}", {transformer: marshal.bigintTransformer, nullable: ${prop.nullable}})` - ) - break - case 'BigDecimal': - imports.useMarshal() - out.line( - `@Column_("${getDbType(prop.type.name)}", {transformer: marshal.bigdecimalTransformer, nullable: ${prop.nullable}})` - ) - break - case 'Float': - imports.useMarshal() - out.line( - `@Column_("${getDbType(prop.type.name)}", {transformer: marshal.floatTransformer, nullable: ${prop.nullable}})` - ) - break - default: - out.line( - `@Column_("${getDbType(prop.type.name)}", {nullable: ${ - prop.nullable - }})` - ) - break - } + out.line(`@${decorator}_({nullable: ${prop.nullable}})`) } break case 'enum': @@ -144,8 +121,12 @@ export function generateOrmModels(model: Model, dir: OutDir): void { if (scalar == 'BigInt' || scalar == 'BigDecimal') { throw new Error(`Property ${name}.${key} has unsupported type: can't generate code for native ${scalar} arrays.`) } + + const decorator = getDecorator(scalar) + imports.useTypeormStore(decorator) + out.line( - `@Column_("${getDbType(scalar)}", {array: true, nullable: ${prop.nullable}})` + `@${decorator}_({array: true, nullable: ${prop.nullable}})` ) break } @@ -186,26 +167,25 @@ export function generateOrmModels(model: Model, dir: OutDir): void { out.write() } - function getDbType(scalar: string): string { + function getDecorator(scalar: string): string { switch(scalar) { case 'ID': case 'String': - return 'text' + return 'StringColumn' case 'Int': - return 'int4' + return 'IntColumn' case 'Float': - return 'numeric' + return 'FloatColumn' case 'Boolean': - return 'bool' + return 'BooleanColumn' case 'DateTime': - return 'timestamp with time zone' + return 'DateTimeColumn' case 'BigInt': + return 'BigIntColumn' case 'BigDecimal': - return 'numeric' + return 'BigDecimalColumn' case 'Bytes': - return 'bytea' - case 'JSON': - return 'jsonb' + return 'BytesColumn' default: throw unexpectedCase(scalar) } diff --git a/typeorm/typeorm-codegen/src/marshal.ts b/typeorm/typeorm-codegen/src/marshal.ts index eaf8d36a8..2cf1c3a8e 100644 --- a/typeorm/typeorm-codegen/src/marshal.ts +++ b/typeorm/typeorm-codegen/src/marshal.ts @@ -127,36 +127,6 @@ export function nonNull(val: T | undefined | null): T { } -export const bigintTransformer = { - to(x?: bigint) { - return x?.toString() - }, - from(s?: string): bigint | undefined { - return s == null ? undefined : BigInt(s) - } -} - - -export const floatTransformer = { - to(x?: number) { - return x?.toString() - }, - from(s?: string): number | undefined { - return s == null ? undefined : Number(s) - } -} - - -export const bigdecimalTransformer = { - to(x?: any) { - return x?.toString() - }, - from(s?: any): any | undefined { - return s == null ? undefined : decimal.BigDecimal(s) - } -} - - export function enumFromJson(json: unknown, enumObject: E): E[keyof E] { assert(typeof json == 'string', 'invalid enum value') let val = (enumObject as any)[json] diff --git a/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts new file mode 100644 index 000000000..ffc12bd2e --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts @@ -0,0 +1,9 @@ +import {bigdecimalTransformer} from '../../transformers' +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type BigDecimalColumnOptions = Pick + +export function BigDecimalColumn(options?: BigDecimalColumnOptions): PropertyDecorator { + return Column('numeric', {...options, transformer: bigdecimalTransformer}) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts new file mode 100644 index 000000000..bde98df28 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts @@ -0,0 +1,9 @@ +import {bigintTransformer} from '../../transformers' +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type BigIntColumnOptions = Pick + +export function BigIntColumn(options?: BigIntColumnOptions): PropertyDecorator { + return Column('numeric', {...options, transformer: bigintTransformer}) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts new file mode 100644 index 000000000..a0d9b35c9 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts @@ -0,0 +1,11 @@ +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type BooleanColumnOptions = Pick< + ColumnCommonOptions, + 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' +> + +export function BooleanColumn(options?: BooleanColumnOptions): PropertyDecorator { + return Column('bool', options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts new file mode 100644 index 000000000..3d7b5f516 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts @@ -0,0 +1,11 @@ +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type BytesColumnOptions = Pick< + ColumnCommonOptions, + 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' +> + +export function BytesColumn(options?: BytesColumnOptions): PropertyDecorator { + return Column('bytea', options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts b/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts new file mode 100644 index 000000000..4c944d6f1 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts @@ -0,0 +1,11 @@ +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type DateTimeColumnOptions = Pick< + ColumnCommonOptions, + 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' +> + +export function DateTimeColumn(options?: DateTimeColumnOptions): PropertyDecorator { + return Column('timestamp with time zone', options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts b/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts new file mode 100644 index 000000000..216812680 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts @@ -0,0 +1,9 @@ +import {floatTransformer} from '../../transformers' +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type FloatColumnOptions = Pick + +export function FloatColumn(options?: FloatColumnOptions): PropertyDecorator { + return Column('numeric', {...options, transformer: floatTransformer}) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts b/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts new file mode 100644 index 000000000..b2a2550a7 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts @@ -0,0 +1,11 @@ +import {Column} from './Column' +import {ColumnCommonOptions, ColumnOptions} from './common' + +export type IntColumnOptions = Pick< + ColumnCommonOptions, + 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' +> + +export function IntColumn(options?: IntColumnOptions): PropertyDecorator { + return Column('int4', options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts b/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts new file mode 100644 index 000000000..cb1f082f4 --- /dev/null +++ b/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts @@ -0,0 +1,11 @@ +import {Column} from './Column' +import {ColumnCommonOptions} from './common' + +export type StringColumnOptions = Pick< + ColumnCommonOptions, + 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' +> + +export function StringColumn(options?: StringColumnOptions): PropertyDecorator { + return Column('text', options) +} diff --git a/typeorm/typeorm-store/src/decorators/columns/index.ts b/typeorm/typeorm-store/src/decorators/columns/index.ts index d7f6c9ca7..cdb7bd8b2 100644 --- a/typeorm/typeorm-store/src/decorators/columns/index.ts +++ b/typeorm/typeorm-store/src/decorators/columns/index.ts @@ -1,3 +1,11 @@ export * from './common' export * from './Column' export * from './PrimaryColumn' +export * from './BigDecimalColumn' +export * from './BigIntColumn' +export * from './BooleanColumn' +export * from './BytesColumn' +export * from './DateTimeColumn' +export * from './FloatColumn' +export * from './IntColumn' +export * from './StringColumn' diff --git a/typeorm/typeorm-store/src/index.ts b/typeorm/typeorm-store/src/index.ts index 56529cb1b..82d4fb142 100644 --- a/typeorm/typeorm-store/src/index.ts +++ b/typeorm/typeorm-store/src/index.ts @@ -1,3 +1,4 @@ export * from './database' export {EntityClass, FindManyOptions, FindOneOptions, Store} from './store' export * from './decorators' +export * from './transformers' \ No newline at end of file diff --git a/typeorm/typeorm-store/src/transformers.ts b/typeorm/typeorm-store/src/transformers.ts new file mode 100644 index 000000000..15c3b86f2 --- /dev/null +++ b/typeorm/typeorm-store/src/transformers.ts @@ -0,0 +1,40 @@ +import {ValueTransformer} from 'typeorm' + +export const bigintTransformer: ValueTransformer = { + to(x?: bigint) { + return x?.toString() + }, + from(s?: string): bigint | undefined { + return s == null ? undefined : BigInt(s) + }, +} + +export const floatTransformer: ValueTransformer = { + to(x?: number) { + return x?.toString() + }, + from(s?: string): number | undefined { + return s == null ? undefined : Number(s) + }, +} + +const decimal = { + get BigDecimal(): any { + throw new Error('Package `@subsquid/big-decimal` is not installed') + }, +} + +try { + Object.defineProperty(decimal, 'BigDecimal', { + value: require('@subsquid/big-decimal').BigDecimal, + }) +} catch (e) {} + +export const bigdecimalTransformer: ValueTransformer = { + to(x?: any) { + return x?.toString() + }, + from(s?: any): any | undefined { + return s == null ? undefined : decimal.BigDecimal(s) + }, +} From 2ab87977864e749f70a14eaee91210d6589e4d51 Mon Sep 17 00:00:00 2001 From: belopash Date: Tue, 2 Apr 2024 11:57:03 +0500 Subject: [PATCH 4/5] changes --- .../typeorm-store-decorators_2024-04-02-06-56.json | 10 ++++++++++ .../typeorm-store-decorators_2024-04-02-06-56.json | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 common/changes/@subsquid/typeorm-codegen/typeorm-store-decorators_2024-04-02-06-56.json create mode 100644 common/changes/@subsquid/typeorm-store/typeorm-store-decorators_2024-04-02-06-56.json diff --git a/common/changes/@subsquid/typeorm-codegen/typeorm-store-decorators_2024-04-02-06-56.json b/common/changes/@subsquid/typeorm-codegen/typeorm-store-decorators_2024-04-02-06-56.json new file mode 100644 index 000000000..591220b00 --- /dev/null +++ b/common/changes/@subsquid/typeorm-codegen/typeorm-store-decorators_2024-04-02-06-56.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@subsquid/typeorm-codegen", + "comment": "use decorators from typeorm-store package", + "type": "minor" + } + ], + "packageName": "@subsquid/typeorm-codegen" +} \ No newline at end of file diff --git a/common/changes/@subsquid/typeorm-store/typeorm-store-decorators_2024-04-02-06-56.json b/common/changes/@subsquid/typeorm-store/typeorm-store-decorators_2024-04-02-06-56.json new file mode 100644 index 000000000..412917ec4 --- /dev/null +++ b/common/changes/@subsquid/typeorm-store/typeorm-store-decorators_2024-04-02-06-56.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@subsquid/typeorm-store", + "comment": "re-export typeorm decorators", + "type": "minor" + } + ], + "packageName": "@subsquid/typeorm-store" +} \ No newline at end of file From 709cb6899c88625beada3fb655788396c647dc45 Mon Sep 17 00:00:00 2001 From: belopash Date: Tue, 2 Apr 2024 12:42:52 +0500 Subject: [PATCH 5/5] add comments --- .../src/decorators/columns/BigDecimalColumn.ts | 6 ++++++ .../typeorm-store/src/decorators/columns/BigIntColumn.ts | 6 ++++++ .../typeorm-store/src/decorators/columns/BooleanColumn.ts | 4 ++++ typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts | 4 ++++ .../typeorm-store/src/decorators/columns/DateTimeColumn.ts | 4 ++++ typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts | 6 ++++++ typeorm/typeorm-store/src/decorators/columns/IntColumn.ts | 4 ++++ .../typeorm-store/src/decorators/columns/StringColumn.ts | 4 ++++ 8 files changed, 38 insertions(+) diff --git a/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts index ffc12bd2e..2f436cc32 100644 --- a/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/BigDecimalColumn.ts @@ -4,6 +4,12 @@ import {ColumnCommonOptions} from './common' export type BigDecimalColumnOptions = Pick +/** + * BigDecimalColumn decorator is used to mark a specific class property as a `numeric` table column. + * Column value is transformed to `BigDecimal` type. + * + * Arrays are not supported. + */ export function BigDecimalColumn(options?: BigDecimalColumnOptions): PropertyDecorator { return Column('numeric', {...options, transformer: bigdecimalTransformer}) } diff --git a/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts index bde98df28..3c572be4f 100644 --- a/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/BigIntColumn.ts @@ -4,6 +4,12 @@ import {ColumnCommonOptions} from './common' export type BigIntColumnOptions = Pick +/** + * BigIntColumn decorator is used to mark a specific class property as a `numeric` table column. + * Column value is transformed to `bigint` type. + * + * Arrays are not supported. + */ export function BigIntColumn(options?: BigIntColumnOptions): PropertyDecorator { return Column('numeric', {...options, transformer: bigintTransformer}) } diff --git a/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts index a0d9b35c9..9b4c4d3aa 100644 --- a/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/BooleanColumn.ts @@ -6,6 +6,10 @@ export type BooleanColumnOptions = Pick< 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' > +/** + * BooleanColumn decorator is used to mark a specific class property as a `bool` table column. + * Column value is transformed to `boolean` type. + */ export function BooleanColumn(options?: BooleanColumnOptions): PropertyDecorator { return Column('bool', options) } diff --git a/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts b/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts index 3d7b5f516..169b99913 100644 --- a/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/BytesColumn.ts @@ -6,6 +6,10 @@ export type BytesColumnOptions = Pick< 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' > +/** + * BytesColumn decorator is used to mark a specific class property as a `bytea` table column. + * Column value is transformed to `Uint8Array` type. + */ export function BytesColumn(options?: BytesColumnOptions): PropertyDecorator { return Column('bytea', options) } diff --git a/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts b/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts index 4c944d6f1..92bf31e82 100644 --- a/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/DateTimeColumn.ts @@ -6,6 +6,10 @@ export type DateTimeColumnOptions = Pick< 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' > +/** + * DateTimeColumn decorator is used to mark a specific class property as a `timestamp with time zone` table column. + * Column value is transformed to `Date` type. + */ export function DateTimeColumn(options?: DateTimeColumnOptions): PropertyDecorator { return Column('timestamp with time zone', options) } diff --git a/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts b/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts index 216812680..fece3d4c9 100644 --- a/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/FloatColumn.ts @@ -4,6 +4,12 @@ import {ColumnCommonOptions} from './common' export type FloatColumnOptions = Pick +/** + * FloatColumn decorator is used to mark a specific class property as a `numeric` table column. + * Column value is transformed to `number` type. + * + * Arrays are not supported. + */ export function FloatColumn(options?: FloatColumnOptions): PropertyDecorator { return Column('numeric', {...options, transformer: floatTransformer}) } diff --git a/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts b/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts index b2a2550a7..7f5af06ab 100644 --- a/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/IntColumn.ts @@ -6,6 +6,10 @@ export type IntColumnOptions = Pick< 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' > +/** + * IntColumn decorator is used to mark a specific class property as a `int4` table column. + * Column value is transformed to `number` type. + */ export function IntColumn(options?: IntColumnOptions): PropertyDecorator { return Column('int4', options) } diff --git a/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts b/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts index cb1f082f4..3630f3e56 100644 --- a/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts +++ b/typeorm/typeorm-store/src/decorators/columns/StringColumn.ts @@ -6,6 +6,10 @@ export type StringColumnOptions = Pick< 'name' | 'unique' | 'nullable' | 'default' | 'comment' | 'array' > +/** + * StringColumn decorator is used to mark a specific class property as a `text` table column. + * Column value is transformed to `string` type. + */ export function StringColumn(options?: StringColumnOptions): PropertyDecorator { return Column('text', options) }