What is the correct way to set the user attribute primaryKey in the latest ember data assembly?

I saw several ways to do this, but I'm curious that the โ€œmost correctโ€ approach uses the last line (master parity) of the data ember-data rev 12

Option number 1 (looks the most obvious)

App.Post = DS.Model.extend({ primaryKey: '_id', _id: DS.attr('string') }); 

Option number 2 (match it using the adapter by type)

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

Option number 3 (hard code in the serializer - all models have the same user pk)

 App.MySerializer = DS.RESTSerializer.extend({ primaryKey: function(type) { return '_id'; } }); 
+4
source share
3 answers

The answer is option # 2, because the adapter is a component related to it. The adapter needs to know which property is the primary key. This is not important for the model or serializer.

+2
source

This question is very old, but it has no accepted answer, and it appeared on google for me.

The current answer is to install this on a serializer ( https://guides.emberjs.com/v3.0.0/models/customizing-serializers/#toc_ids )

 // app/serializers/application.js import DS from 'ember-data'; export default DS.JSONAPISerializer.extend({ primaryKey: '_id' }); 
+1
source

as indicated in this thread, try https://github.com/toranb/ember-data-django-rest-adapter/issues/14

 YourRestadapter.configure('App.Person', { primaryKey: 'slug' }); 

YourRestadapter = optional adapter that you use in your project (e.g. DS.RESTAdapter)

0
source

All Articles