Skip to content

Repository files navigation


MySQL-database


NPM versionNPM downloads


Table of contents

About

  • Easily modify your MySQL database data with easy functions
  • Useful for websites & large projects where it makes managing data easier & faster
  • Supports the Promise-API, you will be able to use .then, .catch, await, etc...
  • & more...

Installation

npm i mysql-database@latest

Documentation

View Documentation

Events

[Note]: Events are only emitted if the action is taken from the library, which means if you manage the database from other connection, events would not work

  • connected (connection)
  • dataModification (event object)
  • tableCreate (table)
  • tableDelete (table)
  • tableClear (table)
  • tableRename (oldName, newName)
constMySQL=require('mysql-database');constdatabase=newMySQL();// Create Your Own Connectionrun();asyncfunctionrun(){letdb=awaitdatabase.connect({// creates a database connectionhost: 'localhost',port: '3306',// the default is 3306user: 'root',password: '',database: 'my_database',charset: 'utf8mb4'});db.on('connected',asyncconnection=>{// database connected eventconsole.log('Database Connected');});db.on('dataModification',asyncevent=>{// data changes & modifications eventconsole.log(event);/* { oldData: 'bar', newData: 'bar2', type: 'UPDATE', table: 'test_table', modifiedAt: 1653815607288 } */});db.on('tableCreate',asynctable=>{console.log(`Table ${table} Created`);});db.on('tableDelete',asynctable=>{console.log(`Table ${table} Deleted`);});db.on('tableClear',asynctable=>{console.log(`Table ${table} Data Cleared`);});db.on('tableRename',async(oldName,newName)=>{console.log(`Table renamed from ${oldName} to ${newName}`);});}

Methods

  • set (table, key, value)
awaitdb.set('my_table','foo','bar');// -> Stores 'bar' in 'foo' key name in the table 'my_table'
  • get (table, key)
awaitdb.get('my_table','foo');// -> Gets foo key name value (which is bar) in the table 'my_table'
  • exists (table, key)
awaitdb.exists('my_table','foo');// -> Checks if a specific data exists
  • base_set (table, key, value)
awaitdb.base_set('my_table','foo','bar');// -> Stores 'bar' in 'foo' key name in the table 'my_table' but base encrypted
  • base_get (table, key)
awaitdb.base_get('my_table','foo');// -> Gets foo key name value (which is bar) in the table 'my_table' for encrypted rows using base_set method
  • push (table, array, value)
awaitdb.push('my_table','fruits','banana');// -> pushs 'banana' to 'fruits' array in 'my_table' table
  • pull (table, array, value)
awaitdb.pull("my_table","fruits","banana");// -> pulls FIRST 'banana' from 'fruits' in 'my_table'awaitdb.pull("my_table","fruits","banana","all");// -> pulls ALL 'banana' from 'fruits' in 'my_table'
  • includes (table, array, value)
awaitdb.includes("my_table","fruits","banana");// -> Checks if the array includes provided value
  • add (table, key, number)
awaitdb.add("my_table","price",10);// -> add 10 to price in 'my_table' table
  • sub (table, key, number)
awaitdb.sub("my_table","price",5);// -> subtracts 5 from price - the remaining is 5 from price in 'my_table' table
  • all (table)
awaitdb.all("my_table");// -> retutn all the data in 'my_table' table
  • delete (table, key)
awaitdb.delete("my_table","foo");// -> delete foo key in 'my_table' table
  • tables ()
awaitdb.tables();// -> return array of all tables existed in the database
  • rename (table, new_table_name)
awaitdb.rename("my_table","new_name");// -> renames table name
  • stats (table)
awaitdb.stats("my_table");// -> return table info
  • query (query)
awaitdb.query("DROP TABLE my_table;")// -> executes a SQL query
  • auto_increment (table, number)
awaitdb.auto_increment("my_table",5);// -> sets 'my_table' table auto increment to 5
  • create (table)
awaitdb.create("table_name");// -> Create empty table with "table_name" name without inserting any data to it
  • drop (table)
awaitdb.drop("table_name");// -> deletes the table 'table_name'
  • clear (table)
awaitdb.clear("table_name");// -> clears all 'table_name' table rows & data
  • variables (variables_object)
awaitdb.variables({max_connections: 100000,max_connect_errors: 100000,wait_timeout: 60});// -> modifies any global variable
  • ping ()
awaitdb.ping();// -> gets database ping (in ms)
  • process ()
awaitdb.process();// -> returns the processes/connections list
  • create_db (database_name)
awaitdb.create_db("second_db");// -> creates a separate database on the server// -> you need to create a new connection manually after creating a new databaseconstsecondDB=newMySQL();letnewDb=awaitsecondDB.connect({host: 'localhost',port: '3306',user: 'root',password: '',database: 'second_db',charset: 'utf8mb4',});// note: if you had an old events, you need to re-register the events since this is a new class creatednewDb.on('connected',asyncconnection=>{console.log('New Database Connected');});// now you can manage your "newDb" connectionawaitnewDb.set("second_db_table","key","value");awaitnewDb.drop("second_db_table");awaitnewDb.end();
  • end ()
awaitdb.end();// -> closes the connection

Contributing

© mysql-database, 2021 - 2023 | TARIQ(contact@itariq.dev)

About

Easily modify your MySQL database data with easy functions

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages