Comparing version 1.1.1 to 1.1.2
{ | ||
"name": "highsql", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "High level MySQL utility", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
import { createPool } from 'mysql2/promise'; | ||
class Connection { | ||
constructor(host, user, password, database, connectionLimit = 10) { | ||
constructor(host, user, password, database, connectionLimit = 10, port = 3306) { | ||
this.host = host; | ||
@@ -9,2 +9,3 @@ this.user = user; | ||
this.connectionLimit = connectionLimit; | ||
this.port = port; | ||
this.pool = createPool({ | ||
@@ -15,17 +16,15 @@ host: this.host, | ||
database: this.database, | ||
connectionLimit: this.connectionLimit | ||
connectionLimit: this.connectionLimit, | ||
port: this.port, | ||
}); | ||
} | ||
async getConnection() { | ||
return this.pool.getConnection(); | ||
} | ||
async query(sql, values) { | ||
const connection = await this.getConnection(); | ||
const connection = await this.pool.getConnection(); | ||
try { | ||
const [result] = await connection.query(sql, values); | ||
return result; | ||
const [rows] = await connection.query(sql, values); | ||
return rows; | ||
} | ||
catch (error) { | ||
console.error('Error executing query:', error); | ||
throw error; // Re-throw the error to handle it at a higher level | ||
throw error; | ||
} | ||
@@ -41,7 +40,7 @@ finally { | ||
} | ||
return await this.query(sql, params); | ||
return this.query(sql, params); | ||
} | ||
async insert(table, values) { | ||
const sql = `INSERT INTO ${table} SET ?`; | ||
return await this.query(sql, values); | ||
return this.query(sql, values); | ||
} | ||
@@ -48,0 +47,0 @@ async update(table, values, where, params) { |
@@ -1,2 +0,2 @@ | ||
import {createPool, Pool, PoolConnection, RowDataPacket, ResultSetHeader} from 'mysql2/promise'; | ||
import { createPool, Pool, RowDataPacket, ResultSetHeader } from 'mysql2/promise'; | ||
@@ -11,3 +11,4 @@ class Connection { | ||
private database: string, | ||
private connectionLimit: number = 10 | ||
private connectionLimit: number = 10, | ||
private port: number = 3306, | ||
) { | ||
@@ -19,18 +20,15 @@ this.pool = createPool({ | ||
database: this.database, | ||
connectionLimit: this.connectionLimit | ||
connectionLimit: this.connectionLimit, | ||
port: this.port, | ||
}); | ||
} | ||
private async getConnection(): Promise<PoolConnection> { | ||
return this.pool.getConnection(); | ||
} | ||
private async query(sql: string, values?: any[]): Promise<ResultSetHeader | RowDataPacket[] | RowDataPacket[][] | any> { | ||
const connection = await this.getConnection(); | ||
async query<T extends RowDataPacket[] | RowDataPacket[][] | ResultSetHeader>(sql: string, values?: any[]): Promise<T> { | ||
const connection = await this.pool.getConnection(); | ||
try { | ||
const [result] = await connection.query(sql, values); | ||
return result; | ||
const [rows] = await connection.query(sql, values); | ||
return rows as T; | ||
} catch (error) { | ||
console.error('Error executing query:', error); | ||
throw error; // Re-throw the error to handle it at a higher level | ||
throw error; | ||
} finally { | ||
@@ -46,3 +44,3 @@ connection.release(); | ||
} | ||
return await this.query(sql, params) as Promise<RowDataPacket[]>; | ||
return this.query<RowDataPacket[]>(sql, params); | ||
} | ||
@@ -52,3 +50,3 @@ | ||
const sql = `INSERT INTO ${table} SET ?`; | ||
return await this.query(sql, values) as Promise<ResultSetHeader>; | ||
return this.query<ResultSetHeader>(sql, values); | ||
} | ||
@@ -58,3 +56,3 @@ | ||
const sql = `UPDATE ${table} SET ? WHERE ${where}`; | ||
return this.query(sql, [values, ...(params || [])]) as Promise<ResultSetHeader>; | ||
return this.query<ResultSetHeader>(sql, [values, ...(params || [])]); | ||
} | ||
@@ -64,3 +62,3 @@ | ||
const sql = `DELETE FROM ${table} WHERE ${where}`; | ||
return this.query(sql, params || []) as Promise<ResultSetHeader>; | ||
return this.query<ResultSetHeader>(sql, params || []); | ||
} | ||
@@ -67,0 +65,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7919
117