Skip to content

Instantly share code, notes, and snippets.

@taterbase
Forked from mrmurphy/message.coffee
Last active August 29, 2015 14: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 taterbase/a486f67e2edd0160e68f to your computer and use it in GitHub Desktop.
Save taterbase/a486f67e2edd0160e68f to your computer and use it in GitHub Desktop.
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