Merge pull request #192 from robblovell/hasmany

Hasmany
This commit is contained in:
Anatoliy Chakkaev 2013-01-21 07:42:01 -08:00
commit 1010d5d64d
3 changed files with 42 additions and 0 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@ doc
coverage.html
coverage
v8.log
.idea**
.DS_Store

View File

@ -841,6 +841,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) {
return {where: x};
}, {
find: find,
all: all,
destroy: destroy
});
@ -859,6 +860,23 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) {
}.bind(this));
}
function all(filter,cb) {
if (!filter) {
filter = {};
}
if (!filter.where) {
filter.where = {};
}
filter.where[fk] = this.id.toString();
anotherClass.all(filter,function(err,inst){
if (err) return cb(err);
if (!inst) return cb(null,[]);
else return cb(null,inst);
}.bind(this));
}
function destroy(id, cb) {
this.find(id, function (err, inst) {
if (err) return cb(err);

View File

@ -542,6 +542,29 @@ function testOrm(schema) {
it('hasMany should be cached', function (test) {
//User.create(function (e, u) {
// u.posts.create({}, function (e, p) {
// find all posts for a user.
/* User.all(function (err,users) {
for (var i=0;i<users.length;i++) {
u = users[i];
Posts.find(user.id, function(err, posts) {
// now check to see that the user has these posts testing the all method of hasMany.
u.posts.all(null,function(err, uposts) {
test.equal(posts.length,uposts.length);
if (post.length == uposts.length) {
for (var j=0;j<uposts.length;j++) {
for (var k= 0,found=false;k<posts.length;k++) {
if (uposts[j] == uposts[k].id) { found = true; break; }
}
if (!found) test.equal(1,0); // not familliar with test framework here... test.fail()?
}
}
})
})
// find the posts with this user id.
// find the posts of the user.
}
})*/
// Finding one post with an existing author associated
Post.all(function (err, posts) {
// We try to get the first post with a userId != NULL