@@ -43,3 +43,70 @@ const someAtomic = db.atomic(function atom () {
43
43
// with savepoints.
44
44
})
45
45
```
46
+
47
+ Database sessions are active whenever their associated domain is active. This means
48
+ that a domain can be associated with a request, and all requests for a connection
49
+ will be managed by the session associated with that domain.
50
+
51
+ Database sessions manage access to the lower-level postgres connection pool.
52
+ This lets users specify maximum concurrency for a given session — for instance,
53
+ retaining a pool of 20 connections, but only allotting a maximum of 4
54
+ concurrent connections per incoming HTTP request.
55
+
56
+ Sessions also manage * transaction* status — functions may be decorated with
57
+ "transaction" or "atomic" wrappers, and the active session will automatically
58
+ create a transactional sub-session for the execution of those functions and any
59
+ subsequent events they spawn. Any requests for a connection will be handled by
60
+ the subsession. The transaction held by the subsession will be committed or
61
+ rolled back based on the fulfillment status of the promise returned by the
62
+ wrapped function. Transactional sessions hold a single connection, releasing it
63
+ to connection requests sequentially — this naturally reduces the connection
64
+ concurrency to one.
65
+
66
+ Atomics, like transactions, hold a single connection, delegating sequentially.
67
+ They're useful for grouping a set of operations atomically within a
68
+ transaction. Atomics are wrapped in a ` SAVEPOINT ` — releasing the savepoint if
69
+ the promise returned by the wrapped function is fulfilled, and rolling back to
70
+ it if the promise is rejected. Atomics may be nested.
71
+
72
+ ## API
73
+
74
+ #### ` db.install(d:Domain, getConnection:ConnPairFn, opts:Options) `
75
+
76
+ Install a database ` Session ` on the domain ` d ` .
77
+
78
+ #### ` ConnPairFn := Function → Promise({connection, release}) `
79
+
80
+ A function that returns a ` Promise ` for an object with ` connection ` and ` release `
81
+ properties, corresponding to the ` client ` and ` done ` parameters handed back by
82
+ [ node-postgres] [ ] .
83
+
84
+ Usually, this will look something like the following:
85
+
86
+ ``` javascript
87
+ function getConnection () {
88
+ return new Promise ((resolve , reject ) => {
89
+ pg .connect (CONNECTION_OPTIONS , (err , client , done ) => {
90
+ err ? reject (err) : resolve ({
91
+ connection: client,
92
+ release: done
93
+ })
94
+ })
95
+ })
96
+ }
97
+ ```
98
+
99
+ #### ` db.getConnection() → Promise({connection, release}) `
100
+
101
+ Request a connection pair. ` release ` should be called when the connection is no
102
+ longer necessary.
103
+
104
+ #### ` db.atomic(Function → Promise<T>) → Function `
105
+
106
+ Wrap a function as an atomic.
107
+
108
+ #### ` db.transaction(Function → Promise<T>) → Function `
109
+
110
+ Wrap a function as requiring a transaction.
111
+
112
+ [ node-postgres ] : https://github.com/brianc/node-postgres
0 commit comments