Skip to content

Commit 2d8282c

Browse files
committed
chore: upgrade mongodb drvier to 4.3.1
chore: upgrade deps
1 parent 03fa8bb commit 2d8282c

File tree

8 files changed

+1673
-2141
lines changed

8 files changed

+1673
-2141
lines changed

package.json

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,47 @@
2121
"class-validator": "^0.13.1"
2222
},
2323
"dependencies": {
24-
"class-transformer": "0.4.0",
24+
"class-transformer": "0.5.1",
2525
"cls-hooked": "4.2.2",
26-
"debug": "4.3.2",
26+
"debug": "4.3.3",
2727
"global": "4.4.0",
2828
"lodash": "4.17.21",
29-
"mongodb": "4.0.1",
30-
"slugify": "1.6.0",
29+
"mongodb": "4.3.1",
30+
"slugify": "1.6.5",
3131
"uuid": "8.3.2"
3232
},
3333
"devDependencies": {
34-
"@nestjs/common": "8.0.5",
35-
"@nestjs/core": "8.0.5",
36-
"@nestjs/testing": "8.0.5",
37-
"@nestjs/platform-express": "8.0.5",
34+
"@nestjs/common": "8.2.6",
35+
"@nestjs/core": "8.2.6",
36+
"@nestjs/testing": "8.2.6",
37+
"@nestjs/platform-express": "8.2.6",
3838
"@types/cls-hooked": "4.3.3",
3939
"@types/debug": "4.1.7",
40-
"@types/jest": "26.0.24",
41-
"@types/lodash": "4.14.172",
40+
"@types/jest": "27.4.0",
41+
"@types/lodash": "4.14.178",
4242
"@types/supertest": "2.0.11",
43-
"@typescript-eslint/eslint-plugin": "4.29.0",
44-
"@typescript-eslint/parser": "4.29.0",
45-
"@types/node": "16.4.10",
46-
"class-validator": "0.13.1",
47-
"eslint": "7.32.0",
43+
"@typescript-eslint/eslint-plugin": "5.10.2",
44+
"@typescript-eslint/parser": "^4.0.1",
45+
"@types/node": "17.0.14",
46+
"class-validator": "0.13.2",
47+
"eslint": "^7.12.1",
4848
"eslint-config-prettier": "8.3.0",
49-
"eslint-config-standard-with-typescript": "20.0.0",
50-
"eslint-plugin-import": "2.23.4",
51-
"eslint-plugin-node": "11.1.0",
49+
"eslint-plugin-import": "2.25.4",
50+
"eslint-plugin-node": "^11.1.0",
5251
"eslint-plugin-prefer-arrow": "1.2.3",
53-
"eslint-plugin-promise": "5.1.0",
52+
"eslint-plugin-promise": "^5.0.0",
5453
"eslint-plugin-standard": "5.0.0",
55-
"jest": "27.0.6",
56-
"prettier": "2.3.2",
54+
"eslint-config-standard-with-typescript": "^21.0.1",
55+
"jest": "27.4.7",
56+
"prettier": "2.5.1",
5757
"reflect-metadata": "0.1.13",
58-
"rxjs": "7.3.0",
59-
"supertest": "6.1.4",
60-
"ts-jest": "27.0.4",
61-
"ts-node": "10.1.0",
62-
"tsconfig-paths": "3.10.1",
63-
"typedoc": "0.21.5",
64-
"typescript": "4.3.5"
58+
"rxjs": "7.5.2",
59+
"supertest": "6.2.2",
60+
"ts-jest": "27.1.3",
61+
"ts-node": "10.4.0",
62+
"tsconfig-paths": "3.12.0",
63+
"typedoc": "0.22.11",
64+
"typescript": "4.5.5"
6565
},
6666
"scripts": {
6767
"build": "rm -Rf dist && tsc -b tsconfig.build.json",

src/entity/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HistoryActions } from '../history';
44
import { SerializableInterface } from '../serializer';
55

