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

Add cookbook page about model names convention #9394

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions guides/cookbook/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Cookbook

- [Incremental Adoption Guide](./incremental-adoption-guide.md)
- [Model Name: singular or plural? What to choose? Why is that?](./model-name.md)
Baltazore marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions guides/cookbook/model-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Model Name: singular or plural? What to choose? Why is that?

- ⮐ [Cookbook](./index.md)

## Model name convention (or why it was singular)
Baltazore marked this conversation as resolved.
Show resolved Hide resolved

If you have been working with EmberData for a while, you might remember convention about model names. It was a convention that model names should be singular. But why is that? Why not plural?
Baltazore marked this conversation as resolved.
Show resolved Hide resolved

There is no strict rule about model names. You was just using singular names because you had default Serializer configured in your app. It was doing all job of data normalization for you. Mainly it was singularizing `type` of response you had received from server.
Baltazore marked this conversation as resolved.
Show resolved Hide resolved

## So what to choose?

When using EmberData without Legacy setup, you are responsible for data normalization. You can choose whatever you want. You can use singular or plural names. It is up to you. Or up to your backend to be precise, as it would be beneficial for you to not do all that normalization on frontend. Just have it as a part of API contract of your app. But remember, you need to be consistent. If you choose singular names, stick with it. If you choose plural names, stick with it.
Baltazore marked this conversation as resolved.
Show resolved Hide resolved

## But what about JSON:API spec?

It's pretty simple, JSON:API spec agnostic about the `type` field convention. Here is the quote from the spec:

> Note: This spec is agnostic about inflection rules, so the value of type can be either plural or singular. However, the same value should be used consistently throughout an implementation.

You can read more about it in the [JSON:API spec](https://jsonapi.org/format/#document-resource-object-identification).

---

- ⮐ [Cookbook](./index.md)
Loading