2.0.0 • Published 1 month ago
gamatek.mysql v2.0.0
MySQL Database Driver
Overview
The MySQL Database Driver is a Node.js module designed to simplify interactions with MySQL databases. It provides functionalities for connecting to the database, creating and dropping tables, performing CRUD operations, and executing custom queries.
Installation
You can install the MySQL Database Driver via npm:
npm install mysql2 gamatek.mysql
Usage
Importing the Module
const { Driver, TableColumn, DataTypes, Order, Condition } = require("gamatek.mysql");
Creating a Database Connection
const config = {
host: "localhost",
user: "username",
password: "password",
database: "database_name"
};
const driver = new Driver(config);
driver.connect()
.then(() => {
console.log("Connected to MySQL database successfully.");
})
.catch((err) => {
console.error("Error connecting to MySQL database:", err);
});
Creating a Table
const tableName = "users";
const columns = [
new TableColumn("id", DataTypes.Integer, true),
new TableColumn("name", DataTypes.VarChar(255)),
new TableColumn("age", DataTypes.Integer)
];
driver.prepareTable(tableName, columns)
.then(() => {
console.log(`Table ${tableName} created successfully.`);
})
.catch((err) => {
console.error(`Error creating table ${tableName}:`, err);
});
Condition() class
new Condition("key")
.equal("abcd")
.greaterThan(2)
.lessThan(5)
.between(0, 6)
.like("%abcd%")
.in([ "a", "b", "c", "d" ])
.isNull()
For negation, please add true, e.g. condition.equal("abcd", true);
.
Inserting Data
const data = {
name: "John Doe",
age: 30
};
const conditions = [new Condition("id").equal(1234)];
driver.setRows(tableName, data, conditions)
.then(() => {
console.log("Data inserted successfully.");
})
.catch((err) => {
console.error("Error inserting data:", err);
});
Querying Data
const conditions = [new Condition("age").greaterThan(25)];
const orderBy = [new Order.Descending("age")];
const limit = 3;
const offset = 10;
driver.getRows(tableName, [], conditions, orderBy, false, limit, offset);
.then((rows) => {
console.log("Fetched rows:", rows);
})
.catch((err) => {
console.error("Error fetching rows:", err);
});
// Return one item
driver.getRows(tableName, [], conditions, [], true);
.then((row) => {
console.log("Fetched row:", row);
})
.catch((err) => {
console.error("Error fetching row:", err);
});
Dropping a Table
driver.dropTable(tableName)
.then(() => {
console.log(`Table ${tableName} dropped successfully.`);
})
.catch((err) => {
console.error(`Error dropping table ${tableName}:`, err);
});
API Reference
Classes
- Driver: Represents a MySQL database driver instance.
- TableColumn: Represents a column in a database table.
- Condition: Represents a condition to be used in database queries.
Constants
- DataTypes: Contains various data types supported by MySQL.
- Order: Contains functions for specifying order in queries.
2.0.0
1 month ago
1.0.0-beta.7
3 months ago
1.0.0-beta.6
6 months ago
1.0.0-beta.2
7 months ago
1.0.0-beta.3
7 months ago
1.0.0-beta.4
7 months ago
1.0.0-beta.5
6 months ago
1.0.0-beta.1
7 months ago
1.0.1
8 months ago
1.0.0
8 months ago