Skip to content

Commit c003bd4

Browse files
authored
Merge pull request #357 from mixmaxhq/feature/add-native-mongodb3-support
feat: add native driver mongodb v3 and v4 support
2 parents df29232 + 6f3b566 commit c003bd4

21 files changed

+12347
-42647
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jspm_packages
3737
.node_repl_history
3838

3939
*.sublime-workspace
40+
.idea
4041

4142
# Node 6 transpiled code.
4243
dist/node

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module.exports = {
22
clearMocks: true,
33
collectCoverageFrom: ['src/**/*.js'],
44
testEnvironment: 'node',
5+
moduleNameMapper: {
6+
'^mongodbMapped$': `mongodb${process.env.DRIVER_VERSION || ''}`,
7+
},
8+
testTimeout: 15000,
59
};

package-lock.json

Lines changed: 12213 additions & 42574 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"lint": "eslint .",
1717
"prepublishOnly": "npm run babelBuild && if [ \"$CI\" = '' ]; then node -p 'JSON.parse(process.env.npm_package_config_manualPublishMessage)'; exit 1; fi",
1818
"semantic-release": "SEMANTIC_COMMITLINT_SKIP=f4543f643bac890c627d538e6200c5f5a1d45ebc semantic-release",
19-
"test": "npm run babelBuild; DRIVER=mongoist jest --forceExit && DRIVER=native jest --forceExit"
19+
"test": "npm run babelBuild; DRIVER=mongoist jest --forceExit && DRIVER=native jest && DRIVER=native DRIVER_VERSION=v3 jest && DRIVER=native DRIVER_VERSION=v2 jest"
2020
},
2121
"repository": {
2222
"type": "git",
@@ -40,10 +40,10 @@
4040
"dependencies": {
4141
"base64-url": "^2.2.0",
4242
"bson": "^4.7.0",
43-
"object-path": "^0.11.5",
43+
"object-path": "^0.11.8",
4444
"projection-utils": "^1.1.0",
4545
"semver": "^5.4.1",
46-
"underscore": "^1.9.2"
46+
"underscore": "^1.12.1"
4747
},
4848
"devDependencies": {
4949
"@babel/cli": "^7.18.10",
@@ -55,14 +55,16 @@
5555
"@mixmaxhq/semantic-release-config": "^2.0.0",
5656
"babel-jest": "^29.0.0",
5757
"cz-conventional-changelog": "^3.2.0",
58-
"eslint": "^6.8.0",
58+
"eslint": "^7.32.0",
5959
"eslint-config-mixmax": "^4.11.2",
60-
"jest": "^26.0.1",
60+
"jest": "^29.6.2",
6161
"mockgoose": "^8.0.4",
62-
"mongodb": "^2.2.11",
62+
"mongodb": "^4.17.0",
63+
"mongodbv3": "npm:mongodb@^3.7.4",
64+
"mongodbv2": "npm:mongodb@^2.2.36",
6365
"mongodb-memory-server": "6.9.6",
6466
"mongoist": "^2.7.0",
65-
"mongoose": "5.11.10",
67+
"mongoose": "^5.13.20",
6668
"prettier": "^1.19.1",
6769
"semantic-release": "^17.2.3"
6870
},

src/aggregate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const _ = require('underscore');
2-
const sanitizeParams = require('./utils/sanitizeParams');
3-
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');
2+
43
const config = require('./config');
4+
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');
5+
const sanitizeParams = require('./utils/sanitizeParams');
56

67
/**
78
* Performs an aggregate() query on a passed-in Mongo collection, using criteria you specify.

src/find.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
const _ = require('underscore');
2-
const sanitizeParams = require('./utils/sanitizeParams');
3-
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');
2+
43
const aggregate = require('./aggregate');
54
const config = require('./config');
5+
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');
6+
const sanitizeParams = require('./utils/sanitizeParams');
7+
8+
const COLLECTION_METHODS = {
9+
FIND: 'find',
10+
FIND_AS_CURSOR: 'findAsCursor',
11+
};
612

713
/**
814
* Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
@@ -55,9 +61,17 @@ module.exports = async function(collection, params) {
5561

5662
// Support both the native 'mongodb' driver and 'mongoist'. See:
5763
// https://www.npmjs.com/package/mongoist#cursor-operations
58-
const findMethod = collection.findAsCursor ? 'findAsCursor' : 'find';
64+
const findMethod = collection.findAsCursor
65+
? COLLECTION_METHODS.FIND_AS_CURSOR
66+
: COLLECTION_METHODS.FIND;
5967

60-
const query = collection[findMethod]({ $and: [cursorQuery, params.query] }, params.fields);
68+
// Required to support native mongodb 3+ and keep the backward compatibility with version 2
69+
let query;
70+
if (findMethod === COLLECTION_METHODS.FIND_AS_CURSOR) {
71+
query = collection[findMethod]({ $and: [cursorQuery, params.query] }, params.fields);
72+
} else {
73+
query = collection[findMethod]({ $and: [cursorQuery, params.query] }).project(params.fields);
74+
}
6175

6276
/**
6377
* IMPORTANT

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const config = require('./config');
21
const aggregate = require('./aggregate');
2+
const config = require('./config');
33
const find = require('./find');
44
const findWithReq = require('./findWithReq');
5+
const mongoosePlugin = require('./mongoose.plugin');
56
const search = require('./search');
6-
const sanitizeQuery = require('./utils/sanitizeQuery');
77
const { encodePaginationTokens } = require('./utils/query');
8-
const mongoosePlugin = require('./mongoose.plugin');
8+
const sanitizeQuery = require('./utils/sanitizeQuery');
99

1010
module.exports = {
1111
config,

src/mongoose.plugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const _ = require('underscore');
2+
13
const find = require('./find');
24
const search = require('./search');
3-
const _ = require('underscore');
45

56
/**
67
* Mongoose plugin

src/search.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('underscore');
2+
23
const config = require('./config');
34
const bsonUrlEncoding = require('./utils/bsonUrlEncoding');
45

src/utils/bsonUrlEncoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { EJSON } = require('bson');
21
const base64url = require('base64-url');
2+
const { EJSON } = require('bson');
33

44
// BSON can't encode undefined values, so we will use this value instead:
55
const BSON_UNDEFINED = '__mixmax__undefined__';

0 commit comments

Comments
 (0)