Ember relationships and mongo relationships

I’m trying to figure out the best approach to my Ember App.

I’m working with two models: events and bookings, and in my database, I structured they totally separate and independent from each other.

Schemas:

Events:

attributes:
  date        : Date
  description : String
  hiw         : Object
  hour        : Date
  meeting     : String
  men         : Number
  name        : String
  women       : Number

  createdAt   : Date
  updatedAt   : Date

  isActive    :
    type    : Boolean
    default : false

type:
  type    : String
  default : 'event'

Bookings:

attributes:
  email         : String
  idiom         : String
  name          : String
  obs           : String
  participants  : Number
  phone         : String
  sex           : String
  vip           : String

  createdAt    : Date
  updatedAt    : Date

  isActive     :
    type    : Boolean
    default : false

type:
  type    : String
  default : 'booking'

As I’m learning Ember, I discovered that ember has relationships (belongsTo and hasMany), and I would like to ask if I should restructure my schemas to deal with Ember data, or if I should make them independent as i’m doing.

The tendency of bookings is be progressively more complex over time…

The Ember data relationships is for this kind of stuff, or is to simple things like, comments in posts… etc. etc…

Thank you!

We have a noSQL database as well. I highly highly recommend you checkout ember-data-model-fragments. @slindberg et al. do a great job keeping it up to date with newer versions of Ember-Data.

Let my understand… hasMany and belongsTo is just to SQL databases??

I’m sorry, I should have explained it more. And I probably should have looked more closely at your schemas. I saw multiple levels of indentation and assumed you had nested entities which are common with noSQL.

hasMany and belongsTo are really intended for normalized entity relationships which are connected with foreign keys. In order to use belongsTo and/or hasMany you must have IDs for your entities. Your entities would be loaded with IDs in the relationship fields and ember-data would establish the connection when it was materializing the records. Here is a generic example:

{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever."
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John"
      }
    }
  ]
}

From: JSONApi

I wasn’t sure about the relationships in your case. Do bookings and events have a relationship?

HEII!!

They have!

One event has many bookings, and one booking belongs to one event. I know that my next question is out of scope… but perhaps you can help me with that… In order to make Ember hasMany and belongsTo work, i need send the data from my API already formatted right?

When i’m going to register my EventSchema, i just need put like this:

let Event = new Mongoose.Schema({
  attributes: {
    bookings: {
      type: Mongoose.Schema.ObjectId,
      ref: 'Booking'
    }
  }
});

And Ember will do all the hard work for me??

I mean… what i need to do in my schema to make it work? I was searching about how format my schema to work with ember data, but i’m not finding anything…

Thanks again!

1 Like

You don’t have to, but I’ll be honest it’ll make your life 1000 times easier if you do. Ember-Data requires that data pushed into its store be in JSON API format. However, they do offer pretty great APIs for you to marshal your data from your API into JSON API. This recent blog post is a great reference: Ember Data v1.13 Released

Yeap! I’d be happy to. I spent a few hours making this Ember Twiddle to hopefully demonstrate how powerful ember-data relationships can be. Ember Twiddle

Take note of the “instance-initializers/mockjax.js” file. If you don’t already know, mockjax allows you to stub out jQuery ajax calls to simulate server communication, that’s what I’m doing there. I have two modes, useSideloaded = true and useSideloaded = false. With sideloading, the server returns the event and all associated bookings. Ember-data takes over and builds the models with their relationships. In the case of no sideloading, Ember-data still sets up the relationships, but it does it lazily. So when someone does event.get('bookings') for the first time, it triggers the server call then. You’ll notice in the later mode, that there is a delay between the bookings table rendering and the content loading. This is where lazy ajax calls are made.

I hope that helps!

Shout out to @wecc for helping me figure out my typo and get this example working.