mongoose.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Connects to MongoDB using the mongoose driver
  2. // Handles related routes
  3. const Promise = require('bluebird');
  4. const h = require('../helper');
  5. // Can treat mongoose library as one that supports Promises
  6. // these methods will then have "-Async" appended to them.
  7. const Mongoose = Promise.promisifyAll(require('mongoose'));
  8. const connection = Mongoose.connect('mongodb://TFB-database/hello_world');
  9. const WorldSchema = new Mongoose.Schema({
  10. id : Number,
  11. randomNumber: Number
  12. }, {
  13. collection: 'world'
  14. });
  15. const FortuneSchema = new Mongoose.Schema({
  16. id: Number,
  17. message: String
  18. }, {
  19. collection: 'fortune'
  20. });
  21. const Worlds = connection.model('World', WorldSchema);
  22. const Fortunes = connection.model('Fortune', FortuneSchema);
  23. const randomWorldPromise = () =>
  24. Worlds.findOneAsync({ id: h.randomTfbNumber() })
  25. .then((world) => world)
  26. .catch((err) => process.exit(1));
  27. const promiseAllFortunes = () =>
  28. Fortunes.findAsync({})
  29. .then((fortunes) => fortunes)
  30. .catch((err) => process.exit(1));
  31. const updateWorld = (world) =>
  32. Worlds
  33. .updateAsync(
  34. { id: world.randomNumber },
  35. { randomNumber: world.randomNumber }
  36. )
  37. .then((result) => world)
  38. .catch((err) => process.exit(1));
  39. module.exports = {
  40. SingleQuery: (ctx, next) => {
  41. return randomWorldPromise()
  42. .then((world) => {
  43. ctx.set('Server', 'Koa');
  44. ctx.type = 'application/json';
  45. ctx.body = world;
  46. return next();
  47. });
  48. },
  49. MultipleQueries: (ctx, next) => {
  50. const queries = h.getQueries(ctx);
  51. const worldPromises = h.fillArray(randomWorldPromise(), queries);
  52. return Promise
  53. .all(worldPromises)
  54. .then((worlds) => {
  55. ctx.set('Server', 'Koa');
  56. ctx.type = 'application/json';
  57. ctx.body = worlds;
  58. return next()
  59. });
  60. },
  61. Fortunes: (ctx, next) => {
  62. return promiseAllFortunes()
  63. .then((fortunes) => {
  64. fortunes.push(h.additionalFortune());
  65. fortunes.sort((a, b) => a.message.localeCompare(b.message));
  66. ctx.type = 'text/html';
  67. ctx.set('Server', 'Koa');
  68. return ctx.render('fortunes', { fortunes: fortunes });
  69. });
  70. },
  71. Updates: (ctx, next) => {
  72. const queries = h.getQueries(ctx);
  73. const worldPromises = [];
  74. for (let i = 0; i < queries; i++) {
  75. worldPromises.push(randomWorldPromise());
  76. }
  77. return Promise
  78. .all(worldPromises)
  79. .map((world) => {
  80. world.randomNumber = h.randomTfbNumber();
  81. return updateWorld(world);
  82. })
  83. .then((worlds) => {
  84. ctx.set('Server', 'Koa');
  85. ctx.type = 'application/json';
  86. ctx.body = worlds;
  87. return next();
  88. });
  89. }
  90. };