easy-mysql-js
You can do MySQL database operations (select, insert, update, delete, insertOrUpdate) easily with this package.
List of Features
- Select (manual query)
- SelectSingle
- SelectMulti
- Insert
- Update
- InsertOrUpdate
- Delete
Download & Installation
1- Install the package
$ npm i easy-mysql-js
2- Initialize Prisma
$ npx prisma init
or
$ sudo npx prisma init
3- Configure schema.prisma file
After run "npx prisma init" command, there will be "prisma" folder in your project.
Open schema.prisma file in the prisma folder and change "postgresql" as "mysql" (for MySQL) or "sqlserver" (for MSSQL).
4- Configure .env file
### MySQL
DATABASE_URL="mysql://your_db_username:your_db_password@your_ip_address:port/your_db_name"
### MSSQL
# DATABASE_URL="sqlserver://your_ip_address:port;database=your_db_name;user=your_db_username;password=your_db_password;trustServerCertificate=true"
NODE_ENV="development"
5- Generate Database Tables
$ npx prisma db pull
$ npx prisma generate
or
$ sudo npx prisma db pull
$ sudo npx prisma generate
NOTE: If you change your MySQL database tables, columns, etc. you need to run:
$ npx prisma db pull
$ npx prisma generate
or
$ sudo npx prisma db pull
$ sudo npx prisma generate
Environment Variables
To run this project, you will need to add the following environment variables to your .env file
NODE_ENV="development"
or NODE_ENV="production"
for MySQL
DATABASE_URL="mysql://your_db_username:your_db_password@your_ip_address:3306/your_db_name"
for MSSQL
DATABASE_URL="sqlserver://your_ip_address:port;database=your_db_name;user=your_db_username;password=your_db_password;trustServerCertificate=true;"
Example Usage
1- Create "index.js" file
const {Select} = require('easy-mysql-js')
async function test() {
const table_name = 'users'
const query = 'select * from '+table_name+' limit 2'
const select_result = await Select(query)
console.log(select_result)
}
test()
2- Run
$ node index.js
Select
const {Select, SelectSingle, SelectMulti} = require('easy-mysql-js')
async function test() {
const query = 'select * from users where id=1'
const select_result = await Select(query)
console.log(select_result)
const table_name = "users"
const where_condition = {id:1}
const return_columns = {id:true,name:true,email:true}
const selectSingle_result = await SelectSingle(table_name, where_condition, return_columns)
console.log(selectSingle_result)
const where_condition2 = {name:'test'}
const return_columns = {id:true,name:true,email:true}
const selectMulti_result = await SelectMulti(table_name, where_condition, return_columns)
console.log(selectMulti_result)
}
test()
Insert
const {Insert} = require('easy-mysql-js')
async function test() {
const table_name = "users"
const data_to_insert = {name:"name",email:"asd@asd.com",phone:"1234567890"}
const insert_result = await Insert(table_name, data_to_insert)
console.log(insert_result)
}
test()
Update
const {Update} = require('easy-mysql-js')
async function test() {
const table_name = "users"
const where_condition = {email:"asd@asd.com"}
const data_to_set = {email:"qwe@qwe.com"}
const update_result = await Update(table_name, where_condition, data_to_set)
console.log(update_result)
}
test()
InsertOrUpdate
const {InsertOrUpdate} = require('easy-mysql-js')
async function test() {
const table_name = "users"
const where_condition = {id:1}
const data_to_update = {phone:"123123"}
const data_to_insert = {name:"name2",email:"qwe@qwe.com",phone:"1234567891"}
const insertOrUpdate_result = await InsertOrUpdate(table_name, where_condition, data_to_update, data_to_insert)
console.log(insertOrUpdate_result)
}
test()
Delete
const {Delete} = require('easy-mysql-js')
async function test() {
const table_name = "users"
const where_condition = {id:1}
const delete_result = await Delete(table_name, where_condition)
console.log(delete_result)
}
test()
Acknowledgements
License
This project is licensed under the MIT License