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

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

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

5.88
/src/repository/MongoRepository.ts
1
import { ObjectLiteral } from "../common/ObjectLiteral"
2
import { Repository } from "./Repository"
4✔
3
import { MongoFindManyOptions } from "../find-options/mongodb/MongoFindManyOptions"
4
import { MongoEntityManager } from "../entity-manager/MongoEntityManager"
5
import { QueryRunner } from "../query-runner/QueryRunner"
6
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder"
7
import { TypeORMError } from "../error/TypeORMError"
4✔
8
import { MongoFindOneOptions } from "../find-options/mongodb/MongoFindOneOptions"
9
import { FindOneOptions } from "../find-options/FindOneOptions"
10

11
import {
12
    CreateIndexesOptions,
13
    ObjectId,
14
    ReplaceOptions,
15
    //
16
    AggregateOptions,
17
    AggregationCursor,
18
    AnyBulkWriteOperation,
19
    BulkWriteOptions,
20
    BulkWriteResult,
21
    Collection,
22
    CollStats,
23
    CollStatsOptions,
24
    CommandOperationOptions,
25
    CountOptions,
26
    DeleteOptions,
27
    DeleteResult,
28
    Document,
29
    Filter,
30
    FilterOperators,
31
    FindCursor,
32
    FindOneAndDeleteOptions,
33
    FindOneAndReplaceOptions,
34
    FindOneAndUpdateOptions,
35
    IndexDescription,
36
    InsertManyResult,
37
    InsertOneOptions,
38
    InsertOneResult,
39
    ListIndexesCursor,
40
    ListIndexesOptions,
41
    OrderedBulkOperation,
42
    UnorderedBulkOperation,
43
    UpdateFilter,
44
    UpdateOptions,
45
    UpdateResult,
46
    CountDocumentsOptions,
47
} from "../driver/mongodb/typings"
48
import { FindManyOptions } from "../find-options/FindManyOptions"
49

50
/**
51
 * Repository used to manage mongodb documents of a single entity type.
52
 */
53
export class MongoRepository<
4✔
54
    Entity extends ObjectLiteral,
55
> extends Repository<Entity> {
56
    // -------------------------------------------------------------------------
57
    // Public Properties
58
    // -------------------------------------------------------------------------
59

60
    /**
61
     * Entity Manager used by this repository.
62
     */
63
    readonly manager: MongoEntityManager
64

65
    // -------------------------------------------------------------------------
66
    // Overridden Methods
67
    // -------------------------------------------------------------------------
68

69
    /**
70
     * Raw SQL query execution is not supported by MongoDB.
71
     * Calling this method will return an error.
72
     */
73
    query(query: string, parameters?: any[]): Promise<any> {
74
        throw new TypeORMError(`Queries aren't supported by MongoDB.`)
×
75
    }
76

77
    /**
78
     * Using Query Builder with MongoDB is not supported yet.
79
     * Calling this method will return an error.
80
     */
81
    createQueryBuilder(
82
        alias: string,
83
        queryRunner?: QueryRunner,
84
    ): SelectQueryBuilder<Entity> {
85
        throw new TypeORMError(`Query Builder is not supported by MongoDB.`)
×
86
    }
87

88
    /**
89
     * Finds entities that match given find options or conditions.
90
     */
91
    find(
92
        options?:
93
            | FindManyOptions<Entity>
94
            | Partial<Entity>
95
            | FilterOperators<Entity>,
96
    ): Promise<Entity[]> {
97
        return this.manager.find(this.metadata.target, options)
×
98
    }
99

100
    /**
101
     * Finds entities that match given find options or conditions.
102
     */
103
    findBy(where: any): Promise<Entity[]> {
104
        return this.manager.findBy(this.metadata.target, where)
×
105
    }
106

107
    /**
108
     * Finds entities that match given find options or conditions.
109
     * Also counts all entities that match given conditions,
110
     * but ignores pagination settings (from and take options).
111
     */
112
    findAndCount(
113
        options?: MongoFindManyOptions<Entity>,
114
    ): Promise<[Entity[], number]> {
115
        return this.manager.findAndCount(this.metadata.target, options)
×
116
    }
117

118
    /**
119
     * Finds entities that match given find options or conditions.
120
     * Also counts all entities that match given conditions,
121
     * but ignores pagination settings (from and take options).
122
     */
123
    findAndCountBy(where: any): Promise<[Entity[], number]> {
124
        return this.manager.findAndCountBy(this.metadata.target, where)
×
125
    }
126

127
    /**
128
     * Finds entities by ids.
129
     * Optionally find options can be applied.
130
     *
131
     * @deprecated use `findBy` method instead in conjunction with `In` operator, for example:
132
     *
133
     * .findBy({
134
     *     id: In([1, 2, 3])
135
     * })
136
     */
137
    findByIds(ids: any[], options?: any): Promise<Entity[]> {
138
        return this.manager.findByIds(this.metadata.target, ids, options)
×
139
    }
140

141
    /**
142
     * Finds first entity that matches given find options.
143
     */
144
    async findOne(
145
        options: MongoFindOneOptions<Entity>,
146
    ): Promise<Entity | null> {
147
        return this.manager.findOne(this.metadata.target, options)
×
148
    }
149

150
    /**
151
     * Finds first entity that matches given WHERE conditions.
152
     */
153
    async findOneBy(where: any): Promise<Entity | null> {
154
        return this.manager.findOneBy(this.metadata.target, where)
×
155
    }
156

157
    /**
158
     * Finds entity that matches given id.
159
     *
160
     * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example:
161
     *
162
     * .findOneBy({
163
     *     id: 1 // where "id" is your primary column name
164
     * })
165
     */
166
    async findOneById(
167
        id: string | number | Date | ObjectId,
168
    ): Promise<Entity | null> {
169
        return this.manager.findOneById(this.metadata.target, id)
×
170
    }
171

172
    /**
173
     * Finds first entity by a given find options.
174
     * If entity was not found in the database - rejects with error.
175
     */
176
    async findOneOrFail(options: FindOneOptions<Entity>): Promise<Entity> {
177
        return this.manager.findOneOrFail(this.metadata.target, options)
×
178
    }
179

180
    /**
181
     * Finds first entity that matches given where condition.
182
     * If entity was not found in the database - rejects with error.
183
     */
184
    async findOneByOrFail(where: any): Promise<Entity> {
185
        return this.manager.findOneByOrFail(this.metadata.target, where)
×
186
    }
187

188
    /**
189
     * Creates a cursor for a query that can be used to iterate over results from MongoDB.
190
     */
191
    createCursor<T = any>(query?: Filter<Entity>): FindCursor<T> {
192
        return this.manager.createCursor(this.metadata.target, query)
×
193
    }
194

195
    /**
196
     * Creates a cursor for a query that can be used to iterate over results from MongoDB.
197
     * This returns modified version of cursor that transforms each result into Entity model.
198
     */
199
    createEntityCursor(query?: Filter<Entity>): FindCursor<Entity> {
200
        return this.manager.createEntityCursor(this.metadata.target, query)
×
201
    }
202

203
    /**
204
     * Execute an aggregation framework pipeline against the collection.
205
     */
206
    aggregate<R = any>(
207
        pipeline: ObjectLiteral[],
208
        options?: AggregateOptions,
209
    ): AggregationCursor<R> {
210
        return this.manager.aggregate<R>(
×
211
            this.metadata.target,
212
            pipeline,
213
            options,
214
        )
215
    }
216

217
    /**
218
     * Execute an aggregation framework pipeline against the collection.
219
     * This returns modified version of cursor that transforms each result into Entity model.
220
     */
221
    aggregateEntity(
222
        pipeline: ObjectLiteral[],
223
        options?: AggregateOptions,
224
    ): AggregationCursor<Entity> {
225
        return this.manager.aggregateEntity(
×
226
            this.metadata.target,
227
            pipeline,
228
            options,
229
        )
230
    }
231
    /**
232
     * Perform a bulkWrite operation without a fluent API.
233
     */
234
    bulkWrite(
235
        operations: AnyBulkWriteOperation[],
236
        options?: BulkWriteOptions,
237
    ): Promise<BulkWriteResult> {
238
        return this.manager.bulkWrite(this.metadata.target, operations, options)
×
239
    }
240

241
    /**
242
     * Count number of matching documents in the db to a query.
243
     */
244
    count(query?: ObjectLiteral, options?: CountOptions): Promise<number> {
245
        return this.manager.count(this.metadata.target, query || {}, options)
×
246
    }
247

248
    /**
249
     * Count number of matching documents in the db to a query.
250
     */
251
    countDocuments(
252
        query?: ObjectLiteral,
253
        options?: CountDocumentsOptions,
254
    ): Promise<number> {
255
        return this.manager.countDocuments(
×
256
            this.metadata.target,
257
            query || {},
×
258
            options,
259
        )
260
    }
261

262
    /**
263
     * Count number of matching documents in the db to a query.
264
     */
265
    countBy(query?: ObjectLiteral, options?: CountOptions): Promise<number> {
266
        return this.manager.countBy(this.metadata.target, query, options)
×
267
    }
268

269
    /**
270
     * Creates an index on the db and collection.
271
     */
272
    createCollectionIndex(
273
        fieldOrSpec: string | any,
274
        options?: CreateIndexesOptions,
275
    ): Promise<string> {
276
        return this.manager.createCollectionIndex(
×
277
            this.metadata.target,
278
            fieldOrSpec,
279
            options,
280
        )
281
    }
282

283
    /**
284
     * Creates multiple indexes in the collection, this method is only supported for MongoDB 2.6 or higher.
285
     * Earlier version of MongoDB will throw a command not supported error.
286
     * Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
287
     */
288
    createCollectionIndexes(indexSpecs: IndexDescription[]): Promise<string[]> {
289
        return this.manager.createCollectionIndexes(
×
290
            this.metadata.target,
291
            indexSpecs,
292
        )
293
    }
294

295
    /**
296
     * Delete multiple documents on MongoDB.
297
     */
298
    deleteMany(
299
        query: ObjectLiteral,
300
        options?: DeleteOptions,
301
    ): Promise<DeleteResult> {
302
        return this.manager.deleteMany(this.metadata.tableName, query, options)
×
303
    }
304

305
    /**
306
     * Delete a document on MongoDB.
307
     */
308
    deleteOne(
309
        query: ObjectLiteral,
310
        options?: DeleteOptions,
311
    ): Promise<DeleteResult> {
312
        return this.manager.deleteOne(this.metadata.tableName, query, options)
×
313
    }
314

315
    /**
316
     * The distinct command returns returns a list of distinct values for the given key across a collection.
317
     */
318
    distinct(
319
        key: string,
320
        query: ObjectLiteral,
321
        options?: CommandOperationOptions,
322
    ): Promise<any> {
323
        return this.manager.distinct(
×
324
            this.metadata.tableName,
325
            key,
326
            query,
327
            options,
328
        )
329
    }
330

331
    /**
332
     * Drops an index from this collection.
333
     */
334
    dropCollectionIndex(
335
        indexName: string,
336
        options?: CommandOperationOptions,
337
    ): Promise<any> {
338
        return this.manager.dropCollectionIndex(
×
339
            this.metadata.tableName,
340
            indexName,
341
            options,
342
        )
343
    }
344

345
    /**
346
     * Drops all indexes from the collection.
347
     */
348
    dropCollectionIndexes(): Promise<any> {
349
        return this.manager.dropCollectionIndexes(this.metadata.tableName)
×
350
    }
351

352
    /**
353
     * Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.
354
     */
355
    findOneAndDelete(
356
        query: ObjectLiteral,
357
        options?: FindOneAndDeleteOptions,
358
    ): Promise<Document | null> {
359
        return this.manager.findOneAndDelete(
×
360
            this.metadata.tableName,
361
            query,
362
            options,
363
        )
364
    }
365

366
    /**
367
     * Find a document and replace it in one atomic operation, requires a write lock for the duration of the operation.
368
     */
369
    findOneAndReplace(
370
        query: ObjectLiteral,
371
        replacement: Object,
372
        options?: FindOneAndReplaceOptions,
373
    ): Promise<Document | null> {
374
        return this.manager.findOneAndReplace(
×
375
            this.metadata.tableName,
376
            query,
377
            replacement,
378
            options,
379
        )
380
    }
381

382
    /**
383
     * Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
384
     */
385
    findOneAndUpdate(
386
        query: ObjectLiteral,
387
        update: Object,
388
        options?: FindOneAndUpdateOptions,
389
    ): Promise<Document | null> {
390
        return this.manager.findOneAndUpdate(
×
391
            this.metadata.tableName,
392
            query,
393
            update,
394
            options,
395
        )
396
    }
397

398
    /**
399
     * Retrieve all the indexes on the collection.
400
     */
401
    collectionIndexes(): Promise<any> {
402
        return this.manager.collectionIndexes(this.metadata.tableName)
×
403
    }
404

405
    /**
406
     * Retrieve all the indexes on the collection.
407
     */
408
    collectionIndexExists(indexes: string | string[]): Promise<boolean> {
409
        return this.manager.collectionIndexExists(
×
410
            this.metadata.tableName,
411
            indexes,
412
        )
413
    }
414

415
    /**
416
     * Retrieves this collections index info.
417
     */
418
    collectionIndexInformation(options?: { full: boolean }): Promise<any> {
419
        return this.manager.collectionIndexInformation(
×
420
            this.metadata.tableName,
421
            options,
422
        )
423
    }
424

425
    /**
426
     * Initiate an In order bulk write operation, operations will be serially executed in the order they are added, creating a new operation for each switch in types.
427
     */
428
    initializeOrderedBulkOp(options?: BulkWriteOptions): OrderedBulkOperation {
429
        return this.manager.initializeOrderedBulkOp(
×
430
            this.metadata.tableName,
431
            options,
432
        )
433
    }
434

435
    /**
436
     * Initiate a Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
437
     */
438
    initializeUnorderedBulkOp(
439
        options?: BulkWriteOptions,
440
    ): UnorderedBulkOperation {
441
        return this.manager.initializeUnorderedBulkOp(
×
442
            this.metadata.tableName,
443
            options,
444
        )
445
    }
446

447
    /**
448
     * Inserts an array of documents into MongoDB.
449
     */
450
    insertMany(
451
        docs: ObjectLiteral[],
452
        options?: BulkWriteOptions,
453
    ): Promise<InsertManyResult<Document>> {
454
        return this.manager.insertMany(this.metadata.tableName, docs, options)
×
455
    }
456

457
    /**
458
     * Inserts a single document into MongoDB.
459
     */
460
    insertOne(
461
        doc: ObjectLiteral,
462
        options?: InsertOneOptions,
463
    ): Promise<InsertOneResult> {
464
        return this.manager.insertOne(this.metadata.tableName, doc, options)
×
465
    }
466

467
    /**
468
     * Returns if the collection is a capped collection.
469
     */
470
    isCapped(): Promise<any> {
471
        return this.manager.isCapped(this.metadata.tableName)
×
472
    }
473

474
    /**
475
     * Get the list of all indexes information for the collection.
476
     */
477
    listCollectionIndexes(options?: ListIndexesOptions): ListIndexesCursor {
478
        return this.manager.listCollectionIndexes(
×
479
            this.metadata.tableName,
480
            options,
481
        )
482
    }
483

484
    /**
485
     * Reindex all indexes on the collection Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
486
     */
487
    rename(
488
        newName: string,
489
        options?: { dropTarget?: boolean },
490
    ): Promise<Collection<Document>> {
491
        return this.manager.rename(this.metadata.tableName, newName, options)
×
492
    }
493

494
    /**
495
     * Replace a document on MongoDB.
496
     */
497
    replaceOne(
498
        query: ObjectLiteral,
499
        doc: ObjectLiteral,
500
        options?: ReplaceOptions,
501
    ): Promise<Document | UpdateResult> {
502
        return this.manager.replaceOne(
×
503
            this.metadata.tableName,
504
            query,
505
            doc,
506
            options,
507
        )
508
    }
509

510
    /**
511
     * Get all the collection statistics.
512
     */
513
    stats(options?: CollStatsOptions): Promise<CollStats> {
514
        return this.manager.stats(this.metadata.tableName, options)
×
515
    }
516

517
    /**
518
     * Update multiple documents on MongoDB.
519
     */
520
    updateMany(
521
        query: ObjectLiteral,
522
        update: UpdateFilter<Document>,
523
        options?: UpdateOptions,
524
    ): Promise<Document | UpdateResult> {
525
        return this.manager.updateMany(
×
526
            this.metadata.tableName,
527
            query,
528
            update,
529
            options,
530
        )
531
    }
532

533
    /**
534
     * Update a single document on MongoDB.
535
     */
536
    updateOne(
537
        query: ObjectLiteral,
538
        update: UpdateFilter<Document>,
539
        options?: UpdateOptions,
540
    ): Promise<Document | UpdateResult> {
541
        return this.manager.updateOne(
×
542
            this.metadata.tableName,
543
            query,
544
            update,
545
            options,
546
        )
547
    }
548
}
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

© 2025 Coveralls, Inc