Skip to content

Commit

Permalink
Added plugin registration logic to database
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 11, 2013
1 parent 600a1b7 commit 1440328
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function Database(config) {

this.connection = null;
this.models = {};
this.plugins = [];

Object.defineProperty(this, 'settings', {
get: function () {
Expand Down Expand Up @@ -76,7 +77,11 @@ Database.prototype.express = function() {
};
};

Database.prototype.register = function (name, model) {
Database.prototype.register = function () {
/// <signature>
/// <summary>Registers a plugin with the ORM, allowing extended functionality</summary>
/// <param name="plugin" type="Object">The plugin</param>
/// </signature>
/// <signature>
/// <summary>Registers a model with the ORM (not entirely necessary)</summary>
/// <param name="name" type="String">The name of the model to register with the current connection</param>
Expand All @@ -85,23 +90,37 @@ Database.prototype.register = function (name, model) {
/// <signature>
/// <summary>Registers a model with the ORM (not entirely necessary)</summary>
/// <param name="name" type="String">The name of the model to register with the ORM</param>
/// <param name="model" type="Function">A function which creates a model for a given database connection</param>
/// <param name="modelFactory" type="Function">A function which creates a model for a given database connection</param>
/// </signature>
"use strict";

var $ = this;

if (model.isModel)
this.models[name] = model;
else this.models[name] = model(this);
var args = Array.prototype.slice.call(arguments, 0);

if(args.length === 1) {
// Plugin registration
var plugin = args[0];

this.plugins.push(plugin);
} else if(args.length === 2) {
// Model registration
var name = args[0];
var model = args[1];

if (model.isModel)
this.models[name] = model;
else this.models[name] = model(this);

Object.defineProperty($, name, {
get: function () {
/// <returns type="Model" />
return $.models[name];
},
enumerable: true
});
}

Object.defineProperty($, name, {
get: function () {
/// <returns type="Model" />
return $.models[name];
},
enumerable: true
});
};

Object.defineProperty(Database.prototype, 'connect_url', {
Expand Down

0 comments on commit 1440328

Please sign in to comment.