Composite primary keys


#1

Hi,

Is it possible to use ember-data with https://github.com/composite-primary-keys/composite_primary_keys ?

I’m modelling a tennis match and for games the setNr and GameNr represent a unique game always. Some goes for sets, a Setnr and a GameId would always be unique.

Not a big deal if it’s not possible but it would make my database schema little cleaner :slight_smile:


#2

I think you could create a serializer that handles that for you. Unless I’m misunderstanding, Ember data doesn’t have to know what a composite key is — it just needs to know how to deserialize it, correct? If so, the extractId hook might be the ticket.

Here’s how you might implement:

// serializers/composite-key.js
export default DS.JSONAPISerializer.extend({
  compositeKeys: ['setNr', 'GameNr'],

  exractId(modelClass, resourceHash) {
    return this.compositeKeys.map((key) => resourceHash.attributes[key]).join('-');
  }
});

#3

Interesting, thanks! i’m gonna try that out and report back here.