Skip to content

Commit

Permalink
Mandatory jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
evgnomon committed Nov 1, 2023
1 parent c074ee4 commit ed9e914
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
69 changes: 31 additions & 38 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<mysql.Pool>}
*/
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;
Expand Down Expand Up @@ -83,7 +77,6 @@ export class DB {
}
}

//eslint-disable-next-line no-unused-vars
class MEMConfig {
/**
* @type {string}
Expand All @@ -107,7 +100,7 @@ export class MEM {
#conf;
/**
* constructor.
* @param {MEMConfig} [conf]
* @param {MEMConfig} [conf] - configuration
*/
constructor(conf) {
if (!conf) {
Expand Down Expand Up @@ -135,7 +128,7 @@ export class MEM {

/**
* connect.
* @returns {Promise<RedisCluster>}
* @returns {Promise<RedisCluster>} - redisio cluster
*/
async connect() {
const ports = [
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}'",
Expand Down

0 comments on commit ed9e914

Please sign in to comment.