UNPKG

24.5 kBTypeScriptView Raw
1import { Connection } from "../connection/Connection";
2import { FindManyOptions } from "../find-options/FindManyOptions";
3import { ObjectType } from "../common/ObjectType";
4import { FindOneOptions } from "../find-options/FindOneOptions";
5import { DeepPartial } from "../common/DeepPartial";
6import { RemoveOptions } from "../repository/RemoveOptions";
7import { SaveOptions } from "../repository/SaveOptions";
8import { MongoRepository } from "../repository/MongoRepository";
9import { TreeRepository } from "../repository/TreeRepository";
10import { Repository } from "../repository/Repository";
11import { PlainObjectToNewEntityTransformer } from "../query-builder/transformer/PlainObjectToNewEntityTransformer";
12import { EntitySchema } from "../index";
13import { QueryRunner } from "../query-runner/QueryRunner";
14import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder";
15import { QueryDeepPartialEntity } from "../query-builder/QueryPartialEntity";
16import { ObjectID } from "../driver/mongodb/typings";
17import { InsertResult } from "../query-builder/result/InsertResult";
18import { UpdateResult } from "../query-builder/result/UpdateResult";
19import { DeleteResult } from "../query-builder/result/DeleteResult";
20import { FindConditions } from "../find-options/FindConditions";
21import { IsolationLevel } from "../driver/types/IsolationLevel";
22/**
23 * Entity manager supposed to work with any entity, automatically find its repository and call its methods,
24 * whatever entity type are you passing.
25 */
26export declare class EntityManager {
27 /**
28 * Connection used by this entity manager.
29 */
30 readonly connection: Connection;
31 /**
32 * Custom query runner to be used for operations in this entity manager.
33 * Used only in non-global entity manager.
34 */
35 readonly queryRunner?: QueryRunner;
36 /**
37 * Once created and then reused by en repositories.
38 */
39 protected repositories: Repository<any>[];
40 /**
41 * Plain to object transformer used in create and merge operations.
42 */
43 protected plainObjectToEntityTransformer: PlainObjectToNewEntityTransformer;
44 constructor(connection: Connection, queryRunner?: QueryRunner);
45 /**
46 * Wraps given function execution (and all operations made there) in a transaction.
47 * All database operations must be executed using provided entity manager.
48 */
49 transaction<T>(runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>;
50 /**
51 * Wraps given function execution (and all operations made there) in a transaction.
52 * All database operations must be executed using provided entity manager.
53 */
54 transaction<T>(isolationLevel: IsolationLevel, runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>;
55 /**
56 * Executes raw SQL query and returns raw database results.
57 */
58 query(query: string, parameters?: any[]): Promise<any>;
59 /**
60 * Creates a new query builder that can be used to build a sql query.
61 */
62 createQueryBuilder<Entity>(entityClass: ObjectType<Entity>, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
63 /**
64 * Creates a new query builder that can be used to build a sql query.
65 */
66 createQueryBuilder<Entity>(entityClass: EntitySchema<Entity>, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
67 /**
68 * Creates a new query builder that can be used to build a sql query.
69 */
70 createQueryBuilder<Entity>(entityName: string, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
71 /**
72 * Creates a new query builder that can be used to build a sql query.
73 */
74 createQueryBuilder(queryRunner?: QueryRunner): SelectQueryBuilder<any>;
75 /**
76 * Checks if entity has an id.
77 */
78 hasId(entity: any): boolean;
79 /**
80 * Checks if entity of given schema name has an id.
81 */
82 hasId(target: Function | string, entity: any): boolean;
83 /**
84 * Gets entity mixed id.
85 */
86 getId(entity: any): any;
87 /**
88 * Gets entity mixed id.
89 */
90 getId(target: Function | string, entity: any): any;
91 /**
92 * Creates a new entity instance and copies all entity properties from this object into a new entity.
93 * Note that it copies only properties that present in entity schema.
94 */
95 create<Entity>(entityClass: ObjectType<Entity>, plainObject?: DeepPartial<Entity>): Entity;
96 /**
97 * Creates a new entities and copies all entity properties from given objects into their new entities.
98 * Note that it copies only properties that present in entity schema.
99 */
100 create<Entity>(entityClass: ObjectType<Entity>, plainObjects?: DeepPartial<Entity>[]): Entity[];
101 /**
102 * Creates a new entity instance and copies all entity properties from this object into a new entity.
103 * Note that it copies only properties that present in entity schema.
104 */
105 create<Entity>(entitySchema: EntitySchema<Entity>, plainObject?: DeepPartial<Entity>): Entity;
106 /**
107 * Creates a new entities and copies all entity properties from given objects into their new entities.
108 * Note that it copies only properties that present in entity schema.
109 */
110 create<Entity>(entitySchema: EntitySchema<Entity>, plainObjects?: DeepPartial<Entity>[]): Entity[];
111 /**
112 * Creates a new entity instance and copies all entity properties from this object into a new entity.
113 * Note that it copies only properties that present in entity schema.
114 */
115 create<Entity>(entityName: string, plainObject?: DeepPartial<Entity>): Entity;
116 /**
117 * Creates a new entities and copies all entity properties from given objects into their new entities.
118 * Note that it copies only properties that present in entity schema.
119 */
120 create<Entity>(entityName: string, plainObjects?: DeepPartial<Entity>[]): Entity[];
121 /**
122 * Merges two entities into one new entity.
123 */
124 merge<Entity>(entityClass: ObjectType<Entity>, mergeIntoEntity: Entity, ...entityLikes: DeepPartial<Entity>[]): Entity;
125 /**
126 * Merges two entities into one new entity.
127 */
128 merge<Entity>(entitySchema: EntitySchema<Entity>, mergeIntoEntity: Entity, ...entityLikes: DeepPartial<Entity>[]): Entity;
129 /**
130 * Merges two entities into one new entity.
131 */
132 merge<Entity>(entityName: string, mergeIntoEntity: Entity, ...entityLikes: DeepPartial<Entity>[]): Entity;
133 /**
134 * Creates a new entity from the given plan javascript object. If entity already exist in the database, then
135 * it loads it (and everything related to it), replaces all values with the new ones from the given object
136 * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
137 * replaced from the new object.
138 */
139 preload<Entity>(entityClass: ObjectType<Entity>, entityLike: DeepPartial<Entity>): Promise<Entity | undefined>;
140 /**
141 * Creates a new entity from the given plan javascript object. If entity already exist in the database, then
142 * it loads it (and everything related to it), replaces all values with the new ones from the given object
143 * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
144 * replaced from the new object.
145 */
146 preload<Entity>(entitySchema: EntitySchema<Entity>, entityLike: DeepPartial<Entity>): Promise<Entity | undefined>;
147 /**
148 * Creates a new entity from the given plan javascript object. If entity already exist in the database, then
149 * it loads it (and everything related to it), replaces all values with the new ones from the given object
150 * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
151 * replaced from the new object.
152 */
153 preload(entityName: string, entityLike: DeepPartial<any>): Promise<any | undefined>;
154 /**
155 * Saves all given entities in the database.
156 * If entities do not exist in the database then inserts, otherwise updates.
157 */
158 save<Entity>(entities: Entity[], options?: SaveOptions): Promise<Entity[]>;
159 /**
160 * Saves all given entities in the database.
161 * If entities do not exist in the database then inserts, otherwise updates.
162 */
163 save<Entity>(entity: Entity, options?: SaveOptions): Promise<Entity>;
164 /**
165 * Saves all given entities in the database.
166 * If entities do not exist in the database then inserts, otherwise updates.
167 */
168 save<Entity, T extends DeepPartial<Entity>>(targetOrEntity: ObjectType<Entity> | EntitySchema<Entity>, entities: T[], options?: SaveOptions): Promise<T[]>;
169 /**
170 * Saves all given entities in the database.
171 * If entities do not exist in the database then inserts, otherwise updates.
172 */
173 save<Entity, T extends DeepPartial<Entity>>(targetOrEntity: ObjectType<Entity> | EntitySchema<Entity>, entity: T, options?: SaveOptions): Promise<T>;
174 /**
175 * Saves all given entities in the database.
176 * If entities do not exist in the database then inserts, otherwise updates.
177 */
178 save<T>(targetOrEntity: string, entities: T[], options?: SaveOptions): Promise<T[]>;
179 /**
180 * Saves all given entities in the database.
181 * If entities do not exist in the database then inserts, otherwise updates.
182 */
183 save<T>(targetOrEntity: string, entity: T, options?: SaveOptions): Promise<T>;
184 /**
185 * Removes a given entity from the database.
186 */
187 remove<Entity>(entity: Entity, options?: RemoveOptions): Promise<Entity>;
188 /**
189 * Removes a given entity from the database.
190 */
191 remove<Entity>(targetOrEntity: ObjectType<Entity>, entity: Entity, options?: RemoveOptions): Promise<Entity>;
192 /**
193 * Removes a given entity from the database.
194 */
195 remove<Entity>(targetOrEntity: EntitySchema<Entity>, entity: Entity, options?: RemoveOptions): Promise<Entity>;
196 /**
197 * Removes a given entity from the database.
198 */
199 remove<Entity>(targetOrEntity: string, entity: Entity, options?: RemoveOptions): Promise<Entity>;
200 /**
201 * Removes a given entity from the database.
202 */
203 remove<Entity>(entity: Entity[], options?: RemoveOptions): Promise<Entity>;
204 /**
205 * Removes a given entity from the database.
206 */
207 remove<Entity>(targetOrEntity: ObjectType<Entity>, entity: Entity[], options?: RemoveOptions): Promise<Entity[]>;
208 /**
209 * Removes a given entity from the database.
210 */
211 remove<Entity>(targetOrEntity: EntitySchema<Entity>, entity: Entity[], options?: RemoveOptions): Promise<Entity[]>;
212 /**
213 * Removes a given entity from the database.
214 */
215 remove<Entity>(targetOrEntity: string, entity: Entity[], options?: RemoveOptions): Promise<Entity[]>;
216 /**
217 * Inserts a given entity into the database.
218 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
219 * Executes fast and efficient INSERT query.
220 * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
221 * You can execute bulk inserts using this method.
222 */
223 insert<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string, entity: QueryDeepPartialEntity<Entity> | (QueryDeepPartialEntity<Entity>[])): Promise<InsertResult>;
224 /**
225 * Updates entity partially. Entity can be found by a given condition(s).
226 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
227 * Executes fast and efficient UPDATE query.
228 * Does not check if entity exist in the database.
229 * Condition(s) cannot be empty.
230 */
231 update<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | any, partialEntity: QueryDeepPartialEntity<Entity>): Promise<UpdateResult>;
232 /**
233 * Deletes entities by a given condition(s).
234 * Unlike save method executes a primitive operation without cascades, relations and other operations included.
235 * Executes fast and efficient DELETE query.
236 * Does not check if entity exist in the database.
237 * Condition(s) cannot be empty.
238 */
239 delete<Entity>(targetOrEntity: ObjectType<Entity> | EntitySchema<Entity> | string, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | any): Promise<DeleteResult>;
240 /**
241 * Counts entities that match given options.
242 * Useful for pagination.
243 */
244 count<Entity>(entityClass: ObjectType<Entity>, options?: FindOneOptions<Entity>): Promise<number>;
245 /**
246 * Counts entities that match given options.
247 * Useful for pagination.
248 */
249 count<Entity>(entityClass: EntitySchema<Entity>, options?: FindOneOptions<Entity>): Promise<number>;
250 /**
251 * Counts entities that match given options.
252 * Useful for pagination.
253 */
254 count<Entity>(entityClass: string, options?: FindOneOptions<Entity>): Promise<number>;
255 /**
256 * Counts entities that match given conditions.
257 * Useful for pagination.
258 */
259 count<Entity>(entityClass: ObjectType<Entity>, conditions?: FindConditions<Entity>): Promise<number>;
260 /**
261 * Counts entities that match given conditions.
262 * Useful for pagination.
263 */
264 count<Entity>(entityClass: EntitySchema<Entity>, conditions?: FindConditions<Entity>): Promise<number>;
265 /**
266 * Counts entities that match given conditions.
267 * Useful for pagination.
268 */
269 count<Entity>(entityClass: string, conditions?: FindConditions<Entity>): Promise<number>;
270 /**
271 * Finds entities that match given options.
272 */
273 find<Entity>(entityClass: ObjectType<Entity>, options?: FindManyOptions<Entity>): Promise<Entity[]>;
274 /**
275 * Finds entities that match given conditions.
276 */
277 find<Entity>(entityClass: ObjectType<Entity>, conditions?: FindConditions<Entity>): Promise<Entity[]>;
278 /**
279 * Finds entities that match given options.
280 */
281 find<Entity>(entitySchema: EntitySchema<Entity>, options?: FindManyOptions<Entity>): Promise<Entity[]>;
282 /**
283 * Finds entities that match given conditions.
284 */
285 find<Entity>(entitySchema: EntitySchema<Entity>, conditions?: FindConditions<Entity>): Promise<Entity[]>;
286 /**
287 * Finds entities that match given conditions.
288 */
289 find<Entity>(entityClass: string, options?: FindManyOptions<Entity>): Promise<Entity[]>;
290 /**
291 * Finds entities that match given conditions.
292 */
293 find<Entity>(entityClass: string, conditions?: FindConditions<Entity>): Promise<Entity[]>;
294 /**
295 * Finds entities that match given find options.
296 * Also counts all entities that match given conditions,
297 * but ignores pagination settings (from and take options).
298 */
299 findAndCount<Entity>(entityClass: ObjectType<Entity>, options?: FindManyOptions<Entity>): Promise<[Entity[], number]>;
300 /**
301 * Finds entities that match given find options.
302 * Also counts all entities that match given conditions,
303 * but ignores pagination settings (from and take options).
304 */
305 findAndCount<Entity>(entityClass: EntitySchema<Entity>, options?: FindManyOptions<Entity>): Promise<[Entity[], number]>;
306 /**
307 * Finds entities that match given find options.
308 * Also counts all entities that match given conditions,
309 * but ignores pagination settings (from and take options).
310 */
311 findAndCount<Entity>(entityClass: string, options?: FindManyOptions<Entity>): Promise<[Entity[], number]>;
312 /**
313 * Finds entities that match given conditions.
314 * Also counts all entities that match given conditions,
315 * but ignores pagination settings (from and take options).
316 */
317 findAndCount<Entity>(entityClass: ObjectType<Entity>, conditions?: FindConditions<Entity>): Promise<[Entity[], number]>;
318 /**
319 * Finds entities that match given conditions.
320 * Also counts all entities that match given conditions,
321 * but ignores pagination settings (from and take options).
322 */
323 findAndCount<Entity>(entityClass: EntitySchema<Entity>, conditions?: FindConditions<Entity>): Promise<[Entity[], number]>;
324 /**
325 * Finds entities that match given conditions.
326 * Also counts all entities that match given conditions,
327 * but ignores pagination settings (from and take options).
328 */
329 findAndCount<Entity>(entityClass: string, conditions?: FindConditions<Entity>): Promise<[Entity[], number]>;
330 /**
331 * Finds entities with ids.
332 * Optionally find options can be applied.
333 */
334 findByIds<Entity>(entityClass: ObjectType<Entity>, ids: any[], options?: FindManyOptions<Entity>): Promise<Entity[]>;
335 /**
336 * Finds entities with ids.
337 * Optionally find options can be applied.
338 */
339 findByIds<Entity>(entityClass: EntitySchema<Entity>, ids: any[], options?: FindManyOptions<Entity>): Promise<Entity[]>;
340 /**
341 * Finds entities with ids.
342 * Optionally find options can be applied.
343 */
344 findByIds<Entity>(entityClass: string, ids: any[], options?: FindManyOptions<Entity>): Promise<Entity[]>;
345 /**
346 * Finds entities with ids.
347 * Optionally conditions can be applied.
348 */
349 findByIds<Entity>(entityClass: ObjectType<Entity>, ids: any[], conditions?: FindConditions<Entity>): Promise<Entity[]>;
350 /**
351 * Finds entities with ids.
352 * Optionally conditions can be applied.
353 */
354 findByIds<Entity>(entityClass: EntitySchema<Entity>, ids: any[], conditions?: FindConditions<Entity>): Promise<Entity[]>;
355 /**
356 * Finds entities with ids.
357 * Optionally conditions can be applied.
358 */
359 findByIds<Entity>(entityClass: string, ids: any[], conditions?: FindConditions<Entity>): Promise<Entity[]>;
360 /**
361 * Finds first entity that matches given find options.
362 */
363 findOne<Entity>(entityClass: ObjectType<Entity>, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
364 /**
365 * Finds first entity that matches given find options.
366 */
367 findOne<Entity>(entityClass: EntitySchema<Entity>, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
368 /**
369 * Finds first entity that matches given find options.
370 */
371 findOne<Entity>(entityClass: string, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
372 /**
373 * Finds first entity that matches given find options.
374 */
375 findOne<Entity>(entityClass: ObjectType<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
376 /**
377 * Finds first entity that matches given find options.
378 */
379 findOne<Entity>(entityClass: EntitySchema<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
380 /**
381 * Finds first entity that matches given find options.
382 */
383 findOne<Entity>(entityClass: string, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
384 /**
385 * Finds first entity that matches given conditions.
386 */
387 findOne<Entity>(entityClass: ObjectType<Entity>, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
388 /**
389 * Finds first entity that matches given conditions.
390 */
391 findOne<Entity>(entityClass: EntitySchema<Entity>, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
392 /**
393 * Finds first entity that matches given conditions.
394 */
395 findOne<Entity>(entityClass: string, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
396 /**
397 * Finds first entity that matches given find options or rejects the returned promise on error.
398 */
399 findOneOrFail<Entity>(entityClass: ObjectType<Entity>, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity>;
400 /**
401 * Finds first entity that matches given find options or rejects the returned promise on error.
402 */
403 findOneOrFail<Entity>(entityClass: EntitySchema<Entity>, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity>;
404 /**
405 * Finds first entity that matches given find options or rejects the returned promise on error.
406 */
407 findOneOrFail<Entity>(entityClass: string, id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity>;
408 /**
409 * Finds first entity that matches given find options or rejects the returned promise on error.
410 */
411 findOneOrFail<Entity>(entityClass: ObjectType<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
412 /**
413 * Finds first entity that matches given find options or rejects the returned promise on error.
414 */
415 findOneOrFail<Entity>(entityClass: EntitySchema<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
416 /**
417 * Finds first entity that matches given find options or rejects the returned promise on error.
418 */
419 findOneOrFail<Entity>(entityClass: string, options?: FindOneOptions<Entity>): Promise<Entity>;
420 /**
421 * Finds first entity that matches given conditions or rejects the returned promise on error.
422 */
423 findOneOrFail<Entity>(entityClass: ObjectType<Entity>, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
424 /**
425 * Finds first entity that matches given conditions or rejects the returned promise on error.
426 */
427 findOneOrFail<Entity>(entityClass: EntitySchema<Entity>, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
428 /**
429 * Finds first entity that matches given conditions or rejects the returned promise on error.
430 */
431 findOneOrFail<Entity>(entityClass: string, conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
432 /**
433 * Clears all the data from the given table (truncates/drops it).
434 *
435 * Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms.
436 * @see https://stackoverflow.com/a/5972738/925151
437 */
438 clear<Entity>(entityClass: ObjectType<Entity> | EntitySchema<Entity> | string): Promise<void>;
439 /**
440 * Increments some column by provided value of the entities matched given conditions.
441 */
442 increment<Entity>(entityClass: ObjectType<Entity> | EntitySchema<Entity> | string, conditions: any, propertyPath: string, value: number | string): Promise<UpdateResult>;
443 /**
444 * Decrements some column by provided value of the entities matched given conditions.
445 */
446 decrement<Entity>(entityClass: ObjectType<Entity> | EntitySchema<Entity> | string, conditions: any, propertyPath: string, value: number | string): Promise<UpdateResult>;
447 /**
448 * Gets repository for the given entity class or name.
449 * If single database connection mode is used, then repository is obtained from the
450 * repository aggregator, where each repository is individually created for this entity manager.
451 * When single database connection is not used, repository is being obtained from the connection.
452 */
453 getRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): Repository<Entity>;
454 /**
455 * Gets tree repository for the given entity class or name.
456 * If single database connection mode is used, then repository is obtained from the
457 * repository aggregator, where each repository is individually created for this entity manager.
458 * When single database connection is not used, repository is being obtained from the connection.
459 */
460 getTreeRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): TreeRepository<Entity>;
461 /**
462 * Gets mongodb repository for the given entity class.
463 */
464 getMongoRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): MongoRepository<Entity>;
465 /**
466 * Gets custom entity repository marked with @EntityRepository decorator.
467 */
468 getCustomRepository<T>(customRepository: ObjectType<T>): T;
469 /**
470 * Releases all resources used by entity manager.
471 * This is used when entity manager is created with a single query runner,
472 * and this single query runner needs to be released after job with entity manager is done.
473 */
474 release(): Promise<void>;
475}