-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 1.71 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
/*
* @Name: SenecaSeraph
* @Description: Seneca plugin for Seraph Lib to connect a Neo4j Database
* @Author: FelipeBarrosCruz<[email protected]>
* @Version: 1.0
* @Date? [2017-11-15]
*/
module.exports = function SenecaSeraph (options) {
const name = 'seneca-seraph'
const seneca = this
const seraph = require('seraph')
const async = require('async')
const _ = require('lodash')
const defaultOptions = {
server: 'http://localhost:7474',
user: 'neo4j',
pass: 'neo4j'
}
const logger = seneca.log || _logger()
seneca.add({role: name, cmd: 'connection'}, cmd_connection)
seneca.add({ init: name }, (args, done) => {
seneca.act({ role: name, cmd: 'connection' }, (err, connection) => {
if (err) return seneca.die(err)
seneca.decorate('seraph', connection)
return done(null)
})
})
function cmd_connection (args, done) {
async.retry({ times: 5, timeout: 1000 }, tryToConnect, (err, connection) => {
if (err) return done(err)
return done(null, connection)
})
}
function tryToConnect (done) {
const connection = seraph(_.merge({}, defaultOptions, options))
connection.query('MATCH (n) RETURN n;', (err, result) => {
if (err) {
logger.error(err.message)
return done(err)
}
logger.info('Connection succeeded')
return done(null, connection)
})
}
function _logger () {
function log (type) {
return (message) => {
console.log('[%s] - Seneca Seraph Internal %s: %s', new Date().toISOString(), type, message)
}
}
return {
error: log('Error'),
info: log('Info'),
warn: log('Warn'),
debug: log('Debug')
}
}
return {
name: name
}
}