mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
renamed entity model to base entity (for active record pattern)
This commit is contained in:
parent
079992ea89
commit
332439a2cb
@ -24,7 +24,7 @@ each for its own `findOne*` or `find*` methods
|
||||
* `SpecificRepository` is deprecated for now
|
||||
* `transaction` method has been removed from `Repository`. Use `EntityManager#transaction` method instead
|
||||
* custom repositories do not support container anymore
|
||||
* added ActiveRecord support (by extending EntityModel) class
|
||||
* added ActiveRecord support (by extending BaseEntity) class
|
||||
* controller / subscriber / migrations from options tsconfig now appended with a project root directory
|
||||
* removed naming strategy decorator, naming strategy by name functionality.
|
||||
Now naming strategy should be registered by passing naming strategy instance directly
|
||||
|
||||
@ -97,7 +97,7 @@ export {InsertEvent} from "./subscriber/event/InsertEvent";
|
||||
export {UpdateEvent} from "./subscriber/event/UpdateEvent";
|
||||
export {RemoveEvent} from "./subscriber/event/RemoveEvent";
|
||||
export {EntitySubscriberInterface} from "./subscriber/EntitySubscriberInterface";
|
||||
export {EntityModel} from "./repository/EntityModel";
|
||||
export {BaseEntity} from "./repository/BaseEntity";
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Deprecated
|
||||
|
||||
@ -12,14 +12,14 @@ import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder";
|
||||
/**
|
||||
* Base abstract entity for all entities, used in ActiveRecord patterns.
|
||||
*/
|
||||
export class EntityModel {
|
||||
export class BaseEntity {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Static Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used in all static methods of the EntityModel.
|
||||
* Connection used in all static methods of the BaseEntity.
|
||||
*/
|
||||
private static usedConnection?: Connection;
|
||||
|
||||
@ -64,7 +64,7 @@ export class EntityModel {
|
||||
/**
|
||||
* Gets current entity's Repository.
|
||||
*/
|
||||
static getRepository<T extends EntityModel>(this: ObjectType<T>, ): Repository<T> {
|
||||
static getRepository<T extends BaseEntity>(this: ObjectType<T>, ): Repository<T> {
|
||||
const connection: Connection = (this as any).usedConnection || getConnection();
|
||||
return connection.getRepository<T>(this);
|
||||
}
|
||||
@ -82,35 +82,35 @@ export class EntityModel {
|
||||
* Checks entity has an id.
|
||||
* If entity composite compose ids, it will check them all.
|
||||
*/
|
||||
static hasId(entity: EntityModel): boolean {
|
||||
static hasId(entity: BaseEntity): boolean {
|
||||
return this.getRepository().hasId(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets entity mixed id.
|
||||
*/
|
||||
static getId<T extends EntityModel>(this: ObjectType<T>, entity: T): any {
|
||||
static getId<T extends BaseEntity>(this: ObjectType<T>, entity: T): any {
|
||||
return (this as any).getRepository().getId(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new query builder that can be used to build a sql query.
|
||||
*/
|
||||
static createQueryBuilder<T extends EntityModel>(this: ObjectType<T>, alias: string): SelectQueryBuilder<T> {
|
||||
static createQueryBuilder<T extends BaseEntity>(this: ObjectType<T>, alias: string): SelectQueryBuilder<T> {
|
||||
return (this as any).getRepository().createQueryBuilder(alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new entity instance.
|
||||
*/
|
||||
static create<T extends EntityModel>(this: ObjectType<T>): T {
|
||||
static create<T extends BaseEntity>(this: ObjectType<T>): T {
|
||||
return (this as any).getRepository().create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges multiple entities (or entity-like objects) into a given entity.
|
||||
*/
|
||||
static merge<T extends EntityModel>(this: ObjectType<T>, mergeIntoEntity: T, ...entityLikes: DeepPartial<T>[]): T {
|
||||
static merge<T extends BaseEntity>(this: ObjectType<T>, mergeIntoEntity: T, ...entityLikes: DeepPartial<T>[]): T {
|
||||
return (this as any).getRepository().merge(mergeIntoEntity, ...entityLikes);
|
||||
}
|
||||
|
||||
@ -123,7 +123,7 @@ export class EntityModel {
|
||||
* Note that given entity-like object must have an entity id / primary key to find entity by.
|
||||
* Returns undefined if entity with given id was not found.
|
||||
*/
|
||||
static preload<T extends EntityModel>(this: ObjectType<T>, entityLike: DeepPartial<T>): Promise<T|undefined> {
|
||||
static preload<T extends BaseEntity>(this: ObjectType<T>, entityLike: DeepPartial<T>): Promise<T|undefined> {
|
||||
return (this as any).getRepository().preload(entityLike);
|
||||
}
|
||||
|
||||
@ -131,100 +131,100 @@ export class EntityModel {
|
||||
* Saves all given entities in the database.
|
||||
* If entities do not exist in the database then inserts, otherwise updates.
|
||||
*/
|
||||
static save<T extends EntityModel>(this: ObjectType<T>, entities: T[], options?: SaveOptions): Promise<T[]>;
|
||||
static save<T extends BaseEntity>(this: ObjectType<T>, entities: T[], options?: SaveOptions): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Saves a given entity in the database.
|
||||
* If entity does not exist in the database then inserts, otherwise updates.
|
||||
*/
|
||||
static save<T extends EntityModel>(this: ObjectType<T>, entity: T, options?: SaveOptions): Promise<T>;
|
||||
static save<T extends BaseEntity>(this: ObjectType<T>, entity: T, options?: SaveOptions): Promise<T>;
|
||||
|
||||
/**
|
||||
* Saves one or many given entities.
|
||||
*/
|
||||
static save<T extends EntityModel>(this: ObjectType<T>, entityOrEntities: T|T[], options?: SaveOptions): Promise<T|T[]> {
|
||||
static save<T extends BaseEntity>(this: ObjectType<T>, entityOrEntities: T|T[], options?: SaveOptions): Promise<T|T[]> {
|
||||
return (this as any).getRepository().save(entityOrEntities as any, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates entity partially. Entity can be found by a given conditions.
|
||||
*/
|
||||
static update<T extends EntityModel>(this: ObjectType<T>, conditions: Partial<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void>;
|
||||
static update<T extends BaseEntity>(this: ObjectType<T>, conditions: Partial<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void>;
|
||||
|
||||
/**
|
||||
* Updates entity partially. Entity can be found by a given find options.
|
||||
*/
|
||||
static update<T extends EntityModel>(this: ObjectType<T>, findOptions: FindOneOptions<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void>;
|
||||
static update<T extends BaseEntity>(this: ObjectType<T>, findOptions: FindOneOptions<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void>;
|
||||
|
||||
/**
|
||||
* Updates entity partially. Entity can be found by a given conditions.
|
||||
*/
|
||||
static update<T extends EntityModel>(this: ObjectType<T>, conditionsOrFindOptions: Partial<T>|FindOneOptions<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void> {
|
||||
static update<T extends BaseEntity>(this: ObjectType<T>, conditionsOrFindOptions: Partial<T>|FindOneOptions<T>, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void> {
|
||||
return (this as any).getRepository().update(conditionsOrFindOptions as any, partialEntity, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates entity partially. Entity will be found by a given id.
|
||||
*/
|
||||
static updateById<T extends EntityModel>(this: ObjectType<T>, id: any, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void> {
|
||||
static updateById<T extends BaseEntity>(this: ObjectType<T>, id: any, partialEntity: DeepPartial<T>, options?: SaveOptions): Promise<void> {
|
||||
return (this as any).getRepository().updateById(id, partialEntity, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a given entities from the database.
|
||||
*/
|
||||
static remove<T extends EntityModel>(this: ObjectType<T>, entities: T[], options?: RemoveOptions): Promise<T[]>;
|
||||
static remove<T extends BaseEntity>(this: ObjectType<T>, entities: T[], options?: RemoveOptions): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Removes a given entity from the database.
|
||||
*/
|
||||
static remove<T extends EntityModel>(this: ObjectType<T>, entity: T, options?: RemoveOptions): Promise<T>;
|
||||
static remove<T extends BaseEntity>(this: ObjectType<T>, entity: T, options?: RemoveOptions): Promise<T>;
|
||||
|
||||
/**
|
||||
* Removes one or many given entities.
|
||||
*/
|
||||
static remove<T extends EntityModel>(this: ObjectType<T>, entityOrEntities: T|T[], options?: RemoveOptions): Promise<T|T[]> {
|
||||
static remove<T extends BaseEntity>(this: ObjectType<T>, entityOrEntities: T|T[], options?: RemoveOptions): Promise<T|T[]> {
|
||||
return (this as any).getRepository().remove(entityOrEntities as any, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes entity by a given entity id.
|
||||
*/
|
||||
static removeById<T extends EntityModel>(this: ObjectType<T>, id: any, options?: RemoveOptions): Promise<void> {
|
||||
static removeById<T extends BaseEntity>(this: ObjectType<T>, id: any, options?: RemoveOptions): Promise<void> {
|
||||
return (this as any).getRepository().removeById(id, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts entities that match given options.
|
||||
*/
|
||||
static count<T extends EntityModel>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<number>;
|
||||
static count<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<number>;
|
||||
|
||||
/**
|
||||
* Counts entities that match given conditions.
|
||||
*/
|
||||
static count<T extends EntityModel>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<number>;
|
||||
static count<T extends BaseEntity>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<number>;
|
||||
|
||||
/**
|
||||
* Counts entities that match given find options or conditions.
|
||||
*/
|
||||
static count<T extends EntityModel>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<number> {
|
||||
static count<T extends BaseEntity>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<number> {
|
||||
return (this as any).getRepository().count(optionsOrConditions as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds entities that match given options.
|
||||
*/
|
||||
static find<T extends EntityModel>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<T[]>;
|
||||
static find<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
*/
|
||||
static find<T extends EntityModel>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<T[]>;
|
||||
static find<T extends BaseEntity>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given find options or conditions.
|
||||
*/
|
||||
static find<T extends EntityModel>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<T[]> {
|
||||
static find<T extends BaseEntity>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<T[]> {
|
||||
return (this as any).getRepository().find(optionsOrConditions as any);
|
||||
}
|
||||
|
||||
@ -233,21 +233,21 @@ export class EntityModel {
|
||||
* Also counts all entities that match given conditions,
|
||||
* but ignores pagination settings (from and take options).
|
||||
*/
|
||||
static findAndCount<T extends EntityModel>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<[ T[], number ]>;
|
||||
static findAndCount<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<[ T[], number ]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given conditions.
|
||||
* Also counts all entities that match given conditions,
|
||||
* but ignores pagination settings (from and take options).
|
||||
*/
|
||||
static findAndCount<T extends EntityModel>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<[ T[], number ]>;
|
||||
static findAndCount<T extends BaseEntity>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<[ T[], number ]>;
|
||||
|
||||
/**
|
||||
* Finds entities that match given find options or conditions.
|
||||
* Also counts all entities that match given conditions,
|
||||
* but ignores pagination settings (from and take options).
|
||||
*/
|
||||
static findAndCount<T extends EntityModel>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<[ T[], number ]> {
|
||||
static findAndCount<T extends BaseEntity>(this: ObjectType<T>, optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<[ T[], number ]> {
|
||||
return (this as any).getRepository().findAndCount(optionsOrConditions as any);
|
||||
}
|
||||
|
||||
@ -255,36 +255,36 @@ export class EntityModel {
|
||||
* Finds entities by ids.
|
||||
* Optionally find options can be applied.
|
||||
*/
|
||||
static findByIds<T extends EntityModel>(this: ObjectType<T>, ids: any[], options?: FindManyOptions<T>): Promise<T[]>;
|
||||
static findByIds<T extends BaseEntity>(this: ObjectType<T>, ids: any[], options?: FindManyOptions<T>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Finds entities by ids.
|
||||
* Optionally conditions can be applied.
|
||||
*/
|
||||
static findByIds<T extends EntityModel>(this: ObjectType<T>, ids: any[], conditions?: DeepPartial<T>): Promise<T[]>;
|
||||
static findByIds<T extends BaseEntity>(this: ObjectType<T>, ids: any[], conditions?: DeepPartial<T>): Promise<T[]>;
|
||||
|
||||
/**
|
||||
* Finds entities by ids.
|
||||
* Optionally find options can be applied.
|
||||
*/
|
||||
static findByIds<T extends EntityModel>(this: ObjectType<T>, ids: any[], optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<T[]> {
|
||||
static findByIds<T extends BaseEntity>(this: ObjectType<T>, ids: any[], optionsOrConditions?: FindManyOptions<T>|DeepPartial<T>): Promise<T[]> {
|
||||
return (this as any).getRepository().findByIds(ids, optionsOrConditions as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given options.
|
||||
*/
|
||||
static findOne<T extends EntityModel>(this: ObjectType<T>, options?: FindOneOptions<T>): Promise<T|undefined>;
|
||||
static findOne<T extends BaseEntity>(this: ObjectType<T>, options?: FindOneOptions<T>): Promise<T|undefined>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
static findOne<T extends EntityModel>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<T|undefined>;
|
||||
static findOne<T extends BaseEntity>(this: ObjectType<T>, conditions?: DeepPartial<T>): Promise<T|undefined>;
|
||||
|
||||
/**
|
||||
* Finds first entity that matches given conditions.
|
||||
*/
|
||||
static findOne<T extends EntityModel>(this: ObjectType<T>, optionsOrConditions?: FindOneOptions<T>|DeepPartial<T>): Promise<T|undefined> {
|
||||
static findOne<T extends BaseEntity>(this: ObjectType<T>, optionsOrConditions?: FindOneOptions<T>|DeepPartial<T>): Promise<T|undefined> {
|
||||
return (this as any).getRepository().findOne(optionsOrConditions as any);
|
||||
}
|
||||
|
||||
@ -292,19 +292,19 @@ export class EntityModel {
|
||||
* Finds entity by given id.
|
||||
* Optionally find options can be applied.
|
||||
*/
|
||||
static findOneById<T extends EntityModel>(this: ObjectType<T>, id: any, options?: FindOneOptions<T>): Promise<T|undefined>;
|
||||
static findOneById<T extends BaseEntity>(this: ObjectType<T>, id: any, options?: FindOneOptions<T>): Promise<T|undefined>;
|
||||
|
||||
/**
|
||||
* Finds entity by given id.
|
||||
* Optionally conditions can be applied.
|
||||
*/
|
||||
static findOneById<T extends EntityModel>(this: ObjectType<T>, id: any, conditions?: DeepPartial<T>): Promise<T|undefined>;
|
||||
static findOneById<T extends BaseEntity>(this: ObjectType<T>, id: any, conditions?: DeepPartial<T>): Promise<T|undefined>;
|
||||
|
||||
/**
|
||||
* Finds entity by given id.
|
||||
* Optionally find options or conditions can be applied.
|
||||
*/
|
||||
static findOneById<T extends EntityModel>(this: ObjectType<T>, id: any, optionsOrConditions?: FindOneOptions<T>|DeepPartial<T>): Promise<T|undefined> {
|
||||
static findOneById<T extends BaseEntity>(this: ObjectType<T>, id: any, optionsOrConditions?: FindOneOptions<T>|DeepPartial<T>): Promise<T|undefined> {
|
||||
return (this as any).getRepository().findOneById(id, optionsOrConditions as any);
|
||||
}
|
||||
|
||||
@ -312,14 +312,14 @@ export class EntityModel {
|
||||
* Executes a raw SQL query and returns a raw database results.
|
||||
* Raw query execution is supported only by relational databases (MongoDB is not supported).
|
||||
*/
|
||||
static query<T extends EntityModel>(this: ObjectType<T>, query: string, parameters?: any[]): Promise<any> {
|
||||
static query<T extends BaseEntity>(this: ObjectType<T>, query: string, parameters?: any[]): Promise<any> {
|
||||
return (this as any).getRepository().query(query, parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all the data from the given table/collection (truncates/drops it).
|
||||
*/
|
||||
static clear<T extends EntityModel>(this: ObjectType<T>, ): Promise<void> {
|
||||
static clear<T extends BaseEntity>(this: ObjectType<T>, ): Promise<void> {
|
||||
return (this as any).getRepository().clear();
|
||||
}
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import {Entity} from "../../../../src/decorator/entity/Entity";
|
||||
import {EntityModel} from "../../../../src/repository/EntityModel";
|
||||
import {BaseEntity} from "../../../../src/repository/BaseEntity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Column} from "../../../../src/decorator/columns/Column";
|
||||
|
||||
@Entity()
|
||||
export class Post extends EntityModel {
|
||||
export class Post extends BaseEntity {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user