mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
refactored decorators
This commit is contained in:
parent
901248cfa4
commit
6eae0b5398
@ -1,4 +1,3 @@
|
||||
|
||||
import {ConnectionOptions} from "../connection/ConnectionOptions";
|
||||
|
||||
/**
|
||||
|
||||
@ -38,18 +38,18 @@ export function Column(typeOrOptions?: ColumnType|ColumnOptions, options?: Colum
|
||||
type = ColumnTypes.determineTypeFromFunction(Reflect.getMetadata("design:type", object, propertyName));
|
||||
|
||||
// if column options are not given then create a new empty options
|
||||
const columnOptions = options ? options : {} as ColumnOptions;
|
||||
|
||||
if (!options) options = {} as ColumnOptions;
|
||||
|
||||
// check if there is no type in column options then set type from first function argument, or guessed one
|
||||
if (!columnOptions.type)
|
||||
columnOptions.type = type;
|
||||
if (!options.type)
|
||||
options.type = type;
|
||||
|
||||
// if we still don't have a type then we need to give error to user that type is required
|
||||
if (!columnOptions.type)
|
||||
if (!options.type)
|
||||
throw new ColumnTypeUndefinedError(object, propertyName);
|
||||
|
||||
// check if auto increment is not set for simple column
|
||||
if (columnOptions.autoIncrement)
|
||||
if (options.autoIncrement)
|
||||
throw new AutoIncrementOnlyForPrimaryError(object, propertyName);
|
||||
|
||||
// create and register a new column metadata
|
||||
@ -57,7 +57,7 @@ export function Column(typeOrOptions?: ColumnType|ColumnOptions, options?: Colum
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
propertyType: reflectedType,
|
||||
options: columnOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -14,10 +14,10 @@ export function CreateDateColumn(options?: ColumnOptions): Function {
|
||||
const reflectedType = ColumnTypes.typeToString(Reflect.getMetadata("design:type", object, propertyName));
|
||||
|
||||
// if column options are not given then create a new empty options
|
||||
const columnOptions = options ? options : {} as ColumnOptions;
|
||||
if (!options) options = {} as ColumnOptions;
|
||||
|
||||
// implicitly set a type, because this column's type cannot be anything else except date
|
||||
columnOptions.type = ColumnTypes.DATETIME;
|
||||
options.type = ColumnTypes.DATETIME;
|
||||
|
||||
// create and register a new column metadata
|
||||
defaultMetadataStorage().addColumnMetadata(new ColumnMetadata({
|
||||
@ -25,7 +25,7 @@ export function CreateDateColumn(options?: ColumnOptions): Function {
|
||||
propertyName: propertyName,
|
||||
propertyType: reflectedType,
|
||||
isCreateDate: true,
|
||||
options: columnOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -41,18 +41,18 @@ export function PrimaryColumn(typeOrOptions?: ColumnType|ColumnOptions, options?
|
||||
type = ColumnTypes.determineTypeFromFunction(Reflect.getMetadata("design:type", object, propertyName));
|
||||
|
||||
// if column options are not given then create a new empty options
|
||||
const columnOptions = options ? options : {} as ColumnOptions;
|
||||
if (!options) options = {} as ColumnOptions;
|
||||
|
||||
// check if there is no type in column options then set type from first function argument, or guessed one
|
||||
if (!columnOptions.type)
|
||||
columnOptions.type = type;
|
||||
if (!options.type)
|
||||
options.type = type;
|
||||
|
||||
// if we still don't have a type then we need to give error to user that type is required
|
||||
if (!columnOptions.type)
|
||||
if (!options.type)
|
||||
throw new ColumnTypeUndefinedError(object, propertyName);
|
||||
|
||||
// check if column is not nullable, because we cannot allow a primary key to be nullable
|
||||
if (columnOptions.nullable)
|
||||
if (options.nullable)
|
||||
throw new PrimaryColumnCannotBeNullableError(object, propertyName);
|
||||
|
||||
// create and register a new column metadata
|
||||
@ -61,7 +61,7 @@ export function PrimaryColumn(typeOrOptions?: ColumnType|ColumnOptions, options?
|
||||
propertyName: propertyName,
|
||||
propertyType: reflectedType,
|
||||
isPrimaryKey: true,
|
||||
options: columnOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import {ColumnOptions} from "../../metadata-builder/options/ColumnOptions";
|
||||
import {ColumnType, ColumnTypes} from "../../metadata-builder/types/ColumnTypes";
|
||||
import {ColumnTypes} from "../../metadata-builder/types/ColumnTypes";
|
||||
import {defaultMetadataStorage} from "../../typeorm";
|
||||
import {ColumnMetadata} from "../../metadata-builder/metadata/ColumnMetadata";
|
||||
import "reflect-metadata";
|
||||
@ -14,10 +14,10 @@ export function UpdateDateColumn(options?: ColumnOptions): Function {
|
||||
const reflectedType = ColumnTypes.typeToString(Reflect.getMetadata("design:type", object, propertyName));
|
||||
|
||||
// if column options are not given then create a new empty options
|
||||
const columnOptions = options ? options : {} as ColumnOptions;
|
||||
if (!options) options = {} as ColumnOptions;
|
||||
|
||||
// implicitly set a type, because this column's type cannot be anything else except date
|
||||
columnOptions.type = ColumnTypes.DATETIME;
|
||||
options.type = ColumnTypes.DATETIME;
|
||||
|
||||
// create and register a new column metadata
|
||||
defaultMetadataStorage().addColumnMetadata(new ColumnMetadata({
|
||||
@ -25,7 +25,7 @@ export function UpdateDateColumn(options?: ColumnOptions): Function {
|
||||
propertyName: propertyName,
|
||||
propertyType: reflectedType,
|
||||
isUpdateDate: true,
|
||||
options: columnOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -36,8 +36,7 @@ export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -46,7 +45,7 @@ export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: true,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -36,8 +36,7 @@ export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFu
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -46,7 +45,7 @@ export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFu
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: false,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -36,8 +36,7 @@ export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -46,7 +45,7 @@ export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: true,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -33,8 +33,7 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -43,7 +42,7 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: false,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
|
||||
@ -33,8 +33,7 @@ export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -43,7 +42,7 @@ export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: true,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
@ -36,8 +36,7 @@ export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunc
|
||||
}
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
|
||||
const relationOptions = options ? options : {} as RelationOptions;
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
|
||||
target: object.constructor,
|
||||
@ -46,7 +45,7 @@ export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunc
|
||||
type: typeFunction,
|
||||
inverseSideProperty: inverseSideProperty,
|
||||
isOwning: false,
|
||||
options: relationOptions
|
||||
options: options
|
||||
}));
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user