Skip to content

Commit

Permalink
chore: update mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
meabed committed Nov 2, 2023
1 parent bde3887 commit ab4574e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 13 deletions.
14 changes: 9 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js'],
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
transform: {
'^.+\\.ts$': ['ts-jest', {
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
}],
'^.+\\.ts$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
},
],
'^.+\\.js$': 'babel-jest',
},
roots: ['<rootDir>/src'],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions src/composeMongoose.ts
Original file line number Diff line number Diff line change
@@ -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 = {
/**
Expand Down Expand Up @@ -120,6 +121,8 @@ export function composeMongoose<TDoc extends Document, TContext = any>(
`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)
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/count-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('count() ->', () => {
'users',
'countDocuments',
{},
Number(version[0]) == 7
Number(version[0]) < 7
? undefined
: {
limit: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/helpers/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type ValidationsWithMessage = {

export async function validateDoc(doc: Document): Promise<ValidationsWithMessage | null> {
const validations: MongooseError.ValidationError | null =
Number(MongooseVersion[0]) > 7
Number(MongooseVersion[0]) >= 7
? doc.validateSync()
: await new Promise((resolve) => doc.validate(resolve as any));

Expand Down
5 changes: 5 additions & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { patchMongooseSchemaIndex } from './utils/patchMongoose';

global.beforeAll(() => {

Check failure on line 3 in src/setupTests.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Check failure on line 3 in src/setupTests.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Check failure on line 3 in src/setupTests.ts

View workflow job for this annotation

GitHub Actions / tests (20.x)

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
patchMongooseSchemaIndex();
});
42 changes: 42 additions & 0 deletions src/utils/patchMongoose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mongoose, { IndexDefinition, IndexOptions } from 'mongoose';

export function renameObjKey(
oldObj: Record<string, unknown>,
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<string, unknown>
);
}

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() {}
5 changes: 3 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ab4574e

Please sign in to comment.