forked from balderdashy/sails-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisAdapter.js
More file actions
124 lines (93 loc) · 2.9 KB
/
RedisAdapter.js
File metadata and controls
124 lines (93 loc) · 2.9 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*---------------------------------------------------------------
:: sails-redis
-> adapter
---------------------------------------------------------------*/
var async = require('async')
, _ = require('underscore')
, _str = require('underscore.string')
, redis = require('redis');
module.exports = (function(){
// Handle to a Redis connection
var client = null;
var adapter = {
syncable: false,
// This is here for testing purposes, until we figure out a way to run tests w/o transactions
commitLog: {
identity: '__default_waterline_redis_transaction',
adapter: 'sails-redis'
},
defaults: {
port: 6379,
host: 'localhost',
options: {
parser: 'hiredis',
return_buffers: false,
detect_buffers: false,
socket_nodelay: true,
no_ready_check: false,
enable_offline_queue: true
},
password: null
},
registerCollection: function(collection, cb) {
connect(collection, cb);
},
create: function(collectionName, data, cb) {
client.on("error", function (err) {
console.log("Error " + err);
});
// Using the collectionName as a key, append a random integer - e.g., users_2940
client.set(collectionName + '_' + Math.round(Math.random() * 10000), data);
client.quit();
cb();
},
find: function(collectionName, options, cb) {
client.on("error", function (err) {
console.log("Error " + err);
});
client.get(collectionName);
client.quit();
cb();
},
update: function(collectionName, options, values, cb) {
// Overwrites old record!
client.on("error", function (err) {
console.log("Error " + err);
});
client.set(collectionName, values);
client.quit();
cb();
},
destroy: function(collectionName, options, cb) {
client.on("error", function (err) {
console.log("Error " + err);
});
client.del(collectionName);
client.quit();
cb();
},
identity: 'sails-redis'
};
////////////// //////////////////////////////////////////
////////////// Private Methods //////////////////////////////////////////
////////////// //////////////////////////////////////////
// Convert standard adapter config
// into a custom configuration object for redis
function marshalConfig(config) {
return _.extend(config, {
port: config.port,
host: config.host,
options: config.options,
password: config.password
});
}
function connect(collection, cb) {
if(collection.password != null) {
client = redis.createClient(collection.port, collection.host, collection.options).auth(collection.password);
} else {
client = redis.createClient(collection.port, collection.host, collection.options);
}
return cb();
}
return adapter;
})();