/
index.js
40 lines (32 loc) · 1.16 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
// https://github.com/lorenwest/node-config/wiki/Sub-Module-Configuration
process.env.SUPPRESS_NO_CONFIG_WARNING = 'y'
var config = require('config')
var elasticsearch = require('elasticsearch')
var async = require('async')
var _ = require('lodash')
// https://github.com/lorenwest/node-config/wiki/Sub-Module-Configuration
config.util.setModuleDefaults('elasticsearchFramework', {
host: 'localhost:9200',
log: 'trace'
})
// the clone deep - https://github.com/elastic/elasticsearch-js/issues/33
var client = new elasticsearch.Client(_.cloneDeep(config.get('elasticsearchFramework')))
client.indices.safeCreate = function (name, cb) {
async.waterfall([
function (cb) { client.indices.exists({ index: name }, cb) },
function (exists, status, cb) {
if (exists) return cb(null, true, 204)
client.indices.create({ index: name }, cb)
}
], cb)
}
client.indices.safeDelete = function (name, cb) {
async.waterfall([
function (cb) { client.indices.exists({ index: name }, cb) },
function (exists, status, cb) {
if (!exists) return cb(null, true, 204)
client.indices.delete({ index: name }, cb)
}
], cb)
}
module.exports = client