With this module synchronize the Mysql database in a more dynamic way to implement in your project created with NodeJs
๐ Installation
npm i object_mysql
Usage
Initial setup
The way to use this module is by declaring the following attributes in the environment variables.
Variable
Definition
DB_HOST
IPv4 addresses and host names (default:'localhost')
DB_USER
User name (default:'root')
DB_PASS
Database password (default:'')
DB_TABLE
Schema name
DB_PORT
Port connection
DB_MULTIPLE_STATEMENT
Multiple statement (default:true)
DB_CONNECTION_LIMIT
Connection limit (default:5000)
DB_CONNECTION_TIMEOUT
Connection timeout (default:30000)
DB_CHARSET
Charset (default:'utf8mb4')
Module Additions Method
Name
Method
Definition
Return data
db
db.query (sql:string,params:object)
Make an inquiry directly
{ error, result }
You can pass parameters to the query so that the data to the query is parsed
Example: direct query
importDotenvfrom'dotenv'importObjectDBfrom'object_mysql'Dotenv.config()constexec=async()=>{// Retrieve object from databaseconst{ ...NameToTable, db }=awaitObjectDB()// ==========================================// "db" is an instance of "new Database()"// ==========================================// Directly run a queryconstsql='SELECT * FROM nametotable WHERE id = :attribute_name'const{ error, result }=awaitdb.query(sql,{attribute_name:1})}exec()
importDotenvfrom'dotenv'importObjectDBfrom'object_mysql'Dotenv.config()constexec=async()=>{// Retrieve object from databaseconst{ ...NameToTable}=awaitObjectDB()// Add data to the tableconst{ error, result }=awaitNameToTable.add({name:"Testing data"})// Muliple add data to the tableconst{ error, result }=awaitNameToTable.add([{name:"Testing data"},{name:"Other data"},{name:"more data"}])if(error)returnfalse//Unique identifier of the inserted dataconst{insertId:id}=result// Update dataconst{ error, result }=awaitNameToTable.update(id,{name:"Testing data 2"})if(error)returnfalse// Recover dataconst{ error, result }=awaitNameToTable.get({id})if(error)returnfalse// Delete dataconst{ error, result }=awaitNameToTable.remove(id)}exec()
String Function
check
Method
Description
โ
ascii
function returns the ASCII value for the specific character
โ
char_length
function return the length of a string (in characters)
โ
length
function returns the length of a string (in bytes)
โ
lower
function converts a string to lower-case
โ
trim
function removes leading and trailing spaces from a string
โ
ltrim
function removes leading spaces from a string
โ
rtrim
function removes trailing spaces from a string
โ
reverse
function reverses a string and returns the result
โ
upper
function converts a string to upper-case
Numeric Function
check
Method
Description
โ
count
function returns the number of records returned by a select query
โ
sum
function calculates the sum of a set of values
โ
avg
function returns the average value of an expression
โ
max
function returns the maximum value in a set of values
โ
min
function returns the minimum value in a set of values
๐๏ธ Roadmap
String Functions
Numeric Functions
Date Functions
Join tables
Last test
Tests have been carried out to check the operation of the connections, create, read, update, remote and also additional functions