mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
replaced single getters with readonly properties
This commit is contained in:
parent
bd36765c54
commit
db7cb6f2e8
@ -21,66 +21,63 @@ export class Connection {
|
||||
// Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private _name: string;
|
||||
private _driver: Driver;
|
||||
private _entityMetadatas: EntityMetadata[] = [];
|
||||
private _entityListenerMetadatas: EntityListenerMetadata[] = [];
|
||||
private _subscribers: EventSubscriberInterface<any>[] = [];
|
||||
private repositoryAndMetadatas: RepositoryAndMetadata[] = [];
|
||||
private _options: ConnectionOptions;
|
||||
private entityManager: EntityManager;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Readonly properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Database connection options.
|
||||
*/
|
||||
readonly options: ConnectionOptions;
|
||||
|
||||
/**
|
||||
* Gets EntityManager of this connection.
|
||||
*/
|
||||
readonly entityManager: EntityManager;
|
||||
|
||||
/**
|
||||
* The name of the connection.
|
||||
*/
|
||||
readonly name: string;
|
||||
|
||||
/**
|
||||
* Database driver used by this connection.
|
||||
*/
|
||||
readonly driver: Driver;
|
||||
|
||||
/**
|
||||
* All entity metadatas that are registered for this connection.
|
||||
*/
|
||||
readonly entityMetadatas: EntityMetadata[] = [];
|
||||
|
||||
/**
|
||||
* All entity listener metadatas that are registered for this connection.
|
||||
*/
|
||||
readonly entityListeners: EntityListenerMetadata[] = [];
|
||||
|
||||
/**
|
||||
* All subscribers that are registered for this connection.
|
||||
*/
|
||||
readonly subscribers: EventSubscriberInterface<any>[] = [];
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(name: string, driver: Driver, options: ConnectionOptions) {
|
||||
this._name = name;
|
||||
this._driver = driver;
|
||||
this._driver.connection = this;
|
||||
this._options = options;
|
||||
this.name = name;
|
||||
this.driver = driver;
|
||||
this.driver.connection = this;
|
||||
this.options = options;
|
||||
this.entityManager = new EntityManager(this);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Getter / Setter Methods
|
||||
// Accessors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The name of the connection.
|
||||
*/
|
||||
get name(): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database driver used by this connection.
|
||||
*/
|
||||
get driver(): Driver {
|
||||
return this._driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* All subscribers that are registered for this connection.
|
||||
*/
|
||||
get subscribers(): EventSubscriberInterface<any>[] {
|
||||
return this._subscribers;
|
||||
}
|
||||
|
||||
/**
|
||||
* All entity metadatas that are registered for this connection.
|
||||
*/
|
||||
get entityMetadatas(): EntityMetadata[] {
|
||||
return this._entityMetadatas;
|
||||
}
|
||||
|
||||
/**
|
||||
* All entity listener metadatas that are registered for this connection.
|
||||
*/
|
||||
get entityListeners(): EntityListenerMetadata[] {
|
||||
return this._entityListenerMetadatas;
|
||||
}
|
||||
|
||||
/**
|
||||
* All repositories that are registered for this connection.
|
||||
*/
|
||||
@ -88,13 +85,6 @@ export class Connection {
|
||||
return this.repositoryAndMetadatas.map(repoAndMeta => repoAndMeta.repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* This connection options and settings.
|
||||
*/
|
||||
get options(): ConnectionOptions {
|
||||
return this._options;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Public Methods
|
||||
// -------------------------------------------------------------------------
|
||||
@ -104,10 +94,10 @@ export class Connection {
|
||||
*/
|
||||
connect(): Promise<void> {
|
||||
const schemaCreator = new SchemaCreator(this);
|
||||
return this._driver.connect().then(() => {
|
||||
if (this._options.autoSchemaCreate === true)
|
||||
return this.driver.connect().then(() => {
|
||||
if (this.options.autoSchemaCreate === true)
|
||||
return schemaCreator.create();
|
||||
|
||||
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
@ -116,14 +106,7 @@ export class Connection {
|
||||
* Closes this connection.
|
||||
*/
|
||||
close(): Promise<void> {
|
||||
return this._driver.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets EntityManager of this connection.
|
||||
*/
|
||||
getEntityManager() {
|
||||
return this.entityManager;
|
||||
return this.driver.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -153,7 +136,7 @@ export class Connection {
|
||||
* Registers entity metadatas for the current connection.
|
||||
*/
|
||||
addEntityMetadatas(metadatas: EntityMetadata[]): Connection {
|
||||
this._entityMetadatas = this._entityMetadatas.concat(metadatas);
|
||||
this.entityMetadatas.push(...metadatas);
|
||||
this.repositoryAndMetadatas = this.repositoryAndMetadatas.concat(metadatas.map(metadata => this.createRepoMeta(metadata)));
|
||||
return this;
|
||||
}
|
||||
@ -162,7 +145,7 @@ export class Connection {
|
||||
* Registers entity listener metadatas for the current connection.
|
||||
*/
|
||||
addEntityListenerMetadatas(metadatas: EntityListenerMetadata[]): Connection {
|
||||
this._entityListenerMetadatas = this._entityListenerMetadatas.concat(metadatas);
|
||||
this.entityListeners.push(...metadatas);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -170,7 +153,7 @@ export class Connection {
|
||||
* Registers subscribers for the current connection.
|
||||
*/
|
||||
addSubscribers(subscribers: EventSubscriberInterface<any>[]): Connection {
|
||||
this._subscribers = this._subscribers.concat(subscribers);
|
||||
this.subscribers.push(...subscribers);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -63,6 +63,92 @@ export class ColumnMetadata extends PropertyMetadata {
|
||||
*/
|
||||
namingStrategy: NamingStrategy;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The real reflected property type.
|
||||
*/
|
||||
readonly propertyType: string;
|
||||
|
||||
/**
|
||||
* The type of the column.
|
||||
*/
|
||||
readonly type: ColumnType;
|
||||
|
||||
/**
|
||||
* Maximum length in the database.
|
||||
*/
|
||||
readonly length = "";
|
||||
|
||||
/**
|
||||
* Indicates if this column is primary key.
|
||||
*/
|
||||
readonly isPrimary = false;
|
||||
|
||||
/**
|
||||
* Indicates if this column is auto increment.
|
||||
*/
|
||||
readonly isAutoIncrement = false;
|
||||
|
||||
/**
|
||||
* Indicates if value should be unique or not.
|
||||
*/
|
||||
readonly isUnique = false;
|
||||
|
||||
/**
|
||||
* Indicates if can contain nulls or not.
|
||||
*/
|
||||
readonly isNullable = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain a created date or not.
|
||||
*/
|
||||
readonly isCreateDate = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain an updated date or not.
|
||||
*/
|
||||
readonly isUpdateDate = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain an updated date or not.
|
||||
*/
|
||||
readonly isVirtual = false;
|
||||
|
||||
/**
|
||||
* Extra sql definition for the given column.
|
||||
*/
|
||||
readonly columnDefinition = "";
|
||||
|
||||
/**
|
||||
* Column comment.
|
||||
*/
|
||||
readonly comment = "";
|
||||
|
||||
/**
|
||||
* Old column name. Used to correctly alter tables when column name is changed.
|
||||
*/
|
||||
readonly oldColumnName: 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.
|
||||
*/
|
||||
readonly 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.
|
||||
*/
|
||||
readonly scale: number;
|
||||
|
||||
/**
|
||||
* Column collation. Note that not all databases support it.
|
||||
*/
|
||||
readonly collation: string;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// ---------------------------------------------------------------------
|
||||
@ -72,88 +158,6 @@ export class ColumnMetadata extends PropertyMetadata {
|
||||
*/
|
||||
private _name: string;
|
||||
|
||||
/**
|
||||
* The real reflected property type.
|
||||
*/
|
||||
private _propertyType: string;
|
||||
|
||||
/**
|
||||
* The type of the column.
|
||||
*/
|
||||
private _type: ColumnType;
|
||||
|
||||
/**
|
||||
* Maximum length in the database.
|
||||
*/
|
||||
private _length = "";
|
||||
|
||||
/**
|
||||
* Indicates if this column is primary key.
|
||||
*/
|
||||
private _isPrimary = false;
|
||||
|
||||
/**
|
||||
* Indicates if this column is auto increment.
|
||||
*/
|
||||
private _isAutoIncrement = false;
|
||||
|
||||
/**
|
||||
* Indicates if value should be unique or not.
|
||||
*/
|
||||
private _isUnique = false;
|
||||
|
||||
/**
|
||||
* Indicates if can contain nulls or not.
|
||||
*/
|
||||
private _isNullable = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain a created date or not.
|
||||
*/
|
||||
private _isCreateDate = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain an updated date or not.
|
||||
*/
|
||||
private _isUpdateDate = false;
|
||||
|
||||
/**
|
||||
* Indicates if column will contain an updated date or not.
|
||||
*/
|
||||
private _isVirtual = false;
|
||||
|
||||
/**
|
||||
* Extra sql definition for the given column.
|
||||
*/
|
||||
private _columnDefinition = "";
|
||||
|
||||
/**
|
||||
* Column comment.
|
||||
*/
|
||||
private _comment = "";
|
||||
|
||||
/**
|
||||
* Old column name. Used to correctly alter tables when column name is changed.
|
||||
*/
|
||||
private _oldColumnName: 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.
|
||||
*/
|
||||
private _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.
|
||||
*/
|
||||
private _scale: number;
|
||||
|
||||
/**
|
||||
* Column collation. Note that not all databases support it.
|
||||
*/
|
||||
private _collation: string;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
@ -162,40 +166,40 @@ export class ColumnMetadata extends PropertyMetadata {
|
||||
super(args.target, args.propertyName);
|
||||
|
||||
if (args.isPrimaryKey)
|
||||
this._isPrimary = args.isPrimaryKey;
|
||||
this.isPrimary = args.isPrimaryKey;
|
||||
if (args.isCreateDate)
|
||||
this._isCreateDate = args.isCreateDate;
|
||||
this.isCreateDate = args.isCreateDate;
|
||||
if (args.isUpdateDate)
|
||||
this._isUpdateDate = args.isUpdateDate;
|
||||
this.isUpdateDate = args.isUpdateDate;
|
||||
if (args.isVirtual)
|
||||
this._isVirtual = args.isVirtual;
|
||||
this.isVirtual = args.isVirtual;
|
||||
if (args.propertyType)
|
||||
this._propertyType = args.propertyType;
|
||||
this.propertyType = args.propertyType.toLowerCase();
|
||||
if (args.options.name)
|
||||
this._name = args.options.name;
|
||||
if (args.options.type)
|
||||
this._type = args.options.type;
|
||||
this.type = args.options.type;
|
||||
|
||||
if (args.options.length)
|
||||
this._length = args.options.length;
|
||||
this.length = args.options.length;
|
||||
if (args.options.autoIncrement)
|
||||
this._isAutoIncrement = args.options.autoIncrement;
|
||||
this.isAutoIncrement = args.options.autoIncrement;
|
||||
if (args.options.unique)
|
||||
this._isUnique = args.options.unique;
|
||||
this.isUnique = args.options.unique;
|
||||
if (args.options.nullable)
|
||||
this._isNullable = args.options.nullable;
|
||||
this.isNullable = args.options.nullable;
|
||||
if (args.options.columnDefinition)
|
||||
this._columnDefinition = args.options.columnDefinition;
|
||||
this.columnDefinition = args.options.columnDefinition;
|
||||
if (args.options.comment)
|
||||
this._comment = args.options.comment;
|
||||
this.comment = args.options.comment;
|
||||
if (args.options.oldColumnName)
|
||||
this._oldColumnName = args.options.oldColumnName;
|
||||
this.oldColumnName = args.options.oldColumnName;
|
||||
if (args.options.scale)
|
||||
this._scale = args.options.scale;
|
||||
this.scale = args.options.scale;
|
||||
if (args.options.precision)
|
||||
this._precision = args.options.precision;
|
||||
this.precision = args.options.precision;
|
||||
if (args.options.collation)
|
||||
this._collation = args.options.collation;
|
||||
this.collation = args.options.collation;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@ -212,121 +216,4 @@ export class ColumnMetadata extends PropertyMetadata {
|
||||
return this.namingStrategy ? this.namingStrategy.columnName(this.propertyName) : this.propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The real reflected property type.
|
||||
*/
|
||||
get propertyType(): string {
|
||||
return this._propertyType.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Type of the column.
|
||||
*/
|
||||
get type(): ColumnType {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Column type's length. For example type = "string" and length = 100 means that ORM will create a column with
|
||||
* type varchar(100).
|
||||
*/
|
||||
get length(): string {
|
||||
return this._length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column is a primary key.
|
||||
*/
|
||||
get isPrimary(): boolean {
|
||||
return this._isPrimary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column's value is auto incremented.
|
||||
*/
|
||||
get isAutoIncrement(): boolean {
|
||||
return this._isAutoIncrement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column has unique key.
|
||||
*/
|
||||
get isUnique(): boolean {
|
||||
return this._isUnique;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column can have a NULL value.
|
||||
*/
|
||||
get isNullable(): boolean {
|
||||
return this._isNullable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column is special and contains object create date.
|
||||
*/
|
||||
get isCreateDate(): boolean {
|
||||
return this._isCreateDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column is special and contains object last update date.
|
||||
*/
|
||||
get isUpdateDate(): boolean {
|
||||
return this._isUpdateDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this column is virtual. Virtual column mean that it does not really exist in class. Virtual columns
|
||||
* are used for many-to-many tables.
|
||||
*/
|
||||
get isVirtual(): boolean {
|
||||
return this._isVirtual;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra column definition value.
|
||||
*/
|
||||
get columnDefinition(): string {
|
||||
return this._columnDefinition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra column's comment.
|
||||
*/
|
||||
get comment(): string {
|
||||
return this._comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Column name used previously for this column. Used to make safe schema updates. Experimental and most probably
|
||||
* will be removed in the future. Avoid using it.
|
||||
*/
|
||||
get oldColumnName(): string {
|
||||
return this._oldColumnName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
get precision(): number {
|
||||
return this._precision;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
get scale(): number {
|
||||
return this._scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Column collation. Note that not all databases support it.
|
||||
*/
|
||||
get collation(): string {
|
||||
return this._collation;
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,44 +4,26 @@
|
||||
export class CompoundIndexMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class to which this decorator is applied.
|
||||
*/
|
||||
private _target: Function;
|
||||
readonly target: Function;
|
||||
|
||||
/**
|
||||
* Fields combination to be used as index.
|
||||
*/
|
||||
private _fields: string[];
|
||||
readonly fields: string[];
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
constructor(target: Function, fields: string[]) {
|
||||
this._target = target;
|
||||
this._fields = fields;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Getters
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The object class to which this metadata is attached.
|
||||
*/
|
||||
get target() {
|
||||
return this._target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fields combination to be used as index.
|
||||
*/
|
||||
get fields() {
|
||||
return this._fields;
|
||||
this.target = target;
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
}
|
||||
@ -6,10 +6,14 @@ import {EventListenerType} from "../types/EventListenerTypes";
|
||||
*/
|
||||
export class EntityListenerMetadata extends PropertyMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The type of the listener.
|
||||
*/
|
||||
type: EventListenerType;
|
||||
readonly type: EventListenerType;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
|
||||
@ -12,15 +12,15 @@ import {ForeignKeyMetadata} from "./ForeignKeyMetadata";
|
||||
export class EntityMetadata {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// Readonly Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private _table: TableMetadata;
|
||||
private _columns: ColumnMetadata[];
|
||||
private _relations: RelationMetadata[];
|
||||
private _indices: IndexMetadata[];
|
||||
private _compoundIndices: CompoundIndexMetadata[];
|
||||
private _foreignKeys: ForeignKeyMetadata[];
|
||||
readonly table: TableMetadata;
|
||||
readonly columns: ColumnMetadata[];
|
||||
readonly relations: RelationMetadata[];
|
||||
readonly indices: IndexMetadata[];
|
||||
readonly compoundIndices: CompoundIndexMetadata[];
|
||||
readonly foreignKeys: ForeignKeyMetadata[];
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
@ -32,12 +32,12 @@ export class EntityMetadata {
|
||||
indices: IndexMetadata[],
|
||||
compoundIndices: CompoundIndexMetadata[],
|
||||
foreignKeys: ForeignKeyMetadata[]) {
|
||||
this._table = table;
|
||||
this._columns = columns;
|
||||
this._relations = relations;
|
||||
this._indices = indices;
|
||||
this._compoundIndices = compoundIndices;
|
||||
this._foreignKeys = foreignKeys;
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.relations = relations;
|
||||
this.indices = indices;
|
||||
this.compoundIndices = compoundIndices;
|
||||
this.foreignKeys = foreignKeys;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -45,71 +45,47 @@ export class EntityMetadata {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
get name(): string {
|
||||
return (<any> this._table.target).name;
|
||||
return (<any> this.table.target).name;
|
||||
}
|
||||
|
||||
get target(): Function {
|
||||
return this._table.target;
|
||||
}
|
||||
|
||||
get table(): TableMetadata {
|
||||
return this._table;
|
||||
}
|
||||
|
||||
get columns(): ColumnMetadata[] {
|
||||
return this._columns;
|
||||
}
|
||||
|
||||
get relations(): RelationMetadata[] {
|
||||
return this._relations;
|
||||
}
|
||||
|
||||
get foreignKeys(): ForeignKeyMetadata[] {
|
||||
return this._foreignKeys;
|
||||
return this.table.target;
|
||||
}
|
||||
|
||||
get oneToOneRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_ONE);
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_ONE);
|
||||
}
|
||||
|
||||
get ownerOneToOneRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_ONE && relation.isOwning);
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_ONE && relation.isOwning);
|
||||
}
|
||||
|
||||
get oneToManyRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_MANY);
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.ONE_TO_MANY);
|
||||
}
|
||||
|
||||
get manyToOneRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_ONE);
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_ONE);
|
||||
}
|
||||
|
||||
get manyToManyRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_MANY);
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_MANY);
|
||||
}
|
||||
|
||||
get ownerManyToManyRelations(): RelationMetadata[] {
|
||||
return this._relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_MANY && relation.isOwning);
|
||||
}
|
||||
|
||||
get indices(): IndexMetadata[] {
|
||||
return this._indices;
|
||||
}
|
||||
|
||||
get compoundIndices(): CompoundIndexMetadata[] {
|
||||
return this._compoundIndices;
|
||||
return this.relations.filter(relation => relation.relationType === RelationTypes.MANY_TO_MANY && relation.isOwning);
|
||||
}
|
||||
|
||||
get primaryColumn(): ColumnMetadata {
|
||||
return this._columns.find(column => column.isPrimary);
|
||||
return this.columns.find(column => column.isPrimary);
|
||||
}
|
||||
|
||||
get createDateColumn(): ColumnMetadata {
|
||||
return this._columns.find(column => column.isCreateDate);
|
||||
return this.columns.find(column => column.isCreateDate);
|
||||
}
|
||||
|
||||
get updateDateColumn(): ColumnMetadata {
|
||||
return this._columns.find(column => column.isUpdateDate);
|
||||
return this.columns.find(column => column.isUpdateDate);
|
||||
}
|
||||
|
||||
get hasPrimaryKey(): boolean {
|
||||
@ -129,8 +105,8 @@ export class EntityMetadata {
|
||||
|
||||
createPropertiesMap(): any {
|
||||
const entity: any = {};
|
||||
this._columns.forEach(column => entity[column.name] = column.name);
|
||||
this._relations.forEach(relation => entity[relation.name] = relation.name);
|
||||
this.columns.forEach(column => entity[column.name] = column.name);
|
||||
this.relations.forEach(relation => entity[relation.name] = relation.name);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@ -147,11 +123,11 @@ export class EntityMetadata {
|
||||
}
|
||||
|
||||
findColumnWithPropertyName(propertyName: string): ColumnMetadata {
|
||||
return this._columns.find(column => column.propertyName === propertyName);
|
||||
return this.columns.find(column => column.propertyName === propertyName);
|
||||
}
|
||||
|
||||
findColumnWithDbName(name: string): ColumnMetadata {
|
||||
return this._columns.find(column => column.name === name);
|
||||
return this.columns.find(column => column.name === name);
|
||||
}
|
||||
|
||||
hasRelationWithPropertyName(propertyName: string): boolean {
|
||||
@ -163,27 +139,27 @@ export class EntityMetadata {
|
||||
}
|
||||
|
||||
findRelationWithPropertyName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.propertyName === propertyName);
|
||||
return this.relations.find(relation => relation.propertyName === propertyName);
|
||||
}
|
||||
|
||||
findRelationWithDbName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.name === propertyName);
|
||||
return this.relations.find(relation => relation.name === propertyName);
|
||||
}
|
||||
|
||||
findRelationWithOneWithPropertyName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.propertyName === propertyName && (relation.isOneToMany || relation.isOneToOne));
|
||||
return this.relations.find(relation => relation.propertyName === propertyName && (relation.isOneToMany || relation.isOneToOne));
|
||||
}
|
||||
|
||||
findRelationWithOneWithDbName(name: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.name === name && (relation.isOneToMany || relation.isOneToOne));
|
||||
return this.relations.find(relation => relation.name === name && (relation.isOneToMany || relation.isOneToOne));
|
||||
}
|
||||
|
||||
findRelationWithManyWithPropertyName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.propertyName === propertyName && (relation.isManyToOne || relation.isManyToMany));
|
||||
return this.relations.find(relation => relation.propertyName === propertyName && (relation.isManyToOne || relation.isManyToMany));
|
||||
}
|
||||
|
||||
findRelationWithManyWithDbName(name: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.name === name && (relation.isManyToOne || relation.isManyToMany));
|
||||
return this.relations.find(relation => relation.name === name && (relation.isManyToOne || relation.isManyToMany));
|
||||
}
|
||||
|
||||
hasRelationWithOneWithPropertyName(propertyName: string): boolean {
|
||||
|
||||
@ -4,31 +4,20 @@
|
||||
export class EventSubscriberMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class to which this decorator is applied.
|
||||
*/
|
||||
private _target: Function;
|
||||
readonly target: Function;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
constructor(target: Function) {
|
||||
this._target = target;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Getters
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The object class to which this metadata is attached.
|
||||
*/
|
||||
get target() {
|
||||
return this._target;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
}
|
||||
@ -9,33 +9,33 @@ export type OnDeleteType = "RESTRICT"|"CASCADE"|"SET NULL";
|
||||
export class ForeignKeyMetadata {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Properties
|
||||
// Readonly Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Table to which this foreign key is applied.
|
||||
*/
|
||||
private _table: TableMetadata;
|
||||
readonly table: TableMetadata;
|
||||
|
||||
/**
|
||||
* Array of columns.
|
||||
*/
|
||||
private _columns: ColumnMetadata[];
|
||||
readonly columns: ColumnMetadata[];
|
||||
|
||||
/**
|
||||
* Table to which this foreign key is references.
|
||||
*/
|
||||
private _referencedTable: TableMetadata;
|
||||
readonly referencedTable: TableMetadata;
|
||||
|
||||
/**
|
||||
* Array of referenced columns.
|
||||
*/
|
||||
private _referencedColumns: ColumnMetadata[];
|
||||
readonly referencedColumns: ColumnMetadata[];
|
||||
|
||||
/**
|
||||
* What to do with a relation on deletion of the row containing a foreign key.
|
||||
*/
|
||||
private _onDelete: OnDeleteType;
|
||||
readonly onDelete: OnDeleteType;
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
@ -46,46 +46,18 @@ export class ForeignKeyMetadata {
|
||||
referencedTable: TableMetadata,
|
||||
referencedColumns: ColumnMetadata[],
|
||||
onDelete?: OnDeleteType) {
|
||||
this._table = table;
|
||||
this._columns = columns;
|
||||
this._referencedTable = referencedTable;
|
||||
this._referencedColumns = referencedColumns;
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.referencedTable = referencedTable;
|
||||
this.referencedColumns = referencedColumns;
|
||||
if (onDelete)
|
||||
this._onDelete = onDelete;
|
||||
this.onDelete = onDelete;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Accessors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Table to which this foreign key is applied.
|
||||
*/
|
||||
get table(): TableMetadata {
|
||||
return this._table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of columns.
|
||||
*/
|
||||
get columns(): ColumnMetadata[] {
|
||||
return this._columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Table to which this foreign key is references.
|
||||
*/
|
||||
get referencedTable(): TableMetadata {
|
||||
return this._referencedTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of referenced columns.
|
||||
*/
|
||||
get referencedColumns(): ColumnMetadata[] {
|
||||
return this._referencedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of column names.
|
||||
*/
|
||||
@ -104,16 +76,10 @@ export class ForeignKeyMetadata {
|
||||
* Foreign key name.
|
||||
*/
|
||||
get name() {
|
||||
// todo: use naming strategy
|
||||
const key = `${this.table.name}_${this.columnNames.join("_")}` +
|
||||
`_${this.referencedTable.name}_${this.referencedColumnNames.join("_")}`;
|
||||
return "fk_" + require("sha1")(key); // todo: use crypto instead?
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of referenced column names.
|
||||
*/
|
||||
get onDelete(): OnDeleteType {
|
||||
return this._onDelete;
|
||||
}
|
||||
|
||||
}
|
||||
@ -5,10 +5,14 @@ import {PropertyMetadata} from "./PropertyMetadata";
|
||||
*/
|
||||
export class IndexMetadata extends PropertyMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The name of the index.
|
||||
*/
|
||||
name: string|undefined;
|
||||
readonly name: string|undefined;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
|
||||
@ -4,18 +4,18 @@
|
||||
export abstract class PropertyMetadata {
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Properties
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class to which this decorator is applied.
|
||||
*/
|
||||
private _target: Function;
|
||||
readonly target: Function;
|
||||
|
||||
/**
|
||||
* Class's property name to which this decorator is applied.
|
||||
*/
|
||||
private _propertyName: string;
|
||||
readonly propertyName: string;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
@ -23,28 +23,10 @@ export abstract class PropertyMetadata {
|
||||
|
||||
constructor(target?: Function, propertyName?: string) {
|
||||
if (target)
|
||||
this._target = target;
|
||||
this.target = target;
|
||||
|
||||
if (propertyName)
|
||||
this._propertyName = propertyName;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Accessors
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The object class to which this metadata is attached.
|
||||
*/
|
||||
get target() {
|
||||
return this._target;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the property of the object to which this metadata is attached.
|
||||
*/
|
||||
get propertyName() {
|
||||
return this._propertyName;
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
}
|
||||
@ -71,6 +71,60 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
*/
|
||||
namingStrategy: NamingStrategy;
|
||||
|
||||
/**
|
||||
* Related entity metadata.
|
||||
*/
|
||||
relatedEntityMetadata: EntityMetadata;
|
||||
|
||||
/**
|
||||
* Junction entity metadata.
|
||||
*/
|
||||
junctionEntityMetadata: EntityMetadata;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Relation type.
|
||||
*/
|
||||
readonly relationType: RelationType;
|
||||
|
||||
/**
|
||||
* Indicates if this side is an owner of this relation.
|
||||
*/
|
||||
readonly isOwning: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be inserted to the db.
|
||||
*/
|
||||
readonly isCascadeInsert: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be updated in the db.
|
||||
*/
|
||||
readonly isCascadeUpdate: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be remove from the db.
|
||||
*/
|
||||
readonly isCascadeRemove: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if relation column value can be nullable or not.
|
||||
*/
|
||||
readonly isNullable: boolean = true;
|
||||
|
||||
/**
|
||||
* Old column name.
|
||||
*/
|
||||
readonly oldColumnName: string;
|
||||
|
||||
/**
|
||||
* What to do with a relation on deletion of the row containing a foreign key.
|
||||
*/
|
||||
readonly onDelete: OnDeleteType;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// ---------------------------------------------------------------------
|
||||
@ -80,11 +134,6 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
*/
|
||||
private _name: string;
|
||||
|
||||
/**
|
||||
* Relation type.
|
||||
*/
|
||||
private _relationType: RelationType;
|
||||
|
||||
/**
|
||||
* The type of the field.
|
||||
*/
|
||||
@ -95,77 +144,33 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
*/
|
||||
private _inverseSideProperty: PropertyTypeInFunction<any>;
|
||||
|
||||
/**
|
||||
* Indicates if this side is an owner of this relation.
|
||||
*/
|
||||
private _isOwning: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be inserted to the db.
|
||||
*/
|
||||
private _isCascadeInsert: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be updated in the db.
|
||||
*/
|
||||
private _isCascadeUpdate: boolean;
|
||||
|
||||
/**
|
||||
* If set to true then it means that related object can be allowed to be remove from the db.
|
||||
*/
|
||||
private _isCascadeRemove: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if relation column value can be nullable or not.
|
||||
*/
|
||||
private _isNullable: boolean = true;
|
||||
|
||||
/**
|
||||
* Old column name.
|
||||
*/
|
||||
private _oldColumnName: string;
|
||||
|
||||
/**
|
||||
* Related entity metadata.
|
||||
*/
|
||||
private _relatedEntityMetadata: EntityMetadata;
|
||||
|
||||
/**
|
||||
* Junction entity metadata.
|
||||
*/
|
||||
private _junctionEntityMetadata: EntityMetadata;
|
||||
|
||||
/**
|
||||
* What to do with a relation on deletion of the row containing a foreign key.
|
||||
*/
|
||||
private _onDelete: OnDeleteType;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
constructor(args: RelationMetadataArgs) {
|
||||
super(args.target, args.propertyName);
|
||||
this._relationType = args.relationType;
|
||||
this._type = args.type;
|
||||
this._isOwning = args.isOwning;
|
||||
this.relationType = args.relationType;
|
||||
this.isOwning = args.isOwning;
|
||||
this._inverseSideProperty = args.inverseSideProperty;
|
||||
|
||||
if (args.options.name)
|
||||
this._name = args.options.name;
|
||||
if (args.options.cascadeInsert)
|
||||
this._isCascadeInsert = args.options.cascadeInsert;
|
||||
this.isCascadeInsert = args.options.cascadeInsert;
|
||||
if (args.options.cascadeUpdate)
|
||||
this._isCascadeUpdate = args.options.cascadeUpdate;
|
||||
this.isCascadeUpdate = args.options.cascadeUpdate;
|
||||
if (args.options.cascadeRemove)
|
||||
this._isCascadeRemove = args.options.cascadeRemove;
|
||||
this.isCascadeRemove = args.options.cascadeRemove;
|
||||
if (args.options.oldColumnName)
|
||||
this._oldColumnName = args.options.oldColumnName;
|
||||
this.oldColumnName = args.options.oldColumnName;
|
||||
if (args.options.nullable)
|
||||
this._isNullable = args.options.nullable;
|
||||
this.isNullable = args.options.nullable;
|
||||
if (args.options.onDelete)
|
||||
this._onDelete = args.options.onDelete;
|
||||
this.onDelete = args.options.onDelete;
|
||||
|
||||
if (!this._type)
|
||||
this._type = args.type;
|
||||
if (!this._name)
|
||||
this._name = args.propertyName;
|
||||
}
|
||||
@ -177,26 +182,6 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
get name(): string {
|
||||
return this.namingStrategy ? this.namingStrategy.relationName(this._name) : this._name;
|
||||
}
|
||||
|
||||
get relatedEntityMetadata(): EntityMetadata {
|
||||
return this._relatedEntityMetadata;
|
||||
}
|
||||
|
||||
set relatedEntityMetadata(metadata: EntityMetadata) {
|
||||
this._relatedEntityMetadata = metadata;
|
||||
}
|
||||
|
||||
get junctionEntityMetadata(): EntityMetadata {
|
||||
return this._junctionEntityMetadata;
|
||||
}
|
||||
|
||||
set junctionEntityMetadata(metadata: EntityMetadata) {
|
||||
this._junctionEntityMetadata = metadata;
|
||||
}
|
||||
|
||||
get relationType(): RelationType {
|
||||
return this._relationType;
|
||||
}
|
||||
|
||||
get type(): Function {
|
||||
return this._type();
|
||||
@ -207,23 +192,7 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
}
|
||||
|
||||
get inverseRelation(): RelationMetadata {
|
||||
return this._relatedEntityMetadata.findRelationWithPropertyName(this.computeInverseSide(this._inverseSideProperty));
|
||||
}
|
||||
|
||||
get isOwning(): boolean {
|
||||
return this._isOwning;
|
||||
}
|
||||
|
||||
get isCascadeInsert(): boolean {
|
||||
return this._isCascadeInsert;
|
||||
}
|
||||
|
||||
get isCascadeUpdate(): boolean {
|
||||
return this._isCascadeUpdate;
|
||||
}
|
||||
|
||||
get isCascadeRemove(): boolean {
|
||||
return this._isCascadeRemove;
|
||||
return this.relatedEntityMetadata.findRelationWithPropertyName(this.computeInverseSide(this._inverseSideProperty));
|
||||
}
|
||||
|
||||
get isOneToOne(): boolean {
|
||||
@ -242,18 +211,6 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
return this.relationType === RelationTypes.MANY_TO_MANY;
|
||||
}
|
||||
|
||||
get isNullable(): boolean {
|
||||
return this._isNullable;
|
||||
}
|
||||
|
||||
get oldColumnName(): string {
|
||||
return this._oldColumnName;
|
||||
}
|
||||
|
||||
get onDelete(): OnDeleteType {
|
||||
return this._onDelete;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Methods
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
@ -15,24 +15,28 @@ export class TableMetadata {
|
||||
namingStrategy: NamingStrategy;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// Readonly Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class to which this column is applied.
|
||||
*/
|
||||
private _target: Function;
|
||||
readonly target: Function;
|
||||
|
||||
/**
|
||||
* Indicates if this table is abstract or not. Regular tables can inherit columns from abstract tables.
|
||||
*/
|
||||
readonly isAbstract = false;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Table name in the database.
|
||||
*/
|
||||
private _name: string;
|
||||
|
||||
/**
|
||||
* Indicates if this table is abstract or not. Regular tables can inherit columns from abstract tables.
|
||||
*/
|
||||
private _isAbstract = false;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Constructor
|
||||
// ---------------------------------------------------------------------
|
||||
@ -41,27 +45,19 @@ export class TableMetadata {
|
||||
constructor(target: Function, isAbstract: boolean);
|
||||
constructor(target: Function, nameOrIsAbstract?: string|boolean, maybeIsAbstract?: boolean) {
|
||||
if (target)
|
||||
this._target = target;
|
||||
this.target = target;
|
||||
if (typeof nameOrIsAbstract === "string")
|
||||
this._name = nameOrIsAbstract;
|
||||
if (typeof nameOrIsAbstract === "boolean")
|
||||
this._isAbstract = nameOrIsAbstract;
|
||||
this.isAbstract = nameOrIsAbstract;
|
||||
if (typeof maybeIsAbstract === "boolean")
|
||||
this._isAbstract = maybeIsAbstract;
|
||||
this.isAbstract = maybeIsAbstract;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Getters
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Target entity of this table.
|
||||
* Target can be empty only for junction tables.
|
||||
*/
|
||||
get target() {
|
||||
return this._target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Table name in the database.
|
||||
*/
|
||||
@ -69,14 +65,7 @@ export class TableMetadata {
|
||||
if (this._name)
|
||||
return this._name;
|
||||
|
||||
return this.namingStrategy ? this.namingStrategy.tableName((<any>this._target).name) : (<any>this._target).name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if this table is abstract or not. Regular tables can inherit columns from abstract tables.
|
||||
*/
|
||||
get isAbstract() {
|
||||
return this._isAbstract;
|
||||
return this.namingStrategy ? this.namingStrategy.tableName((<any>this.target).name) : (<any>this.target).name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user