Skip to content
Browse files

Some light dependencies refactoring

  • Loading branch information...
1 parent 547dc60 commit 232b9131eb00a2b2c50333acfc4f6b8b019ecd20 @rauchg committed
Showing with 82 additions and 81 deletions.
  1. +78 −0 lib/core.js
  2. +1 −1 lib/tests.js
  3. +1 −78 mongoose.js
  4. +2 −2 tests/integration/model.test.js
View
78 lib/core.js
@@ -0,0 +1,78 @@
+var sys = require('sys'),
+ url = require('url'),
+ EventEmitter = require('events').EventEmitter,
+ Connection = require('./connection').Connection,
+
+Mongoose = this.Mongoose = {
+
+ _config: {
+ log: true
+ },
+
+ _models: {},
+
+ _connections: {},
+
+ connect: function(uri, options){
+ var _uri = url.parse(uri);
+ if (_uri.protocol !== 'mongodb:') throw new Error('Please include the mongodb:// protocol');
+ if (!_uri.pathname) throw new Error('Please provide a database name');
+ _uri.port = _uri.port || '27017';
+ _uri.hostname = _uri.hostname.toLowerCase();
+ _uri.host = _uri.host.toLowerCase();
+ _uri.pathname = _uri.pathname.replace(/\//g, '').toLowerCase();
+ return this._lookup(_uri) || this._open(_uri, options);
+ },
+
+ model: function(name, definition){
+ if (definition){
+ this._models[name] = definition;
+ } else {
+ return this._models[name];
+ }
+ },
+
+ _open: function(uri, options){
+ var connection = new Connection(this, uri, options), self = this;
+ this._connections[url.format(uri)] = connection;
+ connection.addListener('error', function(err){
+ self._onError(connection, err);
+ });
+ return connection;
+ },
+
+ _onError: function(connection, err){
+ if (this.set('log')){
+ require('sys').log('Mongoose Error, connection: ' + url.format(connection.uri) + "\n" + err);
+ }
+ },
+
+ _lookup: function(uri){
+ var _uri = url.format(uri);
+ if (_uri in this._connections) return this._connections[_uri];
+ return false;
+ },
+
+ set: function(){
+ if (arguments.length == 1){
+ return this._config[arguments[0]];
+ } else {
+ this._config[arguments[0]] = arguments[1];
+ return this;
+ }
+ },
+
+ enable: function(key){
+ this._config[key] = true;
+ return this;
+ },
+
+ disable: function(key){
+ this._config[key] = false;
+ return this;
+ }
+
+};
+
+sys.inherits(Mongoose, EventEmitter.prototype);
+EventEmitter.call(Mongoose);
View
2 lib/tests.js
@@ -1,5 +1,5 @@
var mongoose = require('mongoose').Mongoose,
- Connection = require('./lib/connection').Connection,
+ Connection = require('./connection').Connection,
Class = require('./util').Class;
// for integration
View
79 mongoose.js
@@ -1,80 +1,3 @@
require.paths.unshift(__dirname + '/lib/support/node-mongodb-native/lib');
-var sys = require('sys'),
- url = require('url'),
- EventEmitter = require('events').EventEmitter,
- Connection = require('./lib/connection').Connection,
-
-Mongoose = this.Mongoose = {
-
- _config: {
- log: true
- },
-
- _models: {},
-
- _connections: {},
-
- connect: function(uri, options){
- var _uri = url.parse(uri);
- if (_uri.protocol !== 'mongodb:') throw new Error('Please include the mongodb:// protocol');
- if (!_uri.pathname) throw new Error('Please provide a database name');
- _uri.port = _uri.port || '27017';
- _uri.hostname = _uri.hostname.toLowerCase();
- _uri.host = _uri.host.toLowerCase();
- _uri.pathname = _uri.pathname.replace(/\//g, '').toLowerCase();
- return this._lookup(_uri) || this._open(_uri, options);
- },
-
- model: function(name, definition){
- if (definition){
- this._models[name] = definition;
- } else {
- return this._models[name];
- }
- },
-
- _open: function(uri, options){
- var connection = new Connection(this, uri, options), self = this;
- this._connections[url.format(uri)] = connection;
- connection.addListener('error', function(err){
- self._onError(connection, err);
- });
- return connection;
- },
-
- _onError: function(connection, err){
- if (this.set('log')){
- require('sys').log('Mongoose Error, connection: ' + url.format(connection.uri) + "\n" + err);
- }
- },
-
- _lookup: function(uri){
- var _uri = url.format(uri);
- if (_uri in this._connections) return this._connections[_uri];
- return false;
- },
-
- set: function(){
- if (arguments.length == 1){
- return this._config[arguments[0]];
- } else {
- this._config[arguments[0]] = arguments[1];
- return this;
- }
- },
-
- enable: function(key){
- this._config[key] = true;
- return this;
- },
-
- disable: function(key){
- this._config[key] = false;
- return this;
- }
-
-};
-
-sys.inherits(Mongoose, EventEmitter.prototype);
-EventEmitter.call(Mongoose);
+this.Mongoose = require('./lib/core').Mongoose;
View
4 tests/integration/model.test.js
@@ -1,10 +1,10 @@
-require.paths.unshift('.', 'lib')
+require.paths.unshift('.')
var assert = require('assert'),
mongoose = require('mongoose').Mongoose,
mongo = require('mongodb'),
ObjectID = require('mongodb/bson/bson').ObjectID;
-require('tests');
+require('lib/tests');
mongoose.model('User', {
properties: ['_someid', '_someother', 'first', 'last', {'nested': ['test']}]

0 comments on commit 232b913

Please sign in to comment.
Something went wrong with that request. Please try again.