Skip to content

Commit 9c64b88

Browse files
gusth-sahishank
authored andcommitted
feat(mongodb): add support to native mongodb 3+
keeping backward compatibility with version 2
1 parent 2110240 commit 9c64b88

File tree

11 files changed

+10848
-42781
lines changed

11 files changed

+10848
-42781
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

package-lock.json

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

package.json

Lines changed: 7 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"
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,14 @@
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": "^3.7.4",
6363
"mongodb-memory-server": "6.9.6",
6464
"mongoist": "^2.7.0",
65-
"mongoose": "5.11.10",
65+
"mongoose": "^5.13.20",
6666
"prettier": "^1.19.1",
6767
"semantic-release": "^17.2.3"
6868
},

src/find.js

Lines changed: 16 additions & 3 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,10 +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

6068
const query = collection[findMethod]({ $and: [cursorQuery, params.query] }, params.fields);
6169

70+
// Required to support native mongodb 3+ and keep the backward compatibility with version 2
71+
if (findMethod === COLLECTION_METHODS.FIND) {
72+
query.project(params.fields);
73+
}
74+
6275
/**
6376
* IMPORTANT
6477
*

test/aggregate.test.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ const paging = require('../');
55

66
const driver = process.env.DRIVER;
77

8-
let mongod;
9-
108
describe('aggregate', () => {
9+
let mongod;
10+
let client;
1111
const t = {};
1212
beforeAll(async () => {
1313
mongod = dbUtils.start();
14-
t.db = await dbUtils.db(mongod, driver);
14+
({ db: t.db, client } = await dbUtils.db(mongod, driver));
1515

1616
// Set up collections once for testing later.
1717
await Promise.all([
18-
t.db.collection('test_paging').insert([
18+
t.db.collection('test_paging').insertMany([
1919
{
2020
counter: 1,
2121
},
@@ -46,7 +46,7 @@ describe('aggregate', () => {
4646
color: 'blue',
4747
},
4848
]),
49-
t.db.collection('test_aggregation').insert([
49+
t.db.collection('test_aggregation').insertMany([
5050
{
5151
items: [1, 2, 3],
5252
},
@@ -60,7 +60,7 @@ describe('aggregate', () => {
6060
items: [2, 4, 5],
6161
},
6262
]),
63-
t.db.collection('test_aggregation_lookup').insert([
63+
t.db.collection('test_aggregation_lookup').insertMany([
6464
{
6565
_id: 1,
6666
name: 'mercury',
@@ -86,15 +86,15 @@ describe('aggregate', () => {
8686
name: 'saturn',
8787
},
8888
]),
89-
t.db.collection('test_aggregation_lookup').ensureIndex(
89+
t.db.collection('test_aggregation_lookup').createIndex(
9090
{
9191
name: 'text',
9292
},
9393
{
9494
name: 'test_index',
9595
}
9696
),
97-
t.db.collection('test_aggregation_sort').insert([
97+
t.db.collection('test_aggregation_sort').insertMany([
9898
{
9999
name: 'Alpha',
100100
},
@@ -116,7 +116,7 @@ describe('aggregate', () => {
116116
]),
117117
t.db
118118
.collection('test_null_values')
119-
.insert(
119+
.insertMany(
120120
[
121121
undefined,
122122
undefined,
@@ -132,7 +132,10 @@ describe('aggregate', () => {
132132
]);
133133
});
134134

135-
afterAll(() => mongod.stop());
135+
afterAll(async () => {
136+
await (client ? client.close() : t.db.close());
137+
await mongod.stop();
138+
});
136139

137140
beforeEach(() => {
138141
paging.config.COLLATION = undefined;

test/find.test.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ const dbUtils = require('./support/db');
55
const paging = require('../');
66
const driver = process.env.DRIVER;
77

8-
let mongod;
9-
108
describe('find', () => {
9+
let mongod;
10+
let client;
1111
const t = {};
1212
beforeAll(async () => {
1313
mongod = dbUtils.start();
14-
t.db = await dbUtils.db(mongod, driver);
14+
({ db: t.db, client } = await dbUtils.db(mongod, driver));
1515

1616
// Set up collections once for testing later.
1717
await Promise.all([
18-
t.db.collection('test_paging').insert([
18+
t.db.collection('test_paging').insertMany([
1919
{
2020
counter: 1,
2121
},
@@ -46,7 +46,7 @@ describe('find', () => {
4646
color: 'blue',
4747
},
4848
]),
49-
t.db.collection('test_duplicate_custom_fields').insert([
49+
t.db.collection('test_duplicate_custom_fields').insertMany([
5050
{
5151
_id: 6,
5252
counter: 6,
@@ -78,7 +78,7 @@ describe('find', () => {
7878
timestamp: 1477347772077,
7979
},
8080
]),
81-
t.db.collection('test_paging_custom_fields').insert([
81+
t.db.collection('test_paging_custom_fields').insertMany([
8282
{
8383
counter: 6,
8484
timestamp: 1477347800603,
@@ -104,7 +104,7 @@ describe('find', () => {
104104
timestamp: 1477347755654,
105105
},
106106
]),
107-
t.db.collection('test_paging_date').insert([
107+
t.db.collection('test_paging_date').insertMany([
108108
{
109109
counter: 2,
110110
date: new Date(1477347763813),
@@ -122,7 +122,7 @@ describe('find', () => {
122122
date: new Date(1477347755654),
123123
},
124124
]),
125-
t.db.collection('test_paging_date_in_object').insert([
125+
t.db.collection('test_paging_date_in_object').insertMany([
126126
{
127127
counter: 2,
128128
start: { date: new Date(1477347763813) },
@@ -140,7 +140,7 @@ describe('find', () => {
140140
start: { date: new Date(1477347755654) },
141141
},
142142
]),
143-
t.db.collection('test_paging_limits').insert([
143+
t.db.collection('test_paging_limits').insertMany([
144144
{
145145
counter: 6,
146146
},
@@ -160,7 +160,7 @@ describe('find', () => {
160160
counter: 1,
161161
},
162162
]),
163-
t.db.collection('test_sorting').insert([
163+
t.db.collection('test_sorting').insertMany([
164164
{
165165
name: 'Alpha',
166166
},
@@ -180,7 +180,7 @@ describe('find', () => {
180180
name: 'aleph',
181181
},
182182
]),
183-
t.db.collection('test_null_values').insert(
183+
t.db.collection('test_null_values').insertMany(
184184
[
185185
undefined,
186186
undefined,
@@ -197,7 +197,10 @@ describe('find', () => {
197197
]);
198198
});
199199

200-
afterAll(() => mongod.stop());
200+
afterAll(async () => {
201+
await (client ? client.close() : t.db.close());
202+
await mongod.stop();
203+
});
201204

202205
beforeEach(() => {
203206
paging.config.COLLATION = undefined;
@@ -520,7 +523,7 @@ describe('find', () => {
520523

521524
it('uses the hint parameter', async () => {
522525
const collection = t.db.collection('test_paging');
523-
await t.db.collection('test_paging').ensureIndex({ color: 1 }, { name: 'color_1' });
526+
await t.db.collection('test_paging').createIndex({ color: 1 }, { name: 'color_1' });
524527
// First page.
525528
const res = await paging.find(collection, {
526529
query: {
@@ -711,7 +714,7 @@ describe('find', () => {
711714

712715
describe('when using strings as _ids', () => {
713716
beforeEach(async () => {
714-
await t.db.collection('test_paging_string_ids').insert([
717+
await t.db.collection('test_paging_string_ids').insertMany([
715718
{
716719
_id: new ObjectId().toString(),
717720
counter: 1,
@@ -1073,7 +1076,7 @@ describe('find', () => {
10731076
const collection = t.db.collection('test_paging_string_ids');
10741077
await t.db
10751078
.collection('test_paging_string_ids')
1076-
.ensureIndex({ color: 1 }, { name: 'color_1' });
1079+
.createIndex({ color: 1 }, { name: 'color_1' });
10771080
// First page.
10781081
const res = await paging.find(collection, {
10791082
query: {

test/findWithReq.test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const driver = process.env.DRIVER;
77

88
describe('findWithReq', () => {
99
let mongod;
10+
let client;
1011
const t = {};
1112
beforeAll(async () => {
1213
mongod = dbUtils.start();
13-
t.db = await dbUtils.db(mongod, driver);
14+
({ db: t.db, client } = await dbUtils.db(mongod, driver));
1415

1516
await Promise.all([
16-
t.db.collection('test_paging').insert([
17+
t.db.collection('test_paging').insertMany([
1718
{
1819
counter: 1,
1920
myfield1: 'a',
@@ -35,7 +36,7 @@ describe('findWithReq', () => {
3536
myfield2: 'b',
3637
},
3738
]),
38-
t.db.collection('test_paging_fields').insert({
39+
t.db.collection('test_paging_fields').insertOne({
3940
obj: {
4041
one: 1,
4142
two: {
@@ -52,7 +53,11 @@ describe('findWithReq', () => {
5253
}),
5354
]);
5455
});
55-
afterAll(() => mongod.stop());
56+
57+
afterAll(async () => {
58+
await (client ? client.close() : t.db.close());
59+
await mongod.stop();
60+
});
5661

5762
describe('basic usage', () => {
5863
it('queries first few pages', async () => {

test/mongoosePlugin.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let mongod;
2929
describe('mongoose plugin', () => {
3030
beforeAll(async () => {
3131
mongod = await dbUtils.start();
32-
await mongoose.connect(await mongod.getConnectionString());
32+
await mongoose.connect(await mongod.getUri());
3333
await mongoose.connection.db.dropDatabase();
3434
const author = await Author.create({ name: 'Pawan Pandey' });
3535

@@ -47,11 +47,12 @@ describe('mongoose plugin', () => {
4747
}
4848

4949
await Post.create(posts);
50-
await Author.ensureIndexes();
51-
await Post.ensureIndexes();
50+
await Author.createIndexes();
51+
await Post.createIndexes();
5252
});
5353

5454
afterAll(async () => {
55+
await mongoose.disconnect();
5556
await mongod.stop();
5657
});
5758

0 commit comments

Comments
 (0)