-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_api.js
41 lines (38 loc) · 1.38 KB
/
as_api.js
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
var aerospike = require('aerospike');
var asConfig = require('./config/aerospike_config');
var aerospikeConfig = asConfig.aerospikeConfig();
var aerospikeDBParams = asConfig.aerospikeDBParams();
var client = aerospike.client(aerospikeConfig);
// Establish connection to the cluster
exports.connect = function(callback) {
client.connect(function(response) {
return callback(response.code);
});
};
// Write a record
exports.writeRecord = function(k, v, callback) {
var key = aerospike.key(aerospikeDBParams.defaultNamespace,aerospikeDBParams.defaultSet,k);
client.put(key, {greet:v}, function(err, rec, meta) {
// Check for errors
if ( err.code === aerospike.status.AEROSPIKE_OK ) {
return callback({status:0,message:'ok'});
} else {
// An error occurred
return callback({status:-1,message:err});
}
});
};
// Read a record
exports.readRecord = function(k, callback) {
var key = aerospike.key(aerospikeDBParams.defaultNamespace,aerospikeDBParams.defaultSet,k);
client.get(key, function(err, rec, meta) {
// Check for errors
if ( err.code === aerospike.status.AEROSPIKE_OK ) {
return callback({status:0,message:k + ' ' + rec.greet});
}
else {
// An error occurred
return callback({status:-1,message:'Record not found!'});
}
});
};