Advertisement
Guest User

database

a guest
May 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. //Database connection
  2. "use strict";
  3.  
  4. var getConnection = function () {
  5. var mysql = require('mysql');
  6.  
  7. var connection = mysql.createConnection({
  8. host: 'localhost',
  9. port: 8889,
  10. user: 'howest',
  11. password: 'howest',
  12. database: 'noteapp'
  13. });
  14.  
  15. connection.connect(function(err) {
  16. if (err) throw err;
  17. });
  18.  
  19. return connection;
  20. };
  21.  
  22. var db = {
  23. getUser: function(email){
  24. getConnection().query('SELECT password, iduser FROM user WHERE email = ?', email).on('result', function (row) {
  25. console.log(row);
  26. return row;
  27. })
  28. },
  29.  
  30. getTodolistId: function (title) {
  31. getConnection().query('SELECT idtodolist FROM todolist WHERE title= ?', [title]).on('result', function (row) {
  32. return row.idtodolist;
  33. });
  34. },
  35.  
  36. createTodolist: function(title, userId){
  37. getConnection().query('INSERT INTO todolist (title, idUser) VALUES (?, ?)', [title, userId], function (err, results) {
  38. if (err) throw err;
  39. return results.insertId;
  40. });
  41. },
  42.  
  43. insertTodo: function (listid) {
  44. getConnection().query('INSERT INTO todoitem (idtodolist, text, date, complete) VALUES (?, ?, ?, ?)', [listid, req.body.new_note, req.body.date, false]).on('result', function () {
  45. return listid;
  46. })
  47. },
  48.  
  49. getTodos: function(listid){
  50. getConnection().query('SELECT text, date, complete FROM todolist JOIN todoitem ON todolist.idtodolist = todoitem.idtodolist WHERE todolist.idtodolist = ?', [listid], function (err, results) {
  51. if (err) throw err;
  52. return results;
  53. });
  54. }
  55. };
  56.  
  57. module.exports = db;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement