Skip to content

Commit b4b093e

Browse files
authored
Merge pull request #1 from Shikyaro/dev
v0.0.3
2 parents 55f6f69 + 7b81cfe commit b4b093e

21 files changed

+6463
-1328
lines changed

README.MD

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ npm install multi-data-source
1212
## Features
1313

1414
* Multiple data sources support
15-
* Multiple RDBMS support (only PostgreSQL and MySQL for now, but you feel free to create your own connector to favorite RDBMS)
16-
* Little query template engine
17-
* Single interface to pass query parameters to all RDBMS by built-in query param escape module
15+
* Multiple RDBMS support (only PostgreSQL for now, but you feel free to contribute your own connector to favorite RDBMS)
16+
* Single interface to pass query parameters to multiple RDBMS with [param wrapping module](https://github.com/shikyaro/node-query-template)
17+
1818

1919
## Dependencies
2020

21-
* [pg](https://github.com/brianc/node-postgres) -- Used to handle postgres connection
21+
* [pg](https://github.com/brianc/node-postgres) -- Used to handle Postgres connection
22+
* [query-template](https://github.com/shikyaro/node-query-template) -- Used to process queries before execution (templating and parametrizing)
2223

2324
## Usage example
2425

@@ -52,15 +53,15 @@ const getUsersByScore = {
5253
`,
5354
addons: {
5455
noGlobalJoin: {
55-
arg: {name: 'global', value: false},
56+
options: {propertyName: 'global', propertyValue: false},
5657
sql: 'INNER JOIN mega_jackpot_halls AS mjh USING(hall_id)',
5758
},
5859
nickname: {
59-
arg: {name: 'needNickname', value: true},
60+
options: {propertyName: 'needNickname', propertyValue: true},
6061
sql: ', nickname',
6162
},
6263
balance: {
63-
arg: {name: 'balanceCondition', value: true},
64+
options: {propertyName: 'balanceCondition', propertyValue: true},
6465
sql: 'AND balance >= :balance',
6566
},
6667
},
@@ -93,6 +94,32 @@ try {
9394
}
9495
```
9596

97+
## Configuration guide
98+
99+
### Storage configuration
100+
By default storage conig is semi-equal to [pg](https://github.com/brianc/node-postgres) config with additional field `storageType`.
101+
102+
**Supported storage types:**
103+
* `pg` -- type for PostgreSQL connection
104+
105+
### Query configuration
106+
This library using equal query definition to [query-template](https://github.com/shikyaro/node-query-template)
107+
108+
```js
109+
const query = {
110+
sql: 'SELECT * FROM table', // String with SQL code
111+
addons: { // Object with named additions
112+
addonName: { // Addition object
113+
sql: 'AND field1 = :field1', // String with addition SQL
114+
options: { // Object with addition config
115+
propertyName: 'field1', // Name of templating property
116+
propertyValue: true, // Value of templating property
117+
}
118+
}
119+
}
120+
}
121+
```
122+
96123
## Author
97124

98125
Pavel Romanov -- alkor@alkor.pw -- [GitHub](https://github.com/Shikyaro)

connections/AbstractConnection.js renamed to connections/abstract-connection.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
const QueryTemplater = require('query-template');
12
/**
23
* @class {AbstractConnection}
34
* @abstract
45
*/
56
class AbstractConnection {
67
/**
7-
* @param {{client: object, queries: object?}} config config obj
8+
* @param {{client: object, queries: object?, templater: object?}} config config obj
89
*/
910
constructor(config) {
1011
this.config = config;
1112
this.client = config.client;
1213
this.queries = config.queries;
14+
this.templater = config.templater || new QueryTemplater();
1315

1416
if (new.target === AbstractConnection) {
1517
throw new TypeError('Abstract class can not be created!');
@@ -19,7 +21,7 @@ class AbstractConnection {
1921
/**
2022
* @abstract
2123
* @async
22-
* @param {{name: String, sql:String, addonds: Object}} queryObject query data
24+
* @param {{name: String, sql:String, addons: Object}} queryObject query data
2325
* @param {Object} queryParams named params
2426
* @param {Object} queryOptions options
2527
* @returns {Promise<Array>} query result

connections/PostgresConnection.js renamed to connections/postgres-connection.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const AbstractConnection = require('./AbstractConnection');
2-
const {escapeParams, QueryTemplater} = require('../query');
1+
const AbstractConnection = require('./abstract-connection.js');
32

43
/**
54
* @inheritDoc
@@ -8,37 +7,40 @@ class PostgresConnection extends AbstractConnection {
87
/**
98
* @async
109
* @param {String} query SQL
10+
* @param {Array} params query params
1111
* @returns {Promise<Array>} result
1212
* @private
1313
*/
14-
async _executeQuery(query) {
15-
const {rows} = await this.client.query(query);
14+
async _executeQuery(query, params) {
15+
const {rows} = await this.client.query(query, params);
1616
return rows;
1717
}
1818

1919
/**
2020
* @inheritDoc
2121
*/
2222
rawQuery(queryText, queryParams, queryOptions = null) {
23-
const preparedQuery = escapeParams(queryText, queryParams);
24-
return this._executeQuery(preparedQuery);
23+
const {query, params} = this.templater.parametrizeQuery(queryText, queryParams, this.config.type);
24+
return this._executeQuery(query, params);
2525
}
2626

2727
/**
2828
* @inheritDoc
2929
*/
3030
query(queryObject, queryParams, queryOptions = {}) {
31-
const {sql} = queryObject;
31+
const {sql, addons} = queryObject;
32+
if (!sql) {
33+
throw new TypeError('Invalid query object, "sql" property missing');
34+
}
35+
const {templateParams = {}} = queryOptions;
3236
let queryText = sql;
3337

34-
const {templateParams} = queryOptions;
35-
if (templateParams) {
36-
queryText = QueryTemplater.buildQuery(queryObject, templateParams);
38+
if (addons) {
39+
queryText = this.templater.processTemplates(queryObject, {...queryParams, ...templateParams});
3740
}
3841

39-
const preparedQuery = escapeParams(queryText, queryParams);
40-
41-
return this._executeQuery(preparedQuery);
42+
const {query, params} = this.templater.parametrizeQuery(queryText, queryParams, this.config.type);
43+
return this._executeQuery(query, params);
4244
}
4345

4446
/**

connections/testing-connection.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const AbstractConnection = require('./abstract-connection');
2+
const MockManager = require('../mock/mock-manager.js');
3+
4+
/**
5+
* @inheritDoc
6+
*/
7+
class TestingConnection extends AbstractConnection {
8+
/**
9+
* @inheritDoc
10+
*/
11+
rawQuery(queryText, queryParams, queryOptions = null) {
12+
}
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
query(queryObject, queryParams, queryOptions = {}) {
18+
// TODO: make standard with NODE_ENV naming rules
19+
// TODO: use utils? module to check environment
20+
return MockManager.mockQuery(queryObject, queryParams);
21+
}
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
async transaction() {
27+
// await this.client.query('BEGIN;');
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
async commit() {
34+
// await this.client.query('COMMIT;');
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
async rollback() {
41+
// await this.client.query('ROLLBACK;');
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
async release() {
48+
// await this.client.release();
49+
}
50+
}
51+
52+
module.exports = TestingConnection;
File renamed without changes.

connectors/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const PostgresConnector = require('./PostgresConnector');
1+
const PostgresConnector = require('./postgres-connector');
2+
const TestingConnector = require('./testing-connector');
23

34
module.exports = {
45
PostgresConnector,
6+
TestingConnector,
57
};

connectors/PostgresConnector.js renamed to connectors/postgres-connector.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const {Pool} = require('pg');
22

3-
const AbstractConnector = require('./AbstractConnector');
4-
const PostgresConnection = require('../connections/PostgresConnection');
3+
const AbstractConnector = require('./abstract-connector.js');
4+
const PostgresConnection = require('../connections/postgres-connection.js');
55

66
/**
77
* @inheritDoc
@@ -12,12 +12,7 @@ class PostgresConnector extends AbstractConnector {
1212
*/
1313
constructor(config) {
1414
super(config);
15-
1615
this.pool = new Pool({...config});
17-
18-
// this.pool.on('error', error => {
19-
// // log.error(`Something happened with Postgres pool ${config.storageName}. errorCode: ${error.code}. error: ${error}`);
20-
// });
2116
}
2217

2318
/**

connectors/testing-connector.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const AbstractConnector = require('./abstract-connector.js');
2+
const TestingConnection = require('../connections/testing-connection.js');
3+
4+
/**
5+
* @inheritDoc
6+
*/
7+
class TestingConnector extends AbstractConnector {
8+
/**
9+
* @inheritDoc
10+
*/
11+
constructor(config) {
12+
super(config);
13+
}
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
async getConnection() {
19+
// TODO simple object just for initial testing
20+
const client = {};
21+
22+
const config = {
23+
client,
24+
...(this.config),
25+
};
26+
27+
const connection = new TestingConnection(config);
28+
29+
return connection;
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
async releaseConnection(connection) {
36+
// await connection.client.release();
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
async closeConnection() {
43+
// await this.pool.end();
44+
}
45+
}
46+
47+
module.exports = TestingConnector;

factories/AbstractConnectorFactory.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

factories/PostgresConnectorFactory.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)