1.5.2 • Published 3 years ago

algaeh-mysql v1.5.2

Weekly downloads
9
License
ISC
Repository
-
Last release
3 years ago

algaeh-mysql

This package is developed as per our company need. it may help others. algaeh-mysql is completely promise base api and developer has the facility to release connection where ever it required.

Importing

import algaehMysql from "algaeh-mysql";
Creating instance

const _mysql = new algaehMysql();
Configure MYSQL Connection

By default configuration took from the path root of current working directory. path.join(process.cwd(), "../keys/keys.js") even you can pass the connection configuration to the 'algaehMysql' instance

const _mysql = new algaehMysql({
  path: {
    mysqlDb: {
      host: "localhost",
      port: 3306,
      user: "root",
      password: "****",
      database: "Optional",
      multipleStatements: true
    }
  }
});

Or by passing configuration file path

const _mysql = new algaehMysql({
  path: "file path"
});
Code Syntax

Simple select

_mysql
  .executeQuery({
    query: "select * from table_name where table_id=?",
    values: [10],
    printQuery: true
  })
  .then(result => {
    _mysql.releaseConnection();
    //result ...........
  })
  .catch(error => {
    _mysql.releaseConnection();
    //error .........
  });

the above basic example execute a simple query.

FieldDescription
executeQuerymethod execute without transaction
queryquery string
valuesarray of values
printQueryit print the query on console in development mode
releaseConnectionmethod to release the perticular connection

========

Bulk insert ,replace keys and exclude values and extravalues

_mysql
  .executeQuery({
    values: [
      { algaeh_d_formulas_id: 3, formula_for: "Testing", status: "A" },
      { algaeh_d_formulas_id: 4, formula_for: "Testing1", status: "I" }
    ],
    extraValues: {
      formula: "A+B"
    },
    excludeValues: ["formula_for"],
    replcaeKeys: { status: "record_status" },
    query: "insert into  algaeh_d_formulas (??) values ?",
    printQuery: true,
    bulkInsertOrUpdate: true
  })
  .then(result => {
    _mysql.releaseConnection();
    //result -----------
  })
  .catch(error => {
    _mysql.releaseConnection();
    //error -----------
  });
FieldDescription
extraValuesinsert extra values
replcaeKeysreplace key name with other key name
bulkInsertOrUpdateis true for array of objects

========

Bulk update ,replace keys and exclude values and extravalues

_mysql
  .executeQuery({
    values: [
      {
        algaeh_d_formulas_id: 4,
        formula_for: "Testing1",
        status: "I"
      }
    ],
    extraValues: {
      formula: "A+B"
    },
    excludeValues: ["formula_for"],
    replcaeKeys: { status: "record_status" },
    where: ["algaeh_d_formulas_id"],
    query: "update  algaeh_d_formulas set ? where algaeh_d_formulas_id=?",
    printQuery: true,
    bulkInsertOrUpdate: true
  })
  .then(result => {
    _mysql.releaseConnection();
    // result ---------
  })
  .catch(error => {
    _mysql.releaseConnection();
    //error
  });

========

Execute with transaction

_mysql
  .executeQueryWithTransaction({
    query: "insert into tableName(`a`,`b`) values(?,?)",
    values: ["aaa", "bbb"],
    printQuery: true
  })
  .then(result => {
    const id = result.insertId;
    _mysql
      .executeQuery({
        query: "insert into tableNameX(`id`,'x') values(?,?)",
        values: [id, "xyz"],
        printQuery: true
      })
      .then(finalresult => {
        _mysql.commitTransaction((error, resu) => {
          if (error) {
            _mysql.rollBackTransaction();
            //error-------
          } else {
            //resu =Success;
            _mysql.releaseConnection();
            //finalresult --------------
          }
        });
      })
      .catch(error => {
        _mysql.rollBackTransaction(() => {
          //error --------
        });
        //or _mysql.rollBackTransaction();
      });
  })
  .catch(error => {
    _mysql.rollBackTransaction(() => {
      //error --------
    });
    //or _mysql.rollBackTransaction();
  });

========

to get connection at anypoint of time by calling the funtion

_mysql.getMysqlConnection((error, connection) => {
  if (error) {
    //error---------
  } else {
    //connection
  }
});

to format our as sql string

let query = "";
for (let i = 0; i < 10; i++) {
  query = _mysql.mysqlQueryFormat(
    "insert into tableName(`a`,`i`) values(?,?)",
    ["a" + i, i]
  );
}
1.5.2

3 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

5 years ago

1.2.13

5 years ago

1.2.12

5 years ago

1.2.11

5 years ago

1.2.10

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago