forked from Nmuta/nest_typeorm-postgres
-
Notifications
You must be signed in to change notification settings - Fork 23
/
repository.ts
220 lines (217 loc) · 9.65 KB
/
repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// THIS IS SIMPLY A DUPLICATE OF THE REPOSITORY FILE FOUND IN node_modules/typeorm/repository/Repository.d.ts
// It is included here for educational purposes and is not meant to be a part of the actual working application.
import { EntityMetadata } from "../metadata/EntityMetadata";
import { FindManyOptions } from "../find-options/FindManyOptions";
import { ObjectLiteral } from "../common/ObjectLiteral";
import { FindOneOptions } from "../find-options/FindOneOptions";
import { DeepPartial } from "../common/DeepPartial";
import { SaveOptions } from "./SaveOptions";
import { RemoveOptions } from "./RemoveOptions";
import { EntityManager } from "../entity-manager/EntityManager";
import { QueryRunner } from "../query-runner/QueryRunner";
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder";
import { DeleteResult } from "../query-builder/result/DeleteResult";
import { UpdateResult } from "../query-builder/result/UpdateResult";
import { InsertResult } from "../query-builder/result/InsertResult";
import { QueryDeepPartialEntity } from "../query-builder/QueryPartialEntity";
import { ObjectID } from "../driver/mongodb/typings";
import { FindConditions } from "../find-options/FindConditions";
/**
* Repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc.
*/
export declare class Repository<Entity extends ObjectLiteral> {
/**
* Entity Manager used by this repository.
*/
readonly manager: EntityManager;
/**
* Entity metadata of the entity current repository manages.
*/
readonly metadata: EntityMetadata;
/**
* Query runner provider used for this repository.
*/
readonly queryRunner?: QueryRunner;
/**
* Creates a new query builder that can be used to build a sql query.
*/
createQueryBuilder(alias?: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
/**
* Returns object that is managed by this repository.
* If this repository manages entity from schema,
* then it returns a name of that schema instead.
*/
readonly target: Function | string;
/**
* Checks if entity has an id.
* If entity composite compose ids, it will check them all.
*/
hasId(entity: Entity): boolean;
/**
* Gets entity mixed id.
*/
getId(entity: Entity): any;
/**
* Creates a new entity instance.
*/
create(): Entity;
/**
* Creates a new entities and copies all entity properties from given objects into their new entities.
* Note that it copies only properties that present in entity schema.
*/
create(entityLikeArray: DeepPartial<Entity>[]): Entity[];
/**
* Creates a new entity instance and copies all entity properties from this object into a new entity.
* Note that it copies only properties that present in entity schema.
*/
create(entityLike: DeepPartial<Entity>): Entity;
/**
* Merges multiple entities (or entity-like objects) into a given entity.
*/
merge(mergeIntoEntity: Entity, ...entityLikes: DeepPartial<Entity>[]): Entity;
/**
* Creates a new entity from the given plan javascript object. If entity already exist in the database, then
* it loads it (and everything related to it), replaces all values with the new ones from the given object
* and returns this new entity. This new entity is actually a loaded from the db entity with all properties
* replaced from the new object.
*
* 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.
*/
preload(entityLike: DeepPartial<Entity>): Promise<Entity | undefined>;
/**
* Saves all given entities in the database.
* If entities do not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entities: T[], options: SaveOptions & {
reload: false;
}): Promise<T[]>;
/**
* Saves all given entities in the database.
* If entities do not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entities: T[], options?: SaveOptions): Promise<(T & Entity)[]>;
/**
* Saves a given entity in the database.
* If entity does not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entity: T, options: SaveOptions & {
reload: false;
}): Promise<T>;
/**
* Saves a given entity in the database.
* If entity does not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>;
/**
* Removes a given entities from the database.
*/
remove(entities: Entity[], options?: RemoveOptions): Promise<Entity[]>;
/**
* Removes a given entity from the database.
*/
remove(entity: Entity, options?: RemoveOptions): Promise<Entity>;
/**
* Inserts a given entity into the database.
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
* Executes fast and efficient INSERT query.
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
*/
insert(entity: QueryDeepPartialEntity<Entity> | (QueryDeepPartialEntity<Entity>[])): Promise<InsertResult>;
/**
* Updates entity partially. Entity can be found by a given conditions.
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
* Executes fast and efficient UPDATE query.
* Does not check if entity exist in the database.
*/
update(criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindConditions<Entity>, partialEntity: QueryDeepPartialEntity<Entity>): Promise<UpdateResult>;
/**
* Deletes entities by a given criteria.
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
* Executes fast and efficient DELETE query.
* Does not check if entity exist in the database.
*/
delete(criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindConditions<Entity>): Promise<DeleteResult>;
/**
* Counts entities that match given options.
*/
count(options?: FindManyOptions<Entity>): Promise<number>;
/**
* Counts entities that match given conditions.
*/
count(conditions?: FindConditions<Entity>): Promise<number>;
/**
* Finds entities that match given options.
*/
find(options?: FindManyOptions<Entity>): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find(conditions?: FindConditions<Entity>): Promise<Entity[]>;
/**
* Finds entities that match given find options.
* Also counts all entities that match given conditions,
* but ignores pagination settings (from and take options).
*/
findAndCount(options?: FindManyOptions<Entity>): Promise<[Entity[], number]>;
/**
* Finds entities that match given conditions.
* Also counts all entities that match given conditions,
* but ignores pagination settings (from and take options).
*/
findAndCount(conditions?: FindConditions<Entity>): Promise<[Entity[], number]>;
/**
* Finds entities by ids.
* Optionally find options can be applied.
*/
findByIds(ids: any[], options?: FindManyOptions<Entity>): Promise<Entity[]>;
/**
* Finds entities by ids.
* Optionally conditions can be applied.
*/
findByIds(ids: any[], conditions?: FindConditions<Entity>): Promise<Entity[]>;
/**
* Finds first entity that matches given options.
*/
findOne(id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
/**
* Finds first entity that matches given options.
*/
findOne(options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
/**
* Finds first entity that matches given conditions.
*/
findOne(conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity | undefined>;
/**
* Finds first entity that matches given options.
*/
findOneOrFail(id?: string | number | Date | ObjectID, options?: FindOneOptions<Entity>): Promise<Entity>;
/**
* Finds first entity that matches given options.
*/
findOneOrFail(options?: FindOneOptions<Entity>): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOneOrFail(conditions?: FindConditions<Entity>, options?: FindOneOptions<Entity>): Promise<Entity>;
/**
* Executes a raw SQL query and returns a raw database results.
* Raw query execution is supported only by relational databases (MongoDB is not supported).
*/
query(query: string, parameters?: any[]): Promise<any>;
/**
* Clears all the data from the given table/collection (truncates/drops it).
*
* Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms.
* @see https://stackoverflow.com/a/5972738/925151
*/
clear(): Promise<void>;
/**
* Increments some column by provided value of the entities matched given conditions.
*/
increment(conditions: FindConditions<Entity>, propertyPath: string, value: number | string): Promise<UpdateResult>;
/**
* Decrements some column by provided value of the entities matched given conditions.
*/
decrement(conditions: FindConditions<Entity>, propertyPath: string, value: number | string): Promise<UpdateResult>;
}