jeanfredrik / meteor-denormalize

Provides simple methods for common denormalization tasks
13 stars 5 forks source link

How to remove all of reference records, if the primary is removed? #5

Closed thearabbit closed 9 years ago

thearabbit commented 9 years ago

How to remove all of reference records, if the primary is removed?

jeanfredrik commented 9 years ago

This package doesn't handle cascade delete of related documents. You can do it yourself with collection-hooks. I think there are some relationship packages too you can use.

thearabbit commented 9 years ago

I think that if the primary field is removed, so the reference field should be removed too. Because the reference field will can't get data. I would like to request you for add like this:

Comments.cacheDoc('post', Posts, ['title'], {
    referenceField: 'postId',
    cacheField: 'cachedPost',
    removal: true // false for default
});

What do you think?

thearabbit commented 9 years ago

After remove, if I insert the same primary fields again it update the cachDoc or not?

jeanfredrik commented 9 years ago

Yes, if you insert a document with a primary field that matches another document's reference field, the cache field will update. So you can insert a comment before a post, like this:

Comments.insert({
    _id: 'comment1',
    post_id: 'post1'
});
// Comments.findOne('comment1')._post == undefined

Posts.insert({
    _id: 'post1',
    …
});
// Comments.findOne('comment1')._post == {…}