diff --git a/app/persistence/PersistenceFactory.js b/app/persistence/PersistenceFactory.ts similarity index 100% rename from app/persistence/PersistenceFactory.js rename to app/persistence/PersistenceFactory.ts diff --git a/app/persistence/fabric/CRUDService.js b/app/persistence/fabric/CRUDService.ts similarity index 98% rename from app/persistence/fabric/CRUDService.js rename to app/persistence/fabric/CRUDService.ts index c5e6c9954..562db832f 100644 --- a/app/persistence/fabric/CRUDService.js +++ b/app/persistence/fabric/CRUDService.ts @@ -1,7 +1,7 @@ /** * SPDX-License-Identifier: Apache-2.0 */ - +import {PgService} from '../postgreSQL/PgService'; import { helper } from '../../common/helper'; const logger = helper.getLogger('CRUDService'); @@ -11,8 +11,10 @@ const logger = helper.getLogger('CRUDService'); * * @class CRUDService */ -class CRUDService { - constructor(sql) { +export class CRUDService { + sql : PgService; + + constructor(sql : PgService) { this.sql = sql; } @@ -260,7 +262,7 @@ class CRUDService { async getCurBlockNum(network_name, channel_genesis_hash) { let curBlockNum; try { - const row = await this.sql.getRowsBySQlCase( + const row : any = await this.sql.getRowsBySQlCase( `select max(blocknum) as blocknum from blocks where channel_genesis_hash='${channel_genesis_hash}' and network_name = '${network_name}' ` ); @@ -421,7 +423,6 @@ class CRUDService { } // Orderer BE-303 } -module.exports = CRUDService; /** * diff --git a/app/persistence/fabric/MetricService.js b/app/persistence/fabric/MetricService.ts similarity index 97% rename from app/persistence/fabric/MetricService.js rename to app/persistence/fabric/MetricService.ts index c4c84aa52..5faccd84e 100644 --- a/app/persistence/fabric/MetricService.js +++ b/app/persistence/fabric/MetricService.ts @@ -1,7 +1,7 @@ /** * SPDX-License-Identifier: Apache-2.0 */ - +import {PgService} from '../postgreSQL/PgService'; import { helper } from '../../common/helper'; const logger = helper.getLogger('MetricService'); @@ -11,8 +11,10 @@ const logger = helper.getLogger('MetricService'); * * @class MetricService */ -class MetricService { - constructor(sql) { +export class MetricService { + sql : PgService; + + constructor(sql : PgService) { this.sql = sql; } @@ -154,7 +156,7 @@ class MetricService { */ async getOrgsData(network_name, channel_genesis_hash) { const orgs = []; - const rows = await this.sql.getRowsBySQlNoCondition( + const rows : any = await this.sql.getRowsBySQlNoCondition( `select distinct on (mspid) mspid from peer where channel_genesis_hash='${channel_genesis_hash}' and network_name='${network_name}'` ); for (let i = 0, len = rows.length; i < len; i++) { @@ -192,24 +194,24 @@ class MetricService { * @memberof MetricService */ async getStatusGenerate(network_name, channel_genesis_hash) { - let chaincodeCount = await this.getChaincodeCount( + let chaincodeCount : any = await this.getChaincodeCount( network_name, channel_genesis_hash ); if (!chaincodeCount) { chaincodeCount = 0; } - let txCount = await this.getTxCount(network_name, channel_genesis_hash); + let txCount : any = await this.getTxCount(network_name, channel_genesis_hash); if (!txCount) { txCount = 0; } txCount.c = txCount.c ? txCount.c : 0; - let blockCount = await this.getBlockCount(network_name, channel_genesis_hash); + let blockCount : any = await this.getBlockCount(network_name, channel_genesis_hash); if (!blockCount) { blockCount = 0; } blockCount.c = blockCount.c ? blockCount.c : 0; - let peerCount = await this.getPeerlistCount( + let peerCount : any = await this.getPeerlistCount( network_name, channel_genesis_hash ); @@ -641,5 +643,3 @@ class MetricService { return this.sql.getRowsBySQlQuery(sqlQuery); } } - -module.exports = MetricService; diff --git a/app/persistence/fabric/UserDataService.js b/app/persistence/fabric/UserDataService.ts similarity index 92% rename from app/persistence/fabric/UserDataService.js rename to app/persistence/fabric/UserDataService.ts index b54667574..d12f979c6 100644 --- a/app/persistence/fabric/UserDataService.js +++ b/app/persistence/fabric/UserDataService.ts @@ -1,19 +1,22 @@ /** * SPDX-License-Identifier: Apache-2.0 */ +import {PgService} from '../postgreSQL/PgService'; /** * * * @class UserDataService */ -class UserDataService { +export class UserDataService { + sql : PgService; + userModel : any; /** * Creates an instance of UserDataService. * @param {*} sql * @memberof UserDataService */ - constructor(sql) { + constructor(sql : PgService) { this.sql = sql; } @@ -101,5 +104,3 @@ class UserDataService { }); } } - -module.exports = UserDataService; diff --git a/app/persistence/fabric/postgreSQL/db/processenv.js b/app/persistence/fabric/postgreSQL/db/processenv.ts similarity index 100% rename from app/persistence/fabric/postgreSQL/db/processenv.js rename to app/persistence/fabric/postgreSQL/db/processenv.ts diff --git a/app/persistence/postgreSQL/Persist.js b/app/persistence/postgreSQL/Persist.ts similarity index 76% rename from app/persistence/postgreSQL/Persist.js rename to app/persistence/postgreSQL/Persist.ts index 5bb61299b..b1c25ca88 100644 --- a/app/persistence/postgreSQL/Persist.js +++ b/app/persistence/postgreSQL/Persist.ts @@ -1,14 +1,21 @@ /** * SPDX-License-Identifier: Apache-2.0 */ -const PgService = require('./PgService'); - +import {PgService} from './PgService'; +import {MetricService} from '../fabric/MetricService'; +import {CRUDService} from '../fabric/CRUDService'; +import {UserDataService} from '../fabric/UserDataService'; /** * * * @class Persist */ class Persist { + pgservice : PgService; + metricservice : MetricService; + crudService : CRUDService; + userdataservice : UserDataService; + constructor(pgconfig) { this.pgservice = new PgService(pgconfig); this.metricservice = null; @@ -22,7 +29,7 @@ class Persist { * @param {*} metricservice * @memberof Persist */ - setMetricService(metricservice) { + setMetricService(metricservice : MetricService) { this.metricservice = metricservice; } diff --git a/app/persistence/postgreSQL/PgService.js b/app/persistence/postgreSQL/PgService.ts similarity index 90% rename from app/persistence/postgreSQL/PgService.js rename to app/persistence/postgreSQL/PgService.ts index 5527ce847..2b37f5ba4 100644 --- a/app/persistence/postgreSQL/PgService.js +++ b/app/persistence/postgreSQL/PgService.ts @@ -18,9 +18,9 @@ * limitations under the License. */ import { helper } from '../../common/helper'; +import { Client } from 'pg'; +import { Sequelize, Optional } from 'sequelize'; -const { Client } = require('pg'); -const Sequelize = require('sequelize'); const fs = require('fs'); const logger = helper.getLogger('PgService'); @@ -30,7 +30,11 @@ const logger = helper.getLogger('PgService'); * * @class PgService */ -class PgService { +export class PgService { + pgconfig : any; + userModel : any; + client : Client; + /** * Creates an instance of PgService. * @param {*} pgconfig @@ -103,7 +107,7 @@ class PgService { */ async handleDisconnect() { try { - this.client.on('error', err => { + this.client.on('error', (err : NodeJS.ErrnoException) => { logger.error('db error', err); if (err.code === 'PROTOCOL_CONNECTION_LOST') { this.handleDisconnect(); @@ -517,7 +521,7 @@ class PgService { * @param datatype limit the page limit. * */ - getRowsBySQlNoCondition(sqlcharacter, limit) { + getRowsBySQlNoCondition(sqlcharacter, limit? ) : Promise{ /* eslint-disable */ const _self = this; return new Promise((resolve, reject) => { @@ -574,75 +578,4 @@ class PgService { }); } - /** - * - * @param sql - * @param key - * @returns {Promise} - * - */ - getSQL2Map(sql, key) { - const _self = this; - return new Promise((resolve, reject) => { - _self.client.query(sql, (err, res) => { - if (err) { - reject(err); - return; - } - - logger.debug(`The solution is: ${res.rows.length} `); - - const keymap = new Map(); - - for (let ind = 0; ind < res.rows.length; ind++) { - logger.debug(`The ind value is: ${res.rows[ind].id} `); - keymap.set(res.rows[ind][key], res.rows[ind]); - } - - resolve(keymap); - }); - }); - } - - /** - * - * - * @param unknown_type sql - * @param unknown_type key - * @return unknown - */ - getSQL2Map4Arr(sql, key) { - const _self = this; - return new Promise((resolve, reject) => { - _self.client.query(sql, (err, rows) => { - if (err) { - reject(err); - return; - } - - // Logger.debug( `The solution is: ${rows.length } ` ); - logger.debug(' the getSqlMap '); - - const keymap = new Map(); - - for (let ind = 0; ind < rows.length; ind++) { - const keyvalue = rows[ind][key]; - let arrvalue = []; - - if (keymap.has(keyvalue)) { - arrvalue = keymap.get(keyvalue); - arrvalue.push(rows); - } else { - arrvalue.push(rows); - } - - keymap.set(keyvalue, arrvalue); - } - - resolve(keymap); - }); - }); - } } - -module.exports = PgService; diff --git a/app/platform/fabric/Platform.ts b/app/platform/fabric/Platform.ts index 908a4f286..778f09e3f 100644 --- a/app/platform/fabric/Platform.ts +++ b/app/platform/fabric/Platform.ts @@ -5,6 +5,9 @@ import * as path from 'path'; import {helper} from '../../common/helper'; import {User} from './models/User'; +import {MetricService} from '../../persistence/fabric/MetricService'; +import {CRUDService} from '../../persistence/fabric/CRUDService'; +import {UserDataService} from '../../persistence/fabric/UserDataService'; const fs = require('fs-extra'); @@ -14,11 +17,6 @@ const ExplorerError = require('../../common/ExplorerError'); const logger = helper.getLogger('Platform'); const FabricUtils = require('./utils/FabricUtils.js'); const ExplorerListener = require('../../sync/listener/ExplorerListener'); - -const CRUDService = require('../../persistence/fabric/CRUDService'); -const MetricService = require('../../persistence/fabric/MetricService'); -const UserDataService = require('../../persistence/fabric/UserDataService'); - const fabric_const = require('./utils/FabricConst').fabric.const; const explorer_error = require('../../common/ExplorerMessage').explorer.error; diff --git a/app/platform/fabric/models/User.ts b/app/platform/fabric/models/User.ts index 06ce3b7ea..c68305bae 100644 --- a/app/platform/fabric/models/User.ts +++ b/app/platform/fabric/models/User.ts @@ -59,5 +59,3 @@ export class User { return this.userJson; } } - -// module.exports = User; diff --git a/app/platform/fabric/sync/SyncPlatform.ts b/app/platform/fabric/sync/SyncPlatform.ts index d2ce7192b..4ef3edd91 100644 --- a/app/platform/fabric/sync/SyncPlatform.ts +++ b/app/platform/fabric/sync/SyncPlatform.ts @@ -3,6 +3,10 @@ */ import * as path from 'path'; +import {helper} from '../../../common/helper'; +import {MetricService} from '../../../persistence/fabric/MetricService'; +import {CRUDService} from '../../../persistence/fabric/CRUDService'; + const fs = require('fs-extra'); const SyncService = require('../sync/SyncService'); @@ -10,13 +14,9 @@ const FabricUtils = require('../utils/FabricUtils'); const FabricEvent = require('./FabricEvent'); const FabricConfig = require('../FabricConfig'); -import {helper} from '../../../common/helper'; - const logger = helper.getLogger('SyncPlatform'); const ExplorerError = require('../../../common/ExplorerError'); -const CRUDService = require('../../../persistence/fabric/CRUDService'); -const MetricService = require('../../../persistence/fabric/MetricService'); const fabric_const = require('../utils/FabricConst').fabric.const; const explorer_mess = require('../../../common/ExplorerMessage').explorer; diff --git a/package-lock.json b/package-lock.json index 4979aa80b..b7b37c9d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -110,6 +110,11 @@ "any-observable": "^0.3.0" } }, + "@types/bluebird": { + "version": "3.5.32", + "resolved": "https://registry.npmjs.org/@types/bluebird/-/bluebird-3.5.32.tgz", + "integrity": "sha512-dIOxFfI0C+jz89g6lQ+TqhGgPQ0MxSnh/E4xuC0blhFtyW269+mPG5QeLgbdwst/LvdP8o1y0o/Gz5EHXLec/g==" + }, "@types/body-parser": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz", @@ -143,6 +148,14 @@ "@types/node": "*" } }, + "@types/continuation-local-storage": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@types/continuation-local-storage/-/continuation-local-storage-3.2.2.tgz", + "integrity": "sha512-aItm+aYPJ4rT1cHmAxO+OdWjSviQ9iB5UKb5f0Uvgln0N4hS2mcDodHtPiqicYBXViUYhqyBjhA5uyOcT+S34Q==", + "requires": { + "@types/node": "*" + } + }, "@types/express": { "version": "4.17.8", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz", @@ -172,6 +185,11 @@ "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", "dev": true }, + "@types/lodash": { + "version": "4.14.161", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.161.tgz", + "integrity": "sha512-EP6O3Jkr7bXvZZSZYlsgt5DIjiGr0dXP1/jVEwVLTFgg0d+3lWVQkRavYVQszV7dYUwvg0B8R0MBDpcmXg7XIA==" + }, "@types/long": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.1.tgz", @@ -188,6 +206,20 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-14.10.0.tgz", "integrity": "sha512-SOIyrdADB4cq6eY1F+9iU48iIomFAPltu11LCvA9PKcyEwHadjCFzNVPotAR+oEJA0bCP4Xvvgy+vwu1ZjVh8g==" }, + "@types/pg": { + "version": "7.14.5", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-7.14.5.tgz", + "integrity": "sha512-wqTKZmqkqXd1YiVRBT2poRrMIojwEi2bKTAAjUX6nEbzr98jc3cfR/7o7ZtubhH5xT7YJ6LRdRr1GZOgs8OUjg==", + "requires": { + "@types/node": "*", + "@types/pg-types": "*" + } + }, + "@types/pg-types": { + "version": "1.11.5", + "resolved": "https://registry.npmjs.org/@types/pg-types/-/pg-types-1.11.5.tgz", + "integrity": "sha512-L8ogeT6vDzT1vxlW3KITTCt+BVXXVkLXfZ/XNm6UqbcJgxf+KPO7yjWx7dQQE8RW07KopL10x2gNMs41+IkMGQ==" + }, "@types/qs": { "version": "6.9.4", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.4.tgz", @@ -211,6 +243,17 @@ "form-data": "^2.5.0" } }, + "@types/sequelize": { + "version": "4.28.9", + "resolved": "https://registry.npmjs.org/@types/sequelize/-/sequelize-4.28.9.tgz", + "integrity": "sha512-QqYgkw/2fEc0FyEQejnxM7cHKB8XBV3Y69k7GSFOToQBOXos0PJVqNpgROXZddXIkl2d6zicYssHuy75ws84sw==", + "requires": { + "@types/bluebird": "*", + "@types/continuation-local-storage": "*", + "@types/lodash": "*", + "@types/validator": "*" + } + }, "@types/serve-static": { "version": "1.13.5", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.5.tgz", @@ -226,6 +269,11 @@ "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.0.tgz", "integrity": "sha512-I99sngh224D0M7XgW1s120zxCt3VYQ3IQsuw3P3jbq5GG4yc79+ZjyKznyOGIQrflfylLgcfekeZW/vk0yng6A==" }, + "@types/validator": { + "version": "13.1.0", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.1.0.tgz", + "integrity": "sha512-gHUHI6pJaANIO2r6WcbT7+WMgbL9GZooR4tWpuBOETpDIqFNxwaJluE+6rj6VGYe8k6OkfhbHz2Fkm8kl06Igw==" + }, "@types/ws": { "version": "7.2.6", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.2.6.tgz", diff --git a/package.json b/package.json index e2ee4f58a..4b7b112b5 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,8 @@ "fabric nodesdk" ], "dependencies": { + "@types/pg": "^7.14.5", + "@types/sequelize": "^4.28.9", "ajv": "^5.5.2", "app-root-path": "^2.0.1", "asn1.js": "^5.0.1",