Skip to content

Commit 859364c

Browse files
initial stab at readme
1 parent 37c08c4 commit 859364c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
```

0 commit comments

Comments
 (0)