Permalink
Browse files

Only hasMany should have assn property in array

  • Loading branch information...
1 parent 32d78a6 commit 121c1933ccc489d2a816746bced51b6fca922411 mde committed Mar 28, 2013
Showing with 57 additions and 56 deletions.
  1. +10 −33 lib/adapters/sql/base.js
  2. +15 −23 lib/adapters/sql/postgres.js
  3. +32 −0 lib/index.js
View
@@ -13,47 +13,25 @@ Adapter = function () {
Adapter.prototype = new BaseAdapter();
utils.mixin(Adapter.prototype, new (function () {
- this._getModelNameForAssociation = function (mainName, assnName) {
- var assn
- , assnItem;
- if (model.descriptionRegistry[assnName]) {
- return assnName;
- }
- assn = model.descriptionRegistry[mainName].associations;
- for (var p in assn) {
- assnItem = assn[p][assnName];
- if (assnItem) {
- return assnItem.model;
- }
- }
- };
-
this._tableizeModelName = function (name) {
- var tableName = utils.inflection.pluralize(name);
- tableName = utils.string.snakeize(tableName);
- return tableName;
+ return utils.string.getInflection(name, 'filename', 'plural');
};
this._modelizeTableName = function (name, ownerName) {
- var infl
- , modelName
+ var modelName
, ownerModelName
- infl = utils.string.getInflections(name);
- modelName = infl.constructor.singular;
+ modelName = utils.string.getInflection(name, 'constructor', 'singular');
if (ownerName && name != ownerName) {
- infl = utils.string.getInflections(ownerName);
- ownerModelName = infl.constructor.singular;
- modelName = this._getModelNameForAssociation(ownerModelName, modelName);
+ ownerModelName = utils.string.getInflection(ownerName, 'constructor', 'singular');
+ modelName = model.getModelNameForAssociation(ownerModelName, modelName);
}
return model[modelName] || null;
};
this._createSelectStatement = function (modelName, ownerModelName) {
-
- var infl = utils.string.getInflections(modelName)
- , name
- , assumedName = infl.constructor.singular
+ var name
+ , assumedName = utils.string.getInflection(modelName, 'constructor', 'singular')
, ownerName
, tableName
, assumedTableName
@@ -67,9 +45,8 @@ utils.mixin(Adapter.prototype, new (function () {
// Otherwise it's a named association, need to look up the
// actual model via it's owner's associations list
else {
- infl = utils.string.getInflections(ownerModelName);
- ownerName = infl.constructor.singular;
- name = this._getModelNameForAssociation(ownerName, assumedName);
+ ownerName = utils.string.getInflection(ownerModelName, 'constructor', 'singular');
+ name = model.getModelNameForAssociation(ownerName, assumedName);
}
tableName = this._tableizeModelName(name);
@@ -101,7 +78,7 @@ utils.mixin(Adapter.prototype, new (function () {
mainTableName = this._tableizeModelName(mainName);
assnInfl = utils.string.getInflections(assnModelName);
assnName = assnInfl.constructor.singular;
- assnModelName = this._getModelNameForAssociation(mainName, assnName);
+ assnModelName = model.getModelNameForAssociation(mainName, assnName);
assnTableName = this._tableizeModelName(assnName);
assnModelTableName = this._tableizeModelName(assnModelName);
if (assnName == assnModelName) {
@@ -52,7 +52,10 @@ utils.mixin(Adapter.prototype, new (function () {
// First item in this list should be the owner table for
// any subsequent associations, e.g., ['users', 'profiles']
tables.forEach(function (t) {
- models[t] = self._modelizeTableName(t, mainTable);
+ models[t] = {
+ ctor: self._modelizeTableName(t, mainTable)
+ , assnType: t == mainTable ? null : model.getAssociationType(mainTable, t)
+ }
});
rows = data.rows;
@@ -62,9 +65,7 @@ utils.mixin(Adapter.prototype, new (function () {
var obj = {}
, colArr
, table
- , key
- , params;
-
+ , key;
// Create a column object with mapped params
// users#login: 'foo', users#name: 'adsf', profiles#nickname: 'qwer'
@@ -84,12 +85,13 @@ utils.mixin(Adapter.prototype, new (function () {
// associations.) For each subsquent record, instantiate the association
// and append it to an array in the named property for that association
tables.forEach(function (p) {
- params = obj[p];
+ var params = obj[p]
+ , modelItem = models[p];
// Owner table-name
if (p == mainTable) {
// This is a new id -- create an owner object
if (params.id != mainId) {
- inst = models[p].create(params);
+ inst = modelItem.ctor.create(params);
inst._saved = true;
res.push(inst);
}
@@ -101,26 +103,16 @@ utils.mixin(Adapter.prototype, new (function () {
// Ignore empty records
if (params.id) {
// Create an array to hold the items if necessary
- // FIXME: hasOne associations should not be an array
- inst[p] = inst[p] || [];
- inst[p].push(models[p].create(params));
+ if (modelItem.assnType == 'hasMany') {
+ inst[p] = inst[p] || [];
+ inst[p].push(modelItem.ctor.create(params));
+ }
+ else {
+ inst[p] = modelItem.ctor.create(params);
+ }
}
}
});
-
- /*
- var item = {}
- , key;
- for (var p in row) {
- if (p.indexOf(tableName + '#') > -1) {
- item[p.replace(tableName + '#', '')] = row[p];
- }
- }
- var inst = model.create(item);
- inst.id = row[tableName + '#id'];
- inst._saved = true;
- res.push(inst);
- */
});
callback(null, res);
}
View
@@ -828,6 +828,38 @@ utils.mixin(model, new (function () {
return adapters.getAdapterInfo(name);
};
+ // FIXME: Move this into an associations lib
+ this.getModelNameForAssociation = function (main, assn) {
+ var mainName = utils.string.getInflection(main, 'constructor', 'singular')
+ , assnName = utils.string.getInflection(assn, 'constructor', 'singular')
+ , assn
+ , assnItem;
+ if (this.descriptionRegistry[assnName]) {
+ return assnName;
+ }
+ assn = this.descriptionRegistry[mainName].associations;
+ for (var p in assn) {
+ assnItem = assn[p][assnName];
+ if (assnItem) {
+ return assnItem.model;
+ }
+ }
+ };
+
+ this.getAssociationType = function (main, assn) {
+ var mainName = utils.string.getInflection(main, 'constructor', 'singular')
+ , assnName = utils.string.getInflection(assn, 'constructor', 'singular')
+ , assn
+ , assnItem;
+ assn = this.descriptionRegistry[mainName].associations;
+ for (var p in assn) {
+ assnItem = assn[p][assnName];
+ if (assnItem) {
+ return p;
+ }
+ }
+ };
+
})());
model.ModelDefinitionBase = function (name) {

0 comments on commit 121c193

Please sign in to comment.