bb-mysql-helper-ts
v1.1.0
Published
A NodeJS library to simplify the database connector
Downloads
1
Readme
README.md
bb-mysql-helper
A NodeJS library to simplify the database connector
bb-mysql-helper is a Custom NPM Module (CNM), created by BroadenBlue Developers. It is focused to reduce the development code for database connection logic while creating projects from scratch especially. It also provides generic methods for CRUD operations.
Available Methods
- fetch
- fetchAll
- insert
- update
- delete
Don't you want an easy way to communicate with your MySQL Database?
Installation
$ npm install bb-mysql-helper
Introduction
Database credentials are to be provided in the .env file on your server which must includes the specified keys below:
BB_MYSQL_HOST=<hostname>
BB_MYSQL_USER=<username>
BB_MYSQL_PASSWORD=<password>
BB_MYSQL_DATABASE=<db_name>
Building where condition
While executing the methods you need need pass atleast one key it should be either 'or' or 'and'.
const conditions = [
{
or: {
email: {
condition: '=',
value: '[email protected]'
},
mobile: {
condition: 'in',
value: ['8801133454','9182900940']
}
},
and: {
is_deleted: {
condition: '=',
value: '0'
},
}
}
]
Usage
Fetch
Used to fetch the selected columns of specified entity based on condition. Columns should be specified as a key-value pair in an object.
Here is an example on how to use it:
import dbQ from "bb-mysql-helper";
dbQ.fetch({tableName:'users', cols:{email:'emailId',mobile:''},whereCondition: [{or:{},and:{}}]}).then((data)=>{
data.forEach((element: any) => {
console.log(element.emailId,'--',element.mobile);
});
});
FetchAll
Used to fetch the all columns of specified entity based on condition. No need to pass cols parameter in this method and whereCondition is optional and can be null if you want to fetch all the records in the database.
Here is an example on how to use it:
import dbQ from "bb-mysql-helper";
dbQ.fetchAll({tableName:'users', whereCondition: [{or:{},and:{}}]}).then((data)=>{
data.forEach((element: any) => {
console.log(element.emailId,'--',element.mobile);
});
});