Skip to content

Instantly share code, notes, and snippets.

@mrmurphy
Created May 2, 2014 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrmurphy/0f44d4e666508c326f5e to your computer and use it in GitHub Desktop.
Save mrmurphy/0f44d4e666508c326f5e to your computer and use it in GitHub Desktop.
Message
db = require "../lib/db"
Q = require "q"
_ = require "lodash"
GROUP = "messages"
class Message
constructor: (obj) ->
# Assign all properties of the object to this.
_.extend(this, obj)
if obj.id is undefined
@id = null
save: =>
Q(do =>
if @id is null
return db.getNewIdFor(GROUP).then (id) =>
@id = id
return this
else
return this
).then =>
db.save GROUP, @id, this
.then =>
return this
.catch (err) ->
console.log "There was an error: #{err}"
# This does't operate on an instance, it just returns
# a message object from the database.
get: (id) ->
db.get GROUP, id
.then (obj) ->
msg = new Message(obj)
module.exports = Message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment