Skip to content

Commit b5e3583

Browse files
committed
New create_db method, drop method fixes
1 parent b363e32 commit b5e3583

File tree

8 files changed

+76
-17
lines changed

8 files changed

+76
-17
lines changed

LICENSE

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
MIT License
2-
3-
Additional Functionality
4-
- Copyright (c) 2022 <[email protected]> (itariq.dev) TARIQ
1+
Copyright (c) 2023 <[email protected]> TARIQ (itariq.dev)
52

63
Permission is hereby granted, free of charge, to any person obtaining a copy
74
of this software and associated documentation files (the "Software"), to deal
@@ -10,13 +7,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
107
copies of the Software, and to permit persons to whom the Software is
118
furnished to do so, subject to the following conditions:
129

13-
The above copyright notice and this permission notice shall be included in
14-
all copies or substantial portions of the Software.
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
1512

1613
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1714
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1815
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1916
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2017
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
THE SOFTWARE.
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
## Installation
3737

3838
``
39-
npm i mysql-database
39+
npm i mysql-database@latest
4040
``
4141

4242
## Documentation
@@ -223,11 +223,36 @@ await db.ping();
223223
await db.process();
224224
// -> returns the processes/connections list
225225
```
226+
- create_db (database_name)
227+
```js
228+
await db.create_db("second_db");
229+
// -> creates a separate database on the server
230+
231+
// -> you need to create a new connection manually after creating a new database
232+
const secondDB = new MySQL();
233+
let newDb = await secondDB.connect({
234+
host: 'localhost',
235+
port: '3306',
236+
user: 'root',
237+
password: '',
238+
database: 'second_db',
239+
charset: 'utf8mb4',
240+
});
241+
// note: if you had an old events, you need to re-register the events since this is a new class created
242+
newDb.on('connected', async connection => {
243+
console.log('New Database Connected');
244+
});
245+
246+
// now you can manage your "newDb" connection
247+
await newDb.set("second_db_table", "key", "value");
248+
await newDb.drop("second_db_table");
249+
await newDb.end();
250+
```
226251
- end ()
227252
```js
228253
await db.end();
229254
// -> closes the connection
230255
```
231256

232257
#### Contributing
233-
© mysql-database, 2021 - 2022 | <a href="https://itariq.dev" target="_blank">TARIQ</a> <a href="mailto:[email protected]">([email protected])</a>
258+
© mysql-database, 2021 - 2023 | <a href="https://itariq.dev" target="_blank">TARIQ</a> <a href="mailto:[email protected]">([email protected])</a>

examples/example.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const MySQL = require('../index.js');
2-
const database = new MySQL();
1+
const MySQL = require('../index.js'); // importing the package
32

43
run();
54
async function run(){
6-
let db = await database.connect({ // creates a database connection
5+
const database = new MySQL(); // creates a database class
6+
let db = await database.connect({ // creates a connection for the database
77
host: 'localhost',
88
port: '3306', // the default port is 3306
99
user: 'root',
@@ -186,8 +186,7 @@ async function run(){
186186
let ping = await db.ping(); // gets database ping (in ms)
187187
console.log(ping); // 27
188188

189-
// clear all table data
190-
await db.clear("new_table");
189+
await db.clear("new_table"); // clear all table data
191190

192191
// lastly delete the table
193192
await db.drop("new_table"); // drops/deletes the table
@@ -272,5 +271,31 @@ async function run(){
272271
]
273272
*/
274273

274+
await db.create_db("second_db"); // creates a separate database on the server
275+
276+
// you need to create a new connection manually after creating a new database
277+
const secondDB = new MySQL();
278+
let newDb = await secondDB.connect({
279+
host: 'localhost',
280+
port: '3306',
281+
user: 'root',
282+
password: '',
283+
database: 'second_db',
284+
charset: 'utf8mb4',
285+
});
286+
// note: if you had an old events, you need to re-register the events since this is a new class created
287+
newDb.on('connected', async connection => { // database connected event
288+
console.log('New Database Connected');
289+
});
290+
291+
// now you can manage your "newDb" connection
292+
await newDb.set("second_db_table", "key", "value");
293+
await newDb.drop("second_db_table");
294+
295+
// you can still manage your old "db" connection as well
296+
await db.set("old_db_table", "key", "value");
297+
await db.drop("old_db_table");
298+
275299
await db.end(); // closes the connection
300+
await newDb.end();
276301
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mysql-database",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Easily modify your MySQL database data with easy functions",
55
"main": "index.js",
66
"scripts": {

structures/classes/MySQL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ module.exports = class MySQL extends EventEmitter{
3333
base_get = require('../methods/base_get');
3434
end = require('../methods/end');
3535
exists = require('../methods/exists');
36+
create_db = require('../methods/create_db');
3637
}

structures/errors/strings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
"number": "Number was not specified in function arguments or not number, received {received}",
66
"numberType": "Number argument must be an positive integer, received {received}",
77
"query": "SQL Query was not specified in function arguments or not string, received {received}",
8+
"databaseName": "Database name was not specified in function arguments or not string, received {received}",
89

910
"array": "{key} key's value is not an array",
1011
"notNumber": "{key} key's value is not a number",

structures/methods/create_db.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const errors = require('../errors/strings.js');
4+
5+
module.exports = async function(dbName){
6+
if(!dbName || typeof dbName !== "string") throw new TypeError(errors.databaseName.replace("{received}", typeof table));
7+
8+
await this.db.query(`CREATE DATABASE IF NOT EXISTS \`${dbName}\``);
9+
return true;
10+
}

structures/methods/drop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = async function(table, isClear){
77

88
let tables = await this.tables();
99
if(!tables.includes(table)) return false;
10-
this.db.query(`DROP TABLE \`${table}\``);
10+
await this.db.query(`DROP TABLE \`${table}\``);
1111
if(tables.includes(table) && !isClear) this.emit("tableDelete", table);
1212
return true;
1313
}

0 commit comments

Comments
 (0)