npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

opticore-mysqldb

v1.0.36

Published

opticore mysql driver data base

Readme

OptiCoreMySQLDriver Database Handler

The OptiCoreMySQLDriver class provides a robust wrapper for OptiCoreMySQLDriver database operations with connection management, query execution, error handling, and logging capabilities.

Table of Contents

Installation

Ensure you have the required dependencies installed:

npm install opticore-mysqldb

Usage

Initialization

import { OptiCoreMySQLDriver } from 'opticore-mysqldb';
import mySQL from 'mysql2';

const getEnvVariable = {
    DB_HOST: 'localhost',
    DB_USER: 'root',
    DB_PASSWORD: 'password',
    DB_NAME: 'test_db',
};

const dbHandler = new OptiCoreMySQLDriver(getEnvVariable, 'en');

Connection

dbHandler.connect();

Query Execution

Using makeQuery (flexible parameters)

// With callback
dbHandler.makeQuery('SELECT * FROM users', [], (err, results, fields) => {
    if (err) console.error(err);
    else console.log(results);
});

// With Promise
try {
    const { results } = await dbHandler.makeQuery('SELECT * FROM users');
    console.log(results);
} catch (error) {
    console.error(error);
}

Using query (structured parameters)

try {
    const { results } = await dbHandler.query({
        sql: 'SELECT * FROM users WHERE id = ?',
        values: [1],
        options: {
            timeout: 10000
        }
    });
    console.log(results);
} catch (error) {
    console.error(error);
}

Repository Methods

insert

await dbHandler.insert('users', {
    name: 'Alice',
    email: '[email protected]'
});

update

await dbHandler.update('users', { name: 'Alice B.' }, {
    where: {
        id: 1
    }
});

delete

await dbHandler.delete('users', {
    where: {
        id: 1
    }
});

find

const users = await dbHandler.find('users', {
    where: {
        role: 'admin'
    }
});

Connection Closing

await dbHandler.close();

API Reference

Constructor

new OptiCoreMySQLDriver(getEnvVariable: any, localLanguage: string)
  • getEnvVariable: Environment variables containing DB configuration from .env
  • localLanguage: Language configuration for translations

Methods

connect(getEnvVariable: any): void

Verifies and establishes OptiCoreMySQLDriver database connection.
Throws an Error when connection fails.

makeQuery<T = any>(sql: string | QueryOptions, values?: any[], callback?: Function): Promise<IQueryResult<T>>

Executes a OptiCoreMySQLDriver query with flexible parameter handling.

  • sql: SQL query string or QueryOptions object
  • values: Optional parameter values for prepared statements
  • callback: Optional callback function
  • Returns: Promise resolving to query results and fields metadata
query<T = any>(params: IQueryParams<T>): Promise<IQueryResult<T>>

Executes a query using structured parameters.

  • params: Query parameters object
  • Returns: Promise resolving to query results
insert(table: string, data: Record<string, any>): Promise<IQueryResult<any>>

Inserts a new row into the specified table.

update(table: string, data: Record<string, any>, options: UpdateOptions): Promise<IQueryResult<any>>

Updates rows in the specified table with conditions.

delete(table: string, options: DeleteOptions): Promise<IQueryResult<any>>

Deletes rows from the specified table based on conditions.

find(table: string, options?: FindOptions): Promise<IQueryResult<any>>

Finds and returns rows from a table with optional filters.

close(): Promise<void>

Closes the database connection.

mySqlDB(): MySQLDatabase

Protected method. Provides access to OptiCoreMySQLDriver database utilities.

Error Handling

The class provides comprehensive error handling with:

  • Automatic error logging
  • Translation support for error messages
  • HTTP status codes for error classification
  • Stack trace preservation

Dependencies

  • mysql2
  • opticore-translator
  • opticore-http-response
  • opticore-logger
  • @opticore-mysqldb/core

License

MIT