Skip to content

Commit 3d93624

Browse files
add: querying single data migration to use request manager
1 parent 6b98bdb commit 3d93624

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

guides/release/models/finding-records.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ This will return a response from the server which has a requested record:
1010
import { service } from '@ember/service';
1111
import { findRecord } from '@ember-data/json-api/request';
1212

13-
@service store
13+
// somewhere in the app
1414
const result = await this.store.request(findRecord('blog-post', '1'));
15-
const blog-post = result.content.data;
15+
const blogPost = result.content.data;
1616
```
1717

1818
Use [`store.peekRecord()`](https://api.emberjs.com/ember-data/release/classes/Store/methods/peekRecord?anchor=peekRecord) to retrieve a record by its type and ID, without making a network request.
@@ -31,7 +31,7 @@ Use [`query()`](https://api.emberjs.com/ember-data/5.3/functions/@ember-data%2Fj
3131
import { query } from '@ember-data/json-api/request';
3232

3333
const result = await store.request(query('blog-post'));
34-
const blog-posts = result.content.data;
34+
const blogPosts = result.content.data;
3535
```
3636

3737
Use [`store.peekAll()`](https://api.emberjs.com/ember-data/release/classes/Store/methods/peekAll?anchor=peekAll) to retrieve all of the records for a given type that are already loaded into the store, without making a network request:
@@ -69,9 +69,9 @@ const person = result.content.data;
6969

7070
### Querying for A Single Record
7171

72-
If you are using an adapter that supports server requests capable of returning a single model object,
73-
EmberData provides a convenience method [`store.queryRecord()`](https://api.emberjs.com/ember-data/release/classes/Store/methods/queryRecord?anchor=queryRecord) that will return a promise that resolves with that single record.
74-
The request is made via a method `queryRecord()` defined by the adapter.
72+
If you are using an builder that supports server requests capable of returning a single model object,
73+
EmberData provides a convenience method [`findRecord()`](https://api.emberjs.com/ember-data/5.3/functions/@ember-data%2Fjson-api%2Frequest/findRecord) that will return a record.
74+
The request is made via a method `findRecord()` defined by the builders.
7575

7676
For example, if your server API provides an endpoint for the currently logged in user:
7777

@@ -85,43 +85,43 @@ For example, if your server API provides an endpoint for the currently logged in
8585
}
8686
```
8787

88-
And if the adapter for the `User` model defines a `queryRecord()` method that targets that endpoint:
89-
90-
```javascript {data-filename=app/adapters/user.js}
91-
import Adapter from '@ember-data/adapter';
92-
import fetch from 'fetch';
93-
94-
export default class UserAdapter extends Adapter {
95-
queryRecord(store, type, query) {
96-
return fetch('/api/current_user');
97-
}
88+
And if the builders for the `User` model defines a `queryData()` method that targets that endpoint:
89+
90+
```javascript {data-filename=app/builders/user.js}
91+
export function queryData() {
92+
return {
93+
url: `/api/current_user`,
94+
method: 'GET',
95+
headers: {
96+
'Content-Type': 'application/json'
97+
},
98+
}
9899
}
99100
```
100101

101-
Then, calling [`store.queryRecord()`](https://api.emberjs.com/ember-data/release/classes/Store/methods/queryRecord?anchor=queryRecord) will retrieve that object from the server:
102+
Then, calling `queryData()` will retrieve that object from the server:
102103

103104
```javascript
104-
store.queryRecord('user', {}).then(function(user) {
105-
let username = user.get('username');
106-
console.log(`Currently logged in as ${username}`);
107-
});
105+
import { queryData } from './builders';
106+
107+
const user = await this.requestManager.request(queryData())
108+
let username = user.get('username');
109+
console.log(`Currently logged in as ${username}`);
108110
```
109111

110-
As in the case of `store.query()`, a query object can also be passed to `store.queryRecord()` and is available for the adapter's `queryRecord()` to use to qualify the request.
111-
However the adapter must return a single model object, not an array containing one element,
112+
As in the case of `query()`, a query object can also be passed to `query()` and is available for the builder's `query()` to use to qualify the request.
113+
However the builder must return a single model object, not an array containing one element,
112114
otherwise EmberData will throw an exception.
113115

114-
Note that Ember's default [JSON:API adapter](https://api.emberjs.com/ember-data/release/classes/JSONAPIAdapter) does not provide the functionality needed to support `queryRecord()` directly as it relies on REST request definitions that return result data in the form of an array.
115-
116-
If your server API or your adapter only provides array responses but you wish to retrieve just a single record, you can alternatively use the `query()` method as follows:
116+
If your server API or your builder only provides array responses but you wish to retrieve just a single record, you can alternatively use the `query()` method as follows:
117117

118118
```javascript
119119
// GET to /users?filter[email][email protected]
120-
tom = store.query('user', {
120+
tom = requestManager.request(queryData('user', {
121121
filter: {
122122
123123
}
124-
}).then(function(users) {
124+
})).then(function(users) {
125125
return users[0]; // the first object
126126
});
127127
```

0 commit comments

Comments
 (0)