From fa1e95f8e91c388910b811a611742d406ae481a6 Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Sun, 23 Feb 2025 01:12:37 +0530
Subject: [PATCH 1/6] fix: update docs to reflect correct record handling and
command usage
---
CONTRIBUTING.md | 2 +-
snippets/updating-data/create-record/handler.js | 9 +++++----
snippets/updating-data/create-record/in-place-body.js | 3 +--
snippets/updating-data/create-record/new.js | 3 +--
snippets/updating-data/save-record/handler.js | 7 +++----
snippets/updating-data/save-record/new.js | 3 +--
.../components/guide-section/subsection-test.js | 2 +-
translations/updating-data/create-record/en-us.yaml | 2 +-
8 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 16d65af..4580220 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -25,7 +25,7 @@ This is an Ember app, so the usual steps follow:
```
1. Run the app.
```bash
- ember serve
+ npm run start
```
1. Visit your app at [http://localhost:4200](http://localhost:4200).
diff --git a/snippets/updating-data/create-record/handler.js b/snippets/updating-data/create-record/handler.js
index 7e5e0f6..56baa52 100644
--- a/snippets/updating-data/create-record/handler.js
+++ b/snippets/updating-data/create-record/handler.js
@@ -18,10 +18,11 @@ const updatesHandler = {
const { data, store } = context.request;
const newRequestParams = Object.assign({}, context.request, {
- body: serializeResources(
- store.cache,
- recordIdentifierFor(data.record)
- )
+ body: JSON.stringify(
+ serializeResources(
+ store.cache,
+ data.record
+ ))
});
return next(newRequestParams);
}
diff --git a/snippets/updating-data/create-record/in-place-body.js b/snippets/updating-data/create-record/in-place-body.js
index 2abd488..ab612f8 100644
--- a/snippets/updating-data/create-record/in-place-body.js
+++ b/snippets/updating-data/create-record/in-place-body.js
@@ -1,4 +1,3 @@
-import { recordIdentifierFor } from '@ember-data/store';
import { createRecord, serializeResources } from '@ember-data/json-api/request';
const record = store.createRecord('user', {});
@@ -7,7 +6,7 @@ const request = createRecord(record);
request.body = JSON.stringify(
serializeResources(
store.cache,
- recordIdentifierFor(record)
+ record
)
);
diff --git a/snippets/updating-data/create-record/new.js b/snippets/updating-data/create-record/new.js
index 951f335..589b3dd 100644
--- a/snippets/updating-data/create-record/new.js
+++ b/snippets/updating-data/create-record/new.js
@@ -1,4 +1,3 @@
-import { recordIdentifierFor } from '@ember-data/store';
import { createRecord, serializeResources } from '@ember-data/json-api/request';
const record = store.createRecord('user', {});
@@ -6,7 +5,7 @@ const request = createRecord(record);
request.body = JSON.stringify(
serializeResources(
store.cache,
- recordIdentifierFor(record)
+ record
)
);
diff --git a/snippets/updating-data/save-record/handler.js b/snippets/updating-data/save-record/handler.js
index 125b343..1a90e29 100644
--- a/snippets/updating-data/save-record/handler.js
+++ b/snippets/updating-data/save-record/handler.js
@@ -1,5 +1,4 @@
// Create handler for serialization of any record
-import { recordIdentifierFor } from '@ember-data/store';
import { serializeResources } from '@ember-data/json-api/request';
const updatesHandler = {
@@ -18,10 +17,10 @@ const updatesHandler = {
const { data, store } = context.request;
const newRequestParams = Object.assign({}, context.request, {
- body: serializeResources(
+ body: JSON.stringify(serializeResources(
store.cache,
- recordIdentifierFor(data.record)
- )
+ data.record
+ ))
});
return next(newRequestParams);
}
diff --git a/snippets/updating-data/save-record/new.js b/snippets/updating-data/save-record/new.js
index 067545a..73fc69c 100644
--- a/snippets/updating-data/save-record/new.js
+++ b/snippets/updating-data/save-record/new.js
@@ -1,4 +1,3 @@
-import { recordIdentifierFor } from '@ember-data/store';
import { updateRecord, serializePatch } from '@ember-data/json-api/request';
user.name = 'Chris';
@@ -7,7 +6,7 @@ const request = updateRecord(user);
request.body = JSON.stringify(
serializePatch(
store.cache,
- recordIdentifierFor(user)
+ user
)
);
diff --git a/tests/integration/components/guide-section/subsection-test.js b/tests/integration/components/guide-section/subsection-test.js
index 997e0e9..6c29c16 100644
--- a/tests/integration/components/guide-section/subsection-test.js
+++ b/tests/integration/components/guide-section/subsection-test.js
@@ -97,7 +97,7 @@ module('Integration | Component | guide-section/subsection', function (hooks) {
assert
.dom('[data-test-field="Subsection Description"]')
.includesText(
- 'To create a new record using Ember Data you should use createRecord request and attach "body" to it. In case of JSON:API backend - you can user serializeResources request utility.'
+ 'To create a new record using Ember Data you should use createRecord request and attach "body" to it. In case of JSON:API backend - you can use serializeResources request utility.'
);
const emberClassic = this.element.querySelector(
diff --git a/translations/updating-data/create-record/en-us.yaml b/translations/updating-data/create-record/en-us.yaml
index c03e200..1efc5d3 100644
--- a/translations/updating-data/create-record/en-us.yaml
+++ b/translations/updating-data/create-record/en-us.yaml
@@ -1,2 +1,2 @@
title: createRecord
-description: To create a new record using Ember Data you should use createRecord
request and attach "body"
to it. In case of JSON:API backend - you can user serializeResources
request utility.
+description: To create a new record using Ember Data you should use createRecord
request and attach "body"
to it. In case of JSON:API backend - you can use serializeResources
request utility.
From b2a16bd88d6d6eb4d6bd2aa89752ad3fb2cbaf0d Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Sun, 23 Feb 2025 13:08:29 +0530
Subject: [PATCH 2/6] request changes
---
snippets/updating-data/create-record/in-place-body.js | 3 ++-
snippets/updating-data/create-record/new.js | 3 ++-
snippets/updating-data/save-record/new.js | 3 ++-
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/snippets/updating-data/create-record/in-place-body.js b/snippets/updating-data/create-record/in-place-body.js
index ab612f8..2abd488 100644
--- a/snippets/updating-data/create-record/in-place-body.js
+++ b/snippets/updating-data/create-record/in-place-body.js
@@ -1,3 +1,4 @@
+import { recordIdentifierFor } from '@ember-data/store';
import { createRecord, serializeResources } from '@ember-data/json-api/request';
const record = store.createRecord('user', {});
@@ -6,7 +7,7 @@ const request = createRecord(record);
request.body = JSON.stringify(
serializeResources(
store.cache,
- record
+ recordIdentifierFor(record)
)
);
diff --git a/snippets/updating-data/create-record/new.js b/snippets/updating-data/create-record/new.js
index 589b3dd..951f335 100644
--- a/snippets/updating-data/create-record/new.js
+++ b/snippets/updating-data/create-record/new.js
@@ -1,3 +1,4 @@
+import { recordIdentifierFor } from '@ember-data/store';
import { createRecord, serializeResources } from '@ember-data/json-api/request';
const record = store.createRecord('user', {});
@@ -5,7 +6,7 @@ const request = createRecord(record);
request.body = JSON.stringify(
serializeResources(
store.cache,
- record
+ recordIdentifierFor(record)
)
);
diff --git a/snippets/updating-data/save-record/new.js b/snippets/updating-data/save-record/new.js
index 73fc69c..067545a 100644
--- a/snippets/updating-data/save-record/new.js
+++ b/snippets/updating-data/save-record/new.js
@@ -1,3 +1,4 @@
+import { recordIdentifierFor } from '@ember-data/store';
import { updateRecord, serializePatch } from '@ember-data/json-api/request';
user.name = 'Chris';
@@ -6,7 +7,7 @@ const request = updateRecord(user);
request.body = JSON.stringify(
serializePatch(
store.cache,
- user
+ recordIdentifierFor(user)
)
);
From 122b7a52ff65445ac0a510315afe8dd0198f85af Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Mon, 21 Apr 2025 12:16:14 +0530
Subject: [PATCH 3/6] Add: comparison between EmberData models and SchemaRecord
---
app/routes/application.js | 10 +++
snippets/models/model-definition/new.js | 64 +++++++++++++++++++
snippets/models/model-definition/old.js | 23 +++++++
translations/models/en-us.yaml | 2 +
.../models/model-definition/en-us.yaml | 2 +
5 files changed, 101 insertions(+)
create mode 100644 snippets/models/model-definition/new.js
create mode 100644 snippets/models/model-definition/old.js
create mode 100644 translations/models/en-us.yaml
create mode 100644 translations/models/model-definition/en-us.yaml
diff --git a/app/routes/application.js b/app/routes/application.js
index 4b9f63f..ee9b050 100644
--- a/app/routes/application.js
+++ b/app/routes/application.js
@@ -86,6 +86,16 @@ export default class ApplicationRoute extends Route {
},
],
},
+ {
+ id: 'models',
+ subsections: [
+ {
+ id: 'model-definition',
+ classicFiles: ['old.js'],
+ octaneFiles: ['new.js'],
+ },
+ ],
+ },
];
}
}
diff --git a/snippets/models/model-definition/new.js b/snippets/models/model-definition/new.js
new file mode 100644
index 0000000..969935b
--- /dev/null
+++ b/snippets/models/model-definition/new.js
@@ -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'
+ }
+ }
+ ]
+ }),
+
+ // 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'
+ }
+ }
+ ]
+ })
+]);
\ No newline at end of file
diff --git a/snippets/models/model-definition/old.js b/snippets/models/model-definition/old.js
new file mode 100644
index 0000000..b8221e5
--- /dev/null
+++ b/snippets/models/model-definition/old.js
@@ -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;
+// }
\ No newline at end of file
diff --git a/translations/models/en-us.yaml b/translations/models/en-us.yaml
new file mode 100644
index 0000000..e3beff5
--- /dev/null
+++ b/translations/models/en-us.yaml
@@ -0,0 +1,2 @@
+title: Models
+description: See how traditional EmberData models compare to the new SchemaRecord approach.
\ No newline at end of file
diff --git a/translations/models/model-definition/en-us.yaml b/translations/models/model-definition/en-us.yaml
new file mode 100644
index 0000000..ef40beb
--- /dev/null
+++ b/translations/models/model-definition/en-us.yaml
@@ -0,0 +1,2 @@
+title: Model Definition
+description: See how to define resources using the traditional EmberData models and the new SchemaRecord approach.
\ No newline at end of file
From 827773b9f895d82be237fe961e887256baf7f56f Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Wed, 23 Apr 2025 14:59:47 +0530
Subject: [PATCH 4/6] Update snippets/models/model-definition/new.js
Co-authored-by: Chris Thoburn
---
snippets/models/model-definition/new.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/snippets/models/model-definition/new.js b/snippets/models/model-definition/new.js
index 969935b..b02b883 100644
--- a/snippets/models/model-definition/new.js
+++ b/snippets/models/model-definition/new.js
@@ -21,7 +21,9 @@ store.schema.registerResources([
options: {
async: false,
inverse: 'owner',
- polymorphic: true
+ polymorphic: true,
+ linksMode: true,
+ resetOnRemoteUpdate: false,
}
}
]
From 9b73606db73d71eeee04bf14570a98a016a687f4 Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Wed, 23 Apr 2025 14:59:54 +0530
Subject: [PATCH 5/6] Update snippets/models/model-definition/new.js
Co-authored-by: Chris Thoburn
---
snippets/models/model-definition/new.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/snippets/models/model-definition/new.js b/snippets/models/model-definition/new.js
index b02b883..b15402d 100644
--- a/snippets/models/model-definition/new.js
+++ b/snippets/models/model-definition/new.js
@@ -40,7 +40,9 @@ store.schema.registerResources([
type: 'user',
options: {
async: false,
- inverse: 'pets'
+ inverse: 'pets',
+ linksMode: true,
+ resetOnRemoteUpdate: false,
}
}
]
From 4e840328bcac1f5f5dc60ae6476eca15ee72f8a4 Mon Sep 17 00:00:00 2001
From: Mehul Kiran Chaudhari
<55375534+MehulKChaudhari@users.noreply.github.com>
Date: Wed, 23 Apr 2025 15:00:44 +0530
Subject: [PATCH 6/6] Update snippets/models/model-definition/new.js
Co-authored-by: Chris Thoburn
---
snippets/models/model-definition/new.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/snippets/models/model-definition/new.js b/snippets/models/model-definition/new.js
index b15402d..71c79de 100644
--- a/snippets/models/model-definition/new.js
+++ b/snippets/models/model-definition/new.js
@@ -60,7 +60,9 @@ store.schema.registerResources([
options: {
async: false,
inverse: 'pets',
- as: 'pet'
+ as: 'pet',
+ linksMode: true,
+ resetOnRemoteUpdate: false,
}
}
]