-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add model definition comparing EmberData models with SchemaRecord #20
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
Merged
jaredgalanis
merged 7 commits into
ember-learn:main
from
MehulKChaudhari:add/model-schemaRecord-comparison
Apr 28, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa1e95f
fix: update docs to reflect correct record handling and command usage
MehulKChaudhari b2a16bd
request changes
MehulKChaudhari 122b7a5
Add: comparison between EmberData models and SchemaRecord
MehulKChaudhari a5bbd1c
Merge branch 'main' into add/model-schemaRecord-comparison
MehulKChaudhari 827773b
Update snippets/models/model-definition/new.js
MehulKChaudhari 9b73606
Update snippets/models/model-definition/new.js
MehulKChaudhari 4e84032
Update snippets/models/model-definition/new.js
MehulKChaudhari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { withDefaults } from '@warp-drive/schema-record'; | ||
|
||
// Register schemas for all resources at once | ||
store.schema.registerResources([ | ||
// User schema | ||
withDefaults({ | ||
type: 'user', | ||
fields: [ | ||
{ kind: 'field', name: 'firstName' }, | ||
{ kind: 'field', name: 'lastName' }, | ||
{ | ||
kind: 'derived', | ||
name: 'name', | ||
type: 'concat', | ||
options: { fields: ['firstName', 'lastName'], separator: ' ' } | ||
}, | ||
{ | ||
kind: 'hasMany', | ||
name: 'pets', | ||
type: 'pet', | ||
options: { | ||
async: false, | ||
inverse: 'owner', | ||
polymorphic: true | ||
} | ||
} | ||
] | ||
}), | ||
|
||
// Pet schema | ||
withDefaults({ | ||
type: 'pet', | ||
fields: [ | ||
{ kind: 'field', name: 'name' }, | ||
{ | ||
kind: 'belongsTo', | ||
name: 'owner', | ||
type: 'user', | ||
options: { | ||
async: false, | ||
inverse: 'pets' | ||
MehulKChaudhari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
] | ||
}), | ||
|
||
// Dog schema (extends pet) | ||
withDefaults({ | ||
type: 'dog', | ||
fields: [ | ||
{ kind: 'field', name: 'breed' }, | ||
{ | ||
kind: 'belongsTo', | ||
name: 'owner', | ||
type: 'user', | ||
options: { | ||
async: false, | ||
inverse: 'pets', | ||
as: 'pet' | ||
MehulKChaudhari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
] | ||
}) | ||
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Traditional EmberData Model Definition | ||
import Model, { attr, hasMany, belongsTo } from '@ember-data/model'; | ||
|
||
export default class UserModel extends Model { | ||
@attr('string') firstName; | ||
@attr('string') lastName; | ||
@hasMany('pet', { polymorphic: true, inverse: 'owner' }) pets; | ||
|
||
get name() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
} | ||
|
||
// Related models would be defined in separate files: | ||
|
||
// export default class PetModel extends Model { | ||
// @attr('string') name; | ||
// @belongsTo('user', { inverse: 'pets' }) owner; | ||
// } | ||
|
||
// export default class DogModel extends PetModel { | ||
// @attr('string') breed; | ||
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: Models | ||
description: See how traditional EmberData models compare to the new SchemaRecord approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: Model Definition | ||
description: See how to define resources using the traditional EmberData models and the new SchemaRecord approach. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.