Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make id datatype configurable #4477

Closed
hezus opened this issue Jul 19, 2016 · 3 comments
Closed

Make id datatype configurable #4477

hezus opened this issue Jul 19, 2016 · 3 comments

Comments

@hezus
Copy link

hezus commented Jul 19, 2016

RELATED: #4166

Assuming a ID is always a string feels very opinionated towards API's which use a UUID.

Wouldn't it be better to set a type for the id? I use ember for many project but most times you work against a REST API which expect the ID to be a integer corresponding to a certain database ID.

It would be nice if the ID datatype is configurable, so it is always a string or integer depending on the settings.

A dynamic typed language like Ruby or Javascript doesn't really care, but whenever I work with a strong typed API this is always a hassle, especially if you work with embedded objects.

@pangratz pangratz added the id label Aug 16, 2016
@fivetanley
Copy link
Member

Hi @hezus,

The reason we don't make it configurable is that id is heavily used internally. Having a consistent type means we have less type errors and keeps the internals a bit more clean. Numbers also get converted to strings when put inside an object in JavaScript (e.g. {}).

If your server only accepts a strict Number type, you could override serialize:

export default DS.JSONAPISerialize.extend({
  serialize: function(snapshot, options) {
    var json = this._super(snapshot, options);
    json.id = parseInt(json, 10);
    return json
  }
});

@fivetanley
Copy link
Member

Actually for symmetry we should probably have a serializeId hook as we have an extractId hook.

@pangratz
Copy link
Member

I am closing this issue since #4620 has been merged: having the ds-serialize-id feature enabled, you can now overwrite serializeId and customize how the id should be serialized into the hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants