2

jugglingdb exposes two functions to create relational dbs: belongsTo and hasMany

i now aks myself how i might use this in daily development.

belongsTo and hasMany are adding functions to the objects, but as it seems there is no way to create relations between existing objects?

see: http://compoundjs.com/juggling.html#hasMany for an example.

i would like to not create the object but instead create linkages between existing objects, how will that work?

maybe i am just misinterpreting the functions?

have fun

jascha

ps: would be great if someone with 1500+ rep could create the jugglingdb tag and add it to this question? i really cant say if its relevant enough though.

robertklep
  • 198,204
  • 35
  • 394
  • 381

1 Answers1

3

When you, i.e. want to add articles for an existing author, you can do it like the following:

User.find(uid, function(err, user) {
        var article = user.articles.build({articleName : 'Article Foo'});
        article.save();
}); 

That will automatically create a foreign key for user inside the new created article.

David Geh
  • 307
  • 1
  • 2
  • 12
  • yeah, this part i figured out easily, but how can i add an existing article to an existing user? is there something like user.articles.add({_id: idOfExistingArticle) ? – Jascha Ehrenreich Apr 27 '13 at 20:43
  • Oh, I'm sorry that I didn't get that. I always do it the other way round, and set the belongsTo from the Article, with the setter function: "existingArticle.mainUser(existingUser);". – David Geh May 02 '13 at 18:28
  • thats pretty nice :) to be honest i switched back to a pure mongoose install, but if you want to incorporate this into your answer i will accept it :) – Jascha Ehrenreich May 03 '13 at 21:24