mongo-backend.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var Logger = require('memdb-logger');
  18. var mongodb = P.promisifyAll(require('mongodb'));
  19. var MongoBackend = function(opts){
  20. opts = opts || {};
  21. this.config = {
  22. url : opts.url || 'mongodb://localhost/test',
  23. options : {server : {socketOptions : {autoReconnect : true}, reconnectTries : 10000000, reconnectInterval : 5000}}, //always retry
  24. };
  25. this.conn = null;
  26. this.connected = false;
  27. this.logger = Logger.getLogger('memdb', __filename, 'shard:' + opts.shardId);
  28. };
  29. var proto = MongoBackend.prototype;
  30. proto.start = function(){
  31. var self = this;
  32. return P.promisify(mongodb.MongoClient.connect)(this.config.url, this.config.options)
  33. .then(function(ret){
  34. self.conn = ret;
  35. self.connected = true;
  36. self.conn.on('close', function(){
  37. self.connected = false;
  38. self.logger.error('backend mongodb disconnected');
  39. });
  40. self.conn.on('reconnect', function(){
  41. self.connected = true;
  42. self.logger.warn('backend mongodb reconnected');
  43. });
  44. self.conn.on('error', function(err){
  45. self.logger.error(err.stack);
  46. });
  47. self.logger.info('backend mongodb connected to %s', self.config.url);
  48. });
  49. };
  50. proto.stop = function(){
  51. var self = this;
  52. this.conn.removeAllListeners('close');
  53. return this.conn.closeAsync()
  54. .then(function(){
  55. self.logger.info('backend mongodb closed');
  56. });
  57. };
  58. proto.get = function(name, id){
  59. this.ensureConnected();
  60. this.logger.debug('backend mongodb get(%s, %s)', name, id);
  61. return this.conn.collection(name).findOneAsync({_id : id});
  62. };
  63. // Return an async iterator with .next(cb) signature
  64. proto.getAll = function(name){
  65. this.ensureConnected();
  66. this.logger.debug('backend mongodb getAll(%s)', name);
  67. return this.conn.collection(name).findAsync();
  68. };
  69. proto.set = function(name, id, doc){
  70. this.ensureConnected();
  71. this.logger.debug('backend mongodb set(%s, %s)', name, id);
  72. if(!!doc){
  73. doc._id = id;
  74. return this.conn.collection(name).updateAsync({_id : id}, doc, {upsert : true});
  75. }
  76. else{
  77. return this.conn.collection(name).removeAsync({_id : id});
  78. }
  79. };
  80. // items : [{name, id, doc}]
  81. proto.setMulti = function(items){
  82. this.ensureConnected();
  83. this.logger.debug('backend mongodb setMulti');
  84. var self = this;
  85. return P.mapLimit(items, function(item){
  86. return self.set(item.name, item.id, item.doc);
  87. });
  88. };
  89. // drop table or database
  90. proto.drop = function(name){
  91. this.ensureConnected();
  92. this.logger.debug('backend mongodb drop %s', name);
  93. if(!!name){
  94. return this.conn.collection(name).dropAsync()
  95. .catch(function(e){
  96. // Ignore ns not found error
  97. if(e.message.indexOf('ns not found') === -1){
  98. throw e;
  99. }
  100. });
  101. }
  102. else{
  103. return this.conn.dropDatabaseAsync();
  104. }
  105. };
  106. proto.getCollectionNames = function(){
  107. return this.conn.collectionsAsync().then(function(collections){
  108. return collections.map(function(collection){
  109. return collection.s.name;
  110. });
  111. });
  112. };
  113. proto.ensureConnected = function(){
  114. if(!this.connected){
  115. throw new Error('backend mongodb not connected');
  116. }
  117. };
  118. module.exports = MongoBackend;