Permalink
Browse files

Way to look up builtin adapter info by name

  • Loading branch information...
1 parent a93a06e commit 5fa9364c18a04f534f55a98f9306906f71c6899b mde committed Aug 15, 2012
Showing with 51 additions and 20 deletions.
  1. +26 −0 lib/adapters/index.js
  2. +25 −20 lib/index.js
View
@@ -0,0 +1,26 @@
+
+var adapters
+ , path = require('path');
+
+adapters = new (function () {
+
+ this.getAdapterInfo = function (adapter) {
+ var ret;
+ switch (adapter) {
+ case 'postgres':
+ case 'postgresql':
+ case 'pg':
+ ret = {
+ name: 'Adapter'
+ , filePath: path.join('sql', 'postgres')
+ };
+ break;
+ default:
+ ret = null;
+ }
+ return ret;
+ };
+
+})();
+
+module.exports = adapters;
View
@@ -43,15 +43,15 @@ User = model.register('User', User);
var model = {}
, utils = require('utilities')
+ , adapters = require('./adapters')
, query; // Lazy-load query; it depends on model/index
-model.datatypes = require('./datatypes.js');
-model.validators = require('./validators.js');
-model.formatters = require('./formatters.js');
-
utils.mixin(model, new (function () {
- this.adapter = null;
+ this.datatypes = require('./datatypes.js');
+ this.validators = require('./validators.js');
+ this.formatters = require('./formatters.js');
+ this.adapters = {};
this.descriptionRegistry = {};
this.useTimestamps = false;
this.forceCamel = true;
@@ -159,7 +159,7 @@ utils.mixin(model, new (function () {
opts = opts || {};
callback = callback || function(){};
query[this.type.toLowerCase() + 'id'] = this.id;
- model.adapter[geddy.inflection.singularize(hasMany)].all(query, opts, callback);
+ model.adapters[geddy.inflection.singularize(hasMany)].all(query, opts, callback);
}
}
@@ -168,14 +168,14 @@ utils.mixin(model, new (function () {
this[utils.string.decapitalize(hasOne)] = function (callback) {
var query = {};
query[this.type.toLowerCase() + 'id'] = this.id;
- model.adapter[hasOne].load(query, {}, callback);
+ model.adapters[hasOne].load(query, {}, callback);
}
}
if (model.descriptionRegistry[this.type].relations.belongsTo) {
var belongsTo = model.descriptionRegistry[this.type].relations.belongsTo;
this[utils.string.decapitalize(belongsTo)] = function (callback) {
- model.adapter[belongsTo].load(this[utils.string.decapitalize(belongsTo)
+ model.adapters[belongsTo].load(this[utils.string.decapitalize(belongsTo)
+ 'id'], {}, callback);
}
}
@@ -234,10 +234,10 @@ utils.mixin(model, new (function () {
query = new Query(model[name], query, opts);
- if (!model.adapter) {
- throw new Error('model.adapter configuration has not been set up');
+ adapt = model.adapters[name];
+ if (!adapt) {
+ throw new Error('Adapter not found for ' + name);
}
- adapt = model.adapter[name];
return adapt.all.apply(adapt, [query, callback]);
};
@@ -248,10 +248,10 @@ utils.mixin(model, new (function () {
, opts = args.shift() || {}
, adapt;
- if (!model.adapter) {
- throw new Error('model.adapter configuration has not been set up');
+ adapt = model.adapters[name];
+ if (!adapt) {
+ throw new Error('Adapter not found for ' + name);
}
- adapt = model.adapter[name];
if (data.errors) {
callback(data.errors, null);
@@ -276,10 +276,10 @@ utils.mixin(model, new (function () {
query = new Query(model[name], query, opts);
- if (!model.adapter) {
- throw new Error('model.adapter configuration has not been set up');
+ adapt = model.adapters[name];
+ if (!adapt) {
+ throw new Error('Adapter not found for ' + name);
}
- adapt = model.adapter[name];
return adapt.update.apply(adapt, [data, query, callback]);
};
@@ -300,10 +300,11 @@ utils.mixin(model, new (function () {
query = new Query(model[name], query, opts);
- if (!model.adapter) {
- throw new Error('model.adapter configuration has not been set up');
+ adapt = model.adapters[name];
+ if (!adapt) {
+ throw new Error('Adapter not found for ' + name);
}
- adapt = model.adapter[name];
+
return adapt.remove.apply(adapt, [query, callback]);
};
@@ -499,6 +500,10 @@ utils.mixin(model, new (function () {
};
};
+ this.getAdapterInfo = function (name) {
+ return adapters.getAdapterInfo(name);
+ };
+
})());
model.ModelDefinitionBase = function (name) {

0 comments on commit 5fa9364

Please sign in to comment.