boiler-mysql
v1.0.2
Published
This a simple package to help developers to execute CRUD operation using NodeJs for SQL databases.
Downloads
7
Maintainers
Readme
This is a simple Node.js package for performing SQL CRUD operations, supported by Codebaz.
Getting started
First, install the package running the following command:
npm install boiler-mysql
Then, go to .env file or create it if you don't have it and set this variables:
DB_HOST='localhost'
DB_PORT=3306
DB_DATABASE='<database_name>'
DB_USER='<database_user>' # usually its root
DB_PASSWORD='<database_password>'
This package has the following functions: Create, Select, Update and Delete.
First, you have to import the function:
import { Create, Select, Update, Delete } from 'sql-crud'
1. Create
You can use the Create function to insert data into some table on database.
Basic usage To create new record you just need two parameters: the tableName whitch must have the name of your table name and the values whitch is a object that contains the data to store.
Example: Lets insert random data to users table
const newData = async () => {
const insertData = await Create({
tableName: 'users',
values: { firstName: 'john', lastName: 'Doe', email: '[email protected]' }
});
return insertData; # it will return an object
}
The response object have the following structure:
{
status: 200 || 404 || 500 ||,
message: 'message according to the status of your request',
data: [] || null # if the status is 200 it will return the data inserted
}
2. Select
The basic usage of Select function is to display data from database
Example 1:
const selectData = async () => {
const promise = await Select({
tableName: 'users'
})
return promise;
}
The Select function have follwing parameters:
| Parameter | Description | |-----------------------|----------------------------------| | tableName | The name of the table | | columns | specify the columns you want to select. It comes into array. Example: ['firstName', 'email'] | | where | specify where to select. example: [ email: '[email protected]' ] | | include | include related tables | | join | it will join tables. | | take | specify the number of rows to retrieve. By default is 12 rows | | orderBy | specify the order. It must be an array whitch the key is the column and the value of that key is the type of order. Example: [ createdAt: 'desc' ] |
Example 2:
const selectData = asycn () => {
const data = await Select({
tableName: 'product',
where: [userId: '312'],
include: ['users'],
join: ['category'],
take: 5,
orderBy: [id: 'desc']
});
return data;
}
You can check out the sql-crud GitHub repository - your feedback and contributions are welcome!