Guest User

Untitled

a guest
Dec 6th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. const mariadb = require('mariadb');
  2. const pool = mariadb.createPool({
  3. host: 'x.x.x.x',
  4. user:'x',
  5. password: 'x',
  6. connectionLimit: 5
  7. });
  8.  
  9. if(!pool){
  10. console.log('conección falló');
  11.  
  12. }else{
  13. console.log('good conección');
  14. }
  15.  
  16. let userModel = {};
  17.  
  18. userModel.getUsers = (callback) => {
  19. pool.getConnection()
  20. .then(conn => {
  21.  
  22. conn.query('SELECT * FROM `users order by id`')
  23. .then((rows) => {
  24. console.log(rows); //[ {val: 1}, meta: ... ]
  25. //return conn.query("INSERT INTO myTable value (?, ?)", [1, "mariadb"]);
  26. callback(rows);
  27. })
  28. .then((res) => {
  29. console.log(res); // { affectedRows: 1, insertId: 1, warningStatus: 0 }
  30. conn.end();
  31. })
  32. .catch(err => {
  33. //handle error
  34. conn.end();
  35. })
  36.  
  37. }).catch(err => {
  38. //not connected
  39. });
  40. };
  41.  
  42.  
  43. module.exports = userModel;
  44.  
  45. const User = require('../models/user');
  46.  
  47.  
  48. module.exports = (app)=>{
  49. app.get('/',(req,res)=>{
  50. User.getUsers((err,data) => {
  51. res.status(200).json(data);
  52. });
  53. });
  54.  
  55. }
Add Comment
Please, Sign In to add comment