Skip to content

Commit

Permalink
enhance: Entity.pk() default provided that uses id
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Aug 14, 2024
1 parent 3ce0bbf commit 6da505f
Show file tree
Hide file tree
Showing 52 changed files with 341 additions and 599 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ For more details, see [the Installation docs page](https://dataclient.io/docs/ge
class User extends Entity {
id = '';
username = '';

pk() {
return this.id;
}
}

class Article extends Entity {
Expand All @@ -58,10 +54,6 @@ class Article extends Entity {
author = User.fromJS();
createdAt = Temporal.Instant.fromEpochSeconds(0);

pk() {
return this.id;
}

static schema = {
author: User,
createdAt: Temporal.Instant.from,
Expand Down
12 changes: 0 additions & 12 deletions __tests__/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ export class User extends Entity {
readonly username: string = '';
readonly email: string = '';
readonly isAdmin: boolean = false;

pk() {
return this.id?.toString();
}
}
export const UserResource = resource({
path: 'http\\://test.com/user/:id',
Expand All @@ -179,10 +175,6 @@ export class Article extends Entity {
readonly author: User | null = null;
readonly tags: string[] = [];

pk() {
return this.id?.toString();
}

static schema = {
author: User,
};
Expand Down Expand Up @@ -621,10 +613,6 @@ export abstract class UnionBase extends Entity {
readonly id: string = '';
readonly body: string = '';
readonly type: string = '';

pk() {
return this.id;
}
}
export class FirstUnion extends UnionBase {
readonly type = 'first';
Expand Down
4 changes: 1 addition & 3 deletions docs/core/api/useCache.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export class User extends Entity {
id = '';
name = '';
isAdmin = false;
pk() {
return this.id;
}

static key = 'User';
}
export const UserResource = resource({
Expand Down
15 changes: 0 additions & 15 deletions docs/core/api/useDLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ export class Profile extends Entity {
fullName = '';
bio = '';

pk() {
return this.id?.toString();
}
static key = 'Profile';
}

Expand Down Expand Up @@ -148,9 +145,6 @@ export class Profile extends Entity {
fullName = '';
bio = '';

pk() {
return this.id?.toString();
}
static key = 'Profile';
}

Expand Down Expand Up @@ -202,9 +196,6 @@ export class Post extends Entity {
title = '';
body = '';

pk() {
return this.id?.toString();
}
static key = 'Post';
}
export const PostResource = resource({
Expand All @@ -224,9 +215,6 @@ export class User extends Entity {
return `https://i.pravatar.cc/64?img=${this.id + 4}`;
}

pk() {
return `${this.id}`;
}
static key = 'User';
}
export const UserResource = resource({
Expand Down Expand Up @@ -272,9 +260,6 @@ export class PaginatedPost extends Entity {
title = '';
content = '';

pk() {
return this.id;
}
static key = 'PaginatedPost';
}

Expand Down
4 changes: 1 addition & 3 deletions docs/core/api/useQuery.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export class User extends Entity {
id = '';
name = '';
isAdmin = false;
pk() {
return this.id;
}

static key = 'User';
}
export const UserResource = resource({
Expand Down
1 change: 0 additions & 1 deletion docs/core/api/useSubscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function useSubscription<
### Only subscribe while element is visible

```tsx title="MasterPrice.tsx"
import { useRef } from 'react';
import { useSuspense, useSubscription } from '@data-client/react';
import { getPrice } from 'api/Price';

Expand Down
17 changes: 1 addition & 16 deletions docs/core/api/useSuspense.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ export class Profile extends Entity {
fullName = '';
bio = '';

pk() {
return this.id?.toString();
}
static key = 'Profile';
}

Expand Down Expand Up @@ -204,9 +201,6 @@ export class Profile extends Entity {
fullName = '';
bio = '';

pk() {
return this.id?.toString();
}
static key = 'Profile';
}

Expand Down Expand Up @@ -276,9 +270,6 @@ export class Post extends Entity {
title = '';
body = '';

pk() {
return this.id?.toString();
}
static key = 'Post';
}
export const PostResource = resource({
Expand All @@ -298,9 +289,6 @@ export class User extends Entity {
return `https://i.pravatar.cc/64?img=${this.id + 4}`;
}

pk() {
return `${this.id}`;
}
static key = 'User';
}
export const UserResource = resource({
Expand Down Expand Up @@ -336,15 +324,12 @@ When entities are stored in [nested structures](/rest/guides/relational-data#nes

<TypeScriptEditor row={false}>

```typescript title="api/Post" {15-19}
```typescript title="api/Post" {12-16}
export class PaginatedPost extends Entity {
id = '';
title = '';
content = '';

pk() {
return this.id;
}
static key = 'PaginatedPost';
}

Expand Down
3 changes: 0 additions & 3 deletions docs/core/concepts/error-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down
18 changes: 0 additions & 18 deletions docs/core/concepts/expiry-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);

pk() {
return this.id;
}
static schema = {
updatedAt: Temporal.Instant.from,
};
Expand Down Expand Up @@ -216,9 +213,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down Expand Up @@ -314,9 +308,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down Expand Up @@ -378,9 +369,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down Expand Up @@ -482,9 +470,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down Expand Up @@ -595,9 +580,6 @@ delay: () => 150,
export class TimedEntity extends Entity {
id = '';
updatedAt = Temporal.Instant.fromEpochSeconds(0);
pk() {
return this.id;
}

static schema = {
updatedAt: Temporal.Instant.from,
Expand Down
25 changes: 6 additions & 19 deletions docs/core/concepts/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ delay: 150,

```typescript title="api/Article"
export class Article extends Entity {
readonly id: string = '';
readonly title: string = '';

pk() {
return this.id;
}
id = '';
title = '';

static validate(processedEntity) {
if (!Object.hasOwn(processedEntity, 'title')) return 'missing title field';
Expand Down Expand Up @@ -79,7 +75,7 @@ render(<ArticlePage id="2" />);

### All fields check

Here's a recipe for checking that every defined field is present.
[validateRequired()](/rest/api/validateRequired) can be used to check if all defined fields are present.

<HooksPlayground fixtures={[
{
Expand All @@ -104,20 +100,11 @@ delay: 150,

```tsx title="api/Article"
export class Article extends Entity {
readonly id: string = '';
readonly title: string = '';

pk() {
return this.id;
}
id = '';
title = '';

static validate(processedEntity) {
if (
!Object.keys(this.defaults).every(key =>
Object.hasOwn(processedEntity, key),
)
)
return 'a field is missing';
return validateRequired(processedEntity, this.defaults);
}
}

Expand Down
9 changes: 0 additions & 9 deletions docs/core/getting-started/data-dependency.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export class User extends Entity {
return `https://i.pravatar.cc/64?img=${this.id + 4}`;
}

pk() {
return this.id;
}
static key = 'User';
}
export const UserResource = resource({
Expand All @@ -57,9 +54,6 @@ export class Post extends Entity {
title = '';
body = '';

pk() {
return this.id;
}
static key = 'Post';

static schema = {
Expand Down Expand Up @@ -322,9 +316,6 @@ export class Profile extends Entity {
fullName = '';
bio = '';

pk() {
return this.id?.toString();
}
static key = 'Profile';
}

Expand Down
4 changes: 1 addition & 3 deletions docs/core/getting-started/mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export class Todo extends Entity {
userId = 0;
title = '';
completed = false;
pk() {
return `${this.id}`;
}

static key = 'Todo';
}
export const TodoResource = resource({
Expand Down
3 changes: 0 additions & 3 deletions docs/core/getting-started/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export class Todo extends Entity {
title = '';
completed = false;

pk() {
return `${this.id}`;
}
static key = 'Todo';
}

Expand Down
3 changes: 0 additions & 3 deletions docs/core/guides/storybook.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ export class Article extends Entity {
author: number | null = null;
contributors: number[] = [];

pk() {
return this.id?.toString();
}
static key = 'Article';
}
export const ArticleResource = resource({
Expand Down
3 changes: 0 additions & 3 deletions docs/core/shared/_VoteDemo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export class Post extends Entity {
body = '';
votes = 0;

pk() {
return this.id;
}
static key = 'Post';

static schema = {
Expand Down
4 changes: 1 addition & 3 deletions docs/core/shared/_useCancelling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export class Todo extends Entity {
userId = 0;
title = '';
completed = false;
pk() {
return `${this.id}`;
}

static key = 'Todo';
}
export const TodoResource = resource({
Expand Down
Loading

0 comments on commit 6da505f

Please sign in to comment.