Guest User

Untitled

a guest
Jan 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. var mongo = require('mongodb')
  2. ,jqd = require('../jquery-jqd').jqd
  3. ,host = process.env['MONGO_NODE_DRIVER_HOST'] != null
  4. ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost'
  5. ,port = process.env['MONGO_NODE_DRIVER_PORT'] != null
  6. ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEFAULT_PORT
  7. ,db = null
  8. ,gConnection = null;
  9.  
  10.  
  11. /*
  12. args can be:
  13. 1) string: 'database name', [mongo.Server] // name of db, optional server, defaults to default port, localhost, autoreconnect
  14. 2) deferred: con // a deferred obj with a 'this' of the db connection
  15. 3) none: // assumed that a connection already exists
  16. */
  17.  
  18. exports.mdb = function mdb(args){
  19.  
  20. switch(arguments.length){
  21.  
  22. case 1:
  23. if(typeof arguments[0] === 'string'){
  24. // check for connection, create default server otherwise
  25. if(db === null){
  26. db = new mongo.Db(arguments[0],
  27. new mongo.Server(host, port, {
  28. auto_reconnect: true
  29. }), { native_parser: true });
  30. gConnection = exports.mdb.connect();
  31. }
  32. } else if (typeof arguments[0].promise !== 'undefined'){
  33. // we have a promise, assume a connection
  34. gConnection = arguments[0];
  35. }
  36. break;
  37.  
  38. case 2:
  39. // assume first param is string, second is mongo.Server instance
  40. db = new mongo.Db(arguments[0], arguments[1]);
  41. gConnection = exports.mdb.connect();
  42. break;
  43.  
  44. default:
  45. // zero params, assume we have a deferred connection somewhere
  46. if(!gConnection) throw new Error('There is no MongoDb connection to use.');
  47. break;
  48. }
  49.  
  50. var con = gConnection;
  51.  
  52. return {
  53. connection: con
  54. ,collection: function(name){
  55. var colDfd = jqd.deferred()
  56. ,p = colDfd.promise();
  57.  
  58. // make sure we have a connection, which we should regardless
  59. con.then(function(){
  60.  
  61. this.collection(name, function(err, col){
  62. if(err){
  63. colDfd.reject();
  64. } else {
  65. colDfd.resolveWith(col, [col]);
  66. }
  67. });
  68.  
  69. });
  70.  
  71. return {
  72. collection: p // the collection's promise
  73. ,get: function(callback){
  74. return p.then(callback);
  75. }
  76.  
  77. ,find: function(query){
  78. var dfd = jqd.deferred();
  79.  
  80. p.then(function(){
  81. this.find(query, function(err, result){
  82. if(err){
  83. dfd.reject();
  84. } else {
  85. result.toArray(function(err, docs){
  86. if(err) dfd.reject();
  87. else dfd.resolveWith(docs, [docs]);
  88. });
  89. }
  90. });
  91. });
  92.  
  93. return dfd.promise();
  94. }
  95.  
  96. ,findOne: function(query){
  97. var dfd = jqd.deferred();
  98.  
  99. p.then(function(){
  100. this.findOne(query, function(err, doc){
  101. if(err){
  102. dfd.reject();
  103. } else {
  104. dfd.resolveWith(doc, [doc]);
  105. }
  106. });
  107. });
  108.  
  109. return dfd.promise();
  110. }
  111.  
  112. ,findOneOrDie: function(query){
  113.  
  114. var dfd = jqd.deferred();
  115.  
  116. p.then(function(){
  117. this.findOne(query, function(err, doc){
  118. if(err || !doc){
  119. dfd.reject();
  120. } else {
  121. dfd.resolveWith(doc, [doc]);
  122. }
  123. });
  124. });
  125.  
  126. return dfd.promise();
  127. }
  128.  
  129. ,insert: function(docs){
  130. var dfd = jqd.deferred();
  131.  
  132. p.then(function(){
  133. this.insert(docs, function(err, docs){
  134. if(err){
  135. dfd.reject(err);
  136. } else {
  137. dfd.resolveWith(docs, [err, docs]);
  138. }
  139. });
  140. });
  141.  
  142. return dfd.promise();
  143. }
  144.  
  145. ,findAndModify: function(options){
  146. var dfd = jqd.deferred();
  147.  
  148. p.then(function(){
  149.  
  150. this.findAndModify(
  151. options.query
  152. ,options.sort || []
  153. ,options.update
  154. ,options.options
  155. ,function(err, result){
  156.  
  157. if(err) {
  158. dfd.rejectWith(err, [err, result]);
  159. } else {
  160. dfd.resolveWith(result, [err, result]);
  161. }
  162.  
  163. });
  164. });
  165.  
  166. return dfd.promise();
  167. }
  168. }
  169. }
  170. }
  171.  
  172. }
  173.  
  174. exports.mdb.connect = function connect(){
  175. return jqd.deferred(function(dfd){
  176. db.open(function(err, client){
  177. if(err){
  178. dfd.reject(err);
  179. } else {
  180. dfd.resolveWith(client, [client]);
  181. }
  182. });
  183. }).promise();
  184. }
  185.  
  186. // attach mongo lib to expo
  187. exports.mdb.mongo = mongo;
  188.  
  189. exports.express = function express(args){
  190.  
  191. // start the connection
  192. var m = exports.mdb(args);
  193.  
  194. exports.mdb.BSON = db.options.native_parser ? mongo.BSONNative : mongo.BSONPure;
  195.  
  196. return function(req, res, next){
  197. if(!req.mdb) req.mdb = exports.mdb;
  198. next();
  199. }
  200.  
  201. }
Add Comment
Please, Sign In to add comment