index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2015 rain1017.
  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 path = require('path');
  17. var requireChildren = require('../../utils/require-children');
  18. var memdb = require('memdb-client');
  19. var P = memdb.Promise;
  20. var logger = memdb.logger.getLogger('memdb-client', __filename);
  21. var DEFAULT_MODELS_PATH = 'app/models';
  22. var MemDB = function(app, opts){
  23. this.app = app;
  24. this.config = app.get('memdbConfig') || opts || {};
  25. this.config.modelsPath = this.config.modelsPath || DEFAULT_MODELS_PATH;
  26. };
  27. var proto = MemDB.prototype;
  28. proto.name = 'memdb';
  29. proto.start = function(cb){
  30. return P.bind(this)
  31. .then(function(){
  32. var basePath = path.join(this.app.getBase(), this.config.modelsPath);
  33. return this.loadModels(basePath);
  34. })
  35. .then(function(){
  36. return this.goose.connect(this.config);
  37. })
  38. .nodeify(cb);
  39. };
  40. proto.stop = function(force, cb){
  41. this.unregisterModels();
  42. return P.bind(this)
  43. .then(function(){
  44. return this.goose.disconnect();
  45. })
  46. .nodeify(cb);
  47. };
  48. proto.loadModels = function(basePath){
  49. var modules = requireChildren(module, basePath);
  50. var self = this;
  51. Object.keys(modules).forEach(function(name){
  52. modules[name](self.app);
  53. });
  54. var mdbgoose = this.goose;
  55. for(var name in mdbgoose.models){
  56. var model = mdbgoose.model(name);
  57. model.schema.options.versionKey = false; //disable versionKey
  58. this.app.models[name] = model;
  59. }
  60. };
  61. // Unregister mongoose models
  62. // WARN: It is not a official supported behavier
  63. proto.unregisterModels = function(){
  64. var models = this.goose.connection.models;
  65. Object.keys(models).forEach(function(name){
  66. delete models[name];
  67. });
  68. };
  69. Object.defineProperty(proto, 'goose', {
  70. get : function(){
  71. return memdb.goose;
  72. }
  73. });
  74. Object.defineProperty(proto, 'autoconn', {
  75. get : function(){
  76. return memdb.goose.autoconn;
  77. }
  78. });
  79. module.exports = function(app, opts){
  80. var db = new MemDB(app, opts);
  81. app.set(db.name, db, true);
  82. app.set('models', {}, true);
  83. return db;
  84. };