mysql.js

This Node.js package simplifies MySQL usage and helps you perform database operations more efficiently in the Node.js environment.

MIT License

Stars
1
Committers
1

MySQL Query Module

This Node.js module is designed to facilitate interaction with a MySQL database. The module offers essential features for connection management, query operations, and event tracking.

Installation

To install this module, run the following command:

npm install @imehmetgenc/mysql.js

Usage

Utilizing the module involves the following steps:

  1. Import the Module:
const { Mysql } = require("@imehmetgenc/mysql.js");

const mysql = new Mysql({
  host: "localhost",
  user: "your_user",
  password: "your_password",
  database: "your_database",
});

//Event Usage
mysql.on("ready", (db) => {
  console.log("I'm ready");
});
mysql.on("error", (error) => {
  console.log("Mysql Error: ", error);
});
mysql.on("disconnected", (db) => {
  console.log("Mysql disconnected.");
});
  1. Executing Queries:
// Query execution example
mysql.query("SELECT * FROM users WHERE id = ?", [1]) // "SELECT * FROM users WHERE id = 1"
.then((result) => {
console.log("Query Results:", result);
})
.catch((error) => {
console.error("Query Error:", error);
});
  1. Basic MySQL Operations:
// Insert a new user
const newUser = { username: "mehmet", email: "[email protected]" };
mysql.insert("users", newUser)
  .then((result) => {
    console.log("Insert Result:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

// Select a user from the database
mysql.selectOne("users", "*", "id = 1")
  .then((result) => {
    console.log("User:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

// Select all data from a table
mysql.selectAll("user", "*")
  .then((result) => {
    console.log("All Users:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

// Update data in a table
const updatedData = { name: "Mehmet Genç" };
mysql.update("users", updatedData, "id = 1")
  .then((result) => {
    console.log("Update Result:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

// Remove data from a table
mysql.remove("users", "id = 1")
  .then((result) => {
    console.log("Removal Result:", result);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
  1. Closing the Connection Pool:
mysql.destroy();

These examples demonstrate how to use this module to interact with a MySQL database.

License

This project is licensed under the MIT license. For more information, please refer to the LICENSE file .