Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 50 lines (44 sloc) 1.142 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
module.exports = class IoCrud

constructor: (socket) ->
socket.on "#{@prefix}:list", (query, cb) =>
@list socket, query, cb
socket.on "#{@prefix}:create", (obj, cb) =>
@add socket, obj, cb
socket.on "#{@prefix}:read", (id, cb) =>
@item socket, id, cb
socket.on "#{@prefix}:delete", (_id, cb) =>
@delete socket, _id, cb
socket.on "#{@prefix}:update", (id, obj, cb) =>
@update socket, id, obj, cb

list: (socket, query, cb) =>
@model.find query, (err, items) ->
if err?
console.log err
return cb err
cb null, items

add: (socket, obj, cb) =>
item = new @model obj
item.save (err) ->
if err?
console.log err
return cb err
cb null, item.toObject()

item: (socket, id, cb) =>
@model.findById id, (err, item) ->
if err?
console.log err
return cb err
cb null, item?.toObject()

delete: (req, res) =>
@model.remove {_id: req.params.id}, (err) ->
if err?
console.log err
return cb err
res.send()

update: (socket, id, obj, cb) =>
delete obj._id
@model.update {_id: id}, obj, (err) ->
if err?
console.log err
return cb err
cb null, obj
Something went wrong with that request. Please try again.