@imehmetgenc/mysql.js

1.0.0 • Public • Published

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((results) => {
console.log("Query Results:", results);
})
.catch((error) => {
console.error("Query Error:", error);
});
  1. Basic MySQL Operations:
// Insert a new user
const newUser = { username: "mehmet", email: "mehmet@muzik.red" };
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((user) => {
    console.log("User:", user);
  })
  .catch((error) => {
    console.error("Error:", error);
  });

// Select all data from a table
mysql.selectAll("user", "*")
  .then((products) => {
    console.log("All Users:", products);
  })
  .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.

Lisans

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

Package Sidebar

Install

npm i @imehmetgenc/mysql.js

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

11.1 kB

Total Files

5

Last publish

Collaborators

  • imehmetgenc