66
export interface EntityInterface extends SerializableInterface {
7-
_id: ObjectId;
7+
_id?: ObjectId;
88
createdAt: Date;
99
updatedAt?: Date;
1010
history?: HistoryActions;

src/entity/manager.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
MongoClient,
1919
ObjectId,
2020
TransactionOptions,
21-
UpdateOptions,
21+
UpdateOptions
2222
} from 'mongodb';
2323

2424
import { DEBUG } from '../constants';
@@ -30,7 +30,7 @@ import { CascadeType } from '../relationship/interfaces';
3030
import {
3131
getRelationshipMetadata,
3232
getRelationshipsCascadesMetadata,
33-
setRelationshipsCascadesMetadata,
33+
setRelationshipsCascadesMetadata
3434
} from '../relationship/metadata';
3535
import { SessionLoaderService } from '../session/service';
3636
import { fromPlain, merge } from '../transformer/utils';
@@ -185,6 +185,7 @@ export class EntityManager {
185185
if (!isEmpty(proxy._id)) {
186186
proxy.updatedAt = new Date();
187187
const $unset: any = {};
188+
// must be done in a recursive way to allow to delete props from subobject
188189
for (const p in entity) {
189190
if (Object.prototype.hasOwnProperty.call(proxy, p) === true) {
190191
const v: any = proxy[p];
@@ -215,7 +216,7 @@ export class EntityManager {
215216
// merge the proxy changes back to the entity
216217
this.merge(entity, proxy);
217218

218-
this.log('%s %s saved', Model.name, entity._id.toHexString());
219+
this.log('%s %s saved', Model.name, entity._id?.toHexString());
219220
return entity;
220221
} catch (e) {
221222
this.log('error saving %s', entityName);
@@ -234,10 +235,7 @@ export class EntityManager {
234235
...(ctx !== undefined ? { session: ctx.session } : {}),
235236
...options
236237
});
237-
return cursor.map((data) => {
238-
const entity = this.fromPlain<Model>(classType, data);
239-
return entity;
240-
});
238+
return cursor.map((data) => this.fromPlain<Model>(classType, data));
241239
}
242240

243241
async findOne<Model extends EntityInterface>(
@@ -251,7 +249,7 @@ export class EntityManager {
251249
...(ctx !== undefined ? { session: ctx.session } : {}),
252250
...options
253251
});
254-
if (obj !== undefined) {
252+
if (obj !== null) {
255253
return this.fromPlain<Model>(classType, obj);
256254
}
257255
}
@@ -384,8 +382,10 @@ export class EntityManager {
384382
);
385383
}
386384

387-
const value = obj[property];
388-
const relationship = await this.findOne<R>(relationMetadata.type, { _id: value }, options);
385+
const id: ObjectId = obj[property];
386+
const filter: Filter<R> = {};
387+
filter._id = id;
388+
const relationship = await this.findOne<R>(relationMetadata.type, filter);
389389

390390
return relationship;
391391
}
@@ -394,7 +394,7 @@ export class EntityManager {
394394
obj: any,
395395
property: string,
396396
options: FindOptions = {}
397-
): Promise<Array<R | Error>> {
397+
): Promise<R[]> {
398398
this.log('getRelationships %s on %s', property, obj);
399399

400400
const relationMetadata = getRelationshipMetadata<R>(obj.constructor, property, this);
@@ -415,7 +415,7 @@ export class EntityManager {
415415
}
416416

417417
const value = obj[property];
418-
const relationshipsCursor = await this.find(
418+
const relationshipsCursor = await this.find<R>(
419419
relationMetadata.type,
420420
{
421421
_id: { $in: value }

src/entity/repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ClassConstructor, ClassTransformOptions } from 'class-transformer';
22
import { ValidatorOptions } from 'class-validator';
3-
import { ChangeStreamOptions, CountDocumentsOptions, DeleteOptions, Filter, FindOptions } from 'mongodb';
3+
import { ChangeStreamOptions, CountDocumentsOptions, DeleteOptions, Document, Filter, FindOptions } from 'mongodb';
44

55
import { EntityInterface } from './interfaces';
66
import { EntityManager } from './manager';
@@ -56,7 +56,7 @@ export class EntityRepository<Model extends EntityInterface> {
5656
object: Model,
5757
property: string,
5858
options: FindOptions = {}
59-
): Promise<Array<E | Error>> {
59+
): Promise<E[]> {
6060
return await this.em.getRelationships<E>(object, property, options);
6161
}
6262

src/entity/service.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
22
import { isEmpty } from 'class-validator';
33
import Debug from 'debug';
44
import { camelCase } from 'lodash';
5-
import { ChangeStreamDocument, ObjectId } from 'mongodb';
5+
import { ChangeStreamDocument, Filter as MongoFilter, ObjectId } from 'mongodb';
66

77
import { DEBUG } from '../constants';
88
import { EventCallback } from '../event/event';
@@ -45,7 +45,9 @@ export abstract class EntityService<
4545
}
4646

4747
async get(itemId: ObjectId, ...rest: any[]): Promise<Model> {
48-
const item = await this.repository.findOne({ _id: itemId }, ...rest);
48+
const filter: MongoFilter<Model> = {};
49+
filter._id = itemId;
50+
const item = await this.repository.findOne(filter, ...rest);
4951
if (item === undefined) {
5052
throw new NotFoundException();
5153
}
@@ -81,12 +83,9 @@ export abstract class EntityService<
8183
}
8284

8385
async delete(itemId: ObjectId, ...rest: any[]): Promise<void> {
84-
const { deletedCount } = await this.repository.deleteOne(
85-
{
86-
_id: itemId
87-
},
88-
...rest
89-
);
86+
const filter: MongoFilter<Model> = {};
87+
filter._id = itemId;
88+
const { deletedCount } = await this.repository.deleteOne(filter, ...rest);
9089
if (deletedCount !== 1) {
9190
throw new NotFoundException();
9291
}

src/relationship/constraint.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class IsValidRelationshipConstraint implements ValidatorConstraintInterfa
3030
);
3131
let relationship: any;
3232

33-
if (relationMetadata.isArray !== undefined && relationMetadata.isArray) {
33+
if (relationMetadata?.isArray === true) {
3434
if (!Array.isArray(value)) {
3535
throw new Error(`The ${args.property} must be an array`);
3636
}
@@ -64,7 +64,6 @@ export class IsValidRelationshipConstraint implements ValidatorConstraintInterfa
6464
if (Array.isArray(value)) {
6565
throw new Error(`The ${args.property} must not be an array`);
6666
}
67-
// console.log('Validate', entity, args);
6867
relationship = await ensureSequentialTransaction(
6968
ctx,
7069
async () => await this.em.getRelationship(entity, args.property)

test/relationship/relationship.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import {
1313
getRelationshipMetadata,
1414
getRelationshipMetadataList,
1515
getRelationshipsCascadesMetadata,
16-
MongoModule,
16+
MongoModule
1717
} from '../../src';
1818
import { DBTEST } from '../constants';
1919
import { EntityTest } from '../entity/entity';
2020
import {
2121
ChildDynamicRelationship,
2222
DynamicRelationshipType,
2323
ParentDynamicRelationship1,
24-
ParentDynamicRelationship2,
24+
ParentDynamicRelationship2
2525
} from './cascade/entity.dynamic.relationship';
2626
import { RelationshipEntityLevel1Test } from './cascade/level1';
2727
import { RelationshipEntityLevel1WithChildrenTest } from './cascade/level1WithChildren';
@@ -157,6 +157,7 @@ describe('Relationship', () => {
157157
const entityTest = new EntityTest();
158158
entityTest.foo = 'foo';
159159
entityTest.bar = 'bar';
160+
160161
await em.save(entityTest);
161162

162163
const entityRelationShip = new EntityRelationship();

0 commit comments

Comments
 (0)