1.4.4 • Published 3 years ago

think-mysql v1.4.4

Weekly downloads
342
License
-
Repository
github
Last release
3 years ago

think-mysql

Build Status Coverage Status npm

Install

npm install think-mysql

How to use

default options

You can find all the config options at https://github.com/mysqljs/mysql#connection-options

const defaultConfig = {
  port: 3306,
  host: '127.0.0.1',
  user: '',
  password: '',
  database: '',
  connectionLimit: 1,
  multipleStatements: true,
  logger: console.log.bind(console),
  logConnect: false,
  logSql: false,
  acquireWaitTimeout: 0 // if set timeout, it will be throw an error after get connection timeout
};

Usage

Custom usage

  import mysql from 'think-mysql';
  let instance = mysql.getInstance(config);
  await instance.execute({
    sql:"insert into `think_test`.`books` (`name`, `author`) values ('thinkjs best practice', ?)",
    timeout: 5000,
    values: ['David']
  });  
  let books = await instance.query({
    sql:'SELECT * FROM `books` WHERE `author` = ?',
    timeout: 5000,
    values: ['David']
  });
  console.log(books[0].name)  //thinkjs best practice

Transactions

  import mysql from 'think-mysql';
  let instance = mysql.getInstance(config);
  let result = null;
  try{
    await instance.transaction(async(conn) => {
      result = instance.execute({
        sql: "insert into `think_test`.`books` (`name`, `author`) values ('1st step', ?)",
        values: ['0-David']
      }, conn);
      result = await instance.execute({
        sql: "insert into `think_test`.`books` (`name`, `author`) values ('2nd step', ?)",
        values: [`${result.insertId}-David`]
      }, conn);
      await instance.execute({
        sql: "insert into `think_test`.`books` (`name`, `autor`) values ('3rd step', ?)",
        values: [`${result.insertId}-David`]
      }, conn);
    });
  }catch (e){
    console.log(e);
  }
  result = await instance.query({
      sql:'SELECT * FROM `books` WHERE `author` = ?',
      values:[`${result.insertId}-David`]
  });
  console.log(result[0].name);  //3rd step
1.4.4

3 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago