Skip to content

Instantly share code, notes, and snippets.

@giobyte8
Created April 1, 2015 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giobyte8/ddb5cd1239a06627c70c to your computer and use it in GitHub Desktop.
Save giobyte8/ddb5cd1239a06627c70c to your computer and use it in GitHub Desktop.
Parte 3 | Creando un sistema de chat sobre NodeJS con Socket.IO, ExpressJS, MongoDB, Foundation y Openshift
/**
* Data Access Object (DAO) para 'messages'
* Debe ser construido con un objeto conectado a la
* base de datos
*
* @Created on: 29 March, 2015
*/
function MessageDAO(db) {
/**
* Si el constructor es llamado sin el operados 'new'
* entonces 'this' apunta al objeto global, muestra una advertencia
* y lo llama correctamente.
*/
if (false == (this instanceof MessageDAO)) {
console.log('WARNING: MessageDAO constructor called without "new" operator');
return new MessageDAO(db);
}
/** Colección 'messages' en la base de datos */
var messages = db.collection('messages');
this.addMessage = function (username, date, message, callback) {
var message = {'username': username, 'date': date, 'message': message};
messages.insert(message, function (err, result) {
if (err) return callback(err, null);
console.log('Message saved');
return callback(null, result[0]);
});
}
this.getLatest = function (limit, callback) {
var qryOptions = {
'sort': [['date', 'desc']],
'limit': limit
}
messages.find({}, qryOptions).toArray(function (err, rmessages) {
if (err) return callback(err, null);
return callback(null, rmessages);
});
}
}
module.exports.MessageDAO = MessageDAO;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment