Skip to content
This repository was archived by the owner on Mar 20, 2020. It is now read-only.

Commit 840246b

Browse files
author
ao10
committed
Stub internal mongo functions
1 parent a4fd8e3 commit 840246b

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed
Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
'use strict';
22

3-
const { MongoClient } = require('mongodb');
4-
//const mock = require('mongodb-mock-server');
5-
// const server = mock.createServer();
6-
const url = process.env.MONGODB_URI || 'mongodb://localhost:27017/';
7-
83
const { EventEmitter } = require('events');
94
const { Db, Collection } = require('mongodb');
105

116
const origEval = Db.prototype.eval;
127
Db.prototype.eval = async function overloadedEval(code, params, opts) {
8+
this.s.topology.s = { promiseLibrary: this.s.promiseLibrary };
9+
this.s.topology.command = function() {
10+
return false;
11+
};
12+
this.s.topology.hasSessionSupport = function() {
13+
return false;
14+
};
15+
this.s.topology.isDestroyed = function() {
16+
return false;
17+
};
1318
// run the original eval asynchronously, ignoring any errors it will throw since we aren't connected.
1419
try {
1520
origEval.call(this, code, params, opts);
@@ -20,22 +25,8 @@ Db.prototype.eval = async function overloadedEval(code, params, opts) {
2025
return { code };
2126
};
2227

23-
const origRename = Collection.prototype.rename;
24-
Collection.prototype.rename = async function overloadedRename(name, options, callback) {
25-
this.s.topology.s = { promiseLibrary: this.s.promiseLibrary };
26-
this.s.topology.command = function() { return false; };
27-
this.s.topology.hasSessionSupport = function() { return false; }
28-
try {
29-
origRename.call(this, name, options, callback);
30-
} catch (err) {
31-
console.log(err);
32-
}
33-
return { name };
34-
};
35-
3628
const topology = new EventEmitter();
3729
const db = new Db('testbench', topology, {});
38-
// Call Collection constructor
3930
const collection = new Collection(
4031
db,
4132
topology,
@@ -44,6 +35,27 @@ const collection = new Collection(
4435
null,
4536
{}
4637
);
38+
const origRename = Collection.prototype.rename;
39+
Collection.prototype.rename = async function overloadedRename(
40+
name,
41+
options,
42+
callback
43+
) {
44+
this.s.topology.s = { promiseLibrary: this.s.promiseLibrary };
45+
this.s.topology.command = function() {
46+
return false;
47+
};
48+
this.s.topology.hasSessionSupport = function() {
49+
return false;
50+
};
51+
// run the original rename asynchronously, ignoring any errors it will throw since we aren't connected.
52+
try {
53+
origRename.call(this, name, options, callback);
54+
} catch (err) {
55+
console.log(err);
56+
}
57+
return { name };
58+
};
4759

4860
/**
4961
* @param {string} input user input string
@@ -57,15 +69,9 @@ module.exports['mongodb.Db.prototype.eval'] = async function _eval(
5769
) {
5870
if (noop) return 'NOOP';
5971

60-
// const client = await MongoClient.connect(url);
61-
debugger;
62-
6372
const fn = safe ? 'function() {}' : input;
6473
const result = await db.eval(fn);
65-
// const result = await client
66-
// .db('testbench')
67-
// .eval(fn)
68-
// .catch(() => ({ code: fn }));
74+
6975
return `<pre>${JSON.stringify(result, null, 2)}</pre>`;
7076
};
7177

@@ -79,17 +85,10 @@ module.exports['mongodb.Collection.prototype.rename'] = async function rename(
7985
input,
8086
{ safe = false, noop = false } = {}
8187
) {
82-
const newName = safe ? 'newName' : input;
83-
// const result = await collection.rename(newName);
8488
if (noop) return 'NOOP';
85-
const result = await collection.rename(newName);
86-
return `<pre>${JSON.stringify(result, null, 2)}</pre>`;
87-
};
8889

89-
// const client = await MongoClient.connect(url);
90-
// const collection = await client.db('testbench').createCollection('new');
90+
const newName = safe ? 'newName' : input;
91+
const result = await collection.rename(newName).catch((err) => {});
9192

92-
// const newName = safe ? 'newName' : input;
93-
// const result = await collection
94-
// .rename(newName)
95-
// .catch(() => ({ name: newName }));
93+
return `<pre>${JSON.stringify(result, null, 2)}</pre>`;
94+
};

0 commit comments

Comments
 (0)