Skip to content

Commit 2d8939f

Browse files
author
Pavel Romanov
committed
jsdoc
1 parent 64fcf60 commit 2d8939f

File tree

9 files changed

+124
-57
lines changed

9 files changed

+124
-57
lines changed

connections/AbstractConnection.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
/**
2+
* @class {AbstractConnection}
3+
* @abstract
4+
*/
15
class AbstractConnection {
6+
/**
7+
* @param {{client: object, queries: object?}} config config obj
8+
*/
29
constructor(config) {
310
this.config = config;
411
this.client = config.client;
@@ -10,61 +17,64 @@ class AbstractConnection {
1017
}
1118

1219
/**
13-
* @param {Object} queryObject
14-
* @param {Object} queryParams
15-
* @param {Object} queryOptions
16-
* @returns {Promise<Array>}
20+
* @abstract
21+
* @async
22+
* @param {{name: String, sql:String, addonds: Object}} queryObject query data
23+
* @param {Object} queryParams named params
24+
* @param {Object} queryOptions options
25+
* @returns {Promise<Array>} query result
1726
*/
1827
query(queryObject, queryParams, queryOptions = null) {
1928
throw new TypeError('This method must be overridden!');
2029
}
2130

2231
/**
23-
* @returns {Promise<null>}
32+
* @abstract
33+
* @async
34+
* @returns {Promise<null>} null
2435
*/
2536
transaction() {
2637
throw new TypeError('This method must be overridden!');
2738
}
2839

2940
/**
30-
* @returns {Promise<null>}
41+
* @abstract
42+
* @async
43+
* @returns {Promise<null>} null
3144
*/
3245
commit() {
3346
throw new TypeError('This method must be overridden!');
3447
}
3548

3649
/**
37-
* @returns {Promise<null>}
50+
* @abstract
51+
* @async
52+
* @returns {Promise<null>} null
3853
*/
3954
rollback() {
4055
throw new TypeError('This method must be overridden!');
4156
}
4257

4358
/**
44-
* @returns {String}
45-
*/
46-
getQueryText(queryName) {
47-
throw new TypeError('This method must be overridden!');
48-
}
49-
50-
/**
51-
* @param {String} queryText
52-
* @param {Array || Object} queryParams
53-
* @param {Object} queryOptions
54-
* @returns {Promise<Array>}
59+
* @abstract
60+
* @async
61+
* @param {String} queryText SQL
62+
* @param {Object} queryParams Named params
63+
* @param {Object} queryOptions Options
64+
* @returns {Promise<Array>} Query result
5565
*/
5666
rawQuery(queryText, queryParams, queryOptions = null) {
5767
throw new TypeError('This method must be overridden!');
5868
}
5969

6070
/**
61-
* @returns {Promise<null>}
71+
* @abstract
72+
* @async
73+
* @returns {Promise<null>} null
6274
*/
6375
release() {
6476
throw new TypeError('This method must be overridden!');
6577
}
66-
67-
6878
}
6979

7080
module.exports = AbstractConnection;

connections/PostgresConnection.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
const AbstractConnection = require('./AbstractConnection');
2-
// const log = require('../../../../app/components/log')(module);
3-
// const {NoSuchQuery} = require('../errors');
42
const {escapeParams, QueryTemplater} = require('../query');
53

4+
/**
5+
* @inheritDoc
6+
*/
67
class PostgresConnection extends AbstractConnection {
7-
constructor(config) {
8-
super(config);
9-
}
10-
8+
/**
9+
* @async
10+
* @param {String} query SQL
11+
* @returns {Promise<Array>} result
12+
* @private
13+
*/
1114
async _executeQuery(query) {
1215
const {rows} = await this.client.query(query);
1316
return rows;
1417
}
1518

16-
async rawQuery(queryText, queryParams, queryOptions = null) {
17-
const {rows} = await this.client.query(queryText, queryParams);
18-
return rows;
19+
/**
20+
* @inheritDoc
21+
*/
22+
rawQuery(queryText, queryParams, queryOptions = null) {
23+
const preparedQuery = escapeParams(queryText, queryParams);
24+
return this._executeQuery(preparedQuery);
1925
}
2026

21-
async query(queryObject, queryParams, queryOptions = {}) {
22-
// TODO: unused destructed "types" need to add opportunity to pass array as PG arrays
23-
const {sql, types} = queryObject;
27+
/**
28+
* @inheritDoc
29+
*/
30+
query(queryObject, queryParams, queryOptions = {}) {
31+
const {sql} = queryObject;
2432
let queryText = sql;
2533

2634
const {templateParams} = queryOptions;
@@ -33,22 +41,30 @@ class PostgresConnection extends AbstractConnection {
3341
return this._executeQuery(preparedQuery);
3442
}
3543

36-
getQueryText(queryName) {
37-
return this.queries[queryName];
38-
}
39-
44+
/**
45+
* @inheritDoc
46+
*/
4047
async transaction() {
4148
await this.client.query('BEGIN;');
4249
}
4350

51+
/**
52+
* @inheritDoc
53+
*/
4454
async commit() {
4555
await this.client.query('COMMIT;');
4656
}
4757

58+
/**
59+
* @inheritDoc
60+
*/
4861
async rollback() {
4962
await this.client.query('ROLLBACK;');
5063
}
5164

65+
/**
66+
* @inheritDoc
67+
*/
5268
async release() {
5369
await this.client.release();
5470
}

connectors/AbstractConnector.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
/**
2+
* @abstract
3+
* @class {AbstractConnector}
4+
*/
15
class AbstractConnector {
6+
/**
7+
* @param {Object} config config
8+
*/
29
constructor(config) {
310
this.config = config;
411

@@ -8,20 +15,25 @@ class AbstractConnector {
815
}
916

1017
/**
18+
* @abstract
19+
* @async
1120
* @returns {Promise<AbstractConnection>} Connection
1221
*/
1322
getConnection() {
1423
throw new TypeError('This method must be overridden!');
1524
}
1625

1726
/**
27+
* @abstract
28+
* @async
1829
* @param {AbstractConnection} connection Abstract connection
1930
*/
2031
releaseConnection(connection) {
2132
throw new TypeError('This method must be overridden!');
2233
}
2334

2435
/**
36+
* @abstract
2537
* @async
2638
*/
2739
closeConnection() {

connectors/PostgresConnector.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ const {Pool} = require('pg');
33
const AbstractConnector = require('./AbstractConnector');
44
const PostgresConnection = require('../connections/PostgresConnection');
55

6+
/**
7+
* @inheritDoc
8+
*/
69
class PostgresConnector extends AbstractConnector {
10+
/**
11+
* @inheritDoc
12+
*/
713
constructor(config) {
814
super(config);
915

@@ -14,6 +20,9 @@ class PostgresConnector extends AbstractConnector {
1420
// });
1521
}
1622

23+
/**
24+
* @inheritDoc
25+
*/
1726
async getConnection() {
1827
const client = await this.pool.connect();
1928

@@ -27,11 +36,16 @@ class PostgresConnector extends AbstractConnector {
2736
return connection;
2837
}
2938

30-
39+
/**
40+
* @inheritDoc
41+
*/
3142
async releaseConnection(connection) {
3243
await connection.client.release();
3344
}
3445

46+
/**
47+
* @inheritDoc
48+
*/
3549
async closeConnection() {
3650
await this.pool.end();
3751
}

factories/AbstractConnectorFactory.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ class AbstractConnectorFactory {
1818
}
1919

2020
/**
21+
* @param {String} storageName storage name in config
22+
* @returns {Object} config
23+
* @protected
24+
*/
25+
_getStorageConfig(storageName) {
26+
const config = this.storageConfig[storageName];
27+
if (!config) {
28+
throw new ReferenceError('Invalid storage name!');
29+
}
30+
31+
return config;
32+
}
33+
34+
/**
35+
* @abstract
2136
* @param {String} storageName storage name
2237
* @returns {Promise<AbstractConnector>} connector
2338
*/

factories/PostgresConnectorFactory.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
const AbstractConnectorFactory = require('./AbstractConnectorFactory');
22
const {PostgresConnector} = require('../connectors');
3-
const {QueryGetter} = require('../query');
43

54
/**
6-
* @class
5+
* @inheritDoc
76
*/
87
class PostgresConnectorFactory extends AbstractConnectorFactory {
9-
_getStorageConfig(storageName) {
10-
const config = this.storageConfig[storageName];
11-
if (!config) {
12-
throw new ReferenceError('Invalid storage name!');
13-
}
14-
15-
return config;
16-
}
17-
18-
async createConnector(storageName) {
8+
/**
9+
* @inheritDoc
10+
*/
11+
createConnector(storageName) {
1912
const config = this._getStorageConfig(storageName);
20-
21-
// const queryGetter = new QueryGetter({fileName: this.queryPath});
22-
23-
// const queries = await queryGetter.getQueries();
24-
// this.storageConfig.queries = queries;
2513
const connector = new PostgresConnector({...config, storageName});
26-
2714
return connector;
2815
}
2916
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "multi-storage",
33
"author": "Pavel Romanov <alkor@alkor.pw>",
44
"version": "0.0.1",
5+
"scripts": {
6+
"autofix": "eslint --fix ."
7+
},
58
"dependencies": {
69
"pg": "^7.4.1",
710
"mobitel-json-schema-template": "^1.0.3",

query/QueryTemplater.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
* @class
33
*/
44
class QueryTemplater {
5+
/**
6+
* @constructor
7+
*/
58
constructor() {
69
this.queryCache = new Map();
710
}
811

12+
/**
13+
* @param {String} name queryName
14+
* @param {Object} buildParams queryTemplateParams
15+
* @param {String} sql queryText
16+
* @param {Object} addons queryTemplateAddons
17+
* @returns {String} built query
18+
*/
919
buildQuery({name, sql, addons}, buildParams) {
1020
let builtQuery;
1121
const queryKey = JSON.stringify({name, buildParams});

query/escapeParams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const escapeValue = (value, type) => {
4040
} else if (value instanceof Buffer) {
4141
return `E'\\\\x${value.toString('hex')}'`;
4242
} else if (Array.isArray(value) === true) {
43-
return processArray(value, type)
43+
return processArray(value, type);
4444
} else if (value === Object(value)) {
4545
literal = JSON.stringify(value);
4646
} else {

0 commit comments

Comments
 (0)