@brainsights/postgres-wrapper
Advanced tools
Comparing version
27
index.js
const pg = require("pg"); | ||
const SQL = require("sql-template-strings"); | ||
const debug = require("debug")("postgres-wrapper"); | ||
const uuid = require("uuid"); | ||
let types = pg.types; | ||
types.setTypeParser(20, function(val) { | ||
return parseInt(val) | ||
}); | ||
const __ = new WeakMap(); | ||
@@ -17,3 +23,4 @@ | ||
__.set(proxy, { | ||
revoke: revoke | ||
revoke: revoke, | ||
id: uuid.v4(), | ||
}); | ||
@@ -29,3 +36,3 @@ return proxy; | ||
client = await pool.connect(); | ||
debug("acquire client"); | ||
debug(`${this.id} acquire client`); | ||
} catch(e) { | ||
@@ -40,2 +47,3 @@ throw new Error(`unable to get database client: ${e.message}`) | ||
result = await __.get(this).client.query(...args); | ||
debug(`${this.id} query`) | ||
} catch(e) { | ||
@@ -50,6 +58,11 @@ throw new Error(`query failed: ${e.message}`) | ||
{ | ||
debug("release client"); | ||
debug(`${__.get(this).id} release client`); | ||
__.get(this).client.end(); | ||
__.get(this).revoke(); | ||
} | ||
get id() | ||
{ | ||
return __.get(this).id; | ||
} | ||
}; | ||
@@ -70,7 +83,7 @@ | ||
await super.query(SQL`BEGIN`); | ||
debug("begin transaction"); | ||
debug(`${this.id} begin transaction`); | ||
} catch(e) { | ||
throw new Error(`unable to start transaction: ${e.message}`); | ||
__.get(this).revoke(); | ||
debug("revoke transaction object"); | ||
debug(`${this.id} revoke transaction object`); | ||
} | ||
@@ -86,3 +99,3 @@ __.get(this).begin = true; | ||
await this.query(SQL`COMMIT`); | ||
debug("commit transaction"); | ||
debug(`${this.id} commit transaction`); | ||
super.end(); | ||
@@ -94,3 +107,3 @@ } | ||
await this.query(SQL`ROLLBACK`); | ||
debug("abort transaction"); | ||
debug(`${this.id} abort transaction`); | ||
super.end(); | ||
@@ -97,0 +110,0 @@ } |
{ | ||
"name": "@brainsights/postgres-wrapper", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -15,5 +15,5 @@ "main": "index.js", | ||
"pg": "^6.1.4", | ||
"pg-native": "^1.10.0", | ||
"sql-template-strings": "^2.2.2" | ||
"sql-template-strings": "^2.2.2", | ||
"uuid": "^3.0.1" | ||
} | ||
} |
2636
12.94%93
13.41%+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed