-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (60 loc) · 1.89 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
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
'use strict'
const _ = require('lodash')
const Trailpack = require('trailpack')
const elasticsearch = require('elasticsearch')
/**
* ElasticSearch integration for Trails.js
*/
module.exports = class ElasticsearchTrailpack extends Trailpack {
/**
* Ensure that this trailpack supports the configured migration
*/
validate() {
if (!_.isObject(this.app.config.elasticsearch))
return Promise.reject(new Error('No configuration found at config.elasticsearch !'))
if (!_.isObject(this.app.config.elasticsearch.connection))
return Promise.reject(new Error('No connection configuration defined !'))
}
configure() {
// setup default log level to warning
if (!this.app.config.elasticsearch.connection.log) {
this.app.config.elasticsearch.connection.log = 'warning'
}
}
initialize() {
super.initialize()
// Notice !!!
// Elastic try to change given config onject. So do not remove `_.clone`
// Otherwise Trails will pass readonly object and Elasticsearch wouldn't
// be able to connect
this.client = new elasticsearch.Client(_.clone(this.app.config.elasticsearch.connection))
this.app.elasticClient = this.client
// If no need to validate connection - exit
if (!this.app.config.elasticsearch.validateConnection)
return Promise.resolve()
// validating connection using ping command
return new Promise((resolve, reject) => {
this.client.ping((err) => {
if (err)
return reject(err)
resolve()
})
})
}
/**
* Close connection to Elasticsearch
*/
unload() {
if (!this.app.elasticClient || !_.isFunction(this.app.elasticClient.close))
return
// Closing elasticsearch connection
this.app.elasticClient.close()
}
constructor(app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}