Skip to content

Commit

Permalink
Allow instantiation of Iridium using a URI
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jul 21, 2014
1 parent 7b2cd29 commit bbb23e4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ When using Iridium, you are required to instantiate a core with a settings objec
}
```

Alternatively, you can pass in a standard MongoDB URI like `mongodb://username:password@localhost:27018/iridium` in place of the configuration object, allowing Iridium to easily be used with MongoS.

Once you've got a core, you need to connect it to the database. This is done by calling the core's *connect* method and giving it a callback function.


Expand Down
78 changes: 41 additions & 37 deletions lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,51 @@ function Database(config) {
this.models = {};
this.plugins = [];

Object.defineProperty(this, 'settings', {
get: function () {
return config;
},
enumerable: false
});
if(typeof config == 'string') {
Object.defineProperty(this, 'uri', {
get: function () {
return config;
},
enumerable: false
});
} else {
Object.defineProperty(this, 'settings', {
get: function () {
return config;
},
enumerable: false
});
}
}

Database.Model = Model;
Database.Instance = Instance;

Database.prototype = {
get uri() {
/// <summary>Gets a URL which can be used to connec to a MongoDB instance based on the configuration</summary>
/// <returns type="String" />

"use strict";
var uri = 'mongodb://';
if (this.settings.username && this.settings.password)
uri += this.settings.username + ':' + this.settings.password + '@';
uri += this.settings.host || 'localhost';
if (this.settings.port)
uri += ':' + this.settings.port;
uri += '/' + this.settings.database;

return uri;
},
get db() {
/// <summary>Gets the MongoDB database connection</summary>

"use strict";
if (!this.connection) throw new Error('Database connection not yet established');
return this.connection;
}
};

Database.prototype.connect = function (cb) {
/// <summary>Connects to the database server specified in the provided configuration</summary>
/// <param name="cb" type="Function">A function to be called when the connection is completed, called immediately if one is already open</param>
Expand All @@ -38,7 +72,7 @@ Database.prototype.connect = function (cb) {

var $ = this;

MongoClient.connect(this.connect_url, function (err, db) {
MongoClient.connect(this.uri, function (err, db) {
/// <param name="err" type="Error"></param>
/// <param name="db" type="Database"></param>

Expand Down Expand Up @@ -125,33 +159,3 @@ Database.prototype.register = function () {
}

};

Object.defineProperty(Database.prototype, 'connect_url', {
get: function () {
/// <summary>Gets a URL which can be used to connec to a MongoDB instance based on the configuration</summary>
/// <returns type="String" />

"use strict";
var uri = 'mongodb://';
if (this.settings.username && this.settings.password)
uri += this.settings.username + ':' + this.settings.password + '@';
uri += this.settings.host || 'localhost';
if (this.settings.port)
uri += ':' + this.settings.port;
uri += '/' + this.settings.database;

return uri;
},
enumerable: false
});

Object.defineProperty(Database.prototype, 'db', {
get: function () {
/// <summary>Gets the MongoDB database connection</summary>

"use strict";
if (!this.connection) throw new Error('Database connection not yet established');
return this.connection;
},
enumerable: false
});

0 comments on commit bbb23e4

Please sign in to comment.