15

I've been struggling for the past few days with primary keys and the last version of Ember Data.

I first read how to do it on the Breaking Changes file on GitHub, but it's apparently outdated. I tried several other ways (with the help of Peter Wagenet on IRC), but none of them seem to work.

I would like to make slug my primary key on my model, and also since I'm working with MongoDB, I would like to use _id instead of id.

Has anyone figured out how to do this? My underlying problem is that model records get loaded twice when I do several App.MyModel.find() on the model.

Ken Browning
  • 28,693
  • 6
  • 56
  • 68
Benjamin Netter
  • 1,501
  • 3
  • 18
  • 34

5 Answers5

9

As of Ember Data 1.0 beta you define primaryKey on the respective serializer.

For the entire app

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

For a single type

App.FooSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

You will still refer to it as id on the model, but Ember Data will serialize/deserialize it to _id during transfer.

Example: http://emberjs.jsbin.com/OxIDiVU/635/edit

Read More about it here: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#property_primaryKey

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
3

I would like to make slug my primary key on my model, and also since I'm working with MongoDB, I would like to use _id instead of id.

Use the adapter's map API to specify the attribute that should be used as primary key:

App.Adapter.map('App.Person', {
  primaryKey: '_id'
});

See serializer.js api docs for detail. If you need to further customize how the records id is serialized, use the addId hook described here.

Since ember-data is still under active development, documentation on this feature is somewhat limited and may change before 1.0 release. In the meantime refer to the ember-data test suite to see examples of this in action. For example:

Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
  • Last 2 links should point to: https://github.com/emberjs/data/blob/master/tests/integration/serializers/json-serializer-test.js – Def_Os Apr 25 '16 at 18:16
1

In case the solution suggested by Nikita doesn't work (didn't for me using revision 11 of ember-data), here is how I changed the primary key when working with the RESTAdapter:

App.Adapter = DS.RESTAdapter.extend({
    serializer: "App.MySerializer"
});

// notice we extend the RESTSerializer, not Serializer!
App.MySerializer = DS.RESTSerializer.extend({
    primaryKey: function(type) {
        return '_id'; // you get it...
    }
});

I'm aware that this probably won't help the OP anmore but I still post it as it may be helpful for some of you.

iStefo
  • 418
  • 3
  • 9
0

Try to extend your adapter like this:

   App.RESTSerializer = DS.RESTSerializer.extend({
      init: function() {
        this._super();

        this.map('App.MyModel', {
          primaryKey: "_id"
        });
      }
    });
xamenrax
  • 1,724
  • 3
  • 27
  • 47
0

I use MongoDB and Ember-Data 1.0.0-beta.6 in my application and the _id posed a problem in Ember 1.4.0 for me too. Here's what I've done to solve the problem, assuming the returned JSON array is nested in the root property "people":

App.ApplicationSerializer = DS.RESTSerializer.extend({
    normalizeHash: {
        people: function(hash) {
            hash.id = hash._id;
            delete hash._id;
            return hash;
        }
    }
});

This is, of course, an application-wide serializer but you can limit it to a specific path with something like App.MyPathSerializer = DS.RESTSerializer.extend({ ... });

ashraf
  • 537
  • 7
  • 16