Skip to content

Commit 613ed0f

Browse files
committed
feat(NODE-4520): add wrappers async driver APIs
1 parent d856d1c commit 613ed0f

File tree

14 files changed

+1338
-3
lines changed

14 files changed

+1338
-3
lines changed

.mocharc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
33
"extension": ["js", "ts"],
44
"recursive": true,
5-
"failZero": false,
5+
"failZero": true,
66
"sort": true,
77
"color": true,
88
"require": [
99
"source-map-support/register",
10-
"ts-node/register"
10+
"ts-node/register",
11+
"test/hooks/addons.js"
1112
]
1213
}

src/index.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
11
'use strict';
22

3-
throw new Error('Work In Progress - Not intended for use at this time');
3+
const mongodb = require('mongodb');
4+
5+
const { makeLegacyMongoClient } = require('./legacy_wrappers/mongo_client');
6+
const { makeLegacyDb } = require('./legacy_wrappers/db');
7+
const { makeLegacyCollection } = require('./legacy_wrappers/collection');
8+
const { makeLegacyAdmin } = require('./legacy_wrappers/admin');
9+
const {
10+
makeLegacyAggregationCursor,
11+
makeLegacyFindCursor,
12+
makeLegacyListCollectionsCursor,
13+
makeLegacyListIndexesCursor
14+
} = require('./legacy_wrappers/cursors');
15+
const { makeLegacyGridFSBucket } = require('./legacy_wrappers/gridfs');
16+
const { makeLegacyChangeStream } = require('./legacy_wrappers/change_stream');
17+
18+
/** @type {import('..')} */
19+
module.exports = Object.create(null);
20+
21+
Object.defineProperty(module.exports, '__esModule', { value: true });
22+
23+
const classesWithAsyncAPIs = new Map([
24+
['Admin', makeLegacyAdmin],
25+
['FindCursor', makeLegacyFindCursor],
26+
['ListCollectionsCursor', makeLegacyListCollectionsCursor],
27+
['ListIndexesCursor', makeLegacyListIndexesCursor],
28+
['AggregationCursor', makeLegacyAggregationCursor],
29+
['ChangeStream', makeLegacyChangeStream],
30+
['Collection', makeLegacyCollection],
31+
['Db', makeLegacyDb],
32+
['GridFSBucket', makeLegacyGridFSBucket],
33+
['MongoClient', makeLegacyMongoClient]
34+
]);
35+
36+
for (const [mongodbExportName, mongodbExportValue] of Object.entries(mongodb)) {
37+
let makeLegacyClass = classesWithAsyncAPIs.get(mongodbExportName);
38+
if (makeLegacyClass != null) {
39+
// Maintain access to underlying classes
40+
Object.defineProperty(module.exports, `__original__${mongodbExportName}`, {
41+
enumerable: false,
42+
get: function () {
43+
return mongodbExportValue;
44+
}
45+
});
46+
const patchedClass = makeLegacyClass(mongodbExportValue);
47+
Object.defineProperty(module.exports, mongodbExportName, {
48+
enumerable: true,
49+
get: function () {
50+
return patchedClass;
51+
}
52+
});
53+
} else {
54+
Object.defineProperty(module.exports, mongodbExportName, {
55+
enumerable: true,
56+
get: function () {
57+
return mongodbExportValue;
58+
}
59+
});
60+
}
61+
}

