Skip to content

Commit

Permalink
feat: add mongoose v8 support (@meabed, #439)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop support for mongoose v5 & v4. Node v16 is a minimal required version.
  • Loading branch information
meabed authored Nov 15, 2023
1 parent 05ccdf0 commit 32bb8e5
Show file tree
Hide file tree
Showing 31 changed files with 1,703 additions and 1,957 deletions.
57 changes: 57 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
root = true

[*]
charset = utf-8
max_line_length = 120
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[{Makefile, makefile}]
indent_style = tab

[*.go]
indent_style = tab

[*.proto]
indent_size = 2

[*.swift]
indent_size = 4

[*.tmpl]
indent_size = 2

[{*.js, *.ts, *.jsx, *.tsx}]
indent_size = 2

[*.html]
indent_size = 2

[*.bat]
end_of_line = crlf

[*.{json, yml, yaml}]
indent_size = 2

[.{babelrc, eslintrc}]
indent_size = 2

[{Fastfile, .buckconfig, BUCK}]
indent_size = 2

[*.diff]
indent_size = 1

[*.m]
indent_size = 1
indent_style = space

[*.java]
indent_size = 4
indent_style = space

[*.md]
trim_trailing_whitespace = false
26 changes: 13 additions & 13 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ jobs:
contents: write
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x]
steps:
- run: echo "🎉 The job was triggered by a ${{ github.event_name }} event."
- uses: styfle/cancel-workflow-action@0.11.0
- uses: styfle/cancel-workflow-action@0.12.0
with:
workflow_id: nodejs.yml
access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: FranzDiebold/github-env-vars-action@v2
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install node_modules
Expand All @@ -32,16 +32,16 @@ jobs:
run: yarn test
env:
CI: true
- name: Testing with previous mongoose versions 6
run: yarn test-prev-vers-6
- name: Testing with previous mongoose versions 7
run: yarn test-prev-vers-7
env:
CI: true
- name: Testing with previous mongoose versions 5
run: yarn test-prev-vers-5
- name: Testing with previous mongoose versions 6
run: yarn test-prev-vers-6
env:
CI: true
- name: Send codecov.io stats
if: matrix.node-version == '14.x'
if: matrix.node-version == '18.x'
run: bash <(curl -s https://codecov.io/bash) || echo ''

publish:
Expand All @@ -52,11 +52,11 @@ jobs:
packages: write
contents: write
steps:
- uses: actions/checkout@v3
- name: Use Node.js 14
uses: actions/setup-node@v3
- uses: actions/checkout@v4
- name: Use Node.js 18
uses: actions/setup-node@v4
with:
node-version: 14.x
node-version: 18.x
- name: Install node_modules
run: yarn install
- name: Build
Expand Down
16 changes: 8 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
},
},
moduleFileExtensions: ['ts', 'js'],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.ts$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
},
],
'^.+\\.js$': 'babel-jest',
},
roots: ['<rootDir>/src'],
Expand Down
39 changes: 21 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@
},
"peerDependencies": {
"graphql-compose": "^7.21.4 || ^8.0.0 || ^9.0.0",
"mongoose": "^7.0.0 || ^6.0.0 || ^5.0.0 || ^4.4.0"
"mongoose": "^8.0.0 || ^7.0.0 || ^6.0.0"
},
"devDependencies": {
"@types/jest": "28.1.8",
"@typescript-eslint/eslint-plugin": "5.54.0",
"@typescript-eslint/parser": "5.54.0",
"eslint": "8.35.0",
"@types/jest": "29.5.8",
"@typescript-eslint/eslint-plugin": "6.11.0",
"@typescript-eslint/parser": "6.11.0",
"eslint": "8.53.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-prettier": "8.6.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-prettier": "4.2.1",
"graphql": "16.6.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-prettier": "5.0.1",
"graphql": "16.8.1",
"graphql-compose": "9.0.10",
"jest": "28.1.3",
"mongodb-memory-server": "8.11.5",
"mongoose": "7.0.0",
"prettier": "2.8.4",
"jest": "29.7.0",
"mongodb-memory-server": "9.0.1",
"mongoose": "8.0.0",
"prettier": "3.1.0",
"request": "2.88.2",
"rimraf": "4.1.3",
"ts-jest": "28.0.8",
"typescript": "4.9.5"
"rimraf": "5.0.5",
"ts-jest": "29.1.1",
"typescript": "5.2.2"
},
"scripts": {
"prepare": "tsc -p ./tsconfig.build.json",
Expand All @@ -64,7 +64,10 @@
"link": "yarn build && yarn link graphql-compose && yarn link graphql-compose-connection && yarn link graphql-compose-pagination && yarn link mongoose && yarn link",
"unlink": "rimraf node_modules && yarn install",
"semantic-release": "semantic-release",
"test-prev-vers-6": "yarn add [email protected] --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock",
"test-prev-vers-5": "yarn add [email protected] --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock"
"test-prev-vers-7": "yarn add [email protected] --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock",
"test-prev-vers-6": "yarn add [email protected] --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock"
},
"engines": {
"node": ">=16.0.0"
}
}
27 changes: 17 additions & 10 deletions src/__mocks__/mongooseCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@

import mongoose from 'mongoose';
import MongoMemoryServer from 'mongodb-memory-server-core';
import net, { AddressInfo } from 'net';

const { Schema, Types } = mongoose;

mongoose.Promise = Promise;

export async function getPortFree() {
return new Promise<number>((res) => {
const srv = net.createServer();
srv.listen(0, () => {
const port = (srv.address() as AddressInfo).port;
srv.close(() => res(port));
});
});
}

const originalConnect = mongoose.connect;
mongoose.createConnection = (async () => {
const mongoServer = await MongoMemoryServer.create();
const mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();

// originalConnect.bind(mongoose)(mongoUri, { useMongoClient: true }); // mongoose 4
originalConnect.bind(mongoose)(
mongoUri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
} as any /* for tests compatibility with mongoose v5 & v6 */
); // mongoose 5
// originalConnect.bind(mongoose)(mongoUri, {}); // mongoose 6
originalConnect.bind(mongoose)(mongoUri, {});

mongoose.connection.on('error', (e) => {
if (e.message.code === 'ETIMEDOUT') {
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/userModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const UserSchema = new Schema(
);

UserSchema.set('autoIndex', false);
UserSchema.index({ name: 1, age: -1 });
UserSchema.index({ n: 1, age: -1 });

// eslint-disable-next-line
UserSchema.virtual('nameVirtual').get(function (this: any) {
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`integration tests projection should request all fields to rawData field: projection from all fields 1`] = `
Array [
[
"__v",
"_id",
"age",
Expand All @@ -24,9 +24,9 @@ Array [
`;

exports[`integration tests projection should request only fields from query: projection from query fields 1`] = `
Object {
"data": Object {
"user": Object {
{
"data": {
"user": {
"name": "Name",
},
},
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/117-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/120-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/128-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/135-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/136-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/157-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/github_issues/194-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
import { getPortFree } from '../../__mocks__/mongooseCommon';

let mongoServer: MongoMemoryServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
mongoServer = await MongoMemoryServer.create({
instance: {
port: await getPortFree(),
},
});
const mongoUri = mongoServer.getUri();
await mongoose.connect(
mongoUri,
Expand Down
Loading

0 comments on commit 32bb8e5

Please sign in to comment.