diff --git a/.eslintrc.json b/.eslintrc.json index a5b6f02..f85270a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -14,7 +14,8 @@ "extends": [ "eslint:recommended", "plugin:eslint-comments/recommended", - "plugin:mocha/recommended" + "plugin:mocha/recommended", + "plugin:jsdoc/recommended-error" ], "globals": { "console": true }, "rules": { diff --git a/lib/index.js b/lib/index.js index b5530db..04ff123 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,49 +8,43 @@ import mysql from "mysql"; * @typedef {import('ioredis').Cluster} RedisCluster */ -function resolveEndpoint(shard) { - let portNum = 3306 - 1 + shard; - if (process.env[`DB_SHARD_${shard}_INTERNAL_PORT`]) { - portNum = parseInt(process.env[`DB_SHARD_${shard}_INTERNAL_PORT`] || "0"); +export class DB { + #pool; + resolveEndpoint(shard) { + let portNum = 3306 - 1 + shard; + if (process.env[`DB_SHARD_${shard}_INTERNAL_PORT`]) { + portNum = parseInt(process.env[`DB_SHARD_${shard}_INTERNAL_PORT`] || "0"); + } + return { + host: process.env[`DB_SHARD_${shard}_INTERNAL_HOST`] || "127.0.0.1", + port: portNum, + }; } - return { - host: process.env[`DB_SHARD_${shard}_INTERNAL_HOST`] || "127.0.0.1", - port: portNum, - }; -} -/** - * createPool. - * - * @param {number} shard - * @returns {Promise} - */ -function createPool(shard) { - return new Promise((resolve, reject) => { - try { - const pool = mysql.createPool({ - ...resolveEndpoint(shard), - user: `test_${shard}`, - password: "password", - database: `myproject_${shard}`, - }); - resolve(pool); - } catch (e) { - reject(e); - } - }); -} + createPool(shard) { + return new Promise((resolve, reject) => { + try { + const pool = mysql.createPool({ + ...this.resolveEndpoint(shard), + user: `test_${shard}`, + password: "password", + database: `myproject_${shard}`, + }); + resolve(pool); + } catch (e) { + reject(e); + } + }); + } -export class DB { - #pool; constructor(shardNum = 1) { - this.#pool = createPool(shardNum); + this.#pool = this.createPool(shardNum); } /** * query. - * @param {string} sql - * @returns {Promise<[unknown, mysql.FieldInfo[]|undefined]>} + * @param {string} sql - sql + * @returns {Promise<[unknown, mysql.FieldInfo[]|undefined]>} - [results, fields] */ async query(sql) { const pool = await this.#pool; @@ -83,7 +77,6 @@ export class DB { } } -//eslint-disable-next-line no-unused-vars class MEMConfig { /** * @type {string} @@ -107,7 +100,7 @@ export class MEM { #conf; /** * constructor. - * @param {MEMConfig} [conf] + * @param {MEMConfig} [conf] - configuration */ constructor(conf) { if (!conf) { @@ -135,7 +128,7 @@ export class MEM { /** * connect. - * @returns {Promise} + * @returns {Promise} - redisio cluster */ async connect() { const ports = [ diff --git a/package.json b/package.json index fe7072d..b8adaf6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "types": "./types/index.d.ts", "scripts": { "build": "tsc --declaration --emitDeclarationOnly --outDir types --allowJs --declarationMap lib/index.js", - "lint": "eslint --max-warnings=0 '{src,test}/**/*.{js,jsx}' && tsc --noEmit", + "lint": "eslint --max-warnings=0 '{lib,test}/**/*.{js,jsx}' && tsc --noEmit", + "lint:fix": "eslint --fix --max-warnings=0 '{lib,test}/**/*.{js,jsx}' && tsc --noEmit", "test": "mocha 'test/**/*.spec.{js,jsx}'", "test:watch": "npm run test -- --watch --watch-files src,test", "fmt:base": "prettier '{src,test}/**/*.{js,jsx}'",