mysql-easy-basic-crud is simple crud implemenation with node.js mysql driver.
Install via npm.
$ npm install --save mysql-easy-basic-crud
"use strict";
const DBPool = require('mysql-easy-basic-crud');
const dbPool = new DBPool({
connectionLimit: 20,
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test',
});
dbPool.create();
let pool = null;
dbPool.get()
.then((ins) => {
pool = ins;
return pool.table('Users').get();
})
.then((rows) => {
console.log(rows);
})
.catch((err) => {
console.error(err);
});
- DBPool.close is now returns Promise object.
- get method now supports ordering
Every API function returns Promise object.
Create new DBPool instance. Possible options are check node-mysql. It uses mysql2 module internally.
Create new pool.
Release the pool. Be sure to call this before application ends.
Get pool connection. Returning value is promise. Parameter passed by promise is DBPoolInstance type object which has plenty of utility functions.
Release the connection.
Set the table you want to CRUD. This must be call before using CRUD methods.
Check matched data is exists in table.
Count how many rows are in specified conditions. This method is useful for implementing paginating.
Get matched row(s). Available options are:
- number page: Page number
- number pagePer: Number of how many records in one page.
- New 1.0.2 object order: Set ordering fields. 1 for Descending(DESC), -1 for Ascending(ASC).
Create new record.
Update matched record(s).
Delete matched record(s).
Begin the transaction.
Commit the transaction.
Rollback the transaction.
Execute normal SQL string.
dbPool.get()
.then((conn) => {
return conn.table('Users').get();
})
.then((rows) => {
console.log(rows);
});
dbPool.get()
.then((conn) => {
return conn.table('Users').get({
name: '.modernator'
});
})
.then((rows) => {
console.log(rows);
});
dbPool.get()
.then((conn) => {
return conn.table('Users').get({}, {
order: {
id: 1 // DESC
}
});
})
.then((rows) => {
console.log(rows);
});
dbPool.get()
.then((conn) => {
return conn.table('Users').create({
userid: 'entvy',
age: 25
});
})
.then((result) => {
console.log(result.insertId);
});
dbPool.get()
.then((conn) => {
return conn.table('Users').update({
userid: 'entvy'
}, {
age: 27
});
});
dbPool.get()
.then((conn) => {
return conn.table('Users').delete({
userid: 'entvy'
});
});
- More complicated SQL jobs like join.
- Arithmetic comparison for querying
- Easy date comparison for querying
- Much more options may needed some specific conditions like LIMIT.
- CRUD Tables.
- Express friendly API
MIT.