mongoose.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(
  9. 'mongodb://TFB-database/hello_world',
  10. { useMongoClient: true }
  11. );
  12. const WorldSchema = new Mongoose.Schema({
  13. id : Number,
  14. randomNumber: Number
  15. }, {
  16. collection: 'world'
  17. });
  18. const FortuneSchema = new Mongoose.Schema({
  19. id: Number,
  20. message: String
  21. }, {
  22. collection: 'fortune'
  23. });
  24. const Worlds = connection.model('World', WorldSchema);
  25. const Fortunes = connection.model('Fortune', FortuneSchema);
  26. const randomWorldPromise = () =>
  27. Worlds.findOneAsync({ id: h.randomTfbNumber() })
  28. .then((world) => world)
  29. .catch((err) => process.exit(1));
  30. const promiseAllFortunes = () =>
  31. Fortunes.findAsync({})
  32. .then((fortunes) => fortunes)
  33. .catch((err) => process.exit(1));
  34. const updateWorld = (world) =>
  35. Worlds
  36. .updateAsync(
  37. { id: world.randomNumber },
  38. { randomNumber: world.randomNumber }
  39. )
  40. .then((result) => world)
  41. .catch((err) => process.exit(1));
  42. module.exports = {
  43. SingleQuery: (ctx, next) => {
  44. return randomWorldPromise()
  45. .then((world) => {
  46. ctx.set('Server', 'Koa');
  47. ctx.type = 'application/json';
  48. ctx.body = world;
  49. return next();
  50. });
  51. },
  52. MultipleQueries: (ctx, next) => {
  53. const queries = h.getQueries(ctx);
  54. const worldPromises = h.fillArray(randomWorldPromise(), queries);
  55. return Promise
  56. .all(worldPromises)
  57. .then((worlds) => {
  58. ctx.set('Server', 'Koa');
  59. ctx.type = 'application/json';
  60. ctx.body = worlds;
  61. return next()
  62. });
  63. },
  64. Fortunes: (ctx, next) => {
  65. return promiseAllFortunes()
  66. .then((fortunes) => {
  67. fortunes.push(h.additionalFortune());
  68. fortunes.sort((a, b) => a.message.localeCompare(b.message));
  69. ctx.type = 'text/html';
  70. ctx.set('Server', 'Koa');
  71. return ctx.render('fortunes', { fortunes: fortunes });
  72. });
  73. },
  74. Updates: (ctx, next) => {
  75. const queries = h.getQueries(ctx);
  76. const worldPromises = [];
  77. for (let i = 0; i < queries; i++) {
  78. worldPromises.push(randomWorldPromise());
  79. }
  80. return Promise
  81. .all(worldPromises)
  82. .map((world) => {
  83. world.randomNumber = h.randomTfbNumber();
  84. return updateWorld(world);
  85. })
  86. .then((worlds) => {
  87. ctx.set('Server', 'Koa');
  88. ctx.type = 'application/json';
  89. ctx.body = worlds;
  90. return next();
  91. });
  92. }
  93. };