-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
124 lines (104 loc) · 3.43 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
const asyncHooks = require('async_hooks')
const uuid = require('uuid')
const {NORMAL, SANDBOX, ROLLBACK} = require('./constants')
/**
* Create a wrapper around pg-postgres that can capture references to transactions
* and trace async function calls.
*/
class Sandboxer {
constructor(options = {}) {
this.pg = options.pg
this.mode = options.mode || NORMAL
this.sandboxes = {}
this.context = new Map()
if (options.mode === SANDBOX ) {
// With async_hooks, we can capture references to event and promise
// creation/destruction by id. Each async call has an id, and each
// function that triggers an async call has an id.
//
// We can store a reference to the initial trigger on every promise, and
// keep track of what "scope" our function is in. We can use this reference
// to isolate our postgres transactions.
asyncHooks.createHook({
init: (asyncId, _type, triggerAsyncId) =>{
if (this.context.has(triggerAsyncId)) {
this.context.set(asyncId, this.context.get(triggerAsyncId))
}
},
destroy: (asyncId) => {
if (this.context.has(asyncId)) {
this.context.delete(asyncId);
}
}
}).enable()
}
}
_getConn() {
// if we're scoped within a sandbox, grab the sandbox client so we can
// transparently add the next query to the sandboxed transaction, otherwise
// use the base database client.
if (this.mode === NORMAL) return this.pg
const id = this.context.get(asyncHooks.executionAsyncId())
const found = this.sandboxes[id]
let conn = found ? found.tx : this.pg
return conn
}
/**
* Creates a transaction sandbox. Puts all database calls made within the
* "promise chain"/async function stack into the current transaction.
*/
createSandbox() {
const id = uuid.v4()
this.context.set(asyncHooks.executionAsyncId(), id)
const grabTransactionReference = (callback) => (tx) => new Promise((_, rollback)=> {
this.sandboxes[id] = { tx, rollback }
callback(id)
})
return new Promise((resolve, reject) => {
let resolved = false
this.pg.tx(grabTransactionReference((id) => {
resolved = true
resolve(id)
}))
.catch((err) => {
if (!resolved || err !== ROLLBACK) {
reject(err)
}
})
})
}
/**
* Closes a transaction sandbox, rolling back all changes.
*/
async closeSandbox() {
const id = this.context.get(asyncHooks.executionAsyncId());
const conn = this.sandboxes[id]
if (!conn) return this
await conn.rollback(ROLLBACK)
delete this.sandboxes[id]
return this
}
}
/**
* Wrap up your pg-promise client with a sandbox mode.
*
* @param {*} options - an object of options.
* @param options.pg - your pg-promise client, initalized.
* @param options.mode - Either 'normal' or 'sandbox'. 'normal' mode does not
* initialize async_hooks.
* @returns {pgPromise.IDatabase<{}, pg.IClient>} a wrapped pg promise client.
*/
const pgSandbox = (options) => {
const sandbox = new Sandboxer(options)
const handler = {
get(target, name, reciever) {
if (!Reflect.has(target, name)) {
const conn = target._getConn()
return Reflect.get(conn, name, reciever)
}
return Reflect.get(target, name, reciever)
}
}
return new Proxy(sandbox, handler)
}
module.exports = { pgSandbox }