Skip to content

Commit 099a333

Browse files
committed
fix: deprecated count documents in entity service list()
1 parent 2cdbe63 commit 099a333

File tree

5 files changed

+1222
-410
lines changed

5 files changed

+1222
-410
lines changed

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-mongo",
3-
"version": "0.14.1",
3+
"version": "0.14.2",
44
"description": "A NestJS module that provide a simple mongodb orm like",
55
"keywords": [
66
"nestjs",
@@ -15,9 +15,9 @@
1515
"author": "Rmannn <alex.hermann@pop-code.com>",
1616
"license": "MIT",
1717
"peerDependencies": {
18-
"@nestjs/common": "^8",
19-
"@nestjs/core": "^8",
20-
"class-validator": "^0.13.1",
18+
"@nestjs/common": "^8 || ^9",
19+
"@nestjs/core": "^8 || ^9",
20+
"class-validator": "^0.13.1 || ^0.14",
2121
"mongodb": "^4.5.0",
2222
"reflect-metadata": "^0.1.13"
2323
},
@@ -31,33 +31,33 @@
3131
"uuid": "8.3.2"
3232
},
3333
"devDependencies": {
34-
"@nestjs/common": "^8.4.4",
35-
"@nestjs/core": "^8.4.4",
36-
"@nestjs/platform-express": "^8.4.4",
37-
"@nestjs/testing": "^8.4.4",
34+
"@nestjs/common": "^9.2.1",
35+
"@nestjs/core": "^9.2.1",
36+
"@nestjs/platform-express": "^9.2.1",
37+
"@nestjs/testing": "^9.2.1",
3838
"@types/cls-hooked": "^4.3.3",
3939
"@types/debug": "^4.1.7",
4040
"@types/jest": "^27.4.1",
41-
"@types/lodash": "^4.14.182",
42-
"@types/node": "^17.0.27",
41+
"@types/lodash": "^4.14.191",
42+
"@types/node": "^18.11.15",
4343
"@types/supertest": "^2.0.12",
4444
"@typescript-eslint/eslint-plugin": "^5.21.0",
4545
"@typescript-eslint/parser": "^5.21.0",
46-
"class-validator": "^0.13.2",
46+
"class-validator": "^0.14.0",
4747
"eslint": "^8.14.0",
4848
"eslint-config-prettier": "^8.5.0",
4949
"eslint-plugin-import": "^2.26.0",
5050
"jest": "^27.5.1",
51-
"mongodb": "^4.5.0",
52-
"prettier": "^2.6.2",
51+
"mongodb": "^4.12.1",
52+
"prettier": "^2.8.1",
5353
"reflect-metadata": "^0.1.13",
54-
"rxjs": "^7.5.5",
55-
"supertest": "^6.2.3",
54+
"rxjs": "^7.6.0",
55+
"supertest": "^6.3.3",
5656
"ts-jest": "^27.1.4",
5757
"ts-node": "^10.7.0",
5858
"tsconfig-paths": "^3.14.1",
59-
"typedoc": "^0.22.15",
60-
"typescript": "^4.6.3"
59+
"typedoc": "^0.23.22",
60+
"typescript": "^4.9.4"
6161
},
6262
"scripts": {
6363
"build": "rm -Rf dist && tsc -b tsconfig.build.json",

src/entity/manager.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,17 @@ export class EntityManager {
170170
const ctx = this.getSessionContext();
171171
try {
172172
this.log('saving %s', entityName);
173-
if (options.skipValidation !== true) {
174-
await this.validate(entity, options.validatorOptions, true);
175-
}
176173
const collection = this.getCollection(entity);
177-
178174
const Model = this.getModel(entityName);
179175
if (Model === undefined) {
180176
throw new Error(`Can not find model ${entityName}`);
181177
}
182-
183178
const proxy = this.merge(new Model(), entity);
184179

180+
if (options.skipValidation !== true) {
181+
await this.validate(proxy, options.validatorOptions, true);
182+
}
183+
185184
const operationOptions = {
186185
...(ctx !== undefined ? { session: ctx.session } : {}),
187186
...options

src/entity/repository.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { ClassConstructor, ClassTransformOptions } from 'class-transformer';
22
import { ValidatorOptions } from 'class-validator';
3-
import { ChangeStreamOptions, CountDocumentsOptions, DeleteOptions, Document, Filter, FindOptions, WithId } from 'mongodb';
3+
import {
4+
ChangeStreamOptions,
5+
CountDocumentsOptions,
6+
DeleteOptions,
7+
Document,
8+
Filter,
9+
FindOptions,
10+
WithId
11+
} from 'mongodb';
412

513
import { EntityInterface } from './interfaces';
614
import { EntityManager } from './manager';
@@ -24,6 +32,10 @@ export class EntityRepository<Model extends EntityInterface> {
2432
return this.em.watch(this.classType, pipes, options);
2533
}
2634

35+
getCollection() {
36+
return this.em.getCollection(this.getClassType());
37+
}
38+
2739
async find(query: Filter<Model>, options: FindOptions<Model> = {}) {
2840
return await this.em.find(this.classType, query, options);
2941
}

src/entity/service.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,15 @@ export abstract class EntityService<
5656

5757
async list(filter: Filter, ResponseType: any, ...rest: any[]): Promise<PaginatedData<Model>> {
5858
const cursor = await this.repository.find(filter.toQuery(), ...rest);
59-
6059
if (!isEmpty(filter.orderBy)) {
6160
cursor.sort(filter.getSort());
6261
}
63-
64-
const count = await cursor.count();
65-
62+
const count = await this.repository.getCollection().countDocuments(filter);
6663
cursor.skip(filter.skip).limit(filter.limit);
6764
const data = await cursor.toArray();
68-
6965
const res = new ResponseType();
7066
res.count = count;
7167
res.data = data;
72-
7368
return res;
7469
}
7570

@@ -109,9 +104,11 @@ export abstract class EntityService<
109104
const em = this.repository.getEm();
110105
const classType = this.repository.getClassType();
111106
const eventName = camelCase(`on_${change.operationType}_${classType.name}`);
112-
this.log('Event:%s for %s:%s', eventName, classType.name, change.documentKey);
113-
if (change.fullDocument !== undefined) {
114-
change.fullDocument = em.fromPlain(classType, change.fullDocument) as WithId<Model>;
107+
this.log('Event:%s for %s:%s', eventName, classType.name, change['documentKey']);
108+
if (change.operationType !== 'drop') {
109+
if (change['fullDocument'] !== undefined) {
110+
change['fullDocument'] = em.fromPlain(classType, change['fullDocument']) as WithId<Model>;
111+
}
115112
}
116113
onData(eventName, change);
117114
} catch (e) {

0 commit comments

Comments
 (0)