-
-
Save taterbase/a486f67e2edd0160e68f to your computer and use it in GitHub Desktop.
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
db = require "../lib/db" | |
Q = require "q" | |
_ = require "lodash" | |
GROUP = "messages" | |
getNewId = (cb) -> | |
db.getNewIdFor(GROUP, cb) | |
class Message | |
constructor: (obj) -> | |
# Sometimes the incoming obj can be missing an id | |
obj.id = obj.id || null | |
# Assign all properties of the object to this. | |
_.extend(this, obj) | |
save: (cb) -> | |
if @id is null | |
@getNewId(@saveToDb.bind(@, cb)) | |
else | |
@saveToDb(cb) | |
saveToDb: (cb) -> db.save GROUP, @id, @, cb | |
# This does't operate on an instance, it just returns | |
# a message object from the database. | |
get: (id, cb) -> | |
db.get GROUP, id, (err, obj) -> | |
cb err, new Message(obj) | |
module.exports = Message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment