Coder Social home page Coder Social logo

think-mysql's Introduction

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

think-mysql's People

Contributors

lizheming avatar lushijie avatar meltifa avatar welefen avatar zhengqingxin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.