-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.js
More file actions
196 lines (193 loc) · 5.02 KB
/
sql.js
File metadata and controls
196 lines (193 loc) · 5.02 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const { query } = require('express');
const {Pool}=require('pg')
// const pool=new Pool(
// {
// host:'127.0.0.1',
// user:'postgres',
// database:'Database_Manager',
// password:'elaya55555',
// port:5432
// }
// )
const pool=new Pool(
{
connectionString:process.env.DB_URL
// connectionString:'postgresql://postgres:5LE9WXSNEElkJLkyNonT@containers-us-west-37.railway.app:7969/railway'
}
)
const signup=async(name,email,pass)=>{
const client=await pool.connect();
console.log("CONNECTED")
try {
const result=await client.query('INSERT INTO db_users(name,email,password) VALUES($1,$2,$3) RETURNING id',[name,email,pass])
return {
error:false,
userId:result.rows[0].id
}
} catch (error) {
return {
error:true,
message:error.message
}
}
finally{
await client.release();
console.log("DISCONNECTED");
}
}
const login=async(email,pass)=>{
const client=await pool.connect();
console.log("CONNECTED")
try {
const result=await client.query('SELECT * FROM db_users WHERE email=$1 AND password=$2',[email,pass])
return {
error:false,
userId:result.rows[0].id
}
} catch (error) {
return {
error:true,
message:error.message
}
}
finally{
await client.release();
console.log("DISCONNECTED")
}
}
const createTable=async(creator,name,cols)=>{
const client=await pool.connect();
try {
let userText=`user_id INT REFERENCES db_users(id) DEFAULT ${creator}`
let colsText=''
cols.map((s,index)=>{
if(index!=cols.length-1)
colsText+= `${s} VARCHAR(100),`
else
colsText+=`${s} VARCHAR(256)`
})
let text=`CREATE TABLE ${name}(${userText},${colsText})`
console.log(text)
const result=await client.query(text)
const updateTableQuery=await client.query('UPDATE db_users SET created_tables=ARRAY_APPEND(created_tables,$1) WHERE id=$2',[name,creator])
return {
error:false,
message:`${name} table created`
}
} catch (error) {
console.log("Error in creating table");
console.log(error);
return {
error:true,
message:error.message
}
}finally{
}
}
const dropTable=async(userId,name)=>{
console.log("id-->",userId,"name-->",name)
const client=await pool.connect();
console.log("CONNECTED")
try {
let text=`DROP TABLE ${name}`
await client.query(text)
await client.query('UPDATE db_users SET created_tables=ARRAY_REMOVE(created_tables,$1) WHERE id=$2',[name,userId])
return {
error:false
}
} catch (error) {
return {
error:true,
message:error.message
}
}
finally{
await client.release();
console.log("DISCONNECTED")
}
}
const getUserData=async(id)=>{
const client=await pool.connect();
console.log("CONNECTED")
try {
const result=await client.query('SELECT * FROM db_users WHERE id=$1',[id])
return {
error:false,
data:result.rows[0]
}
} catch (error) {
console.log('QUERY ER IN USER DATA')
return {
error:true,
message:error.message
}
}
finally{
await client.release()
console.log("DISCONNECTED")
}
}
const getTableData=async(name)=>{
const client=await pool.connect();
console.log("[+]CONNECTED");
try {
let text=`SELECT * FROM ${name}`
const result=await client.query(text);
return {
error:false,
data:result
}
} catch (error) {
console.log("err in getting tbale")
return {
error:true,
message:error.message
}
}
finally{
await client.release();
console.log("DISCONNECTED");
}
}
const addRow=async(text)=>{
const client=await pool.connect()
console.log("CONNECTED");
console.log("qq-->",text)
try {
await client.query(text)
return {
error:false
}
} catch (error) {
console.log("ERROR in row insertion")
console.log(error.message)
return {
error:true,
message:error.message
}
}
finally{
await client.release();
console.log("DISCONNECTED");
}
}
const deleteRow=async(text)=>{
const client=await pool.connect();
console.log("CONNECTED")
try {
await client.query(text)
return {
error:false
}
} catch (error) {
return {
error:true,
message:error.message
}
}
finally{
client.release()
console.log("DISCONNECTED")
}
}
module.exports={signup,login,createTable,dropTable,getUserData,getTableData,addRow,deleteRow}