Skip to content

Commit 2d6cc4b

Browse files
committed
docs: Update relational data client side join example
1 parent b131885 commit 2d6cc4b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

docs/rest/guides/relational-data.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ the relationship in [Entity.schema](../api/Entity.md#schema)
189189
```ts title="resources/User" collapsed
190190
export class User extends Entity {
191191
id = 0;
192+
username = '';
192193
name = '';
193194
email = '';
194195
website = '';
195196
pk() {
196-
return `${this.id}`;
197+
return this.id;
197198
}
198199
}
199200
export const UserResource = resource({
@@ -208,15 +209,19 @@ import { User } from './User';
208209

209210
export class Todo extends Entity {
210211
id = 0;
211-
userId = User.fromJS({});
212+
userId = 0;
213+
user? = User.fromJS({});
212214
title = '';
213215
completed = false;
214216
pk() {
215-
return `${this.id}`;
217+
return this.id;
216218
}
217219
static schema = {
218-
userId: User,
220+
user: User,
219221
};
222+
static process(value) {
223+
return { ...value, user: value.userId };
224+
}
220225
}
221226
export const TodoResource = resource({
222227
urlPrefix: 'https://jsonplaceholder.typicode.com',
@@ -229,15 +234,14 @@ export const TodoResource = resource({
229234
import { TodoResource } from './resources/Todo';
230235
import { UserResource } from './resources/User';
231236

232-
233237
function TodosPage() {
234238
useFetch(UserResource.getList);
235239
const todos = useSuspense(TodoResource.getList);
236240
return (
237241
<div>
238-
{todos.slice(17,24).map(todo => (
242+
{todos.slice(17, 24).map(todo => (
239243
<div key={todo.pk()}>
240-
{todo.title} by <small>{todo.userId?.name}</small>
244+
{todo.title} by <small>{todo.user?.name}</small>
241245
</div>
242246
))}
243247
</div>
@@ -353,7 +357,10 @@ export class User extends Entity {
353357
...existing,
354358
...incoming,
355359
posts: [...(existing.posts || []), ...(incoming.posts || [])],
356-
comments: [...(existing.comments || []), ...(incoming.comments || [])],
360+
comments: [
361+
...(existing.comments || []),
362+
...(incoming.comments || []),
363+
],
357364
};
358365
}
359366

@@ -482,8 +489,13 @@ export default function PostPage({ setRoute }) {
482489
{comment.content}{' '}
483490
<small>
484491
<cite
485-
onClick={() => setRoute(`user/${comment.commenter.id}`)}
486-
style={{ cursor: 'pointer', textDecoration: 'underline' }}
492+
onClick={() =>
493+
setRoute(`user/${comment.commenter.id}`)
494+
}
495+
style={{
496+
cursor: 'pointer',
497+
textDecoration: 'underline',
498+
}}
487499
>
488500
{comment.commenter.name}
489501
{comment.commenter === post.author ? ' [OP]' : ''}

0 commit comments

Comments
 (0)