diff --git a/examples/package.json b/examples/package.json index 232cf3be8..a12fc3d2b 100644 --- a/examples/package.json +++ b/examples/package.json @@ -21,7 +21,7 @@ "@nestjs/core": "7.6.11", "@nestjs/graphql": "7.9.8", "@nestjs/jwt": "7.2.0", - "@nestjs/mongoose": "7.1.2", + "@nestjs/mongoose": "7.2.3", "@nestjs/passport": "7.1.5", "@nestjs/platform-express": "7.6.11", "@nestjs/sequelize": "0.2.0", @@ -32,7 +32,7 @@ "graphql": "15.5.0", "graphql-query-complexity": "0.7.2", "graphql-tools": "7.0.2", - "mongoose": "5.10.19", + "mongoose": "5.11.15", "mysql2": "2.2.5", "passport": "0.4.1", "passport-jwt": "4.0.0", @@ -51,7 +51,6 @@ "@nestjs/testing": "7.6.11", "@types/express": "4.17.11", "@types/jest": "26.0.20", - "@types/mongoose": "5.10.1", "@types/node": "13.13.5", "@types/passport-jwt": "3.0.4", "@types/passport-local": "1.0.33", diff --git a/package-lock.json b/package-lock.json index 0241f4fd9..4232bb9ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6371,9 +6371,9 @@ "integrity": "sha512-AdWVTOg3AhAEcVhPGgUJiLbLXb7L5Pe7vc20YQ0oOXP/KD/nJj0I3BcytVdBhzmgepol67BdivNUvo27Hx3Ndw==" }, "@nestjs/mongoose": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/@nestjs/mongoose/-/mongoose-7.1.2.tgz", - "integrity": "sha512-VjeF0woAWs/J/ecpVqKwpPAEh8oRICJgn60ZHu73XHa9VDw8F1wPCrHdG3SQtPumbjNTTvXEqSyaKSuW/8Qp/A==" + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/@nestjs/mongoose/-/mongoose-7.2.3.tgz", + "integrity": "sha512-uUl6brwvNUN/ijzLi96fSDf07eC9m6Ms98+zjx3lZbaMAkZXhpBeGkM0DoMOawBgcEET7hEN1W8L+izrEqygNA==" }, "@nestjs/passport": { "version": "7.1.5", @@ -7511,15 +7511,6 @@ "@types/node": "*" } }, - "@types/mongoose": { - "version": "5.10.1", - "resolved": "https://registry.npmjs.org/@types/mongoose/-/mongoose-5.10.1.tgz", - "integrity": "sha512-5yqbLHOyCQhUb7GPGW0A2dauUbhwgBvUWMzYcaUQiHdLZ8slgRp2R6i8FETZ+t5xeXpfhylYp9U7dAng7WamqQ==", - "requires": { - "@types/mongodb": "*", - "@types/node": "*" - } - }, "@types/node": { "version": "13.13.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.5.tgz", @@ -18903,9 +18894,9 @@ } }, "kareem": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz", - "integrity": "sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw==" + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.2.tgz", + "integrity": "sha512-STHz9P7X2L4Kwn72fA4rGyqyXdmrMSdxqHx9IXon/FXluXieaFA6KJ2upcHAHxQPQ0LeM/OjLrhFxifHewOALQ==" }, "kind-of": { "version": "6.0.2", @@ -20054,16 +20045,17 @@ } }, "mongoose": { - "version": "5.10.19", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.10.19.tgz", - "integrity": "sha512-SuJwbhQpfZ6WZFM6H2v0Hv1wQNLDeBDLZwOHR3UnR6IlPLKjuLyEx4OLI0vFQihv+JWJKWqJt+LcRRbRyL9PCg==", + "version": "5.11.15", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.15.tgz", + "integrity": "sha512-8T4bT6eCGB7MqCm40oVhnhT/1AyAdwe+y1rYUhdl3ljsks3BpYz8whZgcMkIoh6VoCCjipOXRqZqdk1UByvlYA==", "requires": { + "@types/mongodb": "^3.5.27", "bson": "^1.1.4", - "kareem": "2.3.1", + "kareem": "2.3.2", "mongodb": "3.6.3", "mongoose-legacy-pluralize": "1.0.2", - "mpath": "0.7.0", - "mquery": "3.2.2", + "mpath": "0.8.3", + "mquery": "3.2.3", "ms": "2.1.2", "regexp-clone": "1.0.0", "safe-buffer": "5.2.1", @@ -20131,14 +20123,14 @@ } }, "mpath": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.7.0.tgz", - "integrity": "sha512-Aiq04hILxhz1L+f7sjGyn7IxYzWm1zLNNXcfhDtx04kZ2Gk7uvFdgZ8ts1cWa/6d0TQmag2yR8zSGZUmp0tFNg==" + "version": "0.8.3", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.3.tgz", + "integrity": "sha512-eb9rRvhDltXVNL6Fxd2zM9D4vKBxjVVQNLNijlj7uoXUy19zNDsIif5zR+pWmPCWNKwAtqyo4JveQm4nfD5+eA==" }, "mquery": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.2.tgz", - "integrity": "sha512-XB52992COp0KP230I3qloVUbkLUxJIu328HBP2t2EsxSFtf4W1HPSOBWOXf1bqxK4Xbb66lfMJ+Bpfd9/yZE1Q==", + "version": "3.2.3", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.3.tgz", + "integrity": "sha512-cIfbP4TyMYX+SkaQ2MntD+F2XbqaBHUYWk3j+kqdDztPWok3tgyssOZxMHMtzbV1w9DaSlvEea0Iocuro41A4g==", "requires": { "bluebird": "3.5.1", "debug": "3.1.0", diff --git a/packages/query-mongoose/__tests__/services/mongoose-query.service.spec.ts b/packages/query-mongoose/__tests__/services/mongoose-query.service.spec.ts index 6677140eb..dcaea1acb 100644 --- a/packages/query-mongoose/__tests__/services/mongoose-query.service.spec.ts +++ b/packages/query-mongoose/__tests__/services/mongoose-query.service.spec.ts @@ -55,11 +55,11 @@ describe('MongooseQueryService', () => { }).compile(); }); - function convertDocument(doc: Doc): Doc { + function convertDocument(doc: Doc) { return doc.toObject({ virtuals: true }); } - function convertDocuments(docs: Doc[]): Doc[] { + function convertDocuments(docs: Doc[]) { return docs.map((doc) => convertDocument(doc)); } diff --git a/packages/query-mongoose/package.json b/packages/query-mongoose/package.json index 702abc0bc..aefe182b3 100644 --- a/packages/query-mongoose/package.json +++ b/packages/query-mongoose/package.json @@ -30,16 +30,15 @@ }, "devDependencies": { "@nestjs/common": "7.6.11", - "@nestjs/mongoose": "7.1.2", "@nestjs/testing": "7.6.11", "@types/lodash.escaperegexp": "4.1.6", "@types/lodash.merge": "4.6.6", "@types/mongodb": "3.6.6", - "@types/mongoose": "5.10.1", + "@nestjs/mongoose": "7.2.3", "class-transformer": "0.3.2", "mongodb": "3.6.4", "mongodb-memory-server": "6.9.3", - "mongoose": "5.10.19", + "mongoose": "5.11.15", "ts-mockito": "2.6.1", "typescript": "4.1.3" }, diff --git a/packages/query-mongoose/src/index.ts b/packages/query-mongoose/src/index.ts index 5cb5c632e..afdedc6dd 100644 --- a/packages/query-mongoose/src/index.ts +++ b/packages/query-mongoose/src/index.ts @@ -1,2 +1,2 @@ -export { MongooseQueryService, MongooseQueryServiceOpts } from './services'; +export { MongooseQueryService } from './services'; export { NestjsQueryMongooseModule } from './module'; diff --git a/packages/query-mongoose/src/services/mongoose-query.service.ts b/packages/query-mongoose/src/services/mongoose-query.service.ts index 7a22116ec..cf0eb00ff 100644 --- a/packages/query-mongoose/src/services/mongoose-query.service.ts +++ b/packages/query-mongoose/src/services/mongoose-query.service.ts @@ -14,7 +14,7 @@ import { UpdateOneOptions, } from '@nestjs-query/core'; import { NotFoundException } from '@nestjs/common'; -import { CreateQuery, Document, DocumentToObjectOptions, Model as MongooseModel, UpdateQuery } from 'mongoose'; +import { Document, Model as MongooseModel, UpdateQuery } from 'mongoose'; import { AggregateBuilder, FilterQueryBuilder } from '../query'; import { ReferenceQueryService } from './reference-query.service'; @@ -22,10 +22,9 @@ type MongoDBUpdatedOutput = { nModified: number; }; -export interface MongooseQueryServiceOpts { - documentToObjectOptions?: DocumentToObjectOptions; -} - +type MongoDBDeletedOutput = { + deletedCount: number; +}; /** * Base class for all query services that use Typegoose. * @@ -135,7 +134,7 @@ export class MongooseQueryService */ async createOne(record: DeepPartial): Promise { this.ensureIdIsNotPresent(record); - return this.Model.create(record as CreateQuery); + return this.Model.create(record); } /** @@ -152,7 +151,7 @@ export class MongooseQueryService */ async createMany(records: DeepPartial[]): Promise { records.forEach((r) => this.ensureIdIsNotPresent(r)); - return this.Model.create(records as CreateQuery[]); + return this.Model.create(records); } /** @@ -234,7 +233,7 @@ export class MongooseQueryService */ async deleteMany(filter: Filter): Promise { const filterQuery = this.filterQueryBuilder.buildFilterQuery(filter); - const res = await this.Model.deleteMany(filterQuery).exec(); + const res = (await this.Model.deleteMany(filterQuery).exec()) as MongoDBDeletedOutput; return { deletedCount: res.deletedCount || 0 }; } diff --git a/packages/query-mongoose/src/services/reference-query.service.ts b/packages/query-mongoose/src/services/reference-query.service.ts index 8839e004c..1003769fe 100644 --- a/packages/query-mongoose/src/services/reference-query.service.ts +++ b/packages/query-mongoose/src/services/reference-query.service.ts @@ -11,7 +11,7 @@ import { Query, AssemblerFactory, } from '@nestjs-query/core'; -import { Document, Model as MongooseModel } from 'mongoose'; +import { Document, Model as MongooseModel, UpdateQuery } from 'mongoose'; import { AggregateBuilder, FilterQueryBuilder } from '../query'; import { isEmbeddedSchemaTypeOptions, @@ -198,7 +198,7 @@ export abstract class ReferenceQueryService { if (relationIds.length !== refCount) { throw new Error(`Unable to find all ${relationName} to add to ${this.Model.modelName}`); } - await entity.updateOne({ $push: { [relationName]: { $each: relationIds } } }).exec(); + await entity.updateOne({ $push: { [relationName]: { $each: relationIds } } } as UpdateQuery).exec(); // reload the document return this.getById(id); } @@ -215,7 +215,7 @@ export abstract class ReferenceQueryService { if (refCount !== 1) { throw new Error(`Unable to find ${relationName} to set on ${this.Model.modelName}`); } - await entity.updateOne({ [relationName]: relationId }).exec(); + await entity.updateOne({ [relationName]: relationId } as UpdateQuery).exec(); // reload the document return this.getById(id); } @@ -235,7 +235,7 @@ export abstract class ReferenceQueryService { await entity .updateOne({ $unset: { [relationName]: relationId }, - }) + } as UpdateQuery) .exec(); // reload the document return this.getById(id); @@ -259,7 +259,7 @@ export abstract class ReferenceQueryService { await entity .updateOne({ $pullAll: { [relationName]: relationIds }, - }) + } as UpdateQuery) .exec(); // reload the document return this.getById(id); @@ -291,18 +291,19 @@ export abstract class ReferenceQueryService { } private getReferenceModel(refName: string): MongooseModel { + const { db } = this.Model; if (this.isReferencePath(refName)) { const schemaType = this.Model.schema.path(refName); if (isEmbeddedSchemaTypeOptions(schemaType)) { - return this.Model.model(schemaType.$embeddedSchemaType.options.ref); + return db.model(schemaType.$embeddedSchemaType.options.ref); } if (isSchemaTypeWithReferenceOptions(schemaType)) { - return this.Model.model(schemaType.options.ref); + return db.model(schemaType.options.ref); } } else if (this.isVirtualPath(refName)) { const schemaType = this.Model.schema.virtualpath(refName); if (isVirtualTypeWithReferenceOptions(schemaType)) { - return this.Model.model(schemaType.options.ref); + return db.model(schemaType.options.ref); } } throw new Error(`Unable to lookup reference type for ${refName}`);