
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
@megaorm/mysql
Advanced tools
This package provides a simple, high-level, unified API for interacting with MySQL databases.
This package provides a simple, high-level, unified API for interacting with MySQL databases. It simplifies creating connections, executing queries, and managing transactions.
While this package is designed for MegaORM, you are free to use it independently in any project as needed.
To install this package, run the following command:
npm install @megaorm/mysql
To start interacting with your MySQL database, you need to create a connection.
MySQL
driver from @megaorm/mysql
to use it in your project.const { MySQL } = require('@megaorm/mysql');
MySQL
and provide the necessary database configuration.const driver = new MySQL({
database: 'test', // The name of the database you're connecting to
user: 'root', // The username to access the database
password: 'root', // The password for the database user
host: 'localhost', // The host where the MySQL server is running
});
create()
method to establish a connection to the database.driver
.create()
.then((r) => console.log(r)) // MegaConnection instance
.catch((e) => console.log(e)); // Handles errors
Throws a
CreateConnectionError
if there was an issue creating the connection.
Once you’ve established a connection, you can start executing SQL queries on your MySQL database.
connection
.query('SELECT * FROM users;')
.then((result) => console.log(result)) // Logs the result: [{name: 'John', id: 1}, ...]
.catch((error) => console.log(error)); // Handles errors
const data = ['user1@gmail.com', 'pass1'];
connection
.query('INSERT INTO users (email, password) VALUES (?, ?);', data)
.then((result) => console.log(result)) // Logs the inserted ID for a single row
.catch((error) => console.log(error)); // Handles errors
const data = ['user2@gmail.com', 'pass2', 'user3@gmail.com', 'pass3'];
connection
.query('INSERT INTO users (email, password) VALUES (?, ?), (?, ?);', data)
.then((result) => console.log(result)) // Logs `undefined` for bulk insert
.catch((error) => console.log(error)); // Handles errors
const data = ['updated_email@example.com', 22];
connection
.query('UPDATE users SET email = ? WHERE id = ?;', data)
.then((result) => console.log(result)) // Logs `undefined` (for successful update)
.catch((error) => console.log(error)); // Handles errors
const data = [33];
connection
.query('DELETE FROM users WHERE id = ?;', data)
.then((result) => console.log(result)) // Logs `undefined` (for successful delete)
.catch((error) => console.log(error)); // Handles errors
For queries like
CREATE TABLE
orDROP TABLE
, the result will beundefined
, since no specific data is returned.
Always close the connection after you're done using it. This is important because it frees up resources and prevents problems like memory leaks.
connection
.close()
.then((r) => console.log(r)) // undefined (success)
.catch((e) => console.log(e)); // Handles errors
Throws a
CloseConnectionError
if there was an issue closing the connection.
A transaction ensures that a group of database operations is treated as a single unit. Either all operations succeed (commit), or none of them are applied (rollback). This helps maintain data integrity.
// Begin transaction
await connection.beginTransaction();
try {
// Insert user
const userId = await connection.query(
'INSERT INTO users (email, password) VALUES (?, ?)',
['john@example.com', 'password']
);
// Insert related profile
await connection.query(
'INSERT INTO profiles (user_id, city, age) VALUES (?, ?, ?)',
[userId, 'Tokyo', 30]
);
// Commit if everything is successful
await connection.commit();
} catch (error) {
// Rollback if something goes wrong
await connection.rollback();
}
beginTransaction()
: Throws BeginTransactionError
if there was an issuecommit()
: Throws CommitTransactionError
if there was an issuerollback()
: Throws RollbackTransactionError
if there was an issue.In this example, we’ll walk through the process of creating a connection to your MySQL
database, executing a query to fetch data from a table, and then closing the connection once you’re done. This example uses an async function to handle the asynchronous operations.
// Import MySQL Driver
const { MySQL } = require('@megaorm/mysql');
// Define an async function
const app = async () => {
// Create driver instance with database configuration
const driver = new MySQL({
database: 'test', // The database name
user: 'root', // MySQL username
password: 'root', // MySQL password
host: 'localhost', // Database host
});
// Establish a connection to your MySQL database
const connection = await driver.create();
// Execute a query to fetch all records from the 'users' table
const users = await connection.query('SELECT * FROM users');
// Log the result of the query (list of users)
console.log(users);
// Close the connection to the database
await connection.close();
// The connection is now closed; you should not use it anymore!
};
// Execute your app
app();
user?: string
password?: string
password1?: string
password2?: string
password3?: string
database?: string
charset?: string
'UTF8_GENERAL_CI'
.host?: string
'localhost'
.port?: number
3306
, which is the standard MySQL port.localAddress?: string
socketPath?: string
host
and port
settings.flags?: Array<string>
ssl?: string | SslOptions
bigNumberStrings?: boolean
true
, all BIGINT
values are retrieved as strings instead of numbers, which is useful to avoid precision loss with large values.These options allow you to customize your MySQL connection according to your specific requirements, including multi-factor authentication, SSL configuration, and advanced connection settings.
FAQs
This package provides a simple, high-level, unified API for interacting with MySQL databases.
We found that @megaorm/mysql 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.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.