UNPKG

10.1 kBTypeScriptView Raw
1import { Repository } from "./Repository";
2import { FindConditions } from "../index";
3import { DeepPartial } from "../common/DeepPartial";
4import { SaveOptions } from "./SaveOptions";
5import { FindOneOptions } from "../find-options/FindOneOptions";
6import { RemoveOptions } from "./RemoveOptions";
7import { FindManyOptions } from "../find-options/FindManyOptions";
8import { Connection } from "../connection/Connection";
9import { ObjectType } from "../common/ObjectType";
10import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder";
11import { InsertResult } from "../query-builder/result/InsertResult";
12import { UpdateResult } from "../query-builder/result/UpdateResult";
13import { DeleteResult } from "../query-builder/result/DeleteResult";
14import { ObjectID } from "../driver/mongodb/typings";
15import { QueryDeepPartialEntity } from "../query-builder/QueryPartialEntity";
16/**
17 * Base abstract entity for all entities, used in ActiveRecord patterns.
18 */
19export declare class BaseEntity {
20 /**
21 * Connection used in all static methods of the BaseEntity.
22 */
23 private static usedConnection?;
24 /**
25 * Checks if entity has an id.
26 * If entity composite compose ids, it will check them all.
27 */
28 hasId(): boolean;
29 /**
30 * Saves current entity in the database.
31 * If entity does not exist in the database then inserts, otherwise updates.
32 */
33 save(): Promise<this>;
34 /**
35 * Removes current entity from the database.
36 */
37 remove(): Promise<this>;
38 /**
39 * Reloads entity data from the database.
40 */
41 reload(): Promise<void>;
42 /**
43 * Sets connection to be used by entity.
44 */
45 static useConnection(connection: Connection): void;
46 /**
47 * Gets current entity's Repository.
48 */
49 static getRepository<T extends BaseEntity>(this: ObjectType<T>): Repository<T>;
50 /**
51 * Returns object that is managed by this repository.
52 * If this repository manages entity from schema,
53 * then it returns a name of that schema instead.
54 */
55 static readonly target: Function | string;
56 /**
57 * Checks entity has an id.
58 * If entity composite compose ids, it will check them all.
59 */
60 static hasId(entity: BaseEntity): boolean;
61 /**
62 * Gets entity mixed id.
63 */
64 static getId<T extends BaseEntity>(this: ObjectType<T>, entity: T): any;
65 /**
66 * Creates a new query builder that can be used to build a sql query.
67 */
68 static createQueryBuilder<T extends BaseEntity>(this: ObjectType<T>, alias?: string): SelectQueryBuilder<T>;
69 /**
70 * Creates a new entity instance.
71 */
72 static create<T extends BaseEntity>(this: ObjectType<T>): T;
73 /**
74 * Creates a new entities and copies all entity properties from given objects into their new entities.
75 * Note that it copies only properties that present in entity schema.
76 */
77 static create<T extends BaseEntity>(this: ObjectType<T>, entityLikeArray: DeepPartial<T>[]): T;
78 /**
79 * Creates a new entity instance and copies all entity properties from this object into a new entity.
80 * Note that it copies only properties that present in entity schema.
81 */
82 static create<T extends BaseEntity>(this: ObjectType<T>, entityLike: DeepPartial<T>): T;
83 /**
84 * Merges multiple entities (or entity-like objects) into a given entity.
85 */
86 static merge<T extends BaseEntity>(this: ObjectType<T>, mergeIntoEntity: T, ...entityLikes: DeepPartial<T>[]): T;
87 /**
88 * Creates a new entity from the given plan javascript object. If entity already exist in the database, then
89 * it loads it (and everything related to it), replaces all values with the new ones from the given object
90 * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
91 * replaced from the new object.
92 *
93 * Note that given entity-like object must have an entity id / primary key to find entity by.
94 * Returns undefined if entity with given id was not found.
95 */
96 static preload<T extends BaseEntity>(this: ObjectType<T>, entityLike: DeepPartial<T>): Promise<T | undefined>;
97 /**
98 * Saves all given entities in the database.
99 * If entities do not exist in the database then inserts, otherwise updates.
100 */
101 static save<T extends BaseEntity>(this: ObjectType<T>, entities: T[], options?: SaveOptions): Promise<T[]>;
102 /**
103 * Saves a given entity in the database.
104 * If entity does not exist in the database then inserts, otherwise updates.
105 */
106 static save<T extends BaseEntity>(this: ObjectType<T>, entity: T, options?: SaveOptions): Promise<T>;
107 /**
108 * Removes a given entities from the database.
109 */
110 static remove<T extends BaseEntity>(this: ObjectType<T>, entities: T[], options?: RemoveOptions): Promise<T[]>;
111 /**
112 * Removes a given entity from the database.
113 */
114 static remove<T extends BaseEntity>(this: ObjectType<T>, entity: T, options?: RemoveOptions): Promise<T>;
115 /**
116 * Inserts a given entity into the database.
117 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
118 * Executes fast and efficient INSERT query.
119 * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
120 */
121 static insert<T extends BaseEntity>(this: ObjectType<T>, entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[], options?: SaveOptions): Promise<InsertResult>;
122 /**
123 * Updates entity partially. Entity can be found by a given conditions.
124 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
125 * Executes fast and efficient UPDATE query.
126 * Does not check if entity exist in the database.
127 */
128 static update<T extends BaseEntity>(this: ObjectType<T>, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindConditions<T>, partialEntity: QueryDeepPartialEntity<T>, options?: SaveOptions): Promise<UpdateResult>;
129 /**
130 * Deletes entities by a given criteria.
131 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
132 * Executes fast and efficient DELETE query.
133 * Does not check if entity exist in the database.
134 */
135 static delete<T extends BaseEntity>(this: ObjectType<T>, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindConditions<T>, options?: RemoveOptions): Promise<DeleteResult>;
136 /**
137 * Counts entities that match given options.
138 */
139 static count<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<number>;
140 /**
141 * Counts entities that match given conditions.
142 */
143 static count<T extends BaseEntity>(this: ObjectType<T>, conditions?: FindConditions<T>): Promise<number>;
144 /**
145 * Finds entities that match given options.
146 */
147 static find<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<T[]>;
148 /**
149 * Finds entities that match given conditions.
150 */
151 static find<T extends BaseEntity>(this: ObjectType<T>, conditions?: FindConditions<T>): Promise<T[]>;
152 /**
153 * Finds entities that match given find options.
154 * Also counts all entities that match given conditions,
155 * but ignores pagination settings (from and take options).
156 */
157 static findAndCount<T extends BaseEntity>(this: ObjectType<T>, options?: FindManyOptions<T>): Promise<[T[], number]>;
158 /**
159 * Finds entities that match given conditions.
160 * Also counts all entities that match given conditions,
161 * but ignores pagination settings (from and take options).
162 */
163 static findAndCount<T extends BaseEntity>(this: ObjectType<T>, conditions?: FindConditions<T>): Promise<[T[], number]>;
164 /**
165 * Finds entities by ids.
166 * Optionally find options can be applied.
167 */
168 static findByIds<T extends BaseEntity>(this: ObjectType<T>, ids: any[], options?: FindManyOptions<T>): Promise<T[]>;
169 /**
170 * Finds entities by ids.
171 * Optionally conditions can be applied.
172 */
173 static findByIds<T extends BaseEntity>(this: ObjectType<T>, ids: any[], conditions?: FindConditions<T>): Promise<T[]>;
174 /**
175 * Finds first entity that matches given options.
176 */
177 static findOne<T extends BaseEntity>(this: ObjectType<T>, id?: string | number | Date | ObjectID, options?: FindOneOptions<T>): Promise<T | undefined>;
178 /**
179 * Finds first entity that matches given options.
180 */
181 static findOne<T extends BaseEntity>(this: ObjectType<T>, options?: FindOneOptions<T>): Promise<T | undefined>;
182 /**
183 * Finds first entity that matches given conditions.
184 */
185 static findOne<T extends BaseEntity>(this: ObjectType<T>, conditions?: FindConditions<T>, options?: FindOneOptions<T>): Promise<T | undefined>;
186 /**
187 * Finds first entity that matches given options.
188 */
189 static findOneOrFail<T extends BaseEntity>(this: ObjectType<T>, id?: string | number | Date | ObjectID, options?: FindOneOptions<T>): Promise<T>;
190 /**
191 * Finds first entity that matches given options.
192 */
193 static findOneOrFail<T extends BaseEntity>(this: ObjectType<T>, options?: FindOneOptions<T>): Promise<T>;
194 /**
195 * Finds first entity that matches given conditions.
196 */
197 static findOneOrFail<T extends BaseEntity>(this: ObjectType<T>, conditions?: FindConditions<T>, options?: FindOneOptions<T>): Promise<T>;
198 /**
199 * Executes a raw SQL query and returns a raw database results.
200 * Raw query execution is supported only by relational databases (MongoDB is not supported).
201 */
202 static query<T extends BaseEntity>(this: ObjectType<T>, query: string, parameters?: any[]): Promise<any>;
203 /**
204 * Clears all the data from the given table/collection (truncates/drops it).
205 */
206 static clear<T extends BaseEntity>(this: ObjectType<T>): Promise<void>;
207}