Code Monkey home page Code Monkey logo

Comments (11)

samselikoff avatar samselikoff commented on May 22, 2024

Thanks for the background, and this totally makes sense to me.

What if someone's API sends up json with an id field as an int? Would they not be able to mock our their server using mirage?

from ember-cli-mirage.

lukemelia avatar lukemelia commented on May 22, 2024

Good question. I suppose another option is to simply not transform IDs at all, and put the onus on users to be consistent.

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 22, 2024

I think the default should be, coerce everything to string, and have a config option that disables coercion.

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 22, 2024

although, looking at all our rails apps, they definitely serialize ids as ints. I'd imagine this would be the default for most frameworks?

maybe we should default coerce to ints, but have option for coerce to strings, or disable coercion completely.

I basically don't want people to have to do schema.user.find(+id) or schema.user.find('' + id) all over the place.

from ember-cli-mirage.

lukemelia avatar lukemelia commented on May 22, 2024

@samselikoff an option seems OK to me.

from ember-cli-mirage.

lukemelia avatar lukemelia commented on May 22, 2024

I think conceptual problem here is that when a find operation occurs in a back-end framework, the framework is aware of the stored data type of the ID, and can coerce the incoming string query param value accordingly. That coercion may even vary from model to model. But mirage has no such understanding of the underlying storage type today and so cannot intelligently coerce.

IMO, coercing IDs to strings in all cases would be the most robust solution. Developers who want numeric IDs in their serialized responses can customize the response (eventually through a serializer).

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 22, 2024

Makes sense. Seems like serializers are the answer, and when those land
mirage can do whatever it wants internally (coerce to string). Thanks for
input!
On Fri, May 15, 2015 at 9:17 AM Luke Melia [email protected] wrote:

I think conceptual problem here is that when a find operation occurs in a
back-end framework, the framework is aware of the stored data type of the
ID, and can coerce the incoming string query param value accordingly. That
coercion may even vary from model to model. But mirage has no such
understanding of the underlying storage type today and so cannot
intelligently coerce.

IMO, coercing IDs to strings in all cases would be the most robust
solution. Developers who want numeric IDs in their serialized responses can
customize the response (eventually through a serializer).


Reply to this email directly or view it on GitHub
#67 (comment)
.

from ember-cli-mirage.

yankeeinlondon avatar yankeeinlondon commented on May 22, 2024

I believe I'm running into this too. What's the best way for us string-id'd folks to proceed? Is there a work around (beyond just pulling all records from DB and doing the filter myself which I guess I could do)?

Even with the long form I can't coerce to string:

this.get('/activities/:id', function(db, request) {
  const id = String(request.params.id);

  return {
    activity: db.activities.find(id)
  };
});

from ember-cli-mirage.

yankeeinlondon avatar yankeeinlondon commented on May 22, 2024

BTW, I also wonder if the shorthand should be a bit more robust for singular find operations because when it search for a numeric id and doesn't find it returns undefined and ember-data doesn't handle this gracefully. See below:

2015-06-07_22-43-53_05

Obviously if I went with the long form I could handle this quite easily myself but I'm a sucker for the short form. :)

from ember-cli-mirage.

samselikoff avatar samselikoff commented on May 22, 2024

Yah I agree with your points. I'd say for now, can you just use int ids? Ultimately how the id is stored in mirage's db isn't super important, it's more how it comes across the wire. Right now it could be as int, with serializers it will be whatever you want. But if you use int right now, you can use shorthands without too much work.

You're correct in the fail case. What should it be, a 404?

from ember-cli-mirage.

yankeeinlondon avatar yankeeinlondon commented on May 22, 2024

For the data model I'm referring to here I guess I can do that, many of my other models are non-numeric but I'm guessing in these cases that Mirage will not attempt to coerce it to integer, right? Sorry just getting started in mirage so I'll know the answer to this soon. In any event, I think you'll find a lot more people with non-numeric id's when their backend store is a nosql database (it has a tendency to exert this influence on records unlike SQL which the golden rule was always use integer).

WRT to handling of the "undefined" condition, I think a 404 makes sense.

from ember-cli-mirage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.