src/legacy_wrappers/admin.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'use strict';
2+
3+
const { toLegacy, maybeCallback } = require('../utils');
4+
5+
module.exports = Object.create(null);
6+
7+
module.exports.makeLegacyAdmin = function (baseClass) {
8+
class LegacyAdmin extends baseClass {
9+
constructor(db) {
10+
if (db instanceof baseClass || db instanceof LegacyAdmin) {
11+
super(db.s.db);
12+
} else {
13+
super(db);
14+
}
15+
}
16+
17+
addUser(username, password, options, callback) {
18+
callback =
19+
typeof callback === 'function'
20+
? callback
21+
: typeof options === 'function'
22+
? options
23+
: typeof password === 'function'
24+
? password
25+
: undefined;
26+
options =
27+
typeof options !== 'function'
28+
? options
29+
: typeof password !== 'function'
30+
? password
31+
: undefined;
32+
return maybeCallback(super.addUser(username, password, options), callback);
33+
}
34+
35+
buildInfo(options, callback) {
36+
callback =
37+
typeof callback === 'function'
38+
? callback
39+
: typeof options === 'function'
40+
? options
41+
: undefined;
42+
options = typeof options !== 'function' ? options : undefined;
43+
return maybeCallback(super.buildInfo(options), callback);
44+
}
45+
46+
command(command, options, callback) {
47+
callback =
48+
typeof callback === 'function'
49+
? callback
50+
: typeof options === 'function'
51+
? options
52+
: undefined;
53+
options = typeof options !== 'function' ? options : undefined;
54+
return maybeCallback(super.command(command, options), callback);
55+
}
56+
57+
listDatabases(options, callback) {
58+
callback =
59+
typeof callback === 'function'
60+
? callback
61+
: typeof options === 'function'
62+
? options
63+
: undefined;
64+
options = typeof options !== 'function' ? options : undefined;
65+
return maybeCallback(super.listDatabases(options), callback);
66+
}
67+
68+
ping(options, callback) {
69+
callback =
70+
typeof callback === 'function'
71+
? callback
72+
: typeof options === 'function'
73+
? options
74+
: undefined;
75+
options = typeof options !== 'function' ? options : undefined;
76+
return maybeCallback(super.ping(options), callback);
77+
}
78+
79+
removeUser(username, options, callback) {
80+
callback =
81+
typeof callback === 'function'
82+
? callback
83+
: typeof options === 'function'
84+
? options
85+
: undefined;
86+
options = typeof options !== 'function' ? options : undefined;
87+
return maybeCallback(super.removeUser(username, options), callback);
88+
}
89+
90+
replSetGetStatus(options, callback) {
91+
callback =
92+
typeof callback === 'function'
93+
? callback
94+
: typeof options === 'function'
95+
? options
96+
: undefined;
97+
options = typeof options !== 'function' ? options : undefined;
98+
return maybeCallback(super.replSetGetStatus(options), callback);
99+
}
100+
101+
serverInfo(options, callback) {
102+
callback =
103+
typeof callback === 'function'
104+
? callback
105+
: typeof options === 'function'
106+
? options
107+
: undefined;
108+
options = typeof options !== 'function' ? options : undefined;
109+
return maybeCallback(super.serverInfo(options), callback);
110+
}
111+
112+
serverStatus(options, callback) {
113+
callback =
114+
typeof callback === 'function'
115+
? callback
116+
: typeof options === 'function'
117+
? options
118+
: undefined;
119+
options = typeof options !== 'function' ? options : undefined;
120+
return maybeCallback(super.serverStatus(options), callback);
121+
}
122+
123+
validateCollection(name, options, callback) {
124+
callback =
125+
typeof callback === 'function'
126+
? callback
127+
: typeof options === 'function'
128+
? options
129+
: undefined;
130+
options = typeof options !== 'function' ? options : undefined;
131+
return maybeCallback(super.validateCollection(name, options), callback);
132+
}
133+
}
134+
135+
Object.defineProperty(baseClass.prototype, toLegacy, {
136+
enumerable: false,
137+
value: function () {
138+
return new LegacyAdmin(this);
139+
}
140+
});
141+
142+
return LegacyAdmin;
143+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const { toLegacy, maybeCallback } = require('../utils');
4+
5+
module.exports = Object.create(null);
6+
7+
module.exports.makeLegacyChangeStream = function (baseClass) {
8+
class LegacyChangeStream extends baseClass {
9+
constructor(parent, pipeline, options) {
10+
if (parent instanceof baseClass || parent instanceof LegacyChangeStream) {
11+
super(parent.parent, parent.pipeline, parent.options);
12+
} else {
13+
super(parent, pipeline, options);
14+
}
15+
}
16+
17+
close(callback) {
18+
return maybeCallback(super.close(), callback);
19+
}
20+
hasNext(callback) {
21+
return maybeCallback(super.hasNext(), callback);
22+
}
23+
next(callback) {
24+
return maybeCallback(super.next(), callback);
25+
}
26+
tryNext(callback) {
27+
return maybeCallback(super.tryNext(), callback);
28+
}
29+
}
30+
31+
Object.defineProperty(baseClass.prototype, toLegacy, {
32+
enumerable: false,
33+
value: function () {
34+
return new LegacyChangeStream(this);
35+
}
36+
});
37+
38+
return LegacyChangeStream;
39+
};

0 commit comments

Comments
 (0)