Last active
July 12, 2019 18:15
-
-
Save newbenhd/50a046dc7ef90e98ebeebfc7ec865c10 to your computer and use it in GitHub Desktop.
mongodb client wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CRUD operator | |
const mongodb = require("mongodb"); | |
class dbClient { | |
constructor() { | |
this.MongoClient = mongodb.MongoClient; | |
this.connectionURL = ""; | |
this.client = undefined; | |
this.error = undefined; | |
this.database = undefined; | |
} | |
connect(connectionURL) { | |
return new Promise(async (resolve, reject) => { | |
try { | |
this.client = await this.MongoClient.connect(this.connectionURL, { | |
useNewUrlParser: true | |
}); | |
this.connectURL = connectionURL; | |
resolve(this.client); | |
} catch (error) { | |
this.error = { | |
message: "connection error to mongodb client. try again later.", | |
code: 500, | |
error | |
}; | |
this.client = undefined; | |
reject(this.error); | |
} | |
}); | |
} | |
setDatabase(databaseName) { | |
if (this.error) { | |
console.error(this.error); | |
this.database = undefined; | |
return; | |
} else if (!this.client) { | |
this.error = { | |
message: "client not found.", | |
code: 500 | |
}; | |
this.database = undefined; | |
return; | |
} else { | |
this.database = this.client.db(databaseName); | |
} | |
} | |
getDatabase() { | |
return this.database; | |
} | |
createOne(collectionName, doc, callback) { | |
if (this.error) { | |
console.error(this.error); | |
return; | |
} else if (!this.database) { | |
this.error = { | |
message: "database not found.", | |
code: 500 | |
}; | |
} else { | |
this.database | |
.collection(collectionName) | |
.insertOne(doc, (error, result) => { | |
callback(error, result); | |
}); | |
} | |
} | |
createMany(collectionName, arrayDoc, callback) { | |
if (this.error) { | |
console.error(this.error); | |
return; | |
} else if (!this.database) { | |
this.error = { | |
message: "database not found.", | |
code: 500 | |
}; | |
return; | |
} else { | |
this.database | |
.collection(collectionName) | |
.insertMany(arrayDoc, (error, result) => { | |
callback(error, result); | |
}); | |
} | |
} | |
readOne(collectionName, query, callback) {} | |
updateOne(collectionName, query, doc, callback) {} | |
deleteOne(collectionName, query, callback) {} | |
} | |
const connectionURL = "mongodb://127.0.0.1:27018"; | |
const bot = new dbClient(); | |
bot | |
.connect(connectionURL) | |
.then(client => { | |
client.setDatabase("task-manager"); | |
client.createMany( | |
"tasks", | |
[ | |
{ description: "End badminton membership", completed: false }, | |
{ description: "Read bible 5 chapters", completed: false }, | |
{ description: "Go to friday service", completed: false } | |
], | |
(error, result) => { | |
if (error) { | |
console.log(error); | |
} else { | |
console.log( | |
"successfully added three documents to tasks collection on task-manager db!" | |
); | |
} | |
} | |
); | |
}) | |
.catch(e => { | |
console.log(e); | |
}); | |
module.exports = dbClient; | |
/** log **/ | |
/* | |
{ | |
message: 'connection error to mongodb client. try again later.', | |
code: 500, | |
error: MongoParseError: Invalid connection string | |
at parseConnectionString (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb-core/lib/uri_parser.js:505:21) | |
at connect (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/operations/mongo_client_ops.js:195:3) | |
at connectOp (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3) | |
at /Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/utils.js:433:24 | |
at new Promise (<anonymous>) | |
at executeOperation (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/utils.js:428:10) | |
at MongoClient.connect (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/mongo_client.js:175:10) | |
at Function.MongoClient.connect (/Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/node_modules/mongodb/lib/mongo_client.js:341:22) | |
at /Users/myroot/Note/Node/The-Complete-Node.js-Developer-Course-3rd-Edition/node-course/task-manager/mongodb.js:16:46 | |
at new Promise (<anonymous>) { | |
name: 'MongoParseError', | |
[Symbol(mongoErrorContextSymbol)]: {} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment