-
Notifications
You must be signed in to change notification settings - Fork 20
/
redispersist.coffee
36 lines (29 loc) · 999 Bytes
/
redispersist.coffee
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
dbc = require 'dbc'
_ = require 'underscore'
redis = require 'then-redis'
config = require './config'
client = redis.createClient config.redis_config
log = require './logger'
max_results = 100000
module.exports =
insert: (collectionName, resource) ->
client.incr('resource:id').then (nextId) ->
withId = _.extend(resource, {id: nextId})
client.hset build_key(collectionName), nextId, JSON.stringify withId
withId
all: (collectionName) ->
client.hvals(build_key(collectionName)).then (set) ->
(for el in set
JSON.parse el)
get: (collectionName, id) ->
client.hget(build_key(collectionName), id).then((el) ->
JSON.parse el
)
update: (collectionName, id, resource) ->
_.extend(resource, {id: id})
client.hset(build_key(collectionName), id, JSON.stringify resource)
resource
delete: (collectionName, id) ->
client.hdel build_key(collectionName), id
build_key = (collectionName) ->
"resource:#{collectionName}"