Corner-Cases in Ember Data Modeling

Kyle Coberly

www.kylecoberly.com

Why Do You Need This?

Schema-less back-end means schema-full front-end. In other words...

You're the database designer now, dawg!

One-To-Many

This is the most common relationship between two kinds of data.

But What Other Relationships Are There?

Self-Joins

Use when data has relationships with data just like it.

In Ember:

Use "belongsTo" to the same table.

// Employee Model
export default DS.Model.extend({
    firstName: DS.attr(),
    lastName: DS.attr(),
    manager: DS.belongsTo("employee")
});

Extension Tables

Use when there are multiple types of something that share some attributes

In Ember:

Import the base model and extend it.

// Employee Model
export default DS.Model.extend({
    firstName: DS.attr(),
    lastName: DS.attr()
});

// Manager Model
import Employee from "employee";
export default Employee.extend({
    passedBackgroundCheck: DS.attr("boolean")
});

// Contractor Model
import Employee from "employee";
export default Employee.extend({
    agencyName: DS.attr()
});

Linking Tables

Use to add properties to the "join" of a many-to-many relationship

In Ember:

Use two hasMany relationships.

// Student Model
export default DS.Model.extend({
    firstName: DS.attr(),
    lastName: DS.attr(),
    registrations: DS.hasMany("registration")
});

// Registration Model
export default DS.Model.extend({
    date: DS.attr("date"),
    isRefundable: DS.attr("boolean")
});

// Course Model
export default DS.Model.extend({
    name: DS.attr(),
    registrations: DS.hasMany("registration")
});

Polymorphic Joins

Use when joining to something that could take multiple forms.

In Ember:

Use the {polymorphic: true} option.

// CourseUnit Model
export default DS.Model.extend({
    title: DS.attr(),
    pages: DS.hasMany("page", {polymorphic: true})
});

// Page Model
export default DS.Model.extend({
    headerText: DS.attr()
});

// TextPage Model
import Page from 'page';
export default Page.extend({
    textContent: DS.attr()
});

// ImagePage Model
import Page from 'page';
export default Page.extend({
    imageUrl: DS.attr()
});

Thanks!

www.kylecoberly.com