-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorm.js
75 lines (67 loc) · 1.91 KB
/
orm.js
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
const mysql = require( 'mysql' );
class Database {
constructor( config ) {
this.connection = mysql.createConnection( config );
}
query( sql, args=[] ) {
return new Promise( ( resolve, reject ) => {
this.connection.query( sql, args, ( err, rows ) => {
if ( err )
return reject( err );
resolve( rows );
} );
} );
}
close() {
return new Promise( ( resolve, reject ) => {
this.connection.end( err => {
if ( err )
return reject( err );
resolve();
} );
} );
}
}
// at top INIT DB connection
// const db = new Database({
// host: "localhost",
// port: 3306,
// user: "root",
// password: "root1234",
// database: "burger"
// });
var db;
if(process.env.JAWSDB_URL){
db = new Database(process.env.JAWSDB_URL);
console.log('jaws db is connected');
}else{
db = new Database({
host: "localhost",
port: 3306,
user: "root",
password: "root1234", //change
database: "burger"
});
};
async function saveResultFn(myPost){
const dbResultPost = await db.query("INSERT INTO EATBURGER(name) values(?)",[myPost.name]);
// const dbResultGet = await db.query("SELECT EATBURGER.name FROM EATBURGER");
return dbResultPost;
}
async function burgerDbFunc(id){
// console.log(burgername.name);
const myListDb = await db.query("SELECT * FROM EATBURGER WHERE id = ?", [id]);
// console.log(`[loadUser] thumbsupSql:`, myListDb);
return myListDb;
}
async function getBurgerFunc(){
// console.log(burgername.name);
const getBurgerName = await db.query("SELECT * FROM EATBURGER");
// console.log(`[loadUser] getBurgerName:`, getBurgerName);
return getBurgerName;
};
module.exports = {
saveResultFn,
getBurgerFunc,
burgerDbFunc
}