mysql-easy-query
TypeScript icon, indicating that this package has built-in type declarations

3.16.0 • Public • Published

MySQL Easy Query

install

npm i mysql-easy-query

Example

Query

const mysql = require('mysql');
const { Query } = require('mysql-easy-query');

const conn = mysql.createConnection({
  host: '127.0.0.1',
  user: 'root',
});

const q = new Query(conn);

// sql query
await q.query('SELECT * FROM user WHERE id = ?', [123]);

// builder callback query
await q.query(b => b.select().from('user').where({ id: 123 }));

// builder query
const builder = q.builder().select().from('user').where({ id: 123 });
await q.query(builder);

// count
await q.count('user', { age: 20 });

// auto transaction
await q.transaction(async () => {
  await q.query(b => b.update('user', { age: 100 }).where({ id: 1 }));
  await q.query(b => b.update('user', { age: 100 }).where({ id: 2 }));
  await q.query(b => b.update('user', { age: 100 }).where({ id: 3 }));
});

PoolQuery

const { createPoolCompatible } = require('mysql-easy-query');

const pool = createPoolCompatible({
  pools: {
    MASTER: {
      host: '10.0.0.1',
      user: 'root',
    },
    SLAVE_1: {
      host: '10.0.0.2',
      user: 'root',
    }
  }
});

// raw query
// auto get connect and auto release
await pool.query('SELECT 1+1');

// auto transaction
// auto get connect and auto release
await pool.of('MASTER').transaction(async query => {
  await query.query(b => b.update('user', { age: 100 }).where({ id: 1 }));
});

Related projects: sql-easy-builder ZenORM

Readme

Keywords

none

Package Sidebar

Install

npm i mysql-easy-query

Weekly Downloads

52

Version

3.16.0

License

MIT

Unpacked Size

24.7 kB

Total Files

17

Last publish

Collaborators

  • yefei777