forked from willfarrell/middy-rds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.js
66 lines (51 loc) · 1.71 KB
/
pg.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
const { getInternal, processCache, clearCache/*, createError*/ } = require('@middy/util')
const ssl = require('./ssl')
const defaults = {
client: undefined,
config: {},
internalData: {},
cacheKey: 'rds',
cachePasswordKey: 'rds',
cacheExpiry: -1
}
const defaultConnection = { ssl, port: 5432 }
const rdsMiddleware = (opts = {}) => {
const options = Object.assign({}, defaults, opts)
if (!options.client) throw new Error('client option missing')
const fetch = async (request) => {
const values = await getInternal(options.internalData, request)
options.config = Object.assign({}, defaultConnection, options.config, values)
let pool = new options.client(options.config)
let db
try {
db = await pool.connect()
} catch(e) {
// Connection failed for some reason
// log and clear cache, force re-connect
clearCache([options.cachePasswordKey])
clearCache([options.cacheKey])
throw new Error(e.message) //createError(500, e.message)
}
// const query = async (sql, params) => {
// return db.query(escape(sql, params))
// }
// cache the connection, not the credentials as they may change over time
return db
}
const rdsMiddlewareBefore = async (request) => {
const {value} = processCache(options, fetch, request)
Object.assign(request.context, { db: await value }) // await due to fetch being a promise
}
const rdsMiddlewareAfter = async (request) => {
if (options.cacheExpiry === 0) {
request.context.db.end()
}
}
const rdsMiddlewareOnError = rdsMiddlewareAfter
return {
before: rdsMiddlewareBefore,
after: rdsMiddlewareAfter,
onError: rdsMiddlewareOnError
}
}
module.exports = rdsMiddleware