This repository was archived by the owner on Jan 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (59 loc) · 1.52 KB
/
index.js
File metadata and controls
69 lines (59 loc) · 1.52 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var resourceful = require('resourceful');
var Riak = resourceful.engines.Riak = function(config) {
if(config && config.bucket) {
this.bucket = config.bucket;
} else {
throw new Error('bucket must be set in the config for each model.')
}
this.client = config.client || require('riak-js').getClient(config);
this.cache = new resourceful.Cache();
}
Riak.prototype.protocol = 'riak';
Riak.prototype.save = function (key, value, callback) {
var that = this;
if(!callback) {
callback = value;
value = key;
key = null;
}
this.client.save(this.bucket, key, value, {}, function(e, _, meta) {
if(e) {
callback(e);
} else {
value._id = meta.key;
that.cache.put(value._id, value);
callback(null, value);
}
});
};
Riak.prototype.get = function (key, callback) {
this.client.get(this.bucket, key, {}, function(e, value, meta) {
if(e) {
callback(e);
} else {
value._id = meta.key;
callback(e, value);
}
});
}
Riak.prototype.update = function(key, val, callback) {
var that = this;
that.get(key, function(err, old) {
that.save(key, resourceful.mixin(old, val), callback);
});
}
Riak.prototype.all = function(callback) {
this.client.getAll(this.bucket, function(e, all) {
if(e) {
callback(e);
} else {
var models = all.map(function(obj) {
return obj.data;
});
callback(null, models);
}
});
}
Riak.prototype.destroy = function(key, callback) {
this.client.remove(this.bucket, key, callback);
}