-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (141 loc) · 3.58 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict'
const Hp = require('hemera-plugin')
const Knex = require('knex')
const SqlStore = require('./store')
const StorePattern = require('hemera-store/pattern')
function hemeraSqlStore(hemera, opts, done) {
const databases = new Map()
const topic = 'sql-store'
/**
* Create new knex per database
* Connection pooling is handled by knex
*
* @param {any} name
* @returns
*/
function useDb(name) {
if (databases.has(name)) {
return databases.get(name)
}
if (name) {
opts.knex.connection.database = name
databases.set(name, Knex(opts.knex))
return databases.get(name)
} else if (opts.knex.connection.database) {
databases.set(opts.knex.connection.database, Knex(opts.knex))
return databases.get(opts.knex.connection.database)
}
}
function destroyConnectionPool() {
hemera.log.debug('destroy connections')
let teardowns = []
for (const db of databases.values()) {
teardowns.push(db.destroy())
}
return Promise.all(teardowns)
}
hemera.ext('onClose', () => destroyConnectionPool())
/**
* Create a new record
*/
hemera.add(StorePattern.create(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.create(req, cb)
})
/**
* Find a record by id
*/
hemera.add(StorePattern.findById(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.findById(req, cb)
})
/**
* Update a record by id
*/
hemera.add(StorePattern.updateById(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.updateById(req, req.data, cb)
})
/**
* Replace a record by id
*/
hemera.add(StorePattern.replaceById(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.replaceById(req, req.data, cb)
})
/**
* Update entities
*/
hemera.add(StorePattern.update(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.update(req, req.data, cb)
})
/**
* Update entities
*/
hemera.add(StorePattern.replace(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.replace(req, req.data, cb)
})
/**
* Remove an entity by id
*/
hemera.add(StorePattern.removeById(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.removeById(req, cb)
})
/**
* Remove by query
*/
hemera.add(StorePattern.remove(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.remove(req, cb)
})
/**
* find
*/
hemera.add(StorePattern.find(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.find(req, req.options, cb)
})
/**
* exists
*/
hemera.add(StorePattern.exists(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.exists(req, cb)
})
/**
* count
*/
hemera.add(StorePattern.count(topic), function(req, cb) {
let db = useDb(req.database)
const store = new SqlStore(db)
store.count(req, cb)
})
// init default db
if (opts.knex.connection && opts.knex.connection.database) {
useDb(opts.knex.connection.database)
}
done()
}
module.exports = Hp(hemeraSqlStore, {
hemera: '>=5.0.0',
name: require('./package.json').name,
dependencies: ['hemera-joi'],
options: {
knex: {
connection: {}
}
}
})