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

Commit d91dee5

Browse files
tough-griffao10
authored andcommitted
Fix mongodb sinks to work with a real db backend
1 parent 575702e commit d91dee5

File tree

3 files changed

+89
-54
lines changed

3 files changed

+89
-54
lines changed
Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,7 @@
11
'use strict';
22

3-
const { EventEmitter } = require('events');
4-
const { Db, Collection } = require('mongodb');
5-
6-
const origEval = Db.prototype.eval;
7-
Db.prototype.eval = async function overloadedEval(code, params, opts) {
8-
// run the original eval asynchronously, ignoring any errors it will throw since we aren't connected.
9-
try {
10-
origEval.call(this, code, params, opts);
11-
} catch (err) {
12-
// throw it away
13-
console.log(err);
14-
}
15-
return { code };
16-
};
17-
18-
const origRename = Collection.prototype.rename;
19-
Collection.prototype.rename = async function overloadedRename(name, options, callback) {
20-
try {
21-
origCreateCollection.call(this, name, options, callback);
22-
} catch (err) {
23-
console.log(err);
24-
}
25-
return { name }
26-
};
27-
28-
const topology = new EventEmitter();
29-
debugger;
30-
const db = new Db('testbench', topology, {});
31-
// Call Collection constructor
32-
const collection = new Collection(db, topology, 'testbench', 'collection', null, {});
33-
console.log(collection);
34-
// const collection = db.createCollection('testCollection', { capped: true}, (result) => {
35-
// console.log(result);
36-
// });
37-
38-
3+
const { MongoClient } = require('mongodb');
4+
const url = process.env.MONGODB_URI || 'mongodb://localhost:27017/';
395

406
/**
417
* @param {string} input user input string
@@ -49,8 +15,13 @@ module.exports['mongodb.Db.prototype.eval'] = async function _eval(
4915
) {
5016
if (noop) return 'NOOP';
5117

18+
const client = await MongoClient.connect(url);
19+
5220
const fn = safe ? 'function() {}' : input;
53-
const result = await db.eval(fn);
21+
const result = await client
22+
.db('testbench')
23+
.eval(fn)
24+
.catch(() => ({ code: fn }));
5425
return `<pre>${JSON.stringify(result, null, 2)}</pre>`;
5526
};
5627

@@ -64,15 +35,14 @@ module.exports['mongodb.Collection.prototype.rename'] = async function rename(
6435
input,
6536
{ safe = false, noop = false } = {}
6637
) {
67-
const newname = safe ? 'newName' : input
68-
const result = await collection.rename(newName);
6938
if (noop) return 'NOOP';
7039

71-
if (safe) {
72-
return Collection.rename('newName', options, () => {});
73-
} else {
74-
// Pass in input somewhere
75-
return Collection.rename(input, options, callback);
76-
// return Collection.rename(input, options, callback);
77-
}
40+
const client = await MongoClient.connect(url);
41+
const collection = await client.db('testbench').createCollection('new');
42+
43+
const newName = safe ? 'newName' : input;
44+
const result = await collection
45+
.rename(newName)
46+
.catch(() => ({ name: newName }));
47+
return `<pre>${JSON.stringify(result, null, 2)}</pre>`;
7848
};

package-lock.json

Lines changed: 72 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"libxmljs": "^0.19.7",
2929
"libxmljs2": "^0.22.0",
3030
"lodash": "^4.17.15",
31-
"mongodb": "^3.3.0",
31+
"mongodb": "^3.5.2",
3232
"mysql": "^2.17.1",
3333
"node-fetch": "^2.6.0",
3434
"node-serialize": "0.0.4",

0 commit comments

Comments
 (0)