3.0.3 • Published 1 year ago

object_mysql v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

🚀 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 | Port connection (default:3306)| | 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

NameMethodDefinitionReturn data
dbdb.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

import Dotenv from 'dotenv'
import ObjectDB from 'object_mysql'

Dotenv.config()

const exec = async () => {

    // Retrieve object from database
    const { ...NameToTable, db } = await ObjectDB() 

    // ==========================================
    // "db" is an instance of "new Database()"
    // ==========================================

    // Directly run a query
    const sql = 'SELECT * FROM nametotable WHERE id = :attribute_name'
    const { error, result } = await db.query(sql, {attribute_name:1})
}

exec()

📚 Method of using the objects

MethodParametersDefinitionReturn data
addparams:objectAdd data to the table{ error, result }
getparams:objectRecover data{ error, result }
updateid:number-string, params:objectUpdate data{ error, result }
removeid:number-stringDelete data{ error, result }
getByPkid:number-string, pk: any = 'id'Recover data based on its primary key{ error, result }
getByAttrnameAttr:string, attr: string-number-null, expresion:string ("=")Retrieve data according to its attributes{ error, result }
countrow:string, params:objectRetrieve register totalint
getTotal---Retrieve register totalint
isExistparams:objectCheck if record exists based on attributesboolean

📚 Listening events

Now you can listen to the events that occur in the module |Method | Definition |Return data | |--- |--- |--- | | monitor | Recover insert, update, delete, select processes | object| | error | Recover errors | object |

Example

import Dotenv from 'dotenv'
import ObjectDB from 'object_mysql'

Dotenv.config()

const exec = async () => {

    // Retrieve object from database
    const { ...NameToTable, on } = await ObjectDB() 

    on('monitor', data => {
        console.log(data)
    })

    // Add data to the table
    await NameToTable.add({name:"Testing data"})

    // Recover data
    await NameToTable.get({id})
}

exec()

/*
console.log -> Add data to the table
{
    startTime: 2955.893799999729,
    endTime: 2958.500699999742,
    executionTime:  0.0026069000000134112,
    executionTimeFormat: '0.003s',
    model: 'NameToTable',
    type: 'insert',
    query: 'INSERT INTO `name_to_table` (`name`) VALUES (:name)',
    params: { name: 'Testing data' },
    result:  OkPacket 
    {
        fieldCount: 0,
        affectedRows: 1,
        insertId: 1,
        serverStatus: 2,
        warningCount: 0,
        message: '',
        protocol41: true,
        changedRows: 0
    }
}

console.log -> Recover data
{
    startTime: 2977.9382999995723,
    endTime:  2975.734899999574 ,
    executionTime: 0.001358299999497831,
    executionTimeFormat: '0.001s',
    model: 'NameToTable',
    type: 'insert',
    query: 'SELECT * FROM `name_to_table` WHERE hotel.name = :name',
    params: { name: 'Testing data' },
    result: [
        RowDataPacket {
            id: 1,
            name: 'Testing data',
            created: '2022-10-31 15:42:33',
            updated: '2022-10-31 15:42:33'
        }
    ]
}
*/

Example: directly attack the table object

import Dotenv from 'dotenv'
import ObjectDB from 'object_mysql'

Dotenv.config()

const exec = async () => {

    // Retrieve object from database
    const { ...NameToTable } = await ObjectDB() 

    // Add data to the table
    const { error, result } = await NameToTable.add({name:"Testing data"})

    // Muliple add data to the table
    const { error, result } = await NameToTable.add([{name:"Testing data"},{name:"Other data"},{name:"more data"}])

    if(error) return false 

    //Unique identifier of the inserted data
    const { insertId:id } = result

    // Update data
    const { error, result } = await NameToTable.update(id,{name:"Testing data 2"})

    if(error) return false

    // Recover data
    const { error, result } = await NameToTable.get({id})

    if(error) return false

    // Delete data
    const { error, result } = await NameToTable.remove(id)
}

exec()

String Function

checkMethodDescription
asciifunction returns the ASCII value for the specific character
char_lengthfunction return the length of a string (in characters)
lengthfunction returns the length of a string (in bytes)
lowerfunction converts a string to lower-case
trimfunction removes leading and trailing spaces from a string
ltrimfunction removes leading spaces from a string
rtrimfunction removes trailing spaces from a string
reversefunction reverses a string and returns the result
upperfunction converts a string to upper-case

Numeric Function

checkMethodDescription
countfunction returns the number of records returned by a select query
sumfunction calculates the sum of a set of values
avgfunction returns the average value of an expression
maxfunction returns the maximum value in a set of values
minfunction 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

alt text

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

1.1.9

2 years ago

1.7.10

2 years ago

1.7.11

2 years ago

1.5.10

2 years ago

1.7.12

2 years ago

1.3.10

2 years ago

1.9.19

2 years ago

1.9.18

2 years ago

1.9.17

2 years ago

1.9.16

2 years ago

1.9.15

2 years ago

1.9.14

2 years ago

1.9.13

2 years ago

1.9.12

2 years ago

1.8.12

2 years ago

1.6.10

2 years ago

1.4.10

2 years ago

1.2.9

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago