-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.js
More file actions
107 lines (95 loc) · 4.49 KB
/
Copy pathconnection.js
File metadata and controls
107 lines (95 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const mysql = require("mysql");
const dotenv = require("dotenv")
dotenv.config({path:"./.env"})
const conn = mysql.createConnection({
host:process.env.DATABASE_HOST,
user:process.env.DATABASE_USER,
password:process.env.DATABASE_PASSWORD,
database:process.env.DATABASE
})
conn.connect((err)=>{
if(err) throw err;
// code for creating database
// let query1 = `CREATE DATABASE ${process.env.DATABASE}`;
// conn.query(query1,(error,res)=>{
// if(error){
// console.log("Error while creating database");
// throw error;
// }
// console.log("Database created successfully")
// })
console.log("Successfully Connected To Database.")
// creating user table
let query1 = "CREATE TABLE IF NOT EXISTS user(id INT PRIMARY KEY AUTO_INCREMENT,name TEXT,email TEXT,password TEXT,role TEXT);";
conn.query(query1,(error,res)=> {
if(error){
console.log("Error While Creating user table");
throw error;
}
console.log("USER table created successfully");
})
//creating seller table
query1 = "CREATE TABLE IF NOT EXISTS seller(id INT PRIMARY KEY,phone_number TEXT,address TEXT,FOREIGN KEY(id) REFERENCES user(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error While Creating seller table");
throw error;
}
console.log("seller table created successfully");
})
//creating item table
query1 = "CREATE TABLE IF NOT EXISTS items(id INT PRIMARY KEY AUTO_INCREMENT,product_name TEXT,brand_name TEXT,price INT,discount INT,num_of_items INT,product_color TEXT,category TEXT,prod_description TEXT,seller_id INT,FOREIGN KEY(seller_id) REFERENCES seller(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error While Creating items table");
throw error;
}
console.log("items table created successfully");
})
//creating attachemnt table
query1 = "CREATE TABLE IF NOT EXISTS attachment(id INT PRIMARY KEY AUTO_INCREMENT,item_id INT,imgPath TEXT,FOREIGN KEY(item_id) REFERENCES items(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error While Creating attachment table");
throw error;
}
console.log("attachment table created successfully");
})
// creating cart table
query1 = "CREATE TABLE IF NOT EXISTS cart(user_id INT ,item_id INT,quantity INT,PRIMARY KEY(user_id,item_id),FOREIGN KEY(item_id) REFERENCES items(id) ON DELETE CASCADE,FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error While Creating Cart table");
throw error;
}
console.log("cart table created successfully");
})
// creating wishList table
query1 = "CREATE TABLE IF NOT EXISTS wishList(user_id INT,item_id INT,PRIMARY KEY(user_id,item_id),FOREIGN KEY(item_id) REFERENCES items(id) ON DELETE CASCADE,FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error While Creating wishList table");
throw error;
}
console.log("wishList table created successfully");
})
// creating orders table
query1 = "CREATE TABLE IF NOT EXISTS orders(order_num INT PRIMARY KEY AUTO_INCREMENT, order_date DATETIME DEFAULT now(),seller_id INT , user_id INT, order_amt INT, address VARCHAR(500) NOT NULL, dispatch BOOLEAN DEFAULT FALSE, FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE,FOREIGN KEY(seller_id) REFERENCES user(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error while creating orders table");
throw error;
}
console.log("orders table created successfully");
})
// creating order-item table
query1 = "CREATE TABLE IF NOT EXISTS orderitem(order_num INT, item_id INT, quantity INT, price INT, discount INT, PRIMARY KEY(order_num,item_id), FOREIGN KEY(order_num) REFERENCES orders(order_num) ON DELETE CASCADE, FOREIGN KEY(item_id) REFERENCES items(id) ON DELETE CASCADE);";
conn.query(query1,(error,res)=>{
if(error){
console.log("Error while creating orderitem table");
throw error;
}
console.log("orderitem table created successfully");
})
})
module.exports = conn;