@@ -16,17 +16,27 @@ module.exports = PostgresDB;
16
16
17
17
PostgresDB . prototype = Object . create ( DB . prototype ) ;
18
18
19
- PostgresDB . prototype . close = function ( callback ) {
20
- this . closed = true ;
21
- this . pool . end ( ) ;
22
-
23
- if ( callback ) callback ( ) ;
19
+ PostgresDB . prototype . close = async function ( callback ) {
20
+ let error ;
21
+ try {
22
+ if ( ! this . closed ) {
23
+ this . closed = true ;
24
+ await this . pool . end ( ) ;
25
+ }
26
+ } catch ( err ) {
27
+ error = err ;
28
+ }
29
+
30
+ // FIXME: Don't swallow errors. Emit 'error' event?
31
+ if ( callback ) callback ( error ) ;
24
32
} ;
25
33
26
34
27
35
// Persists an op and snapshot if it is for the next version. Calls back with
28
36
// callback(err, succeeded)
29
- PostgresDB . prototype . commit = function ( collection , id , op , snapshot , options , callback ) {
37
+ PostgresDB . prototype . commit = async function ( collection , id , op , snapshot , options , callback ) {
38
+ let client ;
39
+ try {
30
40
/*
31
41
* op: CreateOp {
32
42
* src: '24545654654646',
@@ -37,29 +47,24 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
37
47
* }
38
48
* snapshot: PostgresSnapshot
39
49
*/
40
- this . pool . connect ( ( err , client , done ) => {
41
- if ( err ) {
42
- done ( client ) ;
43
- callback ( err ) ;
44
- return ;
45
- }
50
+ client = await this . pool . connect ( ) ;
46
51
/*
47
- * This query uses common table expression to upsert the snapshot table
52
+ * This query uses common table expression to upsert the snapshot table
48
53
* (iff the new version is exactly 1 more than the latest table or if
49
54
* the document id does not exists)
50
55
*
51
- * It will then insert into the ops table if it is exactly 1 more than the
56
+ * It will then insert into the ops table if it is exactly 1 more than the
52
57
* latest table or it the first operation and iff the previous insert into
53
58
* the snapshot table is successful.
54
59
*
55
60
* This result of this query the version of the newly inserted operation
56
61
* If either the ops or the snapshot insert fails then 0 rows are returned
57
62
*
58
- * If 0 zeros are return then the callback must return false
63
+ * If 0 zeros are return then the callback must return false
59
64
*
60
65
* Casting is required as postgres thinks that collection and doc_id are
61
- * not varchar
62
- */
66
+ * not varchar
67
+ */
63
68
const query = {
64
69
name : 'sdb-commit-op-and-snap' ,
65
70
text : `WITH snapshot_id AS (
@@ -95,20 +100,14 @@ WHERE (
95
100
RETURNING version` ,
96
101
values : [ collection , id , snapshot . v , snapshot . type , snapshot . data , op ]
97
102
}
98
- client . query ( query , ( err , res ) => {
99
- if ( err ) {
100
- callback ( err )
101
- } else if ( res . rows . length === 0 ) {
102
- done ( client ) ;
103
- callback ( null , false )
104
- }
105
- else {
106
- done ( client ) ;
107
- callback ( null , true )
108
- }
109
- } )
110
-
111
- } )
103
+ const result = await client . query ( query ) ;
104
+ const success = result . rowCount > 0 ;
105
+ callback ( null , success ) ;
106
+ } catch ( error ) {
107
+ callback ( error ) ;
108
+ } finally {
109
+ if ( client ) client . release ( true ) ;
110
+ }
112
111
} ;
113
112
114
113
// Get the named document from the database. The callback is called with (err,
0 commit comments