Skip to content

Instantly share code, notes, and snippets.

@EarthenLynx
Last active January 20, 2020 19:00
Show Gist options
  • Save EarthenLynx/af99acbf8afdfb20edf3a7a964ababab to your computer and use it in GitHub Desktop.
Save EarthenLynx/af99acbf8afdfb20edf3a7a964ababab to your computer and use it in GitHub Desktop.
Javascript module. Exports a class to make communication with mongodb easier
const mc = require('mongodb').MongoClient;
class MonCon {
// Takes all the neede vars to connect to a database
constructor(host_ip, host_port, db_url, db_name, db_collection) {
this.host_ip = host_ip;
this.host_port = host_port;
this.db_url = db_url;
this.db_name = db_name;
this.db_collection = db_collection;
}
/* Create methods */
createCollection(dbName = this.db_name, colName) {
// Takes two strings and creates a collection in the selected database.
mc.connect(`${this.db_url} ${dbName}`, (err, db) => {
if (err) throw err;
let dbo = db.db(dbName);
dbo.createCollection(colName, (err, res) => {
if (err) throw err;
if (!err) {
console.log(`Successfully created collection ${colName} in ${dbName}`);
}
db.close();
});
});
}
cOne(newEntry) {
// Takes an object and creates a new db entry based on its values
mc.connect(this.db_url, (err, db) => {
if (err) throw err;
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).insertOne(newEntry, () => {
if (err) throw err;
if (!err) {
console.log(`Successfully inserted the following entry: ${newEntry.name}`);
}
db.close();
});
});
};
cMany(newEntries) {
// Takes an array of objects and creates new db entries based on its values
mc.connect(this.db_url, (err, db) => {
if (err) throw err;
let dbo = db.db(this.db_name);
for (let i = 0; i < newEntries.length; i++) {
dbo.collection(this.db_collection).insertOne(newEntries[i], () => {
if (err) throw err
if (!err) {
console.log(`Successfully inserted the following entry: ${newEntries[i].name}`);
}
});
db.close();
} // EndFor
});
}
/* Read methods */
rAll(callback) {
// Reads all entries from defined collection and returns results in callback
// Variable to be used is 'arr'
let arr;
mc.connect(this.db_url, (err, db) => {
if (err) throw err;
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).find({}).toArray((err, result) => {
if (err) throw err;
arr = result;
db.close();
// arr can be used with a callback function
callback(arr)
});
});
};
rFilter(query, callback) {
// Takes in an object key - value pair. Key is the propery that's being
// queried, value is the specified value to be searched for.
// Returns a callback that can be worked with.
console.log(query);
let arr;
mc.connect(this.db_url, (err, db) => {
if (err) throw err;
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).find(query)
.toArray((err, result) => {
if (err) throw err;
arr = result;
db.close();
callback(arr);
});
});
}
// rFilterSome(query, callback) {}
/* Update methods */
uOne(query, newEntry) {
/* Takes in two objects. First object is a key-value pair object.
* Key is entry to be changed. Value is pattern to be searched for.
* Second object is a setter ($set) and a linked object with the key-value
* pairs that are to be changed in the object that matches the query.
*
* Example
* updateCollectionEntryAll({name: "John"}, {adress: "New Hampshire"})
* => Filter first entry for name = John
* => Updates these entires adress attribute to New Hampshire
*/
mc.connect(this.db_url, (err, db) => {
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).updateOne(query, newEntry, (err, res) => {
if (err) throw err;
if (!err) {
console.log(`Successfully updated ${res.result.nModified} objects`);
}
db.close();
});
});
}
uMany(query, newEntry) {
/* Takes in two objects. First object is a key-value pair object.
* Key is entry to be changed. Value is pattern to be searched for.
* Second object is a setter ($set) and a linked object with the key-value
* pairs that are to be changed in the objects that match the query.
*
* Example
* updateCollectionEntryAll({name: "John"}, {adress: "New Hampshire"})
* => Filter all entries for name = John
* => Updates these entires adress attribute to New Hampshire
*/
mc.connect(this.db_url, (err, db) => {
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).updateMany(query, newEntry, (err, res) => {
if (err) throw err;
if (!err) {
console.log(`Successfully updated ${res.result.nModified} objects`);
}
db.close();
});
});
}
/* Delete methods */
dOne(query) {
// Takes in an object and deletes the first found entry from the database
mc.connect(this.db_url, (err, db) => {
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).deleteOne(query, (err, res) => {
if (err) throw err;
if (!err) {
console.log(`Removed ${res.result.n} document(s) from database`);
}
db.close()
});
});
}
dMany(query) {
// Takes in an object and deletes the first found entry from the database
mc.connect(this.db_url, (err, db) => {
let dbo = db.db(this.db_name);
dbo.collection(this.db_collection).deleteMany(query, (err, res) => {
if (err) throw err;
if (!err) {
console.log(`Removed ${res.result.n} document(s) from database`);
}
db.close()
});
});
}
/* Class Set Methods */
set setDb(dbName) {
// Change the object's database
this.db_name = dbName;
}
set setCol(colName) {
// Change the object's collection
this.db_collection = colName;
}
}
module.exports = MonCon;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment