Skip to content

Commit 43a955c

Browse files
authored
enhance: schema.Entity -> EntityMixin (#3243)
1 parent f81737c commit 43a955c

File tree

30 files changed

+272
-271
lines changed

30 files changed

+272
-271
lines changed

.changeset/four-terms-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@data-client/normalizr': patch
3+
---
4+
5+
Update README to link to [EntityMixin](https://dataclient.io/rest/api/EntityMixin)

.changeset/wise-seals-cross.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@data-client/endpoint': patch
3+
'@data-client/graphql': patch
4+
'@data-client/rest': patch
5+
---
6+
7+
`schema.Entity` -> [EntityMixin](https://dataclient.io/rest/api/EntityMixin)
8+
9+
```ts
10+
import { EntityMixin } from '@data-client/rest';
11+
12+
export class Article {
13+
id = '';
14+
title = '';
15+
content = '';
16+
tags: string[] = [];
17+
}
18+
19+
export class ArticleEntity extends EntityMixin(Article) {}
20+
```
21+
22+
We keep `schema.Entity` for legacy, and add schema.EntityMixin and [EntityMixin](https://dataclient.io/rest/api/EntityMixin) as direct export

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ For the small price of 9kb gziped.    [🏁Get started now](https://da
373373
<tbody><tr>
374374
<td rowSpan="4"><a href="https://en.wikipedia.org/wiki/Object_(computer_science)">Object</a></td>
375375
<td align="center">✅</td>
376-
<td><a href="https://dataclient.io/rest/api/Entity">Entity</a>, <a href="https://dataclient.io/rest/api/schema.Entity">schema.Entity</a> mixin</td>
376+
<td><a href="https://dataclient.io/rest/api/Entity">Entity</a>, <a href="https://dataclient.io/rest/api/EntityMixin">EntityMixin</a></td>
377377
<td>single <em>unique</em> object</td>
378378
<td align="center">✅</td>
379379
</tr>

__tests__/new.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
import { Temporal } from '@js-temporal/polyfill';
2+
import React, { createContext, useContext } from 'react';
3+
14
import {
25
schema,
36
Endpoint,
47
resource,
58
RestEndpoint,
69
Schema,
710
Entity,
11+
EntityMixin,
812
RestGenerics,
913
hookifyResource,
1014
RestType,
1115
RestInstance,
1216
Resource,
1317
ResourceOptions,
1418
} from '@data-client/rest';
15-
import { Temporal } from '@js-temporal/polyfill';
16-
import React, { createContext, useContext } from 'react';
1719

1820
/** Represents data with primary key being from 'id' field. */
1921
export class IDEntity extends Entity {
@@ -44,7 +46,7 @@ export class VisSettings extends Entity implements Vis {
4446

4547
static key = 'VisSettings';
4648
}
47-
export class VisSettingsFromMixin extends schema.Entity(Vis) {
49+
export class VisSettingsFromMixin extends EntityMixin(Vis) {
4850
static cmpIncoming(
4951
existingMeta: { date: number; fetchedAt: number },
5052
incomingMeta: { date: number; fetchedAt: number },
@@ -179,7 +181,7 @@ class ArticleData {
179181
readonly author: User | null = null;
180182
readonly tags: string[] = [];
181183
}
182-
export class ArticleFromMixin extends schema.Entity(ArticleData, {
184+
export class ArticleFromMixin extends EntityMixin(ArticleData, {
183185
schema: { author: User },
184186
}) {}
185187
class ArticleEndpoint<O extends RestGenerics = any> extends RestEndpoint<O> {}

docs/core/getting-started/resource.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const TodoResource = {
139139
<PkgInstall pkgs="@data-client/endpoint" />
140140

141141
Pre-existing TypeScript definitions can be used in <abbr title="Reactive Data Client">Data Client</abbr> with
142-
[Endpoint](/rest/api/Endpoint) and [schema.Entity](/rest/api/schema.Entity).
142+
[Endpoint](/rest/api/Endpoint) and [EntityMixin](/rest/api/EntityMixin).
143143

144144
<TypeScriptEditor row={false}>
145145

@@ -187,7 +187,7 @@ export const deleteTodo = (body: Partial<Todo>) =>
187187
```
188188

189189
```typescript title="TodoResource"
190-
import { schema, Endpoint } from '@data-client/endpoint';
190+
import { schema, EntityMixin, Endpoint } from '@data-client/endpoint';
191191
import {
192192
Todo,
193193
getTodo,
@@ -198,7 +198,7 @@ import {
198198
deleteTodo,
199199
} from './existing/Todo';
200200

201-
export const TodoEntity = schema.Entity(Todo, { key: 'Todo' });
201+
export const TodoEntity = EntityMixin(Todo, { key: 'Todo' });
202202

203203
export const TodoResource = {
204204
get: new Endpoint(getTodo, { schema: TodoEntity }),
@@ -289,5 +289,5 @@ To aid in defining `Resources`, composable and extensible protocol specific help
289289
[Image/binary](../guides/img-media.md), [Websockets+SSE](../concepts/managers.md#data-stream).
290290

291291
To use existing API definitions, or define your own protocol specific helpers, use
292-
[Endpoint](/rest/api/Endpoint) and [schema.Entity](/rest/api/schema.Entity) from [@data-client/endpoint](https://www.npmjs.com/package/@data-client/endpoint).
292+
[Endpoint](/rest/api/Endpoint) and [EntityMixin](/rest/api/EntityMixin) from [@data-client/endpoint](https://www.npmjs.com/package/@data-client/endpoint).
293293
[See `Async/Promise` tab above]

docs/core/shared/_VoteDemo.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Post extends Entity {
1616
static key = 'Post';
1717

1818
static schema = {
19-
author: schema.Entity(
19+
author: EntityMixin(
2020
class User {
2121
id = 0;
2222
},

docs/rest/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ export const ArticleResource = resource({
8282
<TypeScriptEditor>
8383

8484
```typescript title="User" collapsed
85-
import { schema } from '@data-client/rest';
85+
import { EntityMixin } from '@data-client/rest';
8686

8787
export class User {
8888
id = '';
8989
username = '';
9090
}
91-
export class UserEntity extends schema.Entity(User) {}
91+
export class UserEntity extends EntityMixin(User) {}
9292
```
9393

9494
```typescript title="Article"
95-
import { schema, resource } from '@data-client/rest';
95+
import { EntityMixin, resource } from '@data-client/rest';
9696
import { UserEntity } from './User';
9797

9898
export class Article {
@@ -104,7 +104,7 @@ export class Article {
104104
createdAt = Temporal.Instant.fromEpochSeconds(0);
105105
}
106106

107-
export class ArticleEntity extends schema.Entity(Article, {
107+
export class ArticleEntity extends EntityMixin(Article, {
108108
schema: {
109109
author: UserEntity,
110110
createdAt: Temporal.Instant.from,

docs/rest/api/Entity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Entities are bound to Endpoints using [resource.schema](./resource.md#schema) or
9595

9696
:::tip
9797

98-
If you already have your classes defined, [schema.Entity](./schema.Entity.md) mixin can also be
98+
If you already have your classes defined, [EntityMixin](./EntityMixin.md) mixin can also be
9999
used to make Entities.
100100

101101
:::

docs/rest/api/schema.Entity.md renamed to docs/rest/api/EntityMixin.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: schema.Entity - Entity mixin
3-
sidebar_label: schema.Entity
2+
title: EntityMixin - Declarative unique objects for pre-existing classes
3+
sidebar_label: EntityMixin
44
---
55

66
<head>
@@ -12,16 +12,16 @@ import LanguageTabs from '@site/src/components/LanguageTabs';
1212
import { RestEndpoint } from '@data-client/rest';
1313
import TypeScriptEditor from '@site/src/components/TypeScriptEditor';
1414

15-
# schema.Entity
15+
# EntityMixin
1616

1717
`Entity` defines a single _unique_ object.
1818

19-
If you already have classes for your data-types, `schema.Entity` mixin may be for you.
19+
If you already have classes for your data-types, `EntityMixin` mixin may be for you.
2020

2121
<TypeScriptEditor>
2222

2323
```typescript {10}
24-
import { schema } from '@data-client/rest';
24+
import { EntityMixin } from '@data-client/rest';
2525

2626
export class Article {
2727
id = '';
@@ -30,7 +30,7 @@ export class Article {
3030
tags: string[] = [];
3131
}
3232

33-
export class ArticleEntity extends schema.Entity(Article) {}
33+
export class ArticleEntity extends EntityMixin(Article) {}
3434
```
3535

3636
</TypeScriptEditor>
@@ -48,7 +48,7 @@ class User {
4848
username = '';
4949
createdAt = Temporal.Instant.fromEpochSeconds(0);
5050
}
51-
class UserEntity extends schema.Entity(User, {
51+
class UserEntity extends EntityMixin(User, {
5252
pk: 'username',
5353
key: 'User',
5454
schema: { createdAt: Temporal.Instant.from },
@@ -75,7 +75,7 @@ class Thread {
7575
slug = '';
7676
content = '';
7777
}
78-
class ThreadEntity extends schema.Entity(Thread, {
78+
class ThreadEntity extends EntityMixin(Thread, {
7979
pk(value) {
8080
return [value.forum, value.slug].join(',');
8181
},
@@ -94,7 +94,7 @@ Specifies the [Entity.schema](./Entity.md#schema)
9494

9595
## Methods
9696

97-
`schema.Entity` mixin has the same [methods as the Entity](./Entity.md#lifecycle) class.
97+
`EntityMixin` mixin has the same [methods as the Entity](./Entity.md#lifecycle) class.
9898

9999
## const vs class
100100

@@ -118,8 +118,8 @@ export class Article {
118118
tags: string[] = [];
119119
}
120120

121-
export class ArticleEntity extends schema.Entity(Article) {}
122-
export const ArticleEntity2 = schema.Entity(Article);
121+
export class ArticleEntity extends EntityMixin(Article) {}
122+
export const ArticleEntity2 = EntityMixin(Article);
123123

124124
const article: ArticleEntity = ArticleEntity.fromJS();
125125
const articleFails: ArticleEntity2 = ArticleEntity2.fromJS();

examples/benchmark/schemas.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, schema } from './dist/index.js';
1+
import { Entity, EntityMixin, schema } from './dist/index.js';
22

33
export class BuildTypeDescription extends Entity {
44
id = '';
@@ -13,7 +13,7 @@ export class BuildTypeDescription extends Entity {
1313
export class BuildTypeDescriptionEmpty extends Entity {
1414
static key = 'BuildTypeDescription';
1515
}
16-
export const BuildTypeDescriptionEntity = schema.Entity(
16+
export const BuildTypeDescriptionEntity = EntityMixin(
1717
class {
1818
id = '';
1919
internalId = 'bt17590';
@@ -51,7 +51,7 @@ export class ProjectWithBuildTypesDescriptionEmpty extends Entity {
5151

5252
static key = 'ProjectWithBuildTypesDescription';
5353
}
54-
export const ProjectWithBuildTypesDescriptionEntity = schema.Entity(
54+
export const ProjectWithBuildTypesDescriptionEntity = EntityMixin(
5555
class {
5656
id = '';
5757
internalId = 'project3239';

0 commit comments

Comments
 (0)