-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodejs-mysqli-curd
182 lines (164 loc) · 5.34 KB
/
nodejs-mysqli-curd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const mysql = require('mysql');
/**
* Database class
*
* Provides functionalities for database interactions.
* @author Mokter Hossain
* @email [email protected]
* @website www.gglink.uk
* @github https://github.com/moktermd08
* @linkedin https://www.linkedin.com/in/mr-mokter/
* @twitter https://twitter.com/moGGLink
*/
class Database {
constructor() {
this.host = "localhost";
this.user = "user";
this.password = "password";
this.database = "database";
this.connection = null;
}
/**
* Establishes a database connection.
* @returns {Promise<Boolean>} True on successful connection, False on failure
* @throws {Error} If connection to the database fails
*/
async connect() {
return new Promise((resolve, reject) => {
this.connection = mysql.createConnection({
host: this.host,
user: this.user,
password: this.password,
database: this.database
});
this.connection.connect(error => {
if (error) {
reject(new Error('Failed to connect to the database: ' + error.message));
} else {
resolve(true);
}
});
});
}
/**
* Closes the database connection.
* @returns {Promise<Boolean>} True on success, False on failure
*/
async disconnect() {
return new Promise((resolve, reject) => {
if (this.connection) {
this.connection.end(error => {
if (error) {
reject(new Error('Failed to disconnect from the database: ' + error.message));
} else {
resolve(true);
}
});
} else {
resolve(false);
}
});
}
/**
* Executes a SELECT query.
* @param {string} table The name of the table.
* @param {string} [columns='*'] The columns to select.
* @param {string} [where] The WHERE clause.
* @returns {Promise<Array>} The result rows.
*/
async select(table, columns = '*', where = null) {
return new Promise((resolve, reject) => {
let sql = `SELECT ${columns} FROM ${table}`;
if (where !== null) {
sql += ` WHERE ${where}`;
}
this.connection.query(sql, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
/**
* Inserts a new record into a table.
* @param {string} table The name of the table.
* @param {Object} data The data to insert.
* @returns {Promise<Object>} The result of the insertion.
*/
async insert(table, data) {
return new Promise((resolve, reject) => {
const sql = `INSERT INTO ${table} SET ?`;
this.connection.query(sql, data, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
/**
* Updates records in a table.
* @param {string} table The name of the table.
* @param {Object} data The data to update.
* @param {string} where The WHERE clause.
* @returns {Promise<Object>} The result of the update.
*/
async update(table, data, where) {
return new Promise((resolve, reject) => {
const sql = `UPDATE ${table} SET ? WHERE ${where}`;
this.connection.query(sql, data, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
/**
* Deletes records from a table.
* @param {string} table The name of the table.
* @param {string} where The WHERE clause.
* @returns {Promise<Object>} The result of the deletion.
*/
async delete(table, where) {
return new Promise((resolve, reject) => {
const sql = `DELETE FROM ${table} WHERE ${where}`;
this.connection.query(sql, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
}
module.exports = Database;
/* Example use * /
(async () => {
const db = new Database();
try {
await db.connect();
// Insert
const insertData = { name: 'John Doe', age: 30 };
const insertResult = await db.insert('users', insertData);
console.log('Insert Result:', insertResult);
// Select
const users = await db.select('users', '*', 'age > 20');
console.log('Users:', users);
// Update
const updateData = { age: 31 };
const updateResult = await db.update('users', updateData, 'name = "John Doe"');
console.log('Update Result:', updateResult);
// Delete
const deleteResult = await db.delete('users', 'age < 20');
console.log('Delete Result:', deleteResult);
await db.disconnect();
} catch (error) {
console.error('Database Error:', error.message);
}
})();