Skip to content

Commit a8b7204

Browse files
committed
My Node js express server rest api sequelize orm mysql project
0 parents  commit a8b7204

17 files changed

+2313
-0
lines changed

app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const express = require("express");
2+
const cors = require("cors");
3+
4+
const app = express();
5+
6+
const corsOptions = {
7+
origin: "http://localhost:8090"
8+
}
9+
app.use(cors(corsOptions));
10+
11+
//parse request of content-type - - application/json
12+
app.use(express.json());
13+
14+
//parse request of content-type - - application/ww-form-url-encoded
15+
app.use(express.urlencoded({ extended: true }));
16+
17+
//default app port calling response
18+
app.get("/", (req, res) => {
19+
res.json({ message: "Welcome To our Node JS Express Js using Rest API with Sequelze ORM and MySQL DB Connection" });
20+
});
21+
22+
23+
const PORT = process.env.PORT || 8090;
24+
app.listen(PORT, () => {
25+
console.log("\nYou NodeJS Express Server App is running on the PORT " + PORT + "\n");
26+
});
27+
28+
// set port, listen for requests
29+
require("./app/routes/routes")(app);
30+
//build/establish db conection by calling index.js file
31+
var db = require("./app/models");
32+
33+
//let the nodejs create tables according to the models.
34+
//comment this if you do not want to make tables empty on reRunning the APP in CMD.
35+
//This code on runnig the app will create tabees from models and delete all data if exist.
36+
//if you comment this line, you have to create tables in database.
37+
// db.sequelize.sync({ force: true }).then(() => {
38+
// console.log("Db synced");
39+
// }).catch(() => {
40+
// console.log("Fail to synce DB");
41+
// });
42+

app/config/db.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
HOST: "localhost",
3+
USER: "your_DB_user_name",
4+
DB: "testdb",
5+
PASSWORD: "Your _DB _PASSWORD",
6+
dialect: "mysql",
7+
pool: {
8+
min: 0,
9+
max: 5,
10+
acquire: 30000,
11+
idle: 10000
12+
}
13+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const db = require("../models");
2+
const Editors = db.editors;
3+
const Operator = db.Sequelize.Op;
4+
5+
//create and save new editor
6+
7+
exports.create = (req, res) => {
8+
if (!req.body.fname || !req.body.lname) {
9+
res.status(400).send({
10+
message: "Name Should not be null"
11+
});
12+
return;
13+
}
14+
const Editor = {
15+
fname: req.body.fname,
16+
lname: req.body.lname,
17+
contactNo: req.body.contactNo,
18+
address: req.body.address,
19+
status: req.body.status
20+
};
21+
Editors.create(Editor).then((data) => {
22+
console.log("Editors data");
23+
return res.send(data);
24+
})
25+
.catch((err) => {
26+
console.log("Editors err");
27+
return res.status(500).send({
28+
message: err.message || "Am error occured while creating Editor"
29+
});
30+
});
31+
32+
};
33+
34+
const findAll = (req, res) => {
35+
var fname = req.query.fname;
36+
var whereVlause = fname ? { [Operator.like]: `${fname}` } : null;
37+
Editors.findAll({ where: whereVlause }).then((data) => {
38+
return res.send(data)
39+
}).catch((error) => {
40+
return res.status(500).send({
41+
message: error.message || "An error occurred while retreiving editrs. Please try later"
42+
});
43+
});
44+
45+
};
46+
module.exports.findAll=findAll;

app/controllers/product.controller.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const db = require("../models");
2+
const Products = db.products;
3+
4+
const Operator = db.Sequelize.Op;
5+
6+
const create = (req, res) => {
7+
8+
if (!req.body.name) {
9+
let errObj = {
10+
message: "Product name Shoul not be empty"
11+
};
12+
res.status(400).send(errObj);
13+
return;
14+
}
15+
let product = {
16+
name: req.body.name,
17+
company: req.body.company,
18+
category: req.body.category
19+
};
20+
Products.create(product).then(data => {
21+
return res.send(data);
22+
}).catch(err => {
23+
return res.status(500).send({
24+
message: err.message || "unable to get all custoomers"
25+
});
26+
});
27+
28+
};
29+
module.exports.create = create;
30+
31+
const findAll = (req, res) => {
32+
var name = "";
33+
if (req.query.name) {
34+
name = req.query.name;
35+
}
36+
var whereClause = name ? { [Operator.like]: `${name}` } : null;
37+
var whereCondition = { where: whereClause };
38+
Products.findAll(whereCondition).then(data => {
39+
return res.send(data)
40+
}).catch(error => {
41+
return res.status(500).send({
42+
message: error.message || "An error occureed while getting the products"
43+
});
44+
});
45+
};
46+
module.exports.findAll = findAll;

app/models/customer.model.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports=(sequelize,Sequelize)=>{
2+
const Customer=sequelize.define("Customer",{
3+
fname:{
4+
type:Sequelize.STRING
5+
},
6+
lname:{
7+
type:Sequelize.STRING
8+
},
9+
address:{
10+
type:Sequelize.STRING
11+
},
12+
contactNo:{
13+
type:Sequelize.STRING
14+
},
15+
status:{
16+
type:Sequelize.STRING
17+
}
18+
});
19+
20+
return Customer;
21+
};

app/models/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const dbConfig = require("../config/db.config");
2+
const Sequelize = require("sequelize");
3+
4+
const sequelize = new Sequelize(
5+
dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
6+
HOST: dbConfig.HOST,
7+
dialect: dbConfig.dialect,
8+
operatorAlliases: true,
9+
10+
pool: {
11+
min:dbConfig.min,
12+
max:dbConfig.max,
13+
acquire:dbConfig.acquirem,
14+
idle:dbConfig.idle
15+
}
16+
}
17+
);
18+
19+
const db={};
20+
21+
db.sequelize=sequelize;
22+
db.Sequelize=Sequelize;
23+
24+
db.editors=require("../models/customer.model.js")(sequelize,Sequelize);
25+
db.products=require("../models/product.model.js")(sequelize,Sequelize);
26+
27+
module.exports=db;
28+
29+

app/models/product.model.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Product = sequelize.define("Product", {
3+
name: {
4+
type: Sequelize.STRING
5+
},
6+
category: {
7+
type: Sequelize.STRING
8+
},
9+
company: {
10+
type: Sequelize.STRING
11+
}
12+
});
13+
return Product;
14+
};

app/routes/routes.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports=app =>{
2+
const CustomerControll=require("../controllers/customer.controller.js");
3+
const ProductControll=require("../controllers/product.controller.js");
4+
const router=require("express").Router();
5+
6+
//customers
7+
router.post("/save/customer",CustomerControll.create);
8+
router.get("/get/customer",CustomerControll.findAll);
9+
10+
//Products
11+
router.post("/save/product",ProductControll.create);
12+
router.get("/get/products",ProductControll.findAll);
13+
14+
15+
app.use("/v1",router)
16+
};

appjs(serverjs)-coding-steps.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=> Import express server
2+
3+
=> import cors
4+
5+
=> create app using imported Import express constrcutor
6+
7+
=> declare cors origin use app cors origin
8+
9+
=> use app res json to parse requests of content-type - application/json
10+
11+
=> use app url-encoded to parse requests of content-type - application/x-www-form-urlencoded
12+
13+
=> build/establish db conection by calling index.js file
14+
15+
=> define app default path i.e app.get
16+
17+
=> Import the routes file path
18+
19+
=> call listening to port function
20+

controller-coding-steps.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=> create and initialize DB
2+
3+
4+
=> create and initialize Model
5+
6+
=> import other necessary stuffs like Operators which are returned by db.Sequelize.op;
7+
8+
=> create and initialize fuctions
9+

db-config-coding-steps.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export module with information of database like this
2+
3+
module.export= {
4+
5+
db_name,
6+
psd,
7+
user,
8+
localhost,
9+
dialect,
10+
pool: {
11+
max:
12+
min:
13+
acquire:
14+
idle:
15+
}
16+
};

indexjs-coding-steps.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=> import db config
2+
3+
=> import Sequelize
4+
5+
=> create sequelize connection using Sequelize constructor and define body
6+
7+
=> after body, initailze empty db
8+
9+
=> Assign db.Sequelize value
10+
11+
=> Assign db.seuqelize value
12+
13+
=> Assign db.models(models is collection of tables) value
14+
15+
=> export db varaible
16+

model-coding-steps.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=> module export type function with parametrs sequelize and Sequelize
2+
3+
=> Inside module export type function, declare Model and directlt initialze body by passing the table name.
4+
5+
=> inside body write tables columns.
6+
7+
=> After the body of model return the model.

0 commit comments

Comments
 (0)