• 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

75.0
/src/repository/BaseEntity.ts
1
import { Repository } from "./Repository"
26✔
2
import { FindOptionsWhere } from "../find-options/FindOptionsWhere"
26✔
3
import { DeepPartial } from "../common/DeepPartial"
26✔
4
import { SaveOptions } from "./SaveOptions"
26✔
5
import { FindOneOptions } from "../find-options/FindOneOptions"
26✔
6
import { RemoveOptions } from "./RemoveOptions"
26✔
7
import { FindManyOptions } from "../find-options/FindManyOptions"
26✔
8
import { DataSource } from "../data-source"
26✔
9
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder"
26✔
10
import { InsertResult } from "../query-builder/result/InsertResult"
26✔
11
import { UpdateResult } from "../query-builder/result/UpdateResult"
26✔
12
import { DeleteResult } from "../query-builder/result/DeleteResult"
26✔
13
import { ObjectId } from "../driver/mongodb/typings"
26✔
14
import { ObjectUtils } from "../util/ObjectUtils"
26✔
15
import { QueryDeepPartialEntity } from "../query-builder/QueryPartialEntity"
26✔
16
import { UpsertOptions } from "./UpsertOptions"
26✔
17
import { EntityTarget } from "../common/EntityTarget"
26✔
18
import { PickKeysByType } from "../common/PickKeysByType"
26✔
19

26✔
20
/**
26✔
21
 * Base abstract entity for all entities, used in ActiveRecord patterns.
26✔
22
 */
26✔
23
export class BaseEntity {
26✔
24
    // -------------------------------------------------------------------------
26✔
25
    // Private Static Properties
26✔
26
    // -------------------------------------------------------------------------
26✔
27

26✔
28
    /**
26✔
29
     * DataSource used in all static methods of the BaseEntity.
26✔
30
     */
26✔
31
    private static dataSource: DataSource | null
26✔
32

26✔
33
    // -------------------------------------------------------------------------
26✔
34
    // Public Methods
26✔
35
    // -------------------------------------------------------------------------
26✔
36

26✔
37
    /**
26✔
38
     * Checks if entity has an id.
26✔
39
     * If entity composite compose ids, it will check them all.
26✔
40
     */
26✔
41
    hasId(): boolean {
26✔
42
        const baseEntity = this.constructor as typeof BaseEntity
×
43
        return baseEntity.getRepository().hasId(this)
×
44
    }
×
45

26✔
46
    /**
26✔
47
     * Saves current entity in the database.
26✔
48
     * If entity does not exist in the database then inserts, otherwise updates.
26✔
49
     */
26✔
50
    save(options?: SaveOptions): Promise<this> {
26✔
51
        const baseEntity = this.constructor as typeof BaseEntity
272✔
52
        return baseEntity.getRepository().save(this, options)
272✔
53
    }
272✔
54

26✔
55
    /**
26✔
56
     * Removes current entity from the database.
26✔
57
     */
26✔
58
    remove(options?: RemoveOptions): Promise<this> {
26✔
59
        const baseEntity = this.constructor as typeof BaseEntity
×
60
        return baseEntity.getRepository().remove(this, options) as Promise<this>
×
61
    }
×
62

26✔
63
    /**
26✔
64
     * Records the delete date of current entity.
26✔
65
     */
26✔
66
    softRemove(options?: SaveOptions): Promise<this> {
26✔
67
        const baseEntity = this.constructor as typeof BaseEntity
×
68
        return baseEntity.getRepository().softRemove(this, options)
×
69
    }
×
70

26✔
71
    /**
26✔
72
     * Recovers a given entity in the database.
26✔
73
     */
26✔
74
    recover(options?: SaveOptions): Promise<this> {
26✔
75
        const baseEntity = this.constructor as typeof BaseEntity
×
76
        return baseEntity.getRepository().recover(this, options)
×
77
    }
×
78

26✔
79
    /**
26✔
80
     * Reloads entity data from the database.
26✔
81
     */
26✔
82
    async reload(): Promise<void> {
26✔
83
        const baseEntity = this.constructor as typeof BaseEntity
112✔
84
        const id = baseEntity.getRepository().metadata.getEntityIdMap(this)
112✔
85
        if (!id) {
112!
86
            throw new Error(
×
87
                `Entity doesn't have id-s set, cannot reload entity`,
×
88
            )
×
89
        }
×
90
        const reloadedEntity: BaseEntity = await baseEntity
112✔
91
            .getRepository()
112✔
92
            .findOneByOrFail(id)
112✔
93

112✔
94
        ObjectUtils.assign(this, reloadedEntity)
112✔
95
    }
112✔
96

26✔
97
    // -------------------------------------------------------------------------
26✔
98
    // Public Static Methods
26✔
99
    // -------------------------------------------------------------------------
26✔
100

26✔
101
    /**
26✔
102
     * Sets DataSource to be used by entity.
26✔
103
     */
26✔
104
    static useDataSource(dataSource: DataSource | null) {
26✔
105
        this.dataSource = dataSource
1,500✔
106
    }
1,500✔
107

26✔
108
    /**
26✔
109
     * Gets current entity's Repository.
26✔
110
     */
26✔
111
    static getRepository<T extends BaseEntity>(
26✔
112
        this: { new (): T } & typeof BaseEntity,
972✔
113
    ): Repository<T> {
972✔
114
        const dataSource = (this as typeof BaseEntity).dataSource
972✔
115
        if (!dataSource)
972✔
116
            throw new Error(`DataSource is not set for this entity.`)
972!
117
        return dataSource.getRepository<T>(this)
972✔
118
    }
972✔
119

26✔
120
    /**
26✔
121
     * Returns object that is managed by this repository.
26✔
122
     * If this repository manages entity from schema,
26✔
123
     * then it returns a name of that schema instead.
26✔
124
     */
26✔
125
    static get target(): EntityTarget<any> {
26✔
126
        return this.getRepository().target
×
127
    }
×
128

26✔
129
    /**
26✔
130
     * Checks entity has an id.
26✔
131
     * If entity composite compose ids, it will check them all.
26✔
132
     */
26✔
133
    static hasId(entity: BaseEntity): boolean {
26✔
134
        return this.getRepository().hasId(entity)
×
135
    }
×
136

26✔
137
    /**
26✔
138
     * Gets entity mixed id.
26✔
139
     */
26✔
140
    static getId<T extends BaseEntity>(
26✔
141
        this: { new (): T } & typeof BaseEntity,
×
142
        entity: T,
×
143
    ): any {
×
144
        return this.getRepository<T>().getId(entity)
×
145
    }
×
146

26✔
147
    /**
26✔
148
     * Creates a new query builder that can be used to build a SQL query.
26✔
149
     */
26✔
150
    static createQueryBuilder<T extends BaseEntity>(
26✔
151
        this: { new (): T } & typeof BaseEntity,
4✔
152
        alias?: string,
4✔
153
    ): SelectQueryBuilder<T> {
4✔
154
        return this.getRepository<T>().createQueryBuilder(alias)
4✔
155
    }
4✔
156

26✔
157
    /**
26✔
158
     * Creates a new entity instance.
26✔
159
     */
26✔
160
    static create<T extends BaseEntity>(
26✔
161
        this: { new (): T } & typeof BaseEntity,
26✔
162
    ): T
26✔
163

26✔
164
    /**
26✔
165
     * Creates a new entities and copies all entity properties from given objects into their new entities.
26✔
166
     * Note that it copies only properties that present in entity schema.
26✔
167
     */
26✔
168
    static create<T extends BaseEntity>(
26✔
169
        this: { new (): T } & typeof BaseEntity,
26✔
170
        entityLikeArray: DeepPartial<T>[],
26✔
171
    ): T[]
26✔
172

26✔
173
    /**
26✔
174
     * Creates a new entity instance and copies all entity properties from this object into a new entity.
26✔
175
     * Note that it copies only properties that present in entity schema.
26✔
176
     */
26✔
177
    static create<T extends BaseEntity>(
26✔
178
        this: { new (): T } & typeof BaseEntity,
26✔
179
        entityLike: DeepPartial<T>,
26✔
180
    ): T
26✔
181

26✔
182
    /**
26✔
183
     * Creates a new entity instance and copies all entity properties from this object into a new entity.
26✔
184
     * Note that it copies only properties that present in entity schema.
26✔
185
     */
26✔
186
    static create<T extends BaseEntity>(
26✔
187
        this: { new (): T } & typeof BaseEntity,
176✔
188
        entityOrEntities?: any,
176✔
189
    ) {
176✔
190
        return this.getRepository<T>().create(entityOrEntities)
176✔
191
    }
176✔
192

26✔
193
    /**
26✔
194
     * Merges multiple entities (or entity-like objects) into a given entity.
26✔
195
     */
26✔
196
    static merge<T extends BaseEntity>(
26✔
197
        this: { new (): T } & typeof BaseEntity,
×
198
        mergeIntoEntity: T,
×
199
        ...entityLikes: DeepPartial<T>[]
×
200
    ): T {
×
201
        return this.getRepository<T>().merge(
×
202
            mergeIntoEntity,
×
203
            ...entityLikes,
×
204
        ) as T
×
205
    }
×
206

26✔
207
    /**
26✔
208
     * Creates a new entity from the given plain javascript object. If entity already exist in the database, then
26✔
209
     * it loads it (and everything related to it), replaces all values with the new ones from the given object
26✔
210
     * and returns this new entity. This new entity is actually a loaded from the db entity with all properties
26✔
211
     * replaced from the new object.
26✔
212
     *
26✔
213
     * Note that given entity-like object must have an entity id / primary key to find entity by.
26✔
214
     * Returns undefined if entity with given id was not found.
26✔
215
     */
26✔
216
    static preload<T extends BaseEntity>(
26✔
217
        this: { new (): T } & typeof BaseEntity,
×
218
        entityLike: DeepPartial<T>,
×
219
    ): Promise<T | undefined> {
×
220
        const thisRepository = this.getRepository<T>()
×
221
        return thisRepository.preload(entityLike)
×
222
    }
×
223

26✔
224
    /**
26✔
225
     * Saves all given entities in the database.
26✔
226
     * If entities do not exist in the database then inserts, otherwise updates.
26✔
227
     */
26✔
228
    static save<T extends BaseEntity>(
26✔
229
        this: { new (): T } & typeof BaseEntity,
26✔
230
        entities: DeepPartial<T>[],
26✔
231
        options?: SaveOptions,
26✔
232
    ): Promise<T[]>
26✔
233

26✔
234
    /**
26✔
235
     * Saves a given entity in the database.
26✔
236
     * If entity does not exist in the database then inserts, otherwise updates.
26✔
237
     */
26✔
238
    static save<T extends BaseEntity>(
26✔
239
        this: { new (): T } & typeof BaseEntity,
26✔
240
        entity: DeepPartial<T>,
26✔
241
        options?: SaveOptions,
26✔
242
    ): Promise<T>
26✔
243

26✔
244
    /**
26✔
245
     * Saves one or many given entities.
26✔
246
     */
26✔
247
    static save<T extends BaseEntity>(
26✔
248
        this: { new (): T } & typeof BaseEntity,
6✔
249
        entityOrEntities: DeepPartial<T> | DeepPartial<T>[],
6✔
250
        options?: SaveOptions,
6✔
251
    ) {
6✔
252
        return this.getRepository<T>().save(entityOrEntities as any, options)
6✔
253
    }
6✔
254

26✔
255
    /**
26✔
256
     * Removes a given entities from the database.
26✔
257
     */
26✔
258
    static remove<T extends BaseEntity>(
26✔
259
        this: { new (): T } & typeof BaseEntity,
26✔
260
        entities: T[],
26✔
261
        options?: RemoveOptions,
26✔
262
    ): Promise<T[]>
26✔
263

26✔
264
    /**
26✔
265
     * Removes a given entity from the database.
26✔
266
     */
26✔
267
    static remove<T extends BaseEntity>(
26✔
268
        this: { new (): T } & typeof BaseEntity,
26✔
269
        entity: T,
26✔
270
        options?: RemoveOptions,
26✔
271
    ): Promise<T>
26✔
272

26✔
273
    /**
26✔
274
     * Removes one or many given entities.
26✔
275
     */
26✔
276
    static remove<T extends BaseEntity>(
26✔
277
        this: { new (): T } & typeof BaseEntity,
×
278
        entityOrEntities: T | T[],
×
279
        options?: RemoveOptions,
×
280
    ) {
×
281
        return this.getRepository<T>().remove(entityOrEntities as any, options)
×
282
    }
×
283

26✔
284
    /**
26✔
285
     * Records the delete date of all given entities.
26✔
286
     */
26✔
287
    static softRemove<T extends BaseEntity>(
26✔
288
        this: { new (): T } & typeof BaseEntity,
26✔
289
        entities: T[],
26✔
290
        options?: SaveOptions,
26✔
291
    ): Promise<T[]>
26✔
292

26✔
293
    /**
26✔
294
     * Records the delete date of a given entity.
26✔
295
     */
26✔
296
    static softRemove<T extends BaseEntity>(
26✔
297
        this: { new (): T } & typeof BaseEntity,
26✔
298
        entity: T,
26✔
299
        options?: SaveOptions,
26✔
300
    ): Promise<T>
26✔
301

26✔
302
    /**
26✔
303
     * Records the delete date of one or many given entities.
26✔
304
     */
26✔
305
    static softRemove<T extends BaseEntity>(
26✔
306
        this: { new (): T } & typeof BaseEntity,
×
307
        entityOrEntities: T | T[],
×
308
        options?: SaveOptions,
×
309
    ) {
×
310
        return this.getRepository<T>().softRemove(
×
311
            entityOrEntities as any,
×
312
            options,
×
313
        )
×
314
    }
×
315

26✔
316
    /**
26✔
317
     * Inserts a given entity into the database.
26✔
318
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
319
     * Executes fast and efficient INSERT query.
26✔
320
     * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
26✔
321
     */
26✔
322
    static insert<T extends BaseEntity>(
26✔
323
        this: { new (): T } & typeof BaseEntity,
×
324
        entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[],
×
325
    ): Promise<InsertResult> {
×
326
        return this.getRepository<T>().insert(entity)
×
327
    }
×
328

26✔
329
    /**
26✔
330
     * Updates entity partially. Entity can be found by a given conditions.
26✔
331
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
332
     * Executes fast and efficient UPDATE query.
26✔
333
     * Does not check if entity exist in the database.
26✔
334
     */
26✔
335
    static update<T extends BaseEntity>(
26✔
336
        this: { new (): T } & typeof BaseEntity,
12✔
337
        criteria:
12✔
338
            | string
12✔
339
            | string[]
12✔
340
            | number
12✔
341
            | number[]
12✔
342
            | Date
12✔
343
            | Date[]
12✔
344
            | ObjectId
12✔
345
            | ObjectId[]
12✔
346
            | FindOptionsWhere<T>,
12✔
347
        partialEntity: QueryDeepPartialEntity<T>,
12✔
348
    ): Promise<UpdateResult> {
12✔
349
        return this.getRepository<T>().update(criteria, partialEntity)
12✔
350
    }
12✔
351

26✔
352
    /**
26✔
353
     * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity
26✔
354
     * Unlike save method executes a primitive operation without cascades, relations and other operations included.
26✔
355
     * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.
26✔
356
     */
26✔
357
    static upsert<T extends BaseEntity>(
26✔
358
        this: { new (): T } & typeof BaseEntity,
56✔
359
        entityOrEntities:
56✔
360
            | QueryDeepPartialEntity<T>
56✔
361
            | QueryDeepPartialEntity<T>[],
56✔
362
        conflictPathsOrOptions: string[] | UpsertOptions<T>,
56✔
363
    ): Promise<InsertResult> {
56✔
364
        return this.getRepository<T>().upsert(
56✔
365
            entityOrEntities,
56✔
366
            conflictPathsOrOptions,
56✔
367
        )
56✔
368
    }
56✔
369

26✔
370
    /**
26✔
371
     * Deletes entities by a given criteria.
26✔
372
     * Unlike remove method executes a primitive operation without cascades, relations and other operations included.
26✔
373
     * Executes fast and efficient DELETE query.
26✔
374
     * Does not check if entity exist in the database.
26✔
375
     */
26✔
376
    static delete<T extends BaseEntity>(
26✔
377
        this: { new (): T } & typeof BaseEntity,
×
378
        criteria:
×
379
            | string
×
380
            | string[]
×
381
            | number
×
382
            | number[]
×
383
            | Date
×
384
            | Date[]
×
385
            | ObjectId
×
386
            | ObjectId[]
×
387
            | FindOptionsWhere<T>,
×
388
    ): Promise<DeleteResult> {
×
389
        return this.getRepository<T>().delete(criteria)
×
390
    }
×
391

26✔
392
    /**
26✔
393
     * Checks whether any entity exists that matches the given options.
26✔
394
     */
26✔
395
    static exists<T extends BaseEntity>(
26✔
396
        this: { new (): T } & typeof BaseEntity,
×
397
        options?: FindManyOptions<T>,
×
398
    ): Promise<boolean> {
×
399
        return this.getRepository<T>().exists(options)
×
400
    }
×
401

26✔
402
    /**
26✔
403
     * Checks whether any entity exists that matches the given conditions.
26✔
404
     */
26✔
405
    static existsBy<T extends BaseEntity>(
26✔
406
        this: { new (): T } & typeof BaseEntity,
×
407
        where: FindOptionsWhere<T>,
×
408
    ): Promise<boolean> {
×
409
        return this.getRepository<T>().existsBy(where)
×
410
    }
×
411

26✔
412
    /**
26✔
413
     * Counts entities that match given options.
26✔
414
     */
26✔
415
    static count<T extends BaseEntity>(
26✔
416
        this: { new (): T } & typeof BaseEntity,
×
417
        options?: FindManyOptions<T>,
×
418
    ): Promise<number> {
×
419
        return this.getRepository<T>().count(options)
×
420
    }
×
421

26✔
422
    /**
26✔
423
     * Counts entities that match given WHERE conditions.
26✔
424
     */
26✔
425
    static countBy<T extends BaseEntity>(
26✔
426
        this: { new (): T } & typeof BaseEntity,
×
427
        where: FindOptionsWhere<T>,
×
428
    ): Promise<number> {
×
429
        return this.getRepository<T>().countBy(where)
×
430
    }
×
431

26✔
432
    /**
26✔
433
     * Return the SUM of a column
26✔
434
     */
26✔
435
    static sum<T extends BaseEntity>(
26✔
436
        this: { new (): T } & typeof BaseEntity,
×
437
        columnName: PickKeysByType<T, number>,
×
438
        where: FindOptionsWhere<T>,
×
439
    ): Promise<number | null> {
×
440
        return this.getRepository<T>().sum(columnName, where)
×
441
    }
×
442

26✔
443
    /**
26✔
444
     * Return the AVG of a column
26✔
445
     */
26✔
446
    static average<T extends BaseEntity>(
26✔
447
        this: { new (): T } & typeof BaseEntity,
×
448
        columnName: PickKeysByType<T, number>,
×
449
        where: FindOptionsWhere<T>,
×
450
    ): Promise<number | null> {
×
451
        return this.getRepository<T>().average(columnName, where)
×
452
    }
×
453

26✔
454
    /**
26✔
455
     * Return the MIN of a column
26✔
456
     */
26✔
457
    static minimum<T extends BaseEntity>(
26✔
458
        this: { new (): T } & typeof BaseEntity,
×
459
        columnName: PickKeysByType<T, number>,
×
460
        where: FindOptionsWhere<T>,
×
461
    ): Promise<number | null> {
×
462
        return this.getRepository<T>().minimum(columnName, where)
×
463
    }
×
464

26✔
465
    /**
26✔
466
     * Return the MAX of a column
26✔
467
     */
26✔
468
    static maximum<T extends BaseEntity>(
26✔
469
        this: { new (): T } & typeof BaseEntity,
×
470
        columnName: PickKeysByType<T, number>,
×
471
        where: FindOptionsWhere<T>,
×
472
    ): Promise<number | null> {
×
473
        return this.getRepository<T>().maximum(columnName, where)
×
474
    }
×
475

26✔
476
    /**
26✔
477
     * Finds entities that match given options.
26✔
478
     */
26✔
479
    static find<T extends BaseEntity>(
26✔
480
        this: { new (): T } & typeof BaseEntity,
×
481
        options?: FindManyOptions<T>,
×
482
    ): Promise<T[]> {
×
483
        return this.getRepository<T>().find(options)
×
484
    }
×
485

26✔
486
    /**
26✔
487
     * Finds entities that match given WHERE conditions.
26✔
488
     */
26✔
489
    static findBy<T extends BaseEntity>(
26✔
490
        this: { new (): T } & typeof BaseEntity,
28✔
491
        where: FindOptionsWhere<T>,
28✔
492
    ): Promise<T[]> {
28✔
493
        return this.getRepository<T>().findBy(where)
28✔
494
    }
28✔
495

26✔
496
    /**
26✔
497
     * Finds entities that match given find options.
26✔
498
     * Also counts all entities that match given conditions,
26✔
499
     * but ignores pagination settings (from and take options).
26✔
500
     */
26✔
501
    static findAndCount<T extends BaseEntity>(
26✔
502
        this: { new (): T } & typeof BaseEntity,
×
503
        options?: FindManyOptions<T>,
×
504
    ): Promise<[T[], number]> {
×
505
        return this.getRepository<T>().findAndCount(options)
×
506
    }
×
507

26✔
508
    /**
26✔
509
     * Finds entities that match given WHERE conditions.
26✔
510
     * Also counts all entities that match given conditions,
26✔
511
     * but ignores pagination settings (from and take options).
26✔
512
     */
26✔
513
    static findAndCountBy<T extends BaseEntity>(
26✔
514
        this: { new (): T } & typeof BaseEntity,
×
515
        where: FindOptionsWhere<T>,
×
516
    ): Promise<[T[], number]> {
×
517
        return this.getRepository<T>().findAndCountBy(where)
×
518
    }
×
519

26✔
520
    /**
26✔
521
     * Finds entities by ids.
26✔
522
     * Optionally find options can be applied.
26✔
523
     *
26✔
524
     * @deprecated use `findBy` method instead in conjunction with `In` operator, for example:
26✔
525
     *
26✔
526
     * .findBy({
26✔
527
     *     id: In([1, 2, 3])
26✔
528
     * })
26✔
529
     */
26✔
530
    static findByIds<T extends BaseEntity>(
26✔
531
        this: { new (): T } & typeof BaseEntity,
×
532
        ids: any[],
×
533
    ): Promise<T[]> {
×
534
        return this.getRepository<T>().findByIds(ids)
×
535
    }
×
536

26✔
537
    /**
26✔
538
     * Finds first entity that matches given conditions.
26✔
539
     */
26✔
540
    static findOne<T extends BaseEntity>(
26✔
541
        this: { new (): T } & typeof BaseEntity,
40✔
542
        options: FindOneOptions<T>,
40✔
543
    ): Promise<T | null> {
40✔
544
        return this.getRepository<T>().findOne(options)
40✔
545
    }
40✔
546

26✔
547
    /**
26✔
548
     * Finds first entity that matches given conditions.
26✔
549
     */
26✔
550
    static findOneBy<T extends BaseEntity>(
26✔
551
        this: { new (): T } & typeof BaseEntity,
×
552
        where: FindOptionsWhere<T>,
×
553
    ): Promise<T | null> {
×
554
        return this.getRepository<T>().findOneBy(where)
×
555
    }
×
556

26✔
557
    /**
26✔
558
     * Finds first entity that matches given options.
26✔
559
     *
26✔
560
     * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example:
26✔
561
     *
26✔
562
     * .findOneBy({
26✔
563
     *     id: 1 // where "id" is your primary column name
26✔
564
     * })
26✔
565
     */
26✔
566
    static findOneById<T extends BaseEntity>(
26✔
567
        this: { new (): T } & typeof BaseEntity,
×
568
        id: string | number | Date | ObjectId,
×
569
    ): Promise<T | null> {
×
570
        return this.getRepository<T>().findOneById(id)
×
571
    }
×
572

26✔
573
    /**
26✔
574
     * Finds first entity that matches given conditions.
26✔
575
     */
26✔
576
    static findOneOrFail<T extends BaseEntity>(
26✔
577
        this: { new (): T } & typeof BaseEntity,
56✔
578
        options: FindOneOptions<T>,
56✔
579
    ): Promise<T> {
56✔
580
        return this.getRepository<T>().findOneOrFail(options)
56✔
581
    }
56✔
582

26✔
583
    /**
26✔
584
     * Finds first entity that matches given conditions.
26✔
585
     */
26✔
586
    static findOneByOrFail<T extends BaseEntity>(
26✔
587
        this: { new (): T } & typeof BaseEntity,
98✔
588
        where: FindOptionsWhere<T>,
98✔
589
    ): Promise<T> {
98✔
590
        return this.getRepository<T>().findOneByOrFail(where)
98✔
591
    }
98✔
592

26✔
593
    /**
26✔
594
     * Executes a raw SQL query and returns a raw database results.
26✔
595
     * Raw query execution is supported only by relational databases (MongoDB is not supported).
26✔
596
     */
26✔
597
    static query<T extends BaseEntity>(
26✔
598
        this: { new (): T } & typeof BaseEntity,
×
599
        query: string,
×
600
        parameters?: any[],
×
601
    ): Promise<any> {
×
602
        return this.getRepository<T>().query(query, parameters)
×
603
    }
×
604

26✔
605
    /**
26✔
606
     * Clears all the data from the given table/collection (truncates/drops it).
26✔
607
     */
26✔
608
    static clear<T extends BaseEntity>(
26✔
609
        this: { new (): T } & typeof BaseEntity,
×
610
    ): Promise<void> {
×
611
        return this.getRepository<T>().clear()
×
612
    }
×
613
}
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