From ab4574e6fb7aaebffd50b6fcd85a00b90ee8d34a Mon Sep 17 00:00:00 2001 From: Mohamed Meabed Date: Thu, 2 Nov 2023 13:54:18 -0400 Subject: [PATCH] chore: update mongoose --- jest.config.js | 14 +++++---- package.json | 2 +- src/composeMongoose.ts | 9 ++++-- src/resolvers/__tests__/count-test.ts | 2 +- src/resolvers/helpers/validate.ts | 2 +- src/setupTests.ts | 5 ++++ src/utils/patchMongoose.ts | 42 +++++++++++++++++++++++++++ yarn.lock | 5 ++-- 8 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/setupTests.ts create mode 100644 src/utils/patchMongoose.ts diff --git a/jest.config.js b/jest.config.js index 64c814ad..2fb13eed 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,12 +2,16 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', moduleFileExtensions: ['ts', 'js'], + setupFilesAfterEnv: ['/src/setupTests.ts'], transform: { - '^.+\\.ts$': ['ts-jest', { - tsconfig: '/tsconfig.json', - isolatedModules: true, - diagnostics: false, - }], + '^.+\\.ts$': [ + 'ts-jest', + { + tsconfig: '/tsconfig.json', + isolatedModules: true, + diagnostics: false, + }, + ], '^.+\\.js$': 'babel-jest', }, roots: ['/src'], diff --git a/package.json b/package.json index 46000541..1fd332d4 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "graphql-compose": "9.0.10", "jest": "29.7.0", "mongodb-memory-server": "9.0.1", - "mongoose": "meabed/mongoose#fix-index-alias", + "mongoose": "8.0.0", "prettier": "3.0.3", "request": "2.88.2", "rimraf": "5.0.5", diff --git a/src/composeMongoose.ts b/src/composeMongoose.ts index 0a3cbbec..2c13c35c 100644 --- a/src/composeMongoose.ts +++ b/src/composeMongoose.ts @@ -1,9 +1,10 @@ -import type { SchemaComposer, Resolver, InputTypeComposer } from 'graphql-compose'; -import { schemaComposer as globalSchemaComposer, ObjectTypeComposer } from 'graphql-compose'; -import type { Model, Document } from 'mongoose'; +import type { InputTypeComposer, Resolver, SchemaComposer } from 'graphql-compose'; +import { ObjectTypeComposer, schemaComposer as globalSchemaComposer } from 'graphql-compose'; +import type { Document, Model } from 'mongoose'; import { convertModelToGraphQL } from './fieldsConverter'; import { resolverFactory } from './resolvers'; import MongoID from './types/MongoID'; +import { patchMongooseSchemaIndex } from './utils/patchMongoose'; export type TypeConverterInputTypeOpts = { /** @@ -120,6 +121,8 @@ export function composeMongoose( `You try to generate GraphQL Type with name ${name} from mongoose model but this type already exists in SchemaComposer. Please choose another type name "composeWithMongoose(model, { name: 'NewTypeName' })", or reuse existed type "schemaComposer.getOTC('TypeName')", or remove type from SchemaComposer before calling composeWithMongoose method "schemaComposer.delete('TypeName')".` ); } + patchMongooseSchemaIndex(); + if (sc.has(m.schema)) { // looks like you want to generate new TypeComposer from model // so remove cached model (which is used for cross-reference types) diff --git a/src/resolvers/__tests__/count-test.ts b/src/resolvers/__tests__/count-test.ts index 29b7ba6b..6fcd149f 100644 --- a/src/resolvers/__tests__/count-test.ts +++ b/src/resolvers/__tests__/count-test.ts @@ -112,7 +112,7 @@ describe('count() ->', () => { 'users', 'countDocuments', {}, - Number(version[0]) == 7 + Number(version[0]) < 7 ? undefined : { limit: 1, diff --git a/src/resolvers/helpers/validate.ts b/src/resolvers/helpers/validate.ts index 78f63fa2..34edd291 100644 --- a/src/resolvers/helpers/validate.ts +++ b/src/resolvers/helpers/validate.ts @@ -21,7 +21,7 @@ export type ValidationsWithMessage = { export async function validateDoc(doc: Document): Promise { const validations: MongooseError.ValidationError | null = - Number(MongooseVersion[0]) > 7 + Number(MongooseVersion[0]) >= 7 ? doc.validateSync() : await new Promise((resolve) => doc.validate(resolve as any)); diff --git a/src/setupTests.ts b/src/setupTests.ts new file mode 100644 index 00000000..58f75362 --- /dev/null +++ b/src/setupTests.ts @@ -0,0 +1,5 @@ +import { patchMongooseSchemaIndex } from './utils/patchMongoose'; + +global.beforeAll(() => { + patchMongooseSchemaIndex(); +}); diff --git a/src/utils/patchMongoose.ts b/src/utils/patchMongoose.ts new file mode 100644 index 00000000..c62da024 --- /dev/null +++ b/src/utils/patchMongoose.ts @@ -0,0 +1,42 @@ +import mongoose, { IndexDefinition, IndexOptions } from 'mongoose'; + +export function renameObjKey( + oldObj: Record, + oldKey: string, + newKey: string | number +) { + const keys = Object.keys(oldObj); + return keys.reduce( + (acc, val) => { + if (val === oldKey) { + acc[newKey] = oldObj[oldKey]; + } else { + acc[val] = oldObj[val]; + } + return acc; + }, + {} as Record + ); +} + +const oldMongooseSchemaIndexDef = mongoose.Schema.prototype.index; +mongoose.Schema.prototype.index = function (fields: IndexDefinition, options?: IndexOptions) { + // @ts-ignore + if (mongoose.Schema.prototype.indexMethodPatched) { + return oldMongooseSchemaIndexDef.call(this, fields, options); + } + fields || (fields = {}); + options || (options = {}); + for (const key in fields) { + // @ts-ignore + if (this.aliases[key]) { + // @ts-ignore + fields = renameObjKey(fields, key, this.aliases[key]); + } + } + // @ts-ignore + mongoose.Schema.prototype.indexMethodPatched = true; + return oldMongooseSchemaIndexDef.call(this, fields, options); +}; + +export function patchMongooseSchemaIndex() {} diff --git a/yarn.lock b/yarn.lock index a4da5543..7fc4bb1e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3498,9 +3498,10 @@ mongodb@^5.9.0: optionalDependencies: "@mongodb-js/saslprep" "^1.1.0" -mongoose@meabed/mongoose#fix-index-alias: +mongoose@8.0.0: version "8.0.0" - resolved "https://codeload.github.com/meabed/mongoose/tar.gz/a506cc4d78b471d5d1c5d33d399c061e60ea46b4" + resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-8.0.0.tgz#f14175eebfaf5256855d7cbd58bf862819b3fd12" + integrity sha512-PzwkLgm1Jhj0NQdgGfnFsu0QP9V1sBFgbavEgh/IPAUzKAagzvEhuaBuAQOQGjczVWnpIU9tBqyd02cOTgsPlA== dependencies: bson "^6.2.0" kareem "2.5.1"