index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Connection = require('./connection');
  18. var AutoConnection = require('./autoconnection');
  19. exports.connect = function(opts){
  20. var conn = new Connection(opts);
  21. return P.try(function(){
  22. return conn.connect();
  23. })
  24. .thenReturn(conn);
  25. };
  26. exports.autoConnect = function(opts){
  27. var conn = new AutoConnection(opts);
  28. return P.resolve(conn);
  29. };
  30. exports.connectBackend = function(backend){
  31. var mongodb = P.promisifyAll(require('mongodb'));
  32. return P.promisify(mongodb.MongoClient.connect)(backend.url, backend.options)
  33. .then(function(db){
  34. var getCollection = db.collection;
  35. db.collection = function(){
  36. var coll = getCollection.apply(db, arguments);
  37. var disabledMethods = ['createIndex', 'drop', 'dropIndex', 'dropIndexes', 'ensureIndex',
  38. 'findAndModify', 'getCollection', 'getDB', 'insert', 'remove', 'renameCollection', 'save', 'update'];
  39. disabledMethods.forEach(function(method){
  40. coll[method] = function(){
  41. throw new Error('write to backend is forbidden');
  42. };
  43. });
  44. return coll;
  45. };
  46. return db;
  47. });
  48. };
  49. Object.defineProperty(exports, 'goose', {
  50. get : function(){
  51. return require('./mdbgoose');
  52. },
  53. });
  54. Object.defineProperty(exports, 'logger', {
  55. get : function(){
  56. return require('memdb-logger');
  57. },
  58. });
  59. Object.defineProperty(exports, 'Promise', {
  60. get : function(){
  61. return require('bluebird');
  62. },
  63. });