Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add native driver mongodb v3 and v4 support #357

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module.exports = {
clearMocks: true,
collectCoverageFrom: ['src/**/*.js'],
testEnvironment: 'node',
moduleNameMapper: {
'^mongodbMapped$': `mongodb${process.env.DRIVER_VERSION || ''}`,
},
testTimeout: 15000,
};
54,787 changes: 12,213 additions & 42,574 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 9 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 && DRIVER=native DRIVER_VERSION=v3 jest && DRIVER=native DRIVER_VERSION=v2 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,16 @@
"@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": "^4.17.0",
"mongodbv3": "npm:mongodb@^3.7.4",
"mongodbv2": "npm:mongodb@^2.2.36",
"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
5 changes: 3 additions & 2 deletions src/aggregate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const _ = require('underscore');
const sanitizeParams = require('./utils/sanitizeParams');
const { prepareResponse, generateSort, generateCursorQuery } = require('./utils/query');

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

/**
* Performs an aggregate() query on a passed-in Mongo collection, using criteria you specify.
Expand Down
22 changes: 18 additions & 4 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,9 +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
let query;
if (findMethod === COLLECTION_METHODS.FIND_AS_CURSOR) {
query = collection[findMethod]({ $and: [cursorQuery, params.query] }, params.fields);
} else {
query = collection[findMethod]({ $and: [cursorQuery, params.query] }).project(params.fields);
}

/**
* IMPORTANT
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const config = require('./config');
const aggregate = require('./aggregate');
const config = require('./config');
const find = require('./find');
const findWithReq = require('./findWithReq');
const mongoosePlugin = require('./mongoose.plugin');
const search = require('./search');
const sanitizeQuery = require('./utils/sanitizeQuery');
const { encodePaginationTokens } = require('./utils/query');
const mongoosePlugin = require('./mongoose.plugin');
const sanitizeQuery = require('./utils/sanitizeQuery');

module.exports = {
config,
Expand Down
3 changes: 2 additions & 1 deletion src/mongoose.plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('underscore');

const find = require('./find');
const search = require('./search');
const _ = require('underscore');

/**
* Mongoose plugin
Expand Down
1 change: 1 addition & 0 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('underscore');

const config = require('./config');
const bsonUrlEncoding = require('./utils/bsonUrlEncoding');

Expand Down
2 changes: 1 addition & 1 deletion src/utils/bsonUrlEncoding.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { EJSON } = require('bson');
const base64url = require('base64-url');
const { EJSON } = require('bson');

// BSON can't encode undefined values, so we will use this value instead:
const BSON_UNDEFINED = '__mixmax__undefined__';
Expand Down
3 changes: 2 additions & 1 deletion src/utils/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const bsonUrlEncoding = require('./bsonUrlEncoding');
const objectPath = require('object-path');

const bsonUrlEncoding = require('./bsonUrlEncoding');

/**
* Helper function to encode pagination tokens.
*
Expand Down
2 changes: 1 addition & 1 deletion src/utils/resolveFields.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('underscore');
const { ProjectionFieldSet } = require('projection-utils');
const _ = require('underscore');

/**
* Produce a ProjectionFieldSet from the given mongo projection, after validating it to ensure it
Expand Down
1 change: 1 addition & 0 deletions src/utils/sanitizeParams.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('underscore');

const bsonUrlEncoding = require('./bsonUrlEncoding');
const getPropertyViaDotNotation = require('./getPropertyViaDotNotation');
const config = require('../config');
Expand Down
1 change: 1 addition & 0 deletions src/utils/sanitizeQuery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('underscore');

const resolveFields = require('./resolveFields');

/**
Expand Down
28 changes: 16 additions & 12 deletions test/aggregate.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const paging = require('../');
const dbUtils = require('./support/db');
const _ = require('underscore');

const driver = process.env.DRIVER;
const dbUtils = require('./support/db');
const paging = require('../');

let mongod;
const driver = process.env.DRIVER;

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 @@ -45,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 @@ -59,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 @@ -85,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 @@ -115,7 +116,7 @@ describe('aggregate', () => {
]),
t.db
.collection('test_null_values')
.insert(
.insertMany(
[
undefined,
undefined,
Expand All @@ -131,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
40 changes: 22 additions & 18 deletions test/find.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const paging = require('../');
const dbUtils = require('./support/db');
const _ = require('underscore');
const { ObjectId } = require('mongoist');
const driver = process.env.DRIVER;
const _ = require('underscore');

let mongod;
const dbUtils = require('./support/db');
const paging = require('../');
const driver = process.env.DRIVER;

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 @@ -45,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 @@ -77,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 @@ -103,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 @@ -121,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 @@ -139,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 @@ -159,7 +160,7 @@ describe('find', () => {
counter: 1,
},
]),
t.db.collection('test_sorting').insert([
t.db.collection('test_sorting').insertMany([
{
name: 'Alpha',
},
Expand All @@ -179,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 @@ -196,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 @@ -519,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 @@ -710,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 @@ -1072,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
Loading
Loading