Skip to content

Commit

Permalink
Fix errors adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed May 3, 2013
1 parent a4c2e82 commit 4e45ebe
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 389 deletions.
206 changes: 103 additions & 103 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,22 @@ AbstractClass.findOrCreate = function findOrCreate(query, data, callback) {
if (typeof query === 'undefined') {
query = {
where: {}
};
}
if (typeof data === 'function' || typeof data === 'undefined') {
callback = data;
data = query && query.where;
}
if (typeof callback === 'undefined') {
callback = function () {};
}
};
}
if (typeof data === 'function' || typeof data === 'undefined') {
callback = data;
data = query && query.where;
}
if (typeof callback === 'undefined') {
callback = function () {};
}

var t = this;
this.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record);
t.create(data, callback);
});
var t = this;
this.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record);
t.create(data, callback);
});
};

/**
Expand Down Expand Up @@ -507,71 +507,71 @@ AbstractClass.include = function (objects, include, callback) {

var req = {
'where': {}
};
};

if (!keyVals[relation.keyFrom]) {
objsByKeys[relation.keyFrom] = {};
for (var j = 0; j < objs.length; j++) {
if (!objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]]) {
objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]] = [];
if (!keyVals[relation.keyFrom]) {
objsByKeys[relation.keyFrom] = {};
for (var j = 0; j < objs.length; j++) {
if (!objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]]) {
objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]] = [];
}
objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]].push(objs[j]);
}
objsByKeys[relation.keyFrom][objs[j][relation.keyFrom]].push(objs[j]);
keyVals[relation.keyFrom] = Object.keys(objsByKeys[relation.keyFrom]);
}
keyVals[relation.keyFrom] = Object.keys(objsByKeys[relation.keyFrom]);
}

if (keyVals[relation.keyFrom].length > 0) {
// deep clone is necessary since inq seems to change the processed array
var keysToBeProcessed = {};
var inValues = [];
for (var f = 0; f < keyVals[relation.keyFrom].length; f++) {
keysToBeProcessed[keyVals[relation.keyFrom][f]] = true;
if (keyVals[relation.keyFrom][f] !== 'null') {
inValues.push(keyVals[relation.keyFrom][f]);
if (keyVals[relation.keyFrom].length > 0) {
// deep clone is necessary since inq seems to change the processed array
var keysToBeProcessed = {};
var inValues = [];
for (var f = 0; f < keyVals[relation.keyFrom].length; f++) {
keysToBeProcessed[keyVals[relation.keyFrom][f]] = true;
if (keyVals[relation.keyFrom][f] !== 'null') {
inValues.push(keyVals[relation.keyFrom][f]);
}
}
}

req['where'][relation.keyTo] = {
inq: inValues
};
req['include'] = subInclude;

return function(clbk) {
relation.modelTo.all(req, function(err, objsIncluded) {
for (var i = 0; i < objsIncluded.length; i++) {
delete keysToBeProcessed[objsIncluded[i][relation.keyTo]];
var objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
for (var j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) {
objectsFrom[j].__cachedRelations = {};
}
if (relation.multiple) {
if (!objectsFrom[j].__cachedRelations[relationName]) {
objectsFrom[j].__cachedRelations[relationName] = [];
req['where'][relation.keyTo] = {
inq: inValues
};
req['include'] = subInclude;

return function(clbk) {
relation.modelTo.all(req, function(err, objsIncluded) {
for (var i = 0; i < objsIncluded.length; i++) {
delete keysToBeProcessed[objsIncluded[i][relation.keyTo]];
var objectsFrom = objsByKeys[relation.keyFrom][objsIncluded[i][relation.keyTo]];
for (var j = 0; j < objectsFrom.length; j++) {
if (!objectsFrom[j].__cachedRelations) {
objectsFrom[j].__cachedRelations = {};
}
if (relation.multiple) {
if (!objectsFrom[j].__cachedRelations[relationName]) {
objectsFrom[j].__cachedRelations[relationName] = [];
}
objectsFrom[j].__cachedRelations[relationName].push(objsIncluded[i]);
} else {
objectsFrom[j].__cachedRelations[relationName] = objsIncluded[i];
}
objectsFrom[j].__cachedRelations[relationName].push(objsIncluded[i]);
} else {
objectsFrom[j].__cachedRelations[relationName] = objsIncluded[i];
}
}
}

// No relation have been found for these keys
for (var key in keysToBeProcessed) {
var objectsFromRelation = objsByKeys[relation.keyFrom][key];
for (var n = 0; n < objectsFromRelation.length; n++) {
if (!objectsFromRelation[n].__cachedRelations) {
objectsFromRelation[n].__cachedRelations = {};
// No relation have been found for these keys
for (var key in keysToBeProcessed) {
var objectsFromRelation = objsByKeys[relation.keyFrom][key];
for (var n = 0; n < objectsFromRelation.length; n++) {
if (!objectsFromRelation[n].__cachedRelations) {
objectsFromRelation[n].__cachedRelations = {};
}
objectsFromRelation[n].__cachedRelations[relationName] = relation.multiple ? [] : null;
}
objectsFromRelation[n].__cachedRelations[relationName] = relation.multiple ? [] : null;
}
}
clbk(err, objsIncluded);
});
};
clbk(err, objsIncluded);
});
};
}
return null;
}
return null;
}
}

/**
Expand Down Expand Up @@ -1120,54 +1120,54 @@ function defineScope(cls, targetClass, name, params, methods) {
return new targetClass(mergeParams(this._scope, {
where:data || {}
}).where);
}
}

function create(data, cb) {
if (typeof data === 'function') {
cb = data;
data = {};
function create(data, cb) {
if (typeof data === 'function') {
cb = data;
data = {};
}
this.build(data).save(cb);
}
this.build(data).save(cb);
}

/*
/*
Callback
- The callback will be called after all elements are destroyed
- For every destroy call which results in an error
- If fetching the Elements on which destroyAll is called results in an error
*/
function destroyAll(callback) {
targetClass.all(this._scope, function (err, data) {
if (err) {
cb(err);
} else {
(function loopOfDestruction (data) {
if(data.length > 0) {
data.shift().destroy(function(err) {
if(err && callback) callback(err);
loopOfDestruction(data);
});
} else {
if(callback) callback();
}
}(data));
}
});
}

function mergeParams(base, update) {
if (update.where) {
base.where = merge(base.where, update.where);
function destroyAll(callback) {
targetClass.all(this._scope, function (err, data) {
if (err) {
cb(err);
} else {
(function loopOfDestruction (data) {
if(data.length > 0) {
data.shift().destroy(function(err) {
if(err && callback) callback(err);
loopOfDestruction(data);
});
} else {
if(callback) callback();
}
}(data));
}
});
}

// overwrite order
if (update.order) {
base.order = update.order;
}
function mergeParams(base, update) {
if (update.where) {
base.where = merge(base.where, update.where);
}

return base;
// overwrite order
if (update.order) {
base.order = update.order;
}

}
return base;

}
}

AbstractClass.prototype.inspect = function () {
Expand Down
9 changes: 7 additions & 2 deletions lib/adapters/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ exports.initialize = function initializeSchema(schema, callback) {
if (!schema.settings.rs) {
schema.client = mongoose.connect(schema.settings.url);
} else {
schema.client = mongoose.connectSet(schema.settings.url, {rs_name: schema.settings.rs});
schema.client = mongoose.connectSet(schema.settings.url, {
rs_name: schema.settings.rs
});
}

schema.adapter = new MongooseAdapter(schema.client);
Expand Down Expand Up @@ -68,7 +70,10 @@ MongooseAdapter.prototype.define = function (descr) {

MongooseAdapter.prototype.defineForeignKey = function (model, key, cb) {
var piece = {};
piece[key] = {type: mongoose.Schema.ObjectId, index: true};
piece[key] = {
type: mongoose.Schema.ObjectId,
index: true
};
this._models[model].schema.add(piece);
cb(null, String);
};
Expand Down
Loading

0 comments on commit 4e45ebe

Please sign in to comment.