Skip to content

Commit

Permalink
feat(mongodb): add support to native mongodb 3+
Browse files Browse the repository at this point in the history
keeping backward compatibility with version 2
  • Loading branch information
gusth-sa authored and hishank committed Aug 13, 2024
1 parent 2110240 commit 9c64b88
Show file tree
Hide file tree
Showing 11 changed files with 10,848 additions and 42,781 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jspm_packages
.node_repl_history

*.sublime-workspace
.idea

# Node 6 transpiled code.
dist/node
53,476 changes: 10,748 additions & 42,728 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint": "eslint .",
"prepublishOnly": "npm run babelBuild && if [ \"$CI\" = '' ]; then node -p 'JSON.parse(process.env.npm_package_config_manualPublishMessage)'; exit 1; fi",
"semantic-release": "SEMANTIC_COMMITLINT_SKIP=f4543f643bac890c627d538e6200c5f5a1d45ebc semantic-release",
"test": "npm run babelBuild; DRIVER=mongoist jest --forceExit && DRIVER=native jest --forceExit"
"test": "npm run babelBuild; DRIVER=mongoist jest --forceExit && DRIVER=native jest"
},
"repository": {
"type": "git",
Expand All @@ -40,10 +40,10 @@
"dependencies": {
"base64-url": "^2.2.0",
"bson": "^4.7.0",
"object-path": "^0.11.5",
"object-path": "^0.11.8",
"projection-utils": "^1.1.0",
"semver": "^5.4.1",
"underscore": "^1.9.2"
"underscore": "^1.12.1"
},
"devDependencies": {
"@babel/cli": "^7.18.10",
Expand All @@ -55,14 +55,14 @@
"@mixmaxhq/semantic-release-config": "^2.0.0",
"babel-jest": "^29.0.0",
"cz-conventional-changelog": "^3.2.0",
"eslint": "^6.8.0",
"eslint": "^7.32.0",
"eslint-config-mixmax": "^4.11.2",
"jest": "^26.0.1",
"jest": "^29.6.2",
"mockgoose": "^8.0.4",
"mongodb": "^2.2.11",
"mongodb": "^3.7.4",
"mongodb-memory-server": "6.9.6",
"mongoist": "^2.7.0",
"mongoose": "5.11.10",
"mongoose": "^5.13.20",
"prettier": "^1.19.1",
"semantic-release": "^17.2.3"
},
Expand Down
19 changes: 16 additions & 3 deletions src/find.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const _ = require('underscore');
const sanitizeParams = require('./utils/sanitizeParams');
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');

const aggregate = require('./aggregate');
const config = require('./config');
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');
const sanitizeParams = require('./utils/sanitizeParams');

const COLLECTION_METHODS = {
FIND: 'find',
FIND_AS_CURSOR: 'findAsCursor',
};

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

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

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

// Required to support native mongodb 3+ and keep the backward compatibility with version 2
if (findMethod === COLLECTION_METHODS.FIND) {
query.project(params.fields);
}

/**
* IMPORTANT
*
Expand Down
23 changes: 13 additions & 10 deletions test/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const paging = require('../');

const driver = process.env.DRIVER;

let mongod;

describe('aggregate', () => {
let mongod;
let client;
const t = {};
beforeAll(async () => {
mongod = dbUtils.start();
t.db = await dbUtils.db(mongod, driver);
({ db: t.db, client } = await dbUtils.db(mongod, driver));

// Set up collections once for testing later.
await Promise.all([
t.db.collection('test_paging').insert([
t.db.collection('test_paging').insertMany([
{
counter: 1,
},
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('aggregate', () => {
color: 'blue',
},
]),
t.db.collection('test_aggregation').insert([
t.db.collection('test_aggregation').insertMany([
{
items: [1, 2, 3],
},
Expand All @@ -60,7 +60,7 @@ describe('aggregate', () => {
items: [2, 4, 5],
},
]),
t.db.collection('test_aggregation_lookup').insert([
t.db.collection('test_aggregation_lookup').insertMany([
{
_id: 1,
name: 'mercury',
Expand All @@ -86,15 +86,15 @@ describe('aggregate', () => {
name: 'saturn',
},
]),
t.db.collection('test_aggregation_lookup').ensureIndex(
t.db.collection('test_aggregation_lookup').createIndex(
{
name: 'text',
},
{
name: 'test_index',
}
),
t.db.collection('test_aggregation_sort').insert([
t.db.collection('test_aggregation_sort').insertMany([
{
name: 'Alpha',
},
Expand All @@ -116,7 +116,7 @@ describe('aggregate', () => {
]),
t.db
.collection('test_null_values')
.insert(
.insertMany(
[
undefined,
undefined,
Expand All @@ -132,7 +132,10 @@ describe('aggregate', () => {
]);
});

afterAll(() => mongod.stop());
afterAll(async () => {
await (client ? client.close() : t.db.close());
await mongod.stop();
});

beforeEach(() => {
paging.config.COLLATION = undefined;
Expand Down
33 changes: 18 additions & 15 deletions test/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ const dbUtils = require('./support/db');
const paging = require('../');
const driver = process.env.DRIVER;

let mongod;

describe('find', () => {
let mongod;
let client;
const t = {};
beforeAll(async () => {
mongod = dbUtils.start();
t.db = await dbUtils.db(mongod, driver);
({ db: t.db, client } = await dbUtils.db(mongod, driver));

// Set up collections once for testing later.
await Promise.all([
t.db.collection('test_paging').insert([
t.db.collection('test_paging').insertMany([
{
counter: 1,
},
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('find', () => {
color: 'blue',
},
]),
t.db.collection('test_duplicate_custom_fields').insert([
t.db.collection('test_duplicate_custom_fields').insertMany([
{
_id: 6,
counter: 6,
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('find', () => {
timestamp: 1477347772077,
},
]),
t.db.collection('test_paging_custom_fields').insert([
t.db.collection('test_paging_custom_fields').insertMany([
{
counter: 6,
timestamp: 1477347800603,
Expand All @@ -104,7 +104,7 @@ describe('find', () => {
timestamp: 1477347755654,
},
]),
t.db.collection('test_paging_date').insert([
t.db.collection('test_paging_date').insertMany([
{
counter: 2,
date: new Date(1477347763813),
Expand All @@ -122,7 +122,7 @@ describe('find', () => {
date: new Date(1477347755654),
},
]),
t.db.collection('test_paging_date_in_object').insert([
t.db.collection('test_paging_date_in_object').insertMany([
{
counter: 2,
start: { date: new Date(1477347763813) },
Expand All @@ -140,7 +140,7 @@ describe('find', () => {
start: { date: new Date(1477347755654) },
},
]),
t.db.collection('test_paging_limits').insert([
t.db.collection('test_paging_limits').insertMany([
{
counter: 6,
},
Expand All @@ -160,7 +160,7 @@ describe('find', () => {
counter: 1,
},
]),
t.db.collection('test_sorting').insert([
t.db.collection('test_sorting').insertMany([
{
name: 'Alpha',
},
Expand All @@ -180,7 +180,7 @@ describe('find', () => {
name: 'aleph',
},
]),
t.db.collection('test_null_values').insert(
t.db.collection('test_null_values').insertMany(
[
undefined,
undefined,
Expand All @@ -197,7 +197,10 @@ describe('find', () => {
]);
});

afterAll(() => mongod.stop());
afterAll(async () => {
await (client ? client.close() : t.db.close());
await mongod.stop();
});

beforeEach(() => {
paging.config.COLLATION = undefined;
Expand Down Expand Up @@ -520,7 +523,7 @@ describe('find', () => {

it('uses the hint parameter', async () => {
const collection = t.db.collection('test_paging');
await t.db.collection('test_paging').ensureIndex({ color: 1 }, { name: 'color_1' });
await t.db.collection('test_paging').createIndex({ color: 1 }, { name: 'color_1' });
// First page.
const res = await paging.find(collection, {
query: {
Expand Down Expand Up @@ -711,7 +714,7 @@ describe('find', () => {

describe('when using strings as _ids', () => {
beforeEach(async () => {
await t.db.collection('test_paging_string_ids').insert([
await t.db.collection('test_paging_string_ids').insertMany([
{
_id: new ObjectId().toString(),
counter: 1,
Expand Down Expand Up @@ -1073,7 +1076,7 @@ describe('find', () => {
const collection = t.db.collection('test_paging_string_ids');
await t.db
.collection('test_paging_string_ids')
.ensureIndex({ color: 1 }, { name: 'color_1' });
.createIndex({ color: 1 }, { name: 'color_1' });
// First page.
const res = await paging.find(collection, {
query: {
Expand Down
13 changes: 9 additions & 4 deletions test/findWithReq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const driver = process.env.DRIVER;

describe('findWithReq', () => {
let mongod;
let client;
const t = {};
beforeAll(async () => {
mongod = dbUtils.start();
t.db = await dbUtils.db(mongod, driver);
({ db: t.db, client } = await dbUtils.db(mongod, driver));

await Promise.all([
t.db.collection('test_paging').insert([
t.db.collection('test_paging').insertMany([
{
counter: 1,
myfield1: 'a',
Expand All @@ -35,7 +36,7 @@ describe('findWithReq', () => {
myfield2: 'b',
},
]),
t.db.collection('test_paging_fields').insert({
t.db.collection('test_paging_fields').insertOne({
obj: {
one: 1,
two: {
Expand All @@ -52,7 +53,11 @@ describe('findWithReq', () => {
}),
]);
});
afterAll(() => mongod.stop());

afterAll(async () => {
await (client ? client.close() : t.db.close());
await mongod.stop();
});

describe('basic usage', () => {
it('queries first few pages', async () => {
Expand Down
7 changes: 4 additions & 3 deletions test/mongoosePlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let mongod;
describe('mongoose plugin', () => {
beforeAll(async () => {
mongod = await dbUtils.start();
await mongoose.connect(await mongod.getConnectionString());
await mongoose.connect(await mongod.getUri());
await mongoose.connection.db.dropDatabase();
const author = await Author.create({ name: 'Pawan Pandey' });

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

await Post.create(posts);
await Author.ensureIndexes();
await Post.ensureIndexes();
await Author.createIndexes();
await Post.createIndexes();
});

afterAll(async () => {
await mongoose.disconnect();
await mongod.stop();
});

Expand Down
Loading

0 comments on commit 9c64b88

Please sign in to comment.