Comparing version 0.2.18 to 0.2.19
@@ -129,19 +129,17 @@ var eventEmitter = require('events').EventEmitter; | ||
} | ||
if (doc.getDocSettings().saved !== true) { | ||
doc.getDocSettings().saved = true; | ||
doc.getDocSettings().saved = true; | ||
// Update the parent | ||
if ((parentDoc != null) && (joinClause != null)) { | ||
if (joinType === 'hasOne') { | ||
parentDoc[joinClause.leftKey] = result.new_val[joinClause.rightKey] | ||
// Update the parent | ||
if ((parentDoc != null) && (joinClause != null)) { | ||
if (joinType === 'hasOne') { | ||
parentDoc[joinClause.leftKey] = result.new_val[joinClause.rightKey] | ||
} | ||
/* | ||
else if (joinType === 'hasMany') { | ||
if (parentDoc[joinClause.leftKey] == null) { | ||
parentDoc[joinClause.leftKey] = []; | ||
} | ||
/* | ||
else if (joinType === 'hasMany') { | ||
if (parentDoc[joinClause.leftKey] == null) { | ||
parentDoc[joinClause.leftKey] = []; | ||
} | ||
parentDoc[joinClause.leftKey].push(result.new_val[joinClause.rightKey]) | ||
} | ||
*/ | ||
parentDoc[joinClause.leftKey].push(result.new_val[joinClause.rightKey]) | ||
} | ||
*/ | ||
} | ||
@@ -166,3 +164,3 @@ count.saved++; | ||
} | ||
else { // nextStep === 'saveParent' | ||
else if (nextStep === 'saveParent') { | ||
// Need to emit other events... | ||
@@ -249,2 +247,5 @@ if (result != null) { | ||
var savedDocs = options.savedDocs || []; | ||
savedDocs.push(this); | ||
var self = this; // The document | ||
@@ -269,17 +270,26 @@ var model = this.getModel(); | ||
if ((model.joins[key].type === 'hasOne') && (typeof self[key] === 'object') && (self[key] != null)) { | ||
count.toSave++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallback({ | ||
doc: self[key], | ||
callback: callback, | ||
count: count, | ||
parentDoc: self, | ||
join: model.joins[key], | ||
nextStep: 'saveMain' | ||
}) | ||
var saved = false; | ||
for(var i=0; i<savedDocs.length; i++) { | ||
if (savedDocs[i] === self[key]) { | ||
saved = true; | ||
break; | ||
} | ||
} | ||
else { | ||
//TODO Implement join with lambda functions | ||
if (saved === false) { | ||
count.toSave++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallback({ | ||
doc: self[key], | ||
callback: callback, | ||
count: count, | ||
parentDoc: self, | ||
join: model.joins[key], | ||
nextStep: 'saveMain' | ||
}) | ||
} | ||
else { | ||
//TODO Implement join with lambda functions | ||
} | ||
self[key].save({saveJoin: true, savedDocs: savedDocs}, cb); | ||
} | ||
self[key].save({saveJoin: true}, cb); | ||
} | ||
@@ -352,19 +362,54 @@ } | ||
for(var i= 0; i<self[key].length; i++) { | ||
count.toSave++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallback({ | ||
doc: self[key][i], | ||
callback: callback, | ||
count: count, | ||
nextStep: 'saveParent', | ||
parentDoc: self | ||
}) | ||
var saved = false; | ||
for(var j=0; j<savedDocs.length; j++) { | ||
if (savedDocs[j] === self[key][i]) { | ||
saved = true; | ||
} | ||
} | ||
else { | ||
//TODO Implement join with lambda functions | ||
if (saved === false) { | ||
count.toSave++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallback({ | ||
doc: self[key][i], | ||
callback: callback, | ||
count: count, | ||
nextStep: 'saveParent', | ||
parentDoc: self | ||
}) | ||
} | ||
else { | ||
//TODO Implement join with lambda functions | ||
} | ||
self[key][i].save({saveHasOne: true, saveMain: true, saveHasMany: true, savedDocs: savedDocs}, cb); | ||
} | ||
self[key][i].save({saveHasOne: true, saveMain: true, saveHasMany: true}, cb); | ||
} | ||
} | ||
} | ||
else if (model.joins.hasOwnProperty(key) === true) { | ||
if ((model.joins[key].type === 'hasOne') && (typeof self[key] === 'object') && (self[key] != null)) { | ||
var otherModel = self[key].getModel() | ||
for(var otherKey in self[key]) { | ||
if (self[key][otherKey] === self) { | ||
if ((self[key][otherModel.joins[key].joinClause.leftKey] != self[otherModel.joins[key].joinClause.rightKey]) && (self[otherModel.joins[key].joinClause.rightKey] != null)) { | ||
// There is a mismatch between joinKeys, that can happen if a document is linked with another document of the same model in two ways | ||
self[key][otherModel.joins[key].joinClause.leftKey] = self[otherModel.joins[key].joinClause.rightKey]; | ||
count.toSave++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallback({ | ||
doc: self[key], | ||
callback: callback, | ||
count: count, | ||
nextStep: 'saveParent', | ||
parentDoc: self | ||
}) | ||
} | ||
self[key].save({saveHasOne: false, saveMain: true, saveHasMany: false, savedDocs: savedDocs}, cb); | ||
} | ||
break; | ||
} | ||
} | ||
// if joined doc hasOne relation with self and leftKey is undefined, fire an update | ||
} | ||
} | ||
} | ||
@@ -371,0 +416,0 @@ } |
{ | ||
"name": "thinky", | ||
"version": "0.2.18", | ||
"version": "0.2.19", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1190,4 +1190,25 @@ var thinky = require('../lib/index.js'); | ||
it('should work for 1-1 relations for the same model -- even for pairs)', function(done) { | ||
var Human = thinky.createModel('Human', {id: String, name: String, idBestFriend: String}); | ||
Human.hasOne(Human, 'bestFriend', {leftKey: 'idBestFriend', rightKey: 'id'}); | ||
var michel = new Human({name: "Michel"}); | ||
var laurent = new Human({name: "Laurent"}); | ||
michel['bestFriend'] = laurent; | ||
laurent['bestFriend'] = michel; | ||
michel.save( {saveJoin: true}, function(error, firstResult) { | ||
Human.get(michel.id).getJoin().run(function(error, result) { | ||
should.not.exist(error); | ||
should.exist(result.id); | ||
should.exist(result.idBestFriend); | ||
should.exist(result.bestFriend.id); | ||
should.exist(result.bestFriend.idBestFriend); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should work for n-n relations', function(done) { | ||
@@ -1194,0 +1215,0 @@ var Cat = thinky.createModel('Cat', {id: String, name: String}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
957828
20442