File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ # pg-db-session
2
+
3
+ Abuse domains to get a form of continuation local storage. Associate all events
4
+ originating from a single domain to a single database session, which manages
5
+ maximum concurrency, transactions, and operation ordering for consumers of the
6
+ database connection. * It is almost 3AM.*
7
+
8
+ ``` javascript
9
+ const db = require (' pg-db-session' )
10
+ const domain = require (' domain' )
11
+ const http = require (' http' )
12
+ const pg = require (' pg' )
13
+
14
+ http .createServer ((req , res ) => {
15
+ const d = domain .create ()
16
+ d .add (req)
17
+ d .add (res)
18
+
19
+ db .install (d, () => {
20
+ return new Promise ((resolve , reject ) => {
21
+ pg .connect (CONFIG , (err , connection , release ) => {
22
+ err ? reject (err) : resolve ({connection, release})
23
+ })
24
+ })
25
+ }, {maxConcurrency: 2 })
26
+
27
+ d .run (() => {
28
+ // handle some code.
29
+ someOperation ()
30
+ someAtomic ()
31
+ })
32
+ })
33
+
34
+ const someOperation = db .transaction (function operation () {
35
+ // this code will always run inside an operation
36
+ return db .getConnection ().then (pair => {
37
+ pair .connection .query (' DELETE FROM all' , err => pair .release (err))
38
+ })
39
+ })
40
+
41
+ const someAtomic = db .atomic (function atom () {
42
+ // this code will always be run inside an operation together,
43
+ // with savepoints.
44
+ })
45
+ ```
You can’t perform that action at this time.
0 commit comments