satyaminfo_node_js_crud_mysql
v1.1.0
Published
CRUD API With Validation In Node JS
Downloads
12
Maintainers
Readme
satyaminfo CRUD in node.js useing mysql!
This package is use to create [CRUD API Operation
] in node js with [API Validation
], this package is easy to use and operate also you can modify table or fields as per your requirement using the following step,
visit my website for my info satyaminfo.in
Install
# with npm
npm install satyaminfo_node_js_crud_mysql
# with npm
npm install express mysql dotenv sequelize body-parser joi joi-objectid
Usage
Create a .env
file in the root directory of your project. Add
environment-specific variables on new lines in the form of NAME=VALUE
.
For example:
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=s!mpl3
DB_NAME=demo_db
Create a config/config.js
file in the root directory of your project.
Your config.js
file like this
const mysql=require('mysql');
const dotenv=require('dotenv').config();
var con={
host: process.env.DB_HOST,
user:process.env.DB_USER,
password:process.env.DB_PASSWORD,
database: process.env.DB_NAME,
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
var db_con=mysql.createConnection({
host: process.env.DB_HOST,
user:process.env.DB_USER,
password:process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
module.exports.con=con;
module.exports.db_con=db_con;
Create a crud_request/guest.js
file in the root directory of your project.
Your guest.js
file is like this
const {mysqlCrudNode}=require('satyaminfo_node_js_crud_mysql');
const {db_con} = require('../config/config');
mysqlCrudNode({
table_name:'guest',
table_fields:{
name:'STRING',
email:'STRING',
password:'TEXT',
phone:'INTEGER'
},
valid_fields:{
name:"required|min:2|max:10|alphanum",
email:"email|required",
password:"required",
phone:"required"
},con:db_con
});
$ node crud_request/guest.js
Create a server.js
file in the root directory of your project. for Run API Use On postman
Your server.js
file is like this
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const userRouter = require('./routes/api');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use('/api', userRouter);
const port=8081
app.listen(port, console.log(`server is listen on port ${port}`));
Options
In this package 3 Options are there
- table_name here is your table name
- table_fields here is your table fields name list
- valid_fields here is your table fields multiple vaidation list