Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. var mysql = require('mysql');
  2.  
  3. function createDBConnection() {
  4. var conn = mysql.createPool({
  5. host: 'xxx',
  6. user: 'xxx',
  7. password: 'xxx',
  8. database: 'xxx'
  9. });
  10. return conn;
  11. }
  12.  
  13. module.exports = function() {
  14. return createDBConnection;
  15. }
  16.  
  17. function Dao(connection) {
  18. this._connection = connection;
  19. }
  20.  
  21. Dao.prototype.findAll = function (callback) {
  22. this._connection.query('SELECT * FROM table',
  23. function(errors, results) {
  24. callback(errors,results);
  25. });
  26. };
  27.  
  28. module.exports = function() {
  29. return Dao;
  30. }
  31.  
  32. app.get('/products', function (req,res) {
  33. var connection = app.persistence.connectionFactory();
  34. var dao = new app.persistence.Dao(connection);
  35.  
  36. dao.findAll(function (err, result) {
  37. res.format({
  38. json: function () {
  39. res.json(result);
  40. }
  41. });
  42. });
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement