Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
"extension": ["js", "ts"],
"recursive": true,
"failZero": false,
"failZero": true,
"sort": true,
"color": true,
"require": [
"source-map-support/register",
"ts-node/register"
"ts-node/register",
"test/hooks/addons.js"
]
}
78 changes: 77 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,79 @@
'use strict';

throw new Error('Work In Progress - Not intended for use at this time');
const mongodb = require('mongodb');

const { makeLegacyMongoClient } = require('./legacy_wrappers/mongo_client');
const { makeLegacyDb } = require('./legacy_wrappers/db');
const { makeLegacyCollection } = require('./legacy_wrappers/collection');
const { makeLegacyAdmin } = require('./legacy_wrappers/admin');
const {
makeLegacyAggregationCursor,
makeLegacyFindCursor,
makeLegacyListCollectionsCursor,
makeLegacyListIndexesCursor
} = require('./legacy_wrappers/cursors');
const {
makeLegacyGridFSBucket,
makeLegacyGridFSBucketWriteStream
} = require('./legacy_wrappers/gridfs');
const { makeLegacyChangeStream } = require('./legacy_wrappers/change_stream');
const { makeLegacyClientSession } = require('./legacy_wrappers/session');
const {
makeLegacyUnorderedBulkOperation,
makeLegacyOrderedBulkOperation
} = require('./legacy_wrappers/bulk');

/** @type {import('..')} */
module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

const classesWithAsyncAPIs = new Map([
['Admin', makeLegacyAdmin],
['FindCursor', makeLegacyFindCursor],
['ListCollectionsCursor', makeLegacyListCollectionsCursor],
['ListIndexesCursor', makeLegacyListIndexesCursor],
['AggregationCursor', makeLegacyAggregationCursor],
['ChangeStream', makeLegacyChangeStream],
['Collection', makeLegacyCollection],
['Db', makeLegacyDb],
['GridFSBucket', makeLegacyGridFSBucket],
['ClientSession', makeLegacyClientSession],
['MongoClient', makeLegacyMongoClient],

// Need to be exported top-level still
['ClientSession', makeLegacyClientSession],
['GridFSBucketWriteStream', makeLegacyGridFSBucketWriteStream],
['OrderedBulkOperation', makeLegacyOrderedBulkOperation],
['UnorderedBulkOperation', makeLegacyUnorderedBulkOperation]
]);

const TODO_SPECIAL_IMPORTS = new Map([
['ClientSession', '/lib/sessions'],
['GridFSBucketWriteStream', '/lib/gridfs/upload'],
['OrderedBulkOperation', '/lib/bulk/ordered'],
['UnorderedBulkOperation', '/lib/bulk/unordered']
]);

for (const [missingTopLevelClassName, location] of TODO_SPECIAL_IMPORTS) {
mongodb[missingTopLevelClassName] = require(`mongodb${location}`)[missingTopLevelClassName];
}

for (const [mongodbExportName, mongodbExportValue] of Object.entries(mongodb)) {
let makeLegacyClass = classesWithAsyncAPIs.get(mongodbExportName);
if (makeLegacyClass != null) {
const patchedClass = makeLegacyClass(mongodbExportValue);
Object.defineProperty(module.exports, mongodbExportName, {
enumerable: true,
get: function () {
return patchedClass;
}
});
} else {
Object.defineProperty(module.exports, mongodbExportName, {
enumerable: true,
get: function () {
return mongodbExportValue;
}
});
}
}
136 changes: 136 additions & 0 deletions src/legacy_wrappers/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict';

const { toLegacy, maybeCallback } = require('../utils');

module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

module.exports.makeLegacyAdmin = function (baseClass) {
class LegacyAdmin extends baseClass {
addUser(username, password, options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: typeof password === 'function'
? password
: undefined;
options =
typeof options !== 'function'
? options
: typeof password !== 'function'
? password
: undefined;
return maybeCallback(super.addUser(username, password, options), callback);
}

buildInfo(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.buildInfo(options), callback);
}

command(command, options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.command(command, options), callback);
}

listDatabases(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.listDatabases(options), callback);
}

ping(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.ping(options), callback);
}

removeUser(username, options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.removeUser(username, options), callback);
}

replSetGetStatus(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.replSetGetStatus(options), callback);
}

serverInfo(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.serverInfo(options), callback);
}

serverStatus(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.serverStatus(options), callback);
}

validateCollection(name, options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.validateCollection(name, options), callback);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyAdmin.prototype);
}
});

return LegacyAdmin;
};
36 changes: 36 additions & 0 deletions src/legacy_wrappers/bulk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const { maybeCallback } = require('../utils');

module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

module.exports.makeLegacyOrderedBulkOperation = function (baseClass) {
return class LegacyOrderedBulkOperation extends baseClass {
execute(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.execute(options), callback);
}
};
};

module.exports.makeLegacyUnorderedBulkOperation = function (baseClass) {
return class LegacyUnorderedBulkOperation extends baseClass {
execute(options, callback) {
callback =
typeof callback === 'function'
? callback
: typeof options === 'function'
? options
: undefined;
options = typeof options !== 'function' ? options : undefined;
return maybeCallback(super.execute(options), callback);
}
};
};
32 changes: 32 additions & 0 deletions src/legacy_wrappers/change_stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const { toLegacy, maybeCallback } = require('../utils');

module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

module.exports.makeLegacyChangeStream = function (baseClass) {
class LegacyChangeStream extends baseClass {
close(callback) {
return maybeCallback(super.close(), callback);
}
hasNext(callback) {
return maybeCallback(super.hasNext(), callback);
}
next(callback) {
return maybeCallback(super.next(), callback);
}
tryNext(callback) {
return maybeCallback(super.tryNext(), callback);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyChangeStream.prototype);
}
});

return LegacyChangeStream;
};
Loading