Skip to content

Commit ecdb360

Browse files
authored
Merge pull request #2 from Shikyaro/dev
[v0.0.4] Added "query" method (proxy to pool.query)
2 parents b4b093e + 0b31377 commit ecdb360

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

connectors/abstract-connector.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const QueryTemplater = require('query-template');
12
/**
23
* @abstract
34
* @class {AbstractConnector}
@@ -8,6 +9,11 @@ class AbstractConnector {
89
*/
910
constructor(config) {
1011
this.config = config;
12+
/**
13+
* @type {QueryTemplater}
14+
* @protected
15+
*/
16+
this._templater = config.templater || new QueryTemplater();
1117

1218
if (new.target === AbstractConnector) {
1319
throw new TypeError('Abstract class can not be created!');
@@ -23,6 +29,19 @@ class AbstractConnector {
2329
throw new TypeError('This method must be overridden!');
2430
}
2531

32+
/**
33+
* Pool.query proxy method
34+
* @abstract
35+
* @async
36+
* @param {{name: String, sql:String, addons: Object}} queryObject query data
37+
* @param {Object} queryParams named params
38+
* @param {Object} queryOptions options
39+
* @returns {Promise<Array>} query result
40+
*/
41+
query(queryObject, queryParams, queryOptions = {}) {
42+
throw new TypeError('This method must be overridden!');
43+
}
44+
2645
/**
2746
* @abstract
2847
* @async

connectors/postgres-connector.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const AbstractConnector = require('./abstract-connector.js');
44
const PostgresConnection = require('../connections/postgres-connection.js');
55

66
/**
7-
* @inheritDoc
7+
* @class
8+
* @extends AbstractConnector
89
*/
910
class PostgresConnector extends AbstractConnector {
1011
/**
@@ -27,10 +28,28 @@ class PostgresConnector extends AbstractConnector {
2728
};
2829

2930
const connection = new PostgresConnection(config);
30-
3131
return connection;
3232
}
3333

34+
/**
35+
* @inheritDoc
36+
*/
37+
query(queryObject, queryParams, queryOptions = {}) {
38+
const {sql, addons} = queryObject;
39+
if (!sql) {
40+
throw new TypeError('Invalid query object, "sql" property missing');
41+
}
42+
const {templateParams = {}} = queryOptions;
43+
let queryText = sql;
44+
45+
if (addons) {
46+
queryText = this._templater.processTemplates(queryObject, {...queryParams, ...templateParams});
47+
}
48+
49+
const {query, params} = this._templater.parametrizeQuery(queryText, queryParams, this.config.type);
50+
return this.pool.query(query, params);
51+
}
52+
3453
/**
3554
* @inheritDoc
3655
*/

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ class DataStorage {
4646
return connector.getConnection();
4747
}
4848

49+
/**
50+
* @async
51+
* @param {String} connectorName connector name
52+
* @param {{name: String, sql:String, addons: Object}} queryObject query data
53+
* @param {Object} queryParams named params
54+
* @param {Object} queryOptions options
55+
* @returns {Promise<Array>} query result
56+
*/
57+
async query(connectorName, queryObject, queryParams, queryOptions = {}) {
58+
let connection;
59+
try {
60+
connection = await this.getConnection(connectorName);
61+
const data = await connection.query(queryObject, queryParams, queryOptions);
62+
return data;
63+
} catch (error) {
64+
throw error;
65+
} finally {
66+
if (connection) {
67+
await connection.release();
68+
}
69+
}
70+
}
71+
4972
/**
5073
* Method for graceful shutdown
5174
* @async

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"url": "git://github.com/Shikyaro/multi-data-source.git"
77
},
88
"license": "MIT",
9-
"version": "0.0.3",
9+
"version": "0.0.4",
1010
"scripts": {
1111
"autofix": "eslint --fix .",
1212
"test": "./node_modules/.bin/jest ./test"

0 commit comments

Comments
 (0)