Skip to content

Commit

Permalink
fix: deprecated count documents in entity service list()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmannn committed Dec 14, 2022
1 parent 2cdbe63 commit 099a333
Show file tree
Hide file tree
Showing 5 changed files with 1,222 additions and 410 deletions.
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-mongo",
"version": "0.14.1",
"version": "0.14.2",
"description": "A NestJS module that provide a simple mongodb orm like",
"keywords": [
"nestjs",
Expand All @@ -15,9 +15,9 @@
"author": "Rmannn <[email protected]>",
"license": "MIT",
"peerDependencies": {
"@nestjs/common": "^8",
"@nestjs/core": "^8",
"class-validator": "^0.13.1",
"@nestjs/common": "^8 || ^9",
"@nestjs/core": "^8 || ^9",
"class-validator": "^0.13.1 || ^0.14",
"mongodb": "^4.5.0",
"reflect-metadata": "^0.1.13"
},
Expand All @@ -31,33 +31,33 @@
"uuid": "8.3.2"
},
"devDependencies": {
"@nestjs/common": "^8.4.4",
"@nestjs/core": "^8.4.4",
"@nestjs/platform-express": "^8.4.4",
"@nestjs/testing": "^8.4.4",
"@nestjs/common": "^9.2.1",
"@nestjs/core": "^9.2.1",
"@nestjs/platform-express": "^9.2.1",
"@nestjs/testing": "^9.2.1",
"@types/cls-hooked": "^4.3.3",
"@types/debug": "^4.1.7",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.182",
"@types/node": "^17.0.27",
"@types/lodash": "^4.14.191",
"@types/node": "^18.11.15",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"class-validator": "^0.13.2",
"class-validator": "^0.14.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"jest": "^27.5.1",
"mongodb": "^4.5.0",
"prettier": "^2.6.2",
"mongodb": "^4.12.1",
"prettier": "^2.8.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.5",
"supertest": "^6.2.3",
"rxjs": "^7.6.0",
"supertest": "^6.3.3",
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"tsconfig-paths": "^3.14.1",
"typedoc": "^0.22.15",
"typescript": "^4.6.3"
"typedoc": "^0.23.22",
"typescript": "^4.9.4"
},
"scripts": {
"build": "rm -Rf dist && tsc -b tsconfig.build.json",
Expand Down
9 changes: 4 additions & 5 deletions src/entity/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,17 @@ export class EntityManager {
const ctx = this.getSessionContext();
try {
this.log('saving %s', entityName);
if (options.skipValidation !== true) {
await this.validate(entity, options.validatorOptions, true);
}
const collection = this.getCollection(entity);

const Model = this.getModel(entityName);
if (Model === undefined) {
throw new Error(`Can not find model ${entityName}`);
}

const proxy = this.merge(new Model(), entity);

if (options.skipValidation !== true) {
await this.validate(proxy, options.validatorOptions, true);
}

const operationOptions = {
...(ctx !== undefined ? { session: ctx.session } : {}),
...options
Expand Down
14 changes: 13 additions & 1 deletion src/entity/repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { ClassConstructor, ClassTransformOptions } from 'class-transformer';
import { ValidatorOptions } from 'class-validator';
import { ChangeStreamOptions, CountDocumentsOptions, DeleteOptions, Document, Filter, FindOptions, WithId } from 'mongodb';
import {
ChangeStreamOptions,
CountDocumentsOptions,
DeleteOptions,
Document,
Filter,
FindOptions,
WithId
} from 'mongodb';

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

getCollection() {
return this.em.getCollection(this.getClassType());
}

async find(query: Filter<Model>, options: FindOptions<Model> = {}) {
return await this.em.find(this.classType, query, options);
}
Expand Down
15 changes: 6 additions & 9 deletions src/entity/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,15 @@ export abstract class EntityService<

async list(filter: Filter, ResponseType: any, ...rest: any[]): Promise<PaginatedData<Model>> {
const cursor = await this.repository.find(filter.toQuery(), ...rest);

if (!isEmpty(filter.orderBy)) {
cursor.sort(filter.getSort());
}

const count = await cursor.count();

const count = await this.repository.getCollection().countDocuments(filter);
cursor.skip(filter.skip).limit(filter.limit);
const data = await cursor.toArray();

const res = new ResponseType();
res.count = count;
res.data = data;

return res;
}

Expand Down Expand Up @@ -109,9 +104,11 @@ export abstract class EntityService<
const em = this.repository.getEm();
const classType = this.repository.getClassType();
const eventName = camelCase(`on_${change.operationType}_${classType.name}`);
this.log('Event:%s for %s:%s', eventName, classType.name, change.documentKey);
if (change.fullDocument !== undefined) {
change.fullDocument = em.fromPlain(classType, change.fullDocument) as WithId<Model>;
this.log('Event:%s for %s:%s', eventName, classType.name, change['documentKey']);
if (change.operationType !== 'drop') {
if (change['fullDocument'] !== undefined) {
change['fullDocument'] = em.fromPlain(classType, change['fullDocument']) as WithId<Model>;
}
}
onData(eventName, change);
} catch (e) {
Expand Down
Loading

0 comments on commit 099a333

Please sign in to comment.