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 ) }  ; 
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 ) }  ; 
7848} ; 
0 commit comments