perak / meteor-joins

Collection joins for Meteor
77 stars 7 forks source link

How do you relate two collections? #1

Closed Ajaxsoap closed 8 years ago

Ajaxsoap commented 9 years ago

Hi Perak, I have a scenario, I have two collections. enrollments collection claims collection

I have enrollees list in enrollments collection. How do I pass a certain fields from enrollments collection to claims collection with additional fields?

Example Enrollments collection

NameBirthdateStatus
John 02/02/1990 Single Claim me!
Peter 02/02/1980 Married Claim me!

Let say i placed a claims button (Claim me!) per enrollee in the list, then if I click it, it will route me to add claims, wherein the fields from that particular enrollee will be carried/passed it over plus additional fields(amount,dateofClaims) to the claims collection upon clicking the submit button.

Voila! Claims list.

Claims collection

NameBirthdateStatusClaims AmountDate Claimed
John 02/02/1990 Single $500 06/06/2015

How do i use the generator for this kind of scenario?

Thanks.

perak commented 9 years ago

Sorry for too late answer :(

Here is how (using meteor-kitchen):

First, you need to define both collections in kitchen input .json:

...
"collections": [
  {
    "name": "enrollments",
    "fields": [
      { "name": "name", "title": "Name" },
      { "name": "birthdate", "title": "Birthdate", "type": "date", "input": "datepicker", "required": true },
      { "name": "status", "title": "Status", "input": "select", "input_items": ["Single", "Married"], "required": true }
    ]
  },

  {
    "name": "claims",
    "fields": [
      {
        "name": "enrollmentId",
        "join_collection": "enrollments",
        "join_container": "enrollment",
        "join_fields": [ "name", "birthdate", "status" ]
      },
      { "name": "ammount", "title": "Claims ammount", "type": "float", "required": true },
      { "name": "date", "title": "Date claimed", "type": "date", "input": "datepicker", "required": true }
    ]
  }
]

Now, if you do Claims.find(), you'l get something like:

{
  _id: "5h6b3gh2h3nh6b76b7nhj5",
  enrollmentId: "1g5v2nr45bv6hy5j3b65",
  enrollment: {
    name: "John",
    birthdate: "02/02/1990",
    status: "Single"
  },
  ammount: 500.00,
  date: "06/06/2015"
}

Data from enrollment collection is contained in "enrollment" member (defined in claims collection property "join_container"), and you'l have fields defined by "join_fields" (if you don't specify "join_fields", you'l get all fields from enrollment document.

That's it.

Ajaxsoap commented 9 years ago

Hi Perak,

Sorry, I overlooked this issue as i am busy with my projects.

This is exactly what i want in joining my collections, I want to pass all the fields from enrollee to claims upon claiming the insurance, so that when i checked the details of a claimant's, i will see all the information on his/her enrollment. I will now close this issue.

Thanks Perak! :+1:

perak commented 9 years ago

Great! Glad it helped! :+1:

Ajaxsoap commented 9 years ago

One quick question, assuming the joining of enrollments and claims is successful, then every update will be made on enrollments will reflect in claims joined collection? Something like observed changes.

perak commented 9 years ago

There is a pull request in this repo that should make joins reactive. I didn't reviewed the code and didn't merged yet - being super-busy with other thing, I hope I'l do it in next 12 hours.

Ajaxsoap commented 9 years ago

:+1:

Ajaxsoap commented 9 years ago

I just realized that I am using aldeed:simple schema, is there a way on how to inject your joins package on a simple schema based collections? I see that it's only applicable for a non-schema collections?