sa-mysql
Soft-Artel Tools: MySQL Wrapper on async/await
Last updated 11 days ago by soft-artel .
ISC · Repository · Original npm · Tarball
$ cnpm install sa-mysql 
SYNC missed versions from official npm registry.

Soft-Artel Tools: MySQL Wrapper on async/await

Create Connection: let db = await SAMysql.connect( config );

Close Connection: await db.end();

Methods to make queries to DB:

db.dbStructure();

let res = await db.dbStructure( database = this.database )

Get DataBase Structure in json style;

db.select();

let res = await db.select( {object} )

Make select to DB:

{object} Can contain keys with use to create query:

  • sql : "", // Ready SQL
  • params : [] || {}, // Mysql.format ? params inject to SQL

// Or build SQL by params [some of them can be undefined]:

  • table : "TableName", // Require param
  • fields : "*", // Option, Fields in result
  • where : "" || {}, // Where STRING or OBJ { field: value }
  • order : "" , // ORDER BY String
  • limit : 0 , // LIMIT, if == 1, result = {<row>}

// Also you can group results after select by value of field:

  • groupResultBy : "" , // Field with value uses to group result
  • groupResultArrays : "" , // If true, result = { ${groupResultBy}: [<rows>] }

Examples:

let users = await db.select( { sql: 'SELECT * FROM users;', groupResultBy:'ID' } );

Get all users in object { ID1: User1, ID2: User2, ... }

The same as:

let users = await db.select( { table: 'users', groupResultBy:'ID' } );

Get all users in object { ID1: User1, ID2: User2, ... }

if Limit set to 1 - result by Object = first row

let user = await db.select( { table: 'users', where:{ ID: 1 }, limit: 1 } );

user = User1

db.replace();

let res = await db.replace( table, data )

Make REPLACE query to DB:

  • table : "TableName", // Require param
  • data : {}, // Object data (keys mast by the same as fields in DB)

db.update();

let res = await db.update( table, data, where = null )

Make UPDATE query to DB:

  • table : "TableName", // Require param
  • data : {}, // Object data (keys mast by the same as fields in DB)
  • where : {}, // Where OBJ { field: value }

db.insert();

let res = await db.insert( table, data )

Make INSERT query to DB:

  • table : "TableName", // Require param
  • data : {}, // Object data (keys mast by the same as fields in DB)

db.delete();

let res = await db.delete( table, where, forceDelete = false)

Make DELETE query to DB:

  • table : "TableName", // Require param
  • where : {}, // Where OBJ { field: value }
  • forceDelete : {}, // if true where can be null, else - where require param
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 174
Dependencies (2)
Dev Dependencies (0)
None

Copyright 2014 - 2017 © taobao.org |