Skip to content

Commit c0f1538

Browse files
committed
wip
1 parent d8cc003 commit c0f1538

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

apps/events/src/components/todos.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const Todos = () => {
5656
createEntity({
5757
name: newTodoName,
5858
completed: false,
59-
// assignees: assignees.map(({ value }) => value),
59+
assignees: assignees.map(({ value }) => value),
6060
});
6161
setNewTodoName('');
6262
}}

apps/events/src/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ export const TodoNew = EntityNew(
5858
{
5959
name: TypeNew.String,
6060
completed: TypeNew.Boolean,
61-
// assignees: TypeNew.Relation(UserNew),
61+
assignees: TypeNew.Relation(UserNew),
6262
},
6363
{
6464
types: [Id('44fe82a9-e4c2-4330-a395-ce85ed78e421')],
6565
properties: {
6666
name: Id('c668aa67-bbca-4b2c-908c-9c5599035eab'),
6767
completed: Id('71e7654f-2623-4794-88fb-841c8f3dd9b4'),
68+
assignees: Id('5b80d3ee-2463-4246-b628-44ba808ab3e1'),
6869
},
6970
},
7071
);

packages/hypergraph/src/entity/create.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import { encodeToGrc20Json } from './entity-new.js';
66
import { findOne, findOneNew } from './findOne.js';
77
import type { AnyNoContext, DocumentContent, DocumentRelation, Entity, Insert } from './types.js';
88

9+
/**
10+
* Type utility to transform relation fields to accept string arrays instead of their typed values
11+
* This specifically targets TypeNew.Relation fields which are arrays of objects
12+
*/
13+
type WithRelationsAsStringArrays<T> = {
14+
[K in keyof T]: T[K] extends readonly (infer U)[]
15+
? U extends object
16+
? string[]
17+
: T[K]
18+
: T[K] extends (infer U)[]
19+
? U extends object
20+
? string[]
21+
: T[K]
22+
: T[K];
23+
};
24+
925
/**
1026
* Creates an entity model of given type and stores it in the repo.
1127
*/
@@ -60,7 +76,7 @@ export const create = <const S extends AnyNoContext>(handle: DocHandle<DocumentC
6076

6177
export const createNew = <const S extends Schema.Schema.AnyNoContext>(handle: DocHandle<DocumentContent>, type: S) => {
6278
// TODO: return type
63-
return (data: Readonly<Schema.Schema.Type<S>>) => {
79+
return (data: Readonly<WithRelationsAsStringArrays<Schema.Schema.Type<S>>>) => {
6480
const entityId = generateId();
6581
const encoded = encodeToGrc20Json(type, { ...data, id: entityId });
6682

packages/hypergraph/src/entity/findManyNew.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export function findManyNew<const S extends Schema.Schema.AnyNoContext>(
247247
);
248248

249249
const doc = handle.doc();
250+
console.log('doc', doc);
250251
if (!doc) {
251252
return { entities: [], corruptEntityIds: [] };
252253
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const PropertyIdSymbol = Symbol.for('grc-20/property/id');
22

33
export const TypeIdsSymbol = Symbol.for('grc-20/type/ids');
4+
5+
export const RelationSchemaSymbol = Symbol.for('grc-20/relation/schema');

packages/hypergraph/src/entity/type-new.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Schema from 'effect/Schema';
2-
import { PropertyIdSymbol } from './internal-new.js';
2+
import { PropertyIdSymbol, RelationSchemaSymbol } from './internal-new.js';
33

44
/**
55
* Creates a String schema with the specified GRC-20 property ID
@@ -32,3 +32,11 @@ export const Boolean = (propertyId: string) => {
3232
export const Date = (propertyId: string) => {
3333
return Schema.Date.pipe(Schema.annotations({ [PropertyIdSymbol]: propertyId }));
3434
};
35+
36+
export const Relation =
37+
<S extends Schema.Schema.AnyNoContext>(schema: S) =>
38+
(propertyId: string) => {
39+
return Schema.Array(schema).pipe(
40+
Schema.annotations({ [PropertyIdSymbol]: propertyId, [RelationSchemaSymbol]: schema }),
41+
);
42+
};

0 commit comments

Comments
 (0)