1- const  MySQL  =  require ( '../index.js' ) ; 
2- const  database  =  new  MySQL ( ) ; 
1+ const  MySQL  =  require ( '../index.js' ) ;  // importing the package 
32
43run ( ) ; 
54async  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} 
0 commit comments