Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A TypeScript module for handling MySQL database connections and performing queries.
mysql2/promise
for a modern asynchronous interface.select
, insert
, update
, and delete
.npm install mysql2
import { Connection, ConnectionConfig } from 'mysql-connection'; // Replace with the actual path to your module
async function main() {
const config: ConnectionConfig = {
host: 'your_host',
user: 'your_user',
password: 'your_password',
database: 'your_database'
};
const conn = new Connection(config);
// Example: Select data
const rows = await conn.select('users', 'id, name', 'id = ?', [1]);
console.log(rows);
// Example: Insert data
const result = await conn.insert('users', { name: 'Bob', email: 'bob@example.com' });
console.log(result);
// Example: Update data
await conn.update('users', { email: 'bob@newmail.com' }, 'id = ?', [1]);
// Example: Delete data
await conn.delete('users', 'id = ?', [1]);
// Example: Transaction
await conn.transaction(async (connection) => {
await connection.insert('users', { name: 'Charlie', email: 'charlie@example.com' });
await connection.update('users', { email: 'charlie@newmail.com' }, 'id = ?', [2]); // Assuming Charlie has ID 2
});
await conn.close(); // Close the connection pool when done
}
main().catch(error => {
console.error('Error:', error);
});
### Connection(config: ConnectionConfig)
Creates a new `Connection` object.
- `config`: A configuration object of type `ConnectionConfig` containing database connection details.
### Methods
- `query<T>(sql: string, values?: any[]): Promise<T>`: Executes a SQL query with optional values.
- `select(table: string, columns?: string, where?: string, params?: any[]): Promise<RowDataPacket[]>`: Executes a `SELECT` query.
- `insert(table: string, values: any): Promise<ResultSetHeader>`: Executes an `INSERT` query.
- `update(table: string, values: any, where: string, params?: any[]): Promise<ResultSetHeader>`: Executes an `UPDATE` query.
- `delete(table: string, where: string, params?: any[]): Promise<ResultSetHeader>`: Executes a `DELETE` query.
- `get(table: string, where: string, params?: any[]): Promise<RowDataPacket | null>`: Retrieves a single row based on a condition.
- `getByID(table: string, id: number): Promise<RowDataPacket | null>`: Retrieves a single row by its ID.
- `count(table: string, where?: string, params?: any[]): Promise<number>`: Counts rows based on a condition.
- `exists(table: string, where: string, params?: any[]): Promise<boolean>`: Checks if any rows match a condition.
- `transaction(queries: (connection: Connection) => Promise<any>): Promise<any>`: Executes multiple queries in a transaction.
- `close(): Promise<void>`: Closes the connection pool.
- `getPool(): Pool`: Returns the underlying connection pool.
The module includes error handling for both query errors and connection pool errors. Errors are logged to the console with detailed information, including the error message, query, and values (if applicable). Custom error classes QueryError
and DatabaseError
are thrown to help differentiate between the types of errors.
MIT License
FAQs
High level MySQL utility
We found that highsql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.