-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypescript-mysql-crud.ts
121 lines (113 loc) · 3.3 KB
/
typescript-mysql-crud.ts
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
import mysql, { Connection, MysqlError } from 'mysql';
/**
* Class Database
*
* 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
*
*/
abstract class Database {
private host: string = 'localhost';
private user: string = 'user';
private password: string = 'password';
private database: string = 'database';
protected connection: Connection | null = null;
/**
* Establishes a database connection.
*
* @returns {Promise<boolean>} True on successful connection, False on failure
*
* @throws {Error} If connection to the database fails
*/
public connect(): Promise<boolean> {
return new Promise((resolve, reject) => {
this.connection = mysql.createConnection({
host: this.host,
user: this.user,
password: this.password,
database: this.database
});
this.connection.connect((err: MysqlError | null) => {
if (err) {
reject(new Error('Failed to connect to the database: ' + err.message));
} else {
resolve(true);
}
});
});
}
/**
* Closes the database connection.
*
* @returns {Promise<boolean>} True on success, False on failure
*/
public disconnect(): Promise<boolean> {
return new Promise((resolve, reject) => {
if (this.connection) {
this.connection.end(err => {
if (err) {
reject(false);
} else {
resolve(true);
}
});
} else {
resolve(false);
}
});
}
/**
* Sanitize user input.
*
* @param {string} data - The user input data
* @returns {string} The sanitized data
*/
public sanitizeInput(data: string): string {
data = data.trim();
data = this.connection ? this.connection.escape(data).toString() : data;
return data;
}
/**
* Sanitize output.
*
* @param {string} data - The data to be sent to the client
* @returns {string} The sanitized data
*/
public sanitizeOutput(data: string): string {
return data
.toString()
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
/**
* Class MySQLDatabase
*
* A concrete implementation of the Database abstract class for MySQL databases.
*/
class MySQLDatabase extends Database {
// Here you can implement methods specific to MySQLDatabase.
}
// Example Usage:
// const db = new MySQLDatabase();
// db.connect()
// .then(() => {
// console.log('Connected to the database');
// console.log('Sanitized Input:', db.sanitizeInput("user' OR '1'='1"));
// return db.disconnect();
// })
// .then(() => {
// console.log('Disconnected from the database');
// })
// .catch(err => {
// console.error(err.message);
// });