Last active
February 18, 2021 15:28
-
-
Save JavascriptMick/4720947 to your computer and use it in GitHub Desktop.
Simple Node.js Utility module to enable easy creation of models using node-mongodb-native
This file contains hidden or 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
/* | |
Simple Node.js Utility module to enable easy creation of models using node-mongodb-native | |
Useage:- | |
var dao = require('./mongodb-dao'); | |
exports.NewClient = function(clientId, plan){ | |
return { | |
clientId: clientId, | |
plan: plan, | |
joinDate: new Date() | |
}; | |
}; | |
exports.StoreClient = function (Client, theCallbackFunction) { | |
dao.Store('clients', Client, { | |
clientId: Client.clientId | |
}, theCallbackFunction); | |
}; | |
exports.DeleteClient = function (ClientId, theCallbackFunction) { | |
dao.Delete('clients', { | |
clientId: ClientId | |
}, theCallbackFunction); | |
}; | |
exports.RetrieveClient = function (ClientId, theCallbackFunction) { | |
dao.FindOne('clients', { | |
clientId: ClientId | |
}, theCallbackFunction); | |
}; | |
exports.RetrieveClients = function (theCallbackFunction) { | |
dao.Find('clients', {}, theCallbackFunction); | |
}; | |
*/ | |
//setup the db connection | |
var mongo = require('mongodb'); | |
var DBServer = mongo.Server; | |
var Db = mongo.Db | |
var config = { | |
mongo_db_address = 'localhost'; | |
mongo_db_port = 27017; | |
mongo_db_name = 'mydatabase'; | |
mongo_db_user = null; | |
mongo_db_pass = null; | |
} | |
var dbServer = new DBServer(config.mongo_db_address, config.mongo_db_port, { | |
auto_reconnect: true | |
}); | |
var db = new Db(config.mongo_db_name, dbServer, { | |
safe: false | |
}); | |
var checkDB = function (successCallback, errorCallback) { | |
if(db.openCalled === false) { | |
db.open(function (err, db_p) { | |
if(err) { | |
errorCallback(err); | |
} else { | |
if(config.mongo_db_user != null) { | |
db.authenticate(config.mongo_db_user, config.mongo_db_pass, function (err, replies) { | |
if(err) { | |
errorCallback(err); | |
} else { | |
successCallback(); | |
} | |
}); | |
} else { | |
successCallback(); | |
} | |
} | |
}); | |
} else { | |
successCallback(); | |
} | |
} | |
exports.Store = function (collectionName, item, key, theCallbackFunction) { | |
checkDB(function () { | |
delete item['_id']; | |
db.collection(collectionName, function (err, items) { | |
if(err == null) { | |
items.update(key, item, { | |
safe: true, | |
upsert: true | |
}, function (err, result) { | |
theCallbackFunction(err, result); //just pass the result back.. lazy, also non-DAO ish... the result has a db semantic? | |
}); | |
} else { | |
theCallbackFunction(err, 0); //0 result indicates no update was made | |
} | |
}); | |
}, function (err) { | |
theCallbackFunction(err, 0); | |
}); | |
}; | |
exports.Delete = function (collectionName, key, theCallbackFunction) { | |
checkDB(function () { | |
db.collection(collectionName, function (err, items) { | |
if(err == null) { | |
items.remove(key, function () { | |
theCallbackFunction(); //just pass the result back.. lazy, also non-DAO ish... the result has a db semantic? | |
}); | |
} else { | |
theCallbackFunction(err, 0); //0 result indicates no update was made | |
} | |
}); | |
}, function (err) { | |
theCallbackFunction(err, 0); | |
}); | |
}; | |
exports.FindOne = function (collectionName, key, theCallbackFunction) { | |
checkDB(function () { | |
db.collection(collectionName, function (err, items) { | |
items.findOne(key, function (err, item) { | |
if(item != null) { | |
theCallbackFunction(item); | |
} else { | |
theCallbackFunction(null); | |
} | |
}); | |
}); | |
}, function (err) { | |
theCallbackFunction(err, 0); | |
}); | |
}; | |
exports.Find = function (collectionName, key, theCallbackFunction) { | |
checkDB(function () { | |
db.collection(collectionName, function (err, itemsLink) { | |
itemsLink.find(key).toArray(function (err, items) { | |
if(items != null) { | |
theCallbackFunction(items); | |
} else { | |
theCallbackFunction(null); | |
} | |
}); | |
}); | |
}, function (err) { | |
theCallbackFunction(err, 0); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment