• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 19549987525

20 Nov 2025 08:11PM UTC coverage: 80.769% (+4.3%) from 76.433%
19549987525

push

github

web-flow
ci: run tests on commits to master and next (#11783)

Co-authored-by: Oleg "OSA413" Sokolov <OSA413@users.noreply.github.com>

26500 of 32174 branches covered (82.36%)

Branch coverage included in aggregate %.

91252 of 113615 relevant lines covered (80.32%)

88980.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.21
/src/repository/Repository.ts
1
import { FindManyOptions } from "../find-options/FindManyOptions"
26✔
2
import { ObjectLiteral } from "../common/ObjectLiteral"
26✔
3
import { FindOneOptions } from "../find-options/FindOneOptions"
26✔
4
import { DeepPartial } from "../common/DeepPartial"
26✔
5
import { SaveOptions } from "./SaveOptions"
26✔
6
import { RemoveOptions } from "./RemoveOptions"
26✔
7
import { EntityManager } from "../entity-manager/EntityManager"
26✔
8
import { QueryRunner } from "../query-runner/QueryRunner"
26✔
9
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder"
26✔
10
import { DeleteResult } from "../query-builder/result/DeleteResult"
26✔
11
import { UpdateResult } from "../query-builder/result/UpdateResult"
26✔
12
import { InsertResult } from "../query-builder/result/InsertResult"
26✔
13
import { QueryDeepPartialEntity } from "../query-builder/QueryPartialEntity"
26✔
14
import { ObjectId } from "../driver/mongodb/typings"
26✔
15
import { FindOptionsWhere } from "../find-options/FindOptionsWhere"
26✔
16
import { UpsertOptions } from "./UpsertOptions"
26✔
17
import { EntityTarget } from "../common/EntityTarget"
26✔
18
import { PickKeysByType } from "../common/PickKeysByType"
26✔
19
import { buildSqlTag } from "../util/SqlTagUtils"
26✔
20

26✔
21
/**
26✔
22
 * Repository is supposed to work with your entity objects. Find entities, insert, update, delete, etc.
26✔
23
 */
26✔
24
export class Repository<Entity extends ObjectLiteral> {
26✔
25
    // -------------------------------------------------------------------------
26✔
26
    // Public Properties
26✔
27
    // -------------------------------------------------------------------------
26✔
28

26✔
29
    /**
26✔
30
     * Entity target that is managed by this repository.
26✔
31
     * If this repository manages entity from schema,
26✔
32
     * then it returns a name of that schema instead.
26✔
33
     */
26✔
34
    readonly target: EntityTarget<Entity>
26✔
35

26✔
36
    /**
26✔
37
     * Entity Manager used by this repository.
26✔
38
     */
26✔
39
    readonly manager: EntityManager
26✔
40

26✔
41
    /**
26✔
42
     * Query runner provider used for this repository.
26✔
43
     */
26✔
44
    readonly queryRunner?: QueryRunner
26✔
45

26✔
46
    // -------------------------------------------------------------------------
26✔
47
    // Accessors
26✔
48
    // -------------------------------------------------------------------------
26✔
49

26✔
50
    /**
26✔
51
     * Entity metadata of the entity current repository manages.
26✔
52
     */
26✔
53
    get metadata() {
26✔
54
        return this.manager.connection.getMetadata(this.target)
683,061✔
55
    }
683,061✔
56

26✔
57
    // -------------------------------------------------------------------------
26✔
58
    // Constructor
26✔
59
    // -------------------------------------------------------------------------
26✔
60

26✔
61
    constructor(
26✔
62
        target: EntityTarget<Entity>,
127,477✔
63
        manager: EntityManager,
127,477✔
64
        queryRunner?: QueryRunner,
127,477✔
65
    ) {
127,477✔
66
        this.target = target
127,477✔
67
        this.manager = manager
127,477✔
68
        this.queryRunner = queryRunner
127,477✔
69
    }
127,477✔
70

26✔
71
    // -------------------------------------------------------------------------
26✔
72
    // Public Methods
26✔
73
    // -------------------------------------------------------------------------
26✔
74

26✔
75
    /**
26✔
76
     * Creates a new query builder that can be used to build a SQL query.
26✔
77
     */
26✔
78
    createQueryBuilder(
26✔
79
        alias?: string,
406,706✔
80
        queryRunner?: QueryRunner,
406,706✔
81
    ): SelectQueryBuilder<Entity> {
406,706✔
82
        return this.manager.createQueryBuilder<Entity>(
406,706✔
83
            this.metadata.target as any,
406,706✔
84
            alias || this.metadata.targetName,
406,706✔
85
            queryRunner || this.queryRunner,
406,706✔
86
        )
406,706✔
87
    }
406,706✔
88

26✔
89
    /**
26✔
90
     * Checks if entity has an id.
26✔
91
     * If entity composite compose ids, it will check them all.
26✔
92
     */
26✔
93
    hasId(entity: Entity): boolean {
26✔
94
        return this.manager.hasId(this.metadata.target, entity)
366✔
95
    }
366✔
96

26✔
97
    /**
26✔
98
     * Gets entity mixed id.
26✔
99
     */
26✔
100
    getId(entity: Entity): any {
26✔
101
        return this.manager.getId(this.metadata.target, entity)
×
102
    }
×
103

26✔
104
    /**
26✔
105
     * Creates a new entity instance.
26✔
106
     */
26✔
107
    create(): Entity
26✔
108

26✔
109
    /**
26✔
110
     * Creates new entities and copies all entity properties from given objects into their new entities.
26✔
111
     * Note that it copies only properties that are present in entity schema.
26✔
112
     */
26✔
113
    create(entityLikeArray: DeepPartial<Entity>[]): Entity[]
26✔
114

26✔
115
    /**
26✔
116
     * Creates a new entity instance and copies all entity properties from this object into a new entity.
26✔
117
     * Note that it copies only properties that are present in entity schema.
26✔
118
     */
26✔
119
    create(entityLike: DeepPartial<Entity>): Entity
26✔
120

26✔
121
    /**
26✔
122
     * Creates a new entity instance or instances.
26✔
123
     * Can copy properties from the given object into new entities.
26✔
124
     */
26✔
125
    create(
26✔
126
        plainEntityLikeOrPlainEntityLikes?:
3,840✔
127
            | DeepPartial<Entity>
3,840✔
128
            | DeepPartial<Entity>[],
3,840✔
129
    ): Entity | Entity[] {
3,840✔
130
        return this.manager.create(
3,840✔
131
            this.metadata.target as any,
3,840✔
132
            plainEntityLikeOrPlainEntityLikes as any,
3,840✔
133
        )
3,840✔
134
    }
3,840✔
135

26✔
136
    /**
26✔
137
     * Merges multiple entities (or entity-like objects) into a given entity.
26✔
138
     */
26✔
139
    merge(
26✔
140
        mergeIntoEntity: Entity,
60✔
141
        ...entityLikes: DeepPartial<Entity>[]
60✔
142
    ): Entity {
60✔
143
        return this.manager.merge(
60✔
144
            this.metadata.target as any,
60✔
145
            mergeIntoEntity,
60✔
146
            ...entityLikes,
60✔
147
        )
60✔
148
    }
60✔
149

26✔
150
    /**
26✔
151
     * Creates a new entity from the given plain javascript object. If entity already exist in the database, then
26✔
152
     * it loads it (and everything related to it), replaces all values with the new ones from the given object
26✔
153
     * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
26✔
154
     * replaced from the new object.
26✔
155
     *
26✔
156
     * Note that given entity-like object must have an entity id / primary key to find entity by.
26✔
157
     * Returns undefined if entity with given id was not found.
26✔
158
     */
26✔
159
    preload(entityLike: DeepPartial<Entity>): Promise<Entity | undefined> {
26✔
160
        return this.manager.preload(this.metadata.target as any, entityLike)
90✔
161
    }
90✔
162

26✔
163
    /**
26✔
164
     * Saves all given entities in the database.
26✔
165
     * If entities do not exist in the database then inserts, otherwise updates.
26✔
166
     */
26✔
167
    save<T extends DeepPartial<Entity>>(
26✔
168
        entities: T[],
26✔
169
        options: SaveOptions & { reload: false },
26✔
170
    ): Promise<T[]>
26✔
171

26✔
172
    /**
26✔
173
     * Saves all given entities in the database.
26✔
174
     * If entities do not exist in the database then inserts, otherwise updates.
26✔
175
     */
26✔
176
    save<T extends DeepPartial<Entity>>(
26✔
177
        entities: T[],
26✔
178
        options?: SaveOptions,
26✔
179
    ): Promise<(T & Entity)[]>
26✔
180

26✔
181
    /**
26✔
182
     * Saves a given entity in the database.
26✔
183
     * If entity does not exist in the database then inserts, otherwise updates.
26✔
184
     */
26✔
185
    save<T extends DeepPartial<Entity>>(
26✔
186
        entity: T,
26✔
187
        options: SaveOptions & { reload: false },
26✔
188
    ): Promise<T>
26✔
189

26✔
190
    /**
26✔
191
     * Saves a given entity in the database.
26✔
192
     * If entity does not exist in the database then inserts, otherwise updates.
26✔
193
     */
26✔
194
    save<T extends DeepPartial<Entity>>(
26✔
195
        entity: T,
26✔
196
        options?: SaveOptions,
26✔
197
    ): Promise<T & Entity>
26✔
198

26✔
199
    /**
26✔
200
     * Saves one or many given entities.
26✔
201
     */
26✔
202
    save<T extends DeepPartial<Entity>>(
26✔
203
        entityOrEntities: T | T[],
94,616✔
204
        options?: SaveOptions,
94,616✔
205
    ): Promise<T | T[]> {
94,616✔
206
        return this.manager.save<Entity, T>(
94,616✔
207
            this.metadata.target as any,
94,616✔
208
            entityOrEntities as any,
94,616✔
209
            options,
94,616✔
210
        )
94,616✔
211
    }
94,616✔
212

26✔
213
    /**
26✔
214
     * Removes a given entities from the database.
26✔
215
     */
26✔
216
    remove(entities: Entity[], options?: RemoveOptions): Promise<Entity[]>
26✔
217

26✔
218
    /**
26✔
219
     * Removes a given entity from the database.
26✔
220
     */
26✔
221
    remove(entity: Entity, options?: RemoveOptions): Promise<Entity>
26✔
222

26✔
223
    /**
26✔
224
     * Removes one or many given entities.
26✔
225
     */
26✔
226
    remove(
26✔
227
        entityOrEntities: Entity | Entity[],
862✔
228
        options?: RemoveOptions,
862✔
229
    ): Promise<Entity | Entity[]> {
862✔
230
        return this.manager.remove(
862✔
231
            this.metadata.target as any,
862✔
232
            entityOrEntities as any,
862✔
233
            options,
862✔
234
        )
862✔
235
    }
862✔
236

26✔
237
    /**
26✔
238
     * Records the delete date of all given entities.
26✔
239
     */
26✔
240
    softRemove<T extends DeepPartial<Entity>>(
26✔
241
        entities: T[],
26✔
242
        options: SaveOptions & { reload: false },
26✔
243
    ): Promise<T[]>
26✔
244

26✔
245
    /**
26✔
246
     * Records the delete date of all given entities.
26✔
247
     */
26✔
248
    softRemove<T extends DeepPartial<Entity>>(
26✔
249
        entities: T[],
26✔
250
        options?: SaveOptions,
26✔
251
    ): Promise<(T & Entity)[]>
26✔
252

26✔
253
    /**
26✔
254
     * Records the delete date of a given entity.
26✔
255
     */
26✔
256
    softRemove<T extends DeepPartial<Entity>>(
26✔
257
        entity: T,
26✔
258
        options: SaveOptions & { reload: false },
26✔
259
    ): Promise<T>
26✔
260

26✔
261
    /**
26✔
262
     * Records the delete date of a given entity.
26✔
263
     */
26✔
264
    softRemove<T extends DeepPartial<Entity>>(
26✔
265
        entity: T,
26✔
266
        options?: SaveOptions,
26✔
267
    ): Promise<T & Entity>
26✔
268

26✔
269
    /**
26✔
270
     * Records the delete date of one or many given entities.
26✔
271
     */
26✔
272
    softRemove<T extends DeepPartial<Entity>>(
26✔
273
        entityOrEntities: T | T[],
118✔
274
        options?: SaveOptions,
118✔
275
    ): Promise<T | T[]> {
118✔
276
        return this.manager.softRemove<Entity, T>(
118✔
277
            this.metadata.target as any,
118✔
278
            entityOrEntities as any,
118✔
279
            options,
118✔
280
        )
118✔
281
    }
118✔
282

26✔
283
    /**
26✔
284
     * Recovers all given entities in the database.
26✔
285
     */
26✔
286
    recover<T extends DeepPartial<Entity>>(
26✔
287
        entities: T[],
26✔
288
        options: SaveOptions & { reload: false },
26✔
289
    ): Promise<T[]>
26✔
290

26✔
291
    /**
26✔
292
     * Recovers all given entities in the database.
26✔
293
     */
26✔
294
    recover<T extends DeepPartial<Entity>>(
26✔
295
        entities: T[],
26✔
296
        options?: SaveOptions,
26✔
297
    ): Promise<(T & Entity)[]>
26✔
298

26✔
299
    /**
26✔
300
     * Recovers a given entity in the database.
26✔
301
     */
26✔
302
    recover<T extends DeepPartial<Entity>>(
26✔
303
        entity: T,
26✔
304
        options: SaveOptions & { reload: false },
26✔
305
    ): Promise<T>
26✔
306

26✔
307
    /**
26✔
308
     * Recovers a given entity in the database.
26✔
309
     */
26✔
310
    recover<T extends DeepPartial<Entity>>(
26✔
311
        entity: T,
26✔
312
        options?: SaveOptions,
26✔
313
    ): Promise<T & Entity>
26✔
314

26✔
315
    /**
26✔
316
     * Recovers one or many given entities.
26✔
317
     */
26✔
318
    recover<T extends DeepPartial<Entity>>(
26✔
319
        entityOrEntities: T | T[],
112✔
320
        options?: SaveOptions,
112✔
321
    ): Promise<T | T[]> {
112✔
322
        return this.manager.recover<Entity, T>(
112✔
323
            this.metadata.target as any,
112✔
324
            entityOrEntities as any,
112✔
325
            options,
112✔
326
        )
112✔
327
    }
112✔
328

26✔
329
    /**
26✔
330
     * Inserts a given entity into the database.
26✔
331
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
332
     * Executes fast and efficient INSERT query.
26✔
333
     * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
26✔
334
     */
26✔
335
    insert(
26✔
336
        entity:
371✔
337
            | QueryDeepPartialEntity<Entity>
371✔
338
            | QueryDeepPartialEntity<Entity>[],
371✔
339
    ): Promise<InsertResult> {
371✔
340
        return this.manager.insert(this.metadata.target as any, entity)
371✔
341
    }
371✔
342

26✔
343
    /**
26✔
344
     * Updates entity partially. Entity can be found by a given conditions.
26✔
345
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
346
     * Executes fast and efficient UPDATE query.
26✔
347
     * Does not check if entity exist in the database.
26✔
348
     */
26✔
349
    update(
26✔
350
        criteria:
315✔
351
            | string
315✔
352
            | string[]
315✔
353
            | number
315✔
354
            | number[]
315✔
355
            | Date
315✔
356
            | Date[]
315✔
357
            | ObjectId
315✔
358
            | ObjectId[]
315✔
359
            | FindOptionsWhere<Entity>
315✔
360
            | FindOptionsWhere<Entity>[],
315✔
361
        partialEntity: QueryDeepPartialEntity<Entity>,
315✔
362
    ): Promise<UpdateResult> {
315✔
363
        return this.manager.update(
315✔
364
            this.metadata.target,
315✔
365
            criteria,
315✔
366
            partialEntity,
315✔
367
        )
315✔
368
    }
315✔
369

26✔
370
    /**
26✔
371
     * Updates all entities of target type, setting fields from supplied partial entity.
26✔
372
     * This is a primitive operation without cascades, relations or other operations included.
26✔
373
     * Executes fast and efficient UPDATE query without WHERE clause.
26✔
374
     *
26✔
375
     * WARNING! This method updates ALL rows in the target table.
26✔
376
     */
26✔
377
    updateAll(
26✔
378
        partialEntity: QueryDeepPartialEntity<Entity>,
28✔
379
    ): Promise<UpdateResult> {
28✔
380
        return this.manager.updateAll(this.metadata.target, partialEntity)
28✔
381
    }
28✔
382

26✔
383
    /**
26✔
384
     * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity
26✔
385
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
386
     * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.
26✔
387
     */
26✔
388
    upsert(
26✔
389
        entityOrEntities:
574✔
390
            | QueryDeepPartialEntity<Entity>
574✔
391
            | QueryDeepPartialEntity<Entity>[],
574✔
392
        conflictPathsOrOptions: string[] | UpsertOptions<Entity>,
574✔
393
    ): Promise<InsertResult> {
574✔
394
        return this.manager.upsert(
574✔
395
            this.metadata.target as any,
574✔
396
            entityOrEntities,
574✔
397
            conflictPathsOrOptions,
574✔
398
        )
574✔
399
    }
574✔
400

26✔
401
    /**
26✔
402
     * Deletes entities by a given criteria.
26✔
403
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
404
     * Executes fast and efficient DELETE query.
26✔
405
     * Does not check if entity exist in the database.
26✔
406
     */
26✔
407
    delete(
26✔
408
        criteria:
242✔
409
            | string
242✔
410
            | string[]
242✔
411
            | number
242✔
412
            | number[]
242✔
413
            | Date
242✔
414
            | Date[]
242✔
415
            | ObjectId
242✔
416
            | ObjectId[]
242✔
417
            | FindOptionsWhere<Entity>
242✔
418
            | FindOptionsWhere<Entity>[],
242✔
419
    ): Promise<DeleteResult> {
242✔
420
        return this.manager.delete(this.metadata.target, criteria)
242✔
421
    }
242✔
422

26✔
423
    /**
26✔
424
     * Deletes all entities of target type.
26✔
425
     * This is a primitive operation without cascades, relations or other operations included.
26✔
426
     * Executes fast and efficient DELETE query without WHERE clause.
26✔
427
     *
26✔
428
     * WARNING! This method deletes ALL rows in the target table.
26✔
429
     */
26✔
430
    deleteAll(): Promise<DeleteResult> {
26✔
431
        return this.manager.deleteAll(this.metadata.target)
28✔
432
    }
28✔
433

26✔
434
    /**
26✔
435
     * Records the delete date of entities by a given criteria.
26✔
436
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
437
     * Executes fast and efficient UPDATE query.
26✔
438
     * Does not check if entity exist in the database.
26✔
439
     */
26✔
440
    softDelete(
26✔
441
        criteria:
112✔
442
            | string
112✔
443
            | string[]
112✔
444
            | number
112✔
445
            | number[]
112✔
446
            | Date
112✔
447
            | Date[]
112✔
448
            | ObjectId
112✔
449
            | ObjectId[]
112✔
450
            | FindOptionsWhere<Entity>
112✔
451
            | FindOptionsWhere<Entity>[],
112✔
452
    ): Promise<UpdateResult> {
112✔
453
        return this.manager.softDelete(
112✔
454
            this.metadata.target as any,
112✔
455
            criteria as any,
112✔
456
        )
112✔
457
    }
112✔
458

26✔
459
    /**
26✔
460
     * Restores entities by a given criteria.
26✔
461
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
462
     * Executes fast and efficient UPDATE query.
26✔
463
     * Does not check if entity exist in the database.
26✔
464
     */
26✔
465
    restore(
26✔
466
        criteria:
56✔
467
            | string
56✔
468
            | string[]
56✔
469
            | number
56✔
470
            | number[]
56✔
471
            | Date
56✔
472
            | Date[]
56✔
473
            | ObjectId
56✔
474
            | ObjectId[]
56✔
475
            | FindOptionsWhere<Entity>
56✔
476
            | FindOptionsWhere<Entity>[],
56✔
477
    ): Promise<UpdateResult> {
56✔
478
        return this.manager.restore(
56✔
479
            this.metadata.target as any,
56✔
480
            criteria as any,
56✔
481
        )
56✔
482
    }
56✔
483

26✔
484
    /**
26✔
485
     * Checks whether any entity exists that matches the given options.
26✔
486
     *
26✔
487
     * @deprecated use `exists` method instead, for example:
26✔
488
     *
26✔
489
     * .exists()
26✔
490
     */
26✔
491
    exist(options?: FindManyOptions<Entity>): Promise<boolean> {
26✔
492
        return this.manager.exists(this.metadata.target, options)
×
493
    }
×
494

26✔
495
    /**
26✔
496
     * Checks whether any entity exists that matches the given options.
26✔
497
     */
26✔
498
    exists(options?: FindManyOptions<Entity>): Promise<boolean> {
26✔
499
        return this.manager.exists(this.metadata.target, options)
196✔
500
    }
196✔
501

26✔
502
    /**
26✔
503
     * Checks whether any entity exists that matches the given conditions.
26✔
504
     */
26✔
505
    existsBy(
26✔
506
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
×
507
    ): Promise<boolean> {
×
508
        return this.manager.existsBy(this.metadata.target, where)
×
509
    }
×
510

26✔
511
    /**
26✔
512
     * Counts entities that match given options.
26✔
513
     * Useful for pagination.
26✔
514
     */
26✔
515
    count(options?: FindManyOptions<Entity>): Promise<number> {
26✔
516
        return this.manager.count(this.metadata.target, options)
416✔
517
    }
416✔
518

26✔
519
    /**
26✔
520
     * Counts entities that match given conditions.
26✔
521
     * Useful for pagination.
26✔
522
     */
26✔
523
    countBy(
26✔
524
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
56✔
525
    ): Promise<number> {
56✔
526
        return this.manager.countBy(this.metadata.target, where)
56✔
527
    }
56✔
528

26✔
529
    /**
26✔
530
     * Return the SUM of a column
26✔
531
     */
26✔
532
    sum(
26✔
533
        columnName: PickKeysByType<Entity, number>,
56✔
534
        where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
56✔
535
    ): Promise<number | null> {
56✔
536
        return this.manager.sum(this.metadata.target, columnName, where)
56✔
537
    }
56✔
538

26✔
539
    /**
26✔
540
     * Return the AVG of a column
26✔
541
     */
26✔
542
    average(
26✔
543
        columnName: PickKeysByType<Entity, number>,
56✔
544
        where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
56✔
545
    ): Promise<number | null> {
56✔
546
        return this.manager.average(this.metadata.target, columnName, where)
56✔
547
    }
56✔
548

26✔
549
    /**
26✔
550
     * Return the MIN of a column
26✔
551
     */
26✔
552
    minimum(
26✔
553
        columnName: PickKeysByType<Entity, number>,
56✔
554
        where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
56✔
555
    ): Promise<number | null> {
56✔
556
        return this.manager.minimum(this.metadata.target, columnName, where)
56✔
557
    }
56✔
558

26✔
559
    /**
26✔
560
     * Return the MAX of a column
26✔
561
     */
26✔
562
    maximum(
26✔
563
        columnName: PickKeysByType<Entity, number>,
56✔
564
        where?: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
56✔
565
    ): Promise<number | null> {
56✔
566
        return this.manager.maximum(this.metadata.target, columnName, where)
56✔
567
    }
56✔
568

26✔
569
    /**
26✔
570
     * Finds entities that match given find options.
26✔
571
     */
26✔
572
    async find(options?: FindManyOptions<Entity>): Promise<Entity[]> {
26✔
573
        return this.manager.find(this.metadata.target, options)
3,424✔
574
    }
3,424✔
575

26✔
576
    /**
26✔
577
     * Finds entities that match given find options.
26✔
578
     */
26✔
579
    async findBy(
26✔
580
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
1,172✔
581
    ): Promise<Entity[]> {
1,172✔
582
        return this.manager.findBy(this.metadata.target, where)
1,172✔
583
    }
1,172✔
584

26✔
585
    /**
26✔
586
     * Finds entities that match given find options.
26✔
587
     * Also counts all entities that match given conditions,
26✔
588
     * but ignores pagination settings (from and take options).
26✔
589
     */
26✔
590
    findAndCount(
26✔
591
        options?: FindManyOptions<Entity>,
336✔
592
    ): Promise<[Entity[], number]> {
336✔
593
        return this.manager.findAndCount(this.metadata.target, options)
336✔
594
    }
336✔
595

26✔
596
    /**
26✔
597
     * Finds entities that match given WHERE conditions.
26✔
598
     * Also counts all entities that match given conditions,
26✔
599
     * but ignores pagination settings (from and take options).
26✔
600
     */
26✔
601
    findAndCountBy(
26✔
602
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
×
603
    ): Promise<[Entity[], number]> {
×
604
        return this.manager.findAndCountBy(this.metadata.target, where)
×
605
    }
×
606

26✔
607
    /**
26✔
608
     * Finds entities with ids.
26✔
609
     * Optionally find options or conditions can be applied.
26✔
610
     *
26✔
611
     * @deprecated use `findBy` method instead in conjunction with `In` operator, for example:
26✔
612
     *
26✔
613
     * .findBy({
26✔
614
     *     id: In([1, 2, 3])
26✔
615
     * })
26✔
616
     */
26✔
617
    async findByIds(ids: any[]): Promise<Entity[]> {
26✔
618
        return this.manager.findByIds(this.metadata.target, ids)
28✔
619
    }
28✔
620

26✔
621
    /**
26✔
622
     * Finds first entity by a given find options.
26✔
623
     * If entity was not found in the database - returns null.
26✔
624
     */
26✔
625
    async findOne(options: FindOneOptions<Entity>): Promise<Entity | null> {
26✔
626
        return this.manager.findOne(this.metadata.target, options)
2,669✔
627
    }
2,669✔
628

26✔
629
    /**
26✔
630
     * Finds first entity that matches given where condition.
26✔
631
     * If entity was not found in the database - returns null.
26✔
632
     */
26✔
633
    async findOneBy(
26✔
634
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
3,468✔
635
    ): Promise<Entity | null> {
3,468✔
636
        return this.manager.findOneBy(this.metadata.target, where)
3,468✔
637
    }
3,468✔
638

26✔
639
    /**
26✔
640
     * Finds first entity that matches given id.
26✔
641
     * If entity was not found in the database - returns null.
26✔
642
     *
26✔
643
     * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example:
26✔
644
     *
26✔
645
     * .findOneBy({
26✔
646
     *     id: 1 // where "id" is your primary column name
26✔
647
     * })
26✔
648
     */
26✔
649
    async findOneById(
26✔
650
        id: number | string | Date | ObjectId,
112✔
651
    ): Promise<Entity | null> {
112✔
652
        return this.manager.findOneById(this.metadata.target, id)
112✔
653
    }
112✔
654

26✔
655
    /**
26✔
656
     * Finds first entity by a given find options.
26✔
657
     * If entity was not found in the database - rejects with error.
26✔
658
     */
26✔
659
    async findOneOrFail(options: FindOneOptions<Entity>): Promise<Entity> {
26✔
660
        return this.manager.findOneOrFail(this.metadata.target, options)
378✔
661
    }
378✔
662

26✔
663
    /**
26✔
664
     * Finds first entity that matches given where condition.
26✔
665
     * If entity was not found in the database - rejects with error.
26✔
666
     */
26✔
667
    async findOneByOrFail(
26✔
668
        where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
954✔
669
    ): Promise<Entity> {
954✔
670
        return this.manager.findOneByOrFail(this.metadata.target, where)
954✔
671
    }
954✔
672

26✔
673
    /**
26✔
674
     * Executes a raw SQL query and returns a raw database results.
26✔
675
     * Raw query execution is supported only by relational databases (MongoDB is not supported).
26✔
676
     *
26✔
677
     * @see [Official docs](https://typeorm.io/repository-api) for examples.
26✔
678
     */
26✔
679
    query<T = any>(query: string, parameters?: any[]): Promise<T> {
26✔
680
        return this.manager.query(query, parameters)
32✔
681
    }
32✔
682

26✔
683
    /**
26✔
684
     * Tagged template function that executes raw SQL query and returns raw database results.
26✔
685
     * Template expressions are automatically transformed into database parameters.
26✔
686
     * Raw query execution is supported only by relational databases (MongoDB is not supported).
26✔
687
     * Note: Don't call this as a regular function, it is meant to be used with backticks to tag a template literal.
26✔
688
     * Example: repository.sql`SELECT * FROM table_name WHERE id = ${id}`
26✔
689
     */
26✔
690
    async sql<T = any>(
26✔
691
        strings: TemplateStringsArray,
×
692
        ...values: unknown[]
×
693
    ): Promise<T> {
×
694
        const { query, parameters } = buildSqlTag({
×
695
            driver: this.manager.connection.driver,
×
696
            strings: strings,
×
697
            expressions: values,
×
698
        })
×
699

×
700
        return await this.query(query, parameters)
×
701
    }
×
702

26✔
703
    /**
26✔
704
     * Clears all the data from the given table/collection (truncates/drops it).
26✔
705
     *
26✔
706
     * Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms.
26✔
707
     * @see https://stackoverflow.com/a/5972738/925151
26✔
708
     */
26✔
709
    clear(): Promise<void> {
26✔
710
        return this.manager.clear(this.metadata.target)
48✔
711
    }
48✔
712

26✔
713
    /**
26✔
714
     * Increments some column by provided value of the entities matched given conditions.
26✔
715
     */
26✔
716
    increment(
26✔
717
        conditions: FindOptionsWhere<Entity>,
182✔
718
        propertyPath: string,
182✔
719
        value: number | string,
182✔
720
    ): Promise<UpdateResult> {
182✔
721
        return this.manager.increment(
182✔
722
            this.metadata.target,
182✔
723
            conditions,
182✔
724
            propertyPath,
182✔
725
            value,
182✔
726
        )
182✔
727
    }
182✔
728

26✔
729
    /**
26✔
730
     * Decrements some column by provided value of the entities matched given conditions.
26✔
731
     */
26✔
732
    decrement(
26✔
733
        conditions: FindOptionsWhere<Entity>,
180✔
734
        propertyPath: string,
180✔
735
        value: number | string,
180✔
736
    ): Promise<UpdateResult> {
180✔
737
        return this.manager.decrement(
180✔
738
            this.metadata.target,
180✔
739
            conditions,
180✔
740
            propertyPath,
180✔
741
            value,
180✔
742
        )
180✔
743
    }
180✔
744

26✔
745
    /**
26✔
746
     * Extends repository with provided functions.
26✔
747
     */
26✔
748
    extend<CustomRepository>(
26✔
749
        customs: CustomRepository & ThisType<this & CustomRepository>,
32✔
750
    ): this & CustomRepository {
32✔
751
        // return {
32✔
752
        //     ...this,
32✔
753
        //     ...custom
32✔
754
        // };
32✔
755
        const thisRepo: any = this.constructor
32✔
756
        const { target, manager, queryRunner } = this
32✔
757
        const ChildClass = class extends thisRepo {
32✔
758
            constructor(
32✔
759
                target: EntityTarget<Entity>,
68✔
760
                manager: EntityManager,
68✔
761
                queryRunner?: QueryRunner,
68✔
762
            ) {
68✔
763
                super(target, manager, queryRunner)
68✔
764
            }
68✔
765
        }
32✔
766
        for (const custom in customs)
32✔
767
            ChildClass.prototype[custom] = customs[custom]
32✔
768
        return new ChildClass(target, manager, queryRunner) as any
32✔
769
    }
32✔
770
}
26✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc