Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many to many relationships #8

Closed
chathuraa opened this issue Sep 6, 2017 · 4 comments
Closed

Many to many relationships #8

chathuraa opened this issue Sep 6, 2017 · 4 comments
Labels

Comments

@chathuraa
Copy link

Hi Simon,

What is the best way to have many to many relationships? at which point does this cache the doc, on insert?

Thanks,
Chat.

@Herteby
Copy link
Collaborator

Herteby commented Sep 6, 2017

It might be a bit unclear, but "one" and "inverse" are actually many-to-one (with "one", a parent can only have one child, but many parents could have the same child).

"many" and "many-inverse" are many-to-many. Which one to use depends on if you're storing the _ids on the children or the parents.

There are 5 update hooks: parent.insert, parent.update, child.insert, child.update and child.remove

Basically you should always be able to rely on the caches being updated no matter what you do. If they're not, that should be considered a bug.

Caches are updated immediately and synchronously, so you could do:

Posts.insert({_id:'post1', authorId:'user1'})
Posts.findOne('post1').author //will contain the cached user

There is one exception: the cacheField() update is always deferred, to work properly with "recursive caching"

@chathuraa
Copy link
Author

chathuraa commented Sep 8, 2017

Hey Simon,

I'm struggling to get this working, I might be missing something. This is my code. What am I doing wrong?

import Allocations from '../api/allocations';

const Appointments = new Mongo.Collection('appointments');
// Appointments.attachSchema(AppointmentsFullSchema);
if (Meteor.isServer) {
  Appointments._ensureIndex({
    workorderNo: 1,
    extWorkorderRef1: 1,
    extWorkorderRef2: 1,
  });
  Meteor.publish('appointments', () => Appointments.find({}));
  Appointments.cache({
    type: 'one',
    collection: Allocations,
    fields: ['_id', 'date', 'from', 'to'],
    referenceField: 'appointmentId',
    cacheField: '_scheduledFor',
    bypassSchema: true,
  });
}

@Herteby
Copy link
Collaborator

Herteby commented Sep 8, 2017

Since the referenceField is "appointmentId", I'm guessing this is actually an "inverse" relationship?

@chathuraa
Copy link
Author

chathuraa commented Sep 8, 2017 via email

@Herteby Herteby closed this as completed Nov 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants