Skip to content

Commit 82b867a

Browse files
authored
update schema definition (#200)
1 parent 15eb093 commit 82b867a

File tree

12 files changed

+88
-83
lines changed

12 files changed

+88
-83
lines changed

apps/events/src/schema.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { Entity } from '@graphprotocol/hypergraph';
1+
import { Entity, Type } from '@graphprotocol/hypergraph';
22

33
export class User extends Entity.Class<User>('User')({
4-
name: Entity.Text,
4+
name: Type.Text,
55
}) {}
66

77
export class Todo extends Entity.Class<Todo>('Todo')({
8-
name: Entity.Text,
9-
completed: Entity.Checkbox,
10-
assignees: Entity.Relation(User),
8+
name: Type.Text,
9+
completed: Type.Checkbox,
10+
assignees: Type.Relation(User),
1111
}) {}
1212

1313
export class Todo2 extends Entity.Class<Todo2>('Todo2')({
14-
name: Entity.Text,
15-
checked: Entity.Checkbox,
16-
assignees: Entity.Relation(User),
17-
due: Entity.Date,
18-
amount: Entity.Number,
19-
point: Entity.Point,
20-
website: Entity.Url,
14+
name: Type.Text,
15+
checked: Type.Checkbox,
16+
assignees: Type.Relation(User),
17+
due: Type.Date,
18+
amount: Type.Number,
19+
point: Type.Point,
20+
website: Type.Url,
2121
}) {}
2222

2323
export class NewsStory extends Entity.Class<NewsStory>('NewsStory')({
24-
name: Entity.Text,
25-
description: Entity.Text,
26-
publishDate: Entity.Text,
24+
name: Type.Text,
25+
description: Type.Text,
26+
publishDate: Type.Text,
2727
}) {}

packages/hypergraph-react/src/internal/use-generate-create-ops.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graph, Id, type PropertiesParam, type ValueType } from '@graphprotocol/grc-20';
2-
import { Entity } from '@graphprotocol/hypergraph';
2+
import { type Entity, Type } from '@graphprotocol/hypergraph';
33
import { useHypergraph } from '../HypergraphSpaceContext.js';
44

55
export function useGenerateCreateOps<const S extends Entity.AnyNoContext>(type: S, enabled = true) {
@@ -21,19 +21,19 @@ export function useGenerateCreateOps<const S extends Entity.AnyNoContext>(type:
2121
for (const [key, value] of Object.entries(mappingEntry.properties || {})) {
2222
let valueType: ValueType = 'TEXT';
2323
let serializedValue: string = properties[key];
24-
if (fields[key] === Entity.Checkbox) {
24+
if (fields[key] === Type.Checkbox) {
2525
valueType = 'CHECKBOX';
2626
serializedValue = properties[key] ? '1' : '0';
27-
} else if (fields[key] === Entity.Date) {
27+
} else if (fields[key] === Type.Date) {
2828
valueType = 'TIME';
2929
serializedValue = properties[key].toISOString();
30-
} else if (fields[key] === Entity.Point) {
30+
} else if (fields[key] === Type.Point) {
3131
valueType = 'POINT';
3232
serializedValue = properties[key].join(',');
33-
} else if (fields[key] === Entity.Url) {
33+
} else if (fields[key] === Type.Url) {
3434
valueType = 'URL';
3535
serializedValue = properties[key].toString();
36-
} else if (fields[key] === Entity.Number) {
36+
} else if (fields[key] === Type.Number) {
3737
valueType = 'NUMBER';
3838
serializedValue = properties[key].toString();
3939
}

packages/hypergraph-react/src/internal/use-generate-update-ops.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Id, type Op, Relation, Triple, type Value } from '@graphprotocol/grc-20';
2-
import { Entity } from '@graphprotocol/hypergraph';
2+
import { type Entity, Type } from '@graphprotocol/hypergraph';
33
import { useHypergraph } from '../HypergraphSpaceContext.js';
44
import type { DiffEntry } from '../types.js';
55

@@ -31,30 +31,30 @@ export function useGenerateUpdateOps<const S extends Entity.AnyNoContext>(type:
3131
const rawValue = propertyDiff.new;
3232

3333
let value: Value;
34-
if (type.fields[key] === Entity.Checkbox) {
34+
if (type.fields[key] === Type.Checkbox) {
3535
value = {
3636
type: 'CHECKBOX',
3737
value: rawValue ? '1' : '0',
3838
};
39-
} else if (type.fields[key] === Entity.Point) {
39+
} else if (type.fields[key] === Type.Point) {
4040
value = {
4141
type: 'POINT',
4242
// @ts-expect-error: must be an array of numbers
4343
value: rawValue.join(','),
4444
};
45-
} else if (type.fields[key] === Entity.Url) {
45+
} else if (type.fields[key] === Type.Url) {
4646
value = {
4747
type: 'URL',
4848
// @ts-expect-error: must be a URL
4949
value: rawValue.toString(),
5050
};
51-
} else if (type.fields[key] === Entity.Date) {
51+
} else if (type.fields[key] === Type.Date) {
5252
value = {
5353
type: 'TIME',
5454
// @ts-expect-error: must be a Date
5555
value: rawValue.toISOString(),
5656
};
57-
} else if (type.fields[key] === Entity.Number) {
57+
} else if (type.fields[key] === Type.Number) {
5858
value = {
5959
type: 'NUMBER',
6060
// @ts-expect-error: must be a number

packages/hypergraph-react/src/internal/use-query-public-geo.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity } from '@graphprotocol/hypergraph';
1+
import { type Entity, Type } from '@graphprotocol/hypergraph';
22
import { useQuery as useQueryTanstack } from '@tanstack/react-query';
33
import * as Either from 'effect/Either';
44
import * as Schema from 'effect/Schema';
@@ -113,15 +113,15 @@ export const parseResult = <S extends Entity.AnyNoContext>(
113113
for (const [key, value] of Object.entries(mappingEntry?.properties ?? {})) {
114114
const property = queryEntityVersion.triples.nodes.find((a) => a.attributeId === value);
115115
if (property) {
116-
if (type.fields[key] === Entity.Checkbox) {
116+
if (type.fields[key] === Type.Checkbox) {
117117
rawEntity[key] = property.booleanValue;
118-
} else if (type.fields[key] === Entity.Point) {
118+
} else if (type.fields[key] === Type.Point) {
119119
rawEntity[key] = property.textValue;
120-
} else if (type.fields[key] === Entity.Url) {
120+
} else if (type.fields[key] === Type.Url) {
121121
rawEntity[key] = property.textValue;
122-
} else if (type.fields[key] === Entity.Date) {
122+
} else if (type.fields[key] === Type.Date) {
123123
rawEntity[key] = property.textValue;
124-
} else if (type.fields[key] === Entity.Number) {
124+
} else if (type.fields[key] === Type.Number) {
125125
rawEntity[key] = Number(property.textValue);
126126
} else {
127127
rawEntity[key] = property.textValue;

packages/hypergraph-react/src/internal/use-query-public-kg.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity } from '@graphprotocol/hypergraph';
1+
import { type Entity, Type } from '@graphprotocol/hypergraph';
22
import { useQuery as useQueryTanstack } from '@tanstack/react-query';
33
import * as Either from 'effect/Either';
44
import * as Schema from 'effect/Schema';
@@ -61,7 +61,7 @@ export function useQueryPublic<const S extends Entity.AnyNoContext>(type: S, par
6161
for (const [key, value] of Object.entries(mappingEntry?.properties ?? {})) {
6262
const property = queryEntity.attributes.find((a) => a.attribute === value);
6363
if (property) {
64-
if (type.fields[key] === Entity.Checkbox) {
64+
if (type.fields[key] === Type.Checkbox) {
6565
rawEntity[key] = property.value === '1';
6666
} else {
6767
rawEntity[key] = property.value;

packages/hypergraph-react/src/use-query.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, Utils } from '@graphprotocol/hypergraph';
1+
import { type Entity, Type, Utils } from '@graphprotocol/hypergraph';
22
import type * as Schema from 'effect/Schema';
33
import { useMemo } from 'react';
44
import { useHypergraph, useQueryLocal } from './HypergraphSpaceContext.js';
@@ -90,23 +90,23 @@ const getDiff = <S extends Entity.AnyNoContext>(
9090
};
9191
}
9292
} else {
93-
if (field === Entity.Date) {
93+
if (field === Type.Date) {
9494
if (entity[key].getTime() !== localEntity[key].getTime()) {
9595
diff[key] = {
9696
type: 'property',
9797
current: entity[key],
9898
new: localEntity[key],
9999
};
100100
}
101-
} else if (field === Entity.Url) {
101+
} else if (field === Type.Url) {
102102
if (entity[key].toString() !== localEntity[key].toString()) {
103103
diff[key] = {
104104
type: 'property',
105105
current: entity[key],
106106
new: localEntity[key],
107107
};
108108
}
109-
} else if (field === Entity.Point) {
109+
} else if (field === Type.Point) {
110110
if (entity[key].join(',') !== localEntity[key].join(',')) {
111111
diff[key] = {
112112
type: 'property',

packages/hypergraph-react/test/HypergraphSpaceContext.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type AnyDocumentId, Repo } from '@automerge/automerge-repo';
22
import { RepoContext } from '@automerge/automerge-repo-react-hooks';
3-
import { Entity, Utils } from '@graphprotocol/hypergraph';
3+
import { Entity, Type, Utils } from '@graphprotocol/hypergraph';
44
import '@testing-library/jest-dom/vitest';
55
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
66
// biome-ignore lint/style/useImportType: <explanation>
@@ -21,17 +21,17 @@ afterEach(() => {
2121

2222
describe('HypergraphSpaceContext', () => {
2323
class Person extends Entity.Class<Person>('Person')({
24-
name: Entity.Text,
25-
age: Entity.Number,
24+
name: Type.Text,
25+
age: Type.Number,
2626
}) {}
2727

2828
class User extends Entity.Class<User>('User')({
29-
name: Entity.Text,
30-
email: Entity.Text,
29+
name: Type.Text,
30+
email: Type.Text,
3131
}) {}
3232

3333
class Event extends Entity.Class<Event>('Event')({
34-
name: Entity.Text,
34+
name: Type.Text,
3535
}) {}
3636

3737
const spaceId = '52gTkePWSoGdXmgZF3nRU';
Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as Data from 'effect/Data';
2-
import * as Schema from 'effect/Schema';
3-
import type { AnyNoContext, EntityWithRelation } from './types.js';
2+
import type { AnyNoContext } from './types.js';
43
import * as VariantSchema from './variant-schema.js';
54

65
const {
@@ -18,32 +17,10 @@ const {
1817
defaultVariant: 'select',
1918
});
2019

21-
export { Class };
22-
23-
export const Text = Schema.String;
24-
// biome-ignore lint/suspicious/noShadowRestrictedNames: is part of a namespaces module and therefor ok
25-
export const Number = Schema.Number;
26-
export const Checkbox = Schema.Boolean;
27-
// biome-ignore lint/suspicious/noShadowRestrictedNames: is part of a namespaces module and therefor ok
28-
export const Date = Schema.Date;
29-
export const Url = Schema.URL;
30-
export const Point = Schema.transform(Schema.String, Schema.Array(Number), {
31-
strict: true,
32-
decode: (str: string) => {
33-
return str.split(',').map((n: string) => globalThis.Number(n));
34-
},
35-
encode: (points: readonly number[]) => points.join(','),
36-
});
20+
export { Class, Field };
3721

3822
export class EntityNotFoundError extends Data.TaggedError('EntityNotFoundError')<{
3923
id: string;
4024
type: AnyNoContext;
4125
cause?: unknown;
4226
}> {}
43-
44-
export const Relation = <S extends AnyNoContext>(schema: S) =>
45-
Field({
46-
select: Schema.Array(schema) as unknown as Schema.Schema<ReadonlyArray<EntityWithRelation<S>>>,
47-
insert: Schema.optional(Schema.Array(Schema.String)),
48-
update: Schema.Undefined,
49-
});

packages/hypergraph/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export * as Entity from './entity/index.js';
22
export * as Identity from './identity/index.js';
3+
export * as Inboxes from './inboxes/index.js';
34
export * as Key from './key/index.js';
45
export * as Messages from './messages/index.js';
56
export * as SpaceEvents from './space-events/index.js';
6-
export * as Inboxes from './inboxes/index.js';
77
export * from './store.js';
8+
export * as Type from './type/type.js';
89
export * as Utils from './utils/index.js';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as Schema from 'effect/Schema';
2+
import { Field } from '../entity/entity.js';
3+
import type { AnyNoContext, EntityWithRelation } from '../entity/types.js';
4+
5+
export const Text = Schema.String;
6+
// biome-ignore lint/suspicious/noShadowRestrictedNames: is part of a namespaces module and therefor ok
7+
export const Number = Schema.Number;
8+
export const Checkbox = Schema.Boolean;
9+
// biome-ignore lint/suspicious/noShadowRestrictedNames: is part of a namespaces module and therefor ok
10+
export const Date = Schema.Date;
11+
export const Url = Schema.URL;
12+
export const Point = Schema.transform(Schema.String, Schema.Array(Number), {
13+
strict: true,
14+
decode: (str: string) => {
15+
return str.split(',').map((n: string) => globalThis.Number(n));
16+
},
17+
encode: (points: readonly number[]) => points.join(','),
18+
});
19+
20+
export const Relation = <S extends AnyNoContext>(schema: S) =>
21+
Field({
22+
select: Schema.Array(schema) as unknown as Schema.Schema<ReadonlyArray<EntityWithRelation<S>>>,
23+
insert: Schema.optional(Schema.Array(Schema.String)),
24+
update: Schema.Undefined,
25+
});

0 commit comments

Comments
 (0)