sequelize-postgres.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Connects to Postgres using the sequelize driver
  2. // Handles related routes
  3. const Promise = require('bluebird');
  4. const h = require('../helper');
  5. const Sequelize = require('sequelize');
  6. const sequelize = new Sequelize('hello_world', 'benchmarkdbuser', 'benchmarkdbpass', {
  7. host: 'TFB-database',
  8. dialect: 'postgres',
  9. logging: false
  10. });
  11. const Worlds = sequelize.define('world', {
  12. id: {
  13. type: 'Sequelize.INTEGER',
  14. primaryKey: true
  15. },
  16. randomnumber: { type: 'Sequelize.INTEGER' }
  17. }, {
  18. timestamps: false,
  19. freezeTableName: true
  20. });
  21. const Fortunes = sequelize.define('Fortune', {
  22. id: {
  23. type: 'Sequelize.INTEGER',
  24. primaryKey: true
  25. },
  26. message: { type: 'Sequelize.STRING' }
  27. }, {
  28. timestamps: false,
  29. freezeTableName: true
  30. });
  31. const randomWorldPromise = () =>
  32. Worlds.findOne({ where: { id: h.randomTfbNumber() } })
  33. .then((results) => results)
  34. .catch((err) => process.exit(1));
  35. module.exports = {
  36. SingleQuery: (ctx, next) =>
  37. randomWorldPromise().then((world) => {
  38. ctx.set('Server', 'Koa');
  39. ctx.type = 'application/json';
  40. ctx.body = world;
  41. return next();
  42. }),
  43. MultipleQueries: (ctx, next) => {
  44. const queries = h.getQueries(ctx),
  45. worldPromises = [];
  46. for (let i = 0; i < queries; i++) {
  47. worldPromises.push(randomWorldPromise());
  48. }
  49. return Promise.all(worldPromises)
  50. .then((worlds) => {
  51. ctx.set('Server', 'Koa');
  52. ctx.type = 'application/json';
  53. ctx.body = worlds;
  54. return next();
  55. });
  56. },
  57. Fortunes: (ctx, next) => {
  58. return Fortunes.findAll().then((fortunes) => {
  59. fortunes.push(h.additionalFortune());
  60. fortunes.sort((a, b) => a.message.localeCompare(b.message));
  61. ctx.set('Server', 'Koa');
  62. ctx.type = 'text/html';
  63. return ctx.render('fortunes', { fortunes });
  64. }).catch((err) => process.exit(1));
  65. },
  66. Updates: (ctx, next) => {
  67. const queries = h.getQueries(ctx),
  68. worldPromises = [];
  69. for (let i = 0; i < queries; i++) {
  70. worldPromises.push(randomWorldPromise());
  71. }
  72. const worldUpdate = (world) => {
  73. world.randomNumber = h.randomTfbNumber();
  74. return Worlds.update(
  75. {randomNumber: world.randomNumber},
  76. {where: {id: world.id}}
  77. )
  78. .then((results) => world)
  79. .catch((err) => process.exit(1));
  80. };
  81. return Promise
  82. .all(worldPromises)
  83. .map((world) => worldUpdate(world))
  84. .then((updated) => {
  85. ctx.set('Server', 'Koa');
  86. ctx.type = 'application/json';
  87. ctx.body = updated;
  88. return next();
  89. })
  90. .catch((err) => process.exit(1));
  91. }
  92. };