1.0.2 • Published 1 year ago

boiler-mysql v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

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: 'johndoe@example.com' }
    });

    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:

ParameterDescription
tableNameThe name of the table
columnsspecify the columns you want to select. It comes into array. Example: 'firstName', 'email'
wherespecify where to select. example: email: 'johndoe@example.com'
includeinclude related tables
joinit will join tables.
takespecify the number of rows to retrieve. By default is 12 rows
orderByspecify 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!

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago