Browse Source

Promisify mongoose for hapi

Zane Kansil 10 years ago
parent
commit
ff18e2c3a7
1 changed files with 78 additions and 57 deletions
  1. 78 57
      frameworks/JavaScript/hapi/handlers/mongoose.js

+ 78 - 57
frameworks/JavaScript/hapi/handlers/mongoose.js

@@ -2,8 +2,10 @@
 // Handles related routes
 // Handles related routes
 
 
 var h = require('../helper');
 var h = require('../helper');
-var Mongoose = require('mongoose');
-var async = require('async');
+var Promise = require('bluebird');
+// Can treat mongoose library as one that supports Promises
+// these methods will then have "-Async" appended to them.
+var Mongoose = Promise.promisifyAll(require('mongoose'));
 var connection = Mongoose.connect('mongodb://127.0.0.1/hello_world');
 var connection = Mongoose.connect('mongodb://127.0.0.1/hello_world');
 
 
 var WorldSchema = new Mongoose.Schema({
 var WorldSchema = new Mongoose.Schema({
@@ -22,86 +24,105 @@ var FortuneSchema = new Mongoose.Schema({
 var Worlds = connection.model('World', WorldSchema);
 var Worlds = connection.model('World', WorldSchema);
 var Fortunes = connection.model('Fortune', FortuneSchema);
 var Fortunes = connection.model('Fortune', FortuneSchema);
 
 
-function mongooseRandomWorld(callback) {
-  Worlds.findOne({
-    id: h.randomTfbNumber()
-  }).exec(callback);
+function randomWorldPromise() {
+  var id = h.randomTfbNumber();
+  var promise = Worlds
+    .findOneAsync({
+      id: id
+    })
+    .then(function (world) {
+      return world;
+    })
+    .catch(function (err) {
+      process.exit(1);
+    });
+  return promise;
 }
 }
 
 
-function mongooseGetAllFortunes(callback) {
-  Fortunes.find({})
-    .exec(callback);
+function promiseAllFortunes() {
+  var promise = Fortunes
+    .findAsync({})
+    .then(function (fortunes) {
+      return fortunes;
+    })
+    .catch(function (err) {
+      process.exit(1);
+    });
+  return promise;
+}
+
+function updateWorld(world) {
+  var promise = Worlds
+    .updateAsync({
+      id: world.randomNumber
+    }, {
+      randomNumber: world.randomNumber
+    })
+    .then(function (result) {
+      return world;
+    })
+    .catch(function (err) {
+      process.exit(1);
+    });
+  return promise;
 }
 }
 
 
 module.exports = {
 module.exports = {
 
 
   SingleQuery: function(req, reply) {
   SingleQuery: function(req, reply) {
-    mongooseRandomWorld(function (err, world) {
-      if (err) { return process.exit(1); }
-
-      reply(world)
-        .header('Server', 'hapi');
-    });
+    randomWorldPromise()
+      .then(function (world) {
+        reply(world)
+          .header('Server', 'hapi');
+      });
   },
   },
 
 
   MultipleQueries: function(req, reply) {
   MultipleQueries: function(req, reply) {
     var queries = h.getQueries(req);
     var queries = h.getQueries(req);
-    var worldsToGet = h.fillArray(mongooseRandomWorld, queries);
-
-    async.parallel(worldsToGet, function (err, worlds) {
-      if (err) { return process.exit(1); }
+    var worldPromises = h.fillArray(randomWorldPromise(), queries);
 
 
-      reply(worlds)
-        .header('Server', 'hapi');
-    });
+    Promise
+      .all(worldPromises)
+      .then(function (worlds) {
+        reply(worlds)
+          .header('Server', 'hapi');
+      });
   },
   },
 
 
   Fortunes: function(req, reply) {
   Fortunes: function(req, reply) {
-    mongooseGetAllFortunes(function (err, fortunes) {
-      if (err) { return process.exit(1); }
-
-      fortunes.push(h.ADDITIONAL_FORTUNE);
-      fortunes.sort(function (a, b) {
-        return a.message.localeCompare(b.message);
-      });
+    promiseAllFortunes()
+      .then(function (fortunes) {
+        fortunes.push(h.ADDITIONAL_FORTUNE);
+        fortunes.sort(function (a, b) {
+          return a.message.localeCompare(b.message);
+        });
       
       
-      reply.view('fortunes', {
-        fortunes: fortunes
-      })
-        .header('Content-Type', 'text/html')
-        .header('Server', 'hapi');
-    });
+        reply.view('fortunes', {
+          fortunes: fortunes
+        })
+          .header('Content-Type', 'text/html')
+          .header('Server', 'hapi');
+      });
   },
   },
 
 
   Updates: function(req, reply) {
   Updates: function(req, reply) {
     var queries = h.getQueries(req);
     var queries = h.getQueries(req);
-    var worldsToGet = h.fillArray(mongooseRandomWorld, queries);
-
-    async.parallel(worldsToGet, function (err, worlds) {
-      if (err) { return process.exit(1); }
-
-      var updateFunctions = []
+    var worldPromises = [];
 
 
-      for (var i = 0; i < queries; i++) {
-        (function (i) {
-          updateFunctions.push(function (callback) {
-            worlds[i].randomNumber = h.randomTfbNumber();
-            Worlds.update({
-              id: worlds[i].id
-            }, {
-              randomNumber: worlds[i].randomNumber
-            }, callback);
-          });
-        }(i));
-      }
-
-      async.parallel(updateFunctions, function (err, results) {
-        if (err) { return process.exit(1); }
+    for (var i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise());
+    }
 
 
+    Promise
+      .all(worldPromises)
+      .map(function (world) {
+        world.randomNumber = h.randomTfbNumber();
+        return updateWorld(world);
+      })
+      .then(function (worlds) {
         reply(worlds)
         reply(worlds)
           .header('Server', 'hapi');
           .header('Server', 'hapi');
       });
       });
-    });
   }
   }
 
 
 };
 };