Created
April 1, 2015 03:00
-
-
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
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
/** | |
* 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