-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (43 loc) · 1.66 KB
/
index.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
42
43
44
45
/*jslint node: true, nomen: true, white: true */
module.exports = function (app) {
'use strict';
var logger = app.logger && app.logger.child({component: 'Database'}),
couchbase = require('couchbase'),
crypto = require('crypto-js'),
cluster = null,
bucket = null,
database = {
init: function (host, bucketName) {
cluster = new couchbase.Cluster('couchbase://' + host);
bucket = cluster.openBucket(bucketName, function (err) {
if (err) {
if (logger) {
app.logger.fatal('Erreur lors de la connection à la base de données %s', host);
}
return new Error({code: 'DATABASE_CONNECT_FAIL'});
}
app.logger.info('Connecté à la base de données %s', host);
});
return bucket;
},
get: function () {
return bucket;
},
getViewQuery: function () {
return couchbase.ViewQuery;
},
getN1qlQuery: function () {
return couchbase.N1qlQuery;
},
getSpatialQuery: function () {
return couchbase.SpatialQuery;
},
getKey: function (secret) {
var date = (new Date()).valueOf().toString(),
random = Math.random().toString(),
secret = secret || '';
return crypto.HmacSHA1(date + random, secret).toString(crypto.enc.Hex);
}
};
return database;
};