Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Entity.pk() default provided that uses id #3188

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/hot-cows-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@data-client/endpoint': patch
'@data-client/graphql': patch
'@data-client/rest': patch
---

Do not require [Entity.pk()](https://dataclient.io/rest/api/Entity#pk)

Default implementation uses `this.id`
11 changes: 11 additions & 0 deletions .changeset/pink-plums-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@data-client/normalizr': patch
'@data-client/endpoint': patch
'@data-client/graphql': patch
'@data-client/react': patch
'@data-client/core': patch
'@data-client/rest': patch
'@data-client/test': patch
---

Update README to remove Entity.pk() when it is default ('id')
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
26 changes: 0 additions & 26 deletions __tests__/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ import React, { createContext, useContext } from 'react';
/** Represents data with primary key being from 'id' field. */
export class IDEntity extends Entity {
readonly id: string | number | undefined = undefined;

/**
* A unique identifier for each Entity
*
* @param [parent] When normalizing, the object which included the entity
* @param [key] When normalizing, the key where this entity was found
*/
pk(parent?: any, key?: string): string | undefined {
return `${this.id}`;
}
}

class Vis {
Expand All @@ -43,10 +33,6 @@ export class VisSettings extends Entity implements Vis {
readonly numCols: number = 0;
readonly updatedAt: number = 0;

pk() {
return `${this.id}`;
}

static shouldUpdate(
existingMeta: { date: number },
incomingMeta: { date: number },
Expand Down Expand Up @@ -162,10 +148,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 +161,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 +599,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
6 changes: 1 addition & 5 deletions docs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class Todo extends Entity {
completed = false;

pk() {
return `${this.id}`;
return this.id;
}
}
```
Expand Down Expand Up @@ -346,10 +346,6 @@ class Todo extends Entity {
userId = 0;
title = '';
completed = false;

pk() {
return `${this.id}`;
}
}

const TodoResource = resource({
Expand Down
3 changes: 0 additions & 3 deletions docs/core/api/Manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ import { Entity } from '@data-client/endpoint';
export default class CurrentTime extends Entity {
id = 0;
time = 0;
pk() {
return this.id;
}
}
```

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
14 changes: 1 addition & 13 deletions docs/core/concepts/normalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ class Presentation extends Entity {
id = '';
title = '';

pk() {
return this.id;
}
static key = 'presentation';
static key = 'Presentation';
}
```

Expand Down Expand Up @@ -303,9 +300,6 @@ class Todo extends Entity {
title = '';
completed = false;

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

// highlight-start
Expand All @@ -319,9 +313,6 @@ class User extends Entity {
id = 0;
username = '';

pk() {
return `${this.id}`;
}
static key = 'User';
}
```
Expand Down Expand Up @@ -362,9 +353,6 @@ class Todo extends Entity {
// highlight-next-line
dueDate = Temporal.Instant.fromEpochSeconds(0);

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

static schema = {
Expand Down
Loading
Loading