data-promise
v1.0.8
Published
Data access layer for MySQL using promises
Downloads
12
Readme
data-promise
data-promise is CRUD layer for MySQL. It uses promises to return the results or errors.
Installation
npm
npm install data-promise
Usage
Let's say you have a table users
on your MySQL database with a userId
autogenerated key. Import the module, create a data object for the table you want to have the CRUD layer, and call any of the available methods on the data object.
// Import the module
var data = require('data-promise')
// MySQL connection details
var mysql = {
host: 'mysqlhost.example.com',
user: 'database_user',
password: 'database_password',
database: 'database_name',
port: 3306
}
// Create a data object for the specific table
var userData = new data(mysql, 'users', 'userId')
// Let's create a new record on the users table!
// Create the object that we will insert in the database (the table contains a field "name")
var user = {
name: 'Lucía'
}
// Just call the method .create on the data object
userData.create(user).then((userId) => {
// user created
console.log(userId)
})
.catch((error) => {
// error creating user
console.log(error)
})
// Let's list all the records on the table (CRUDL?? :D)
userData.listAll().then((results) => {
console.log(results)
})
.catch((error) => {
// error retrieving the list
console.log(error)
})
// Let's read one record on the list, filtering by Id (assume the userId is 1077)
userData.read(1077).then((results) => {
console.log(results)
})
.catch((error) => {
// error retrieving one record
console.log(error)
})
Available methods
- create
- createMultiple (pass an array of elements to create all at once)
- read
- update
- delete
- exists (returns
true
if there is a row with the specified Id) - listAll
- runSql (runs an arbitrary SQL query and returns the results)