Skip to content

Commit 5324cc8

Browse files
committed
cleanup
1 parent e5eee3d commit 5324cc8

File tree

6 files changed

+6
-76
lines changed

6 files changed

+6
-76
lines changed

packages/hypergraph-react/src/hooks/use-create-entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export function useCreateEntity<const S extends Schema.Schema.AnyNoContext>(type
1515
throw new Error('Space not found or not ready');
1616
};
1717
}
18-
return Entity.createNew(handle, type);
18+
return Entity.create(handle, type);
1919
}

packages/hypergraph/src/entity/create.ts

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { DocHandle } from '@automerge/automerge-repo';
22
import * as Option from 'effect/Option';
3-
import * as Schema from 'effect/Schema';
3+
import type * as Schema from 'effect/Schema';
44
import * as SchemaAST from 'effect/SchemaAST';
55
import { PropertyIdSymbol } from '../constants.js';
66
import { generateId } from '../utils/generateId.js';
77
import { isRelation } from '../utils/isRelation.js';
8-
import { isRelationField } from '../utils/isRelationField.js';
98
import { encodeToGrc20Json } from './entity.js';
109
import { findOne } from './findOne.js';
11-
import type { AnyNoContext, DocumentContent, DocumentRelation, Entity, Insert } from './types.js';
10+
import type { DocumentContent, DocumentRelation, Entity } from './types.js';
1211

1312
/**
1413
* Type utility to transform relation fields to accept string arrays instead of their typed values
@@ -26,59 +25,7 @@ type WithRelationsAsStringArrays<T> = {
2625
: T[K];
2726
};
2827

29-
/**
30-
* Creates an entity model of given type and stores it in the repo.
31-
*/
32-
export const create = <const S extends AnyNoContext>(handle: DocHandle<DocumentContent>, type: S) => {
33-
// TODO: what's the right way to get the name of the type?
34-
// @ts-expect-error name is defined
35-
const typeName = type.name;
36-
const encode = Schema.encodeSync(type.insert);
37-
38-
return (data: Readonly<Schema.Schema.Type<Insert<S>>>): Entity<S> => {
39-
const entityId = generateId();
40-
const encoded = encode(data);
41-
42-
const relations: Record<string, DocumentRelation> = {};
43-
44-
for (const [propertyName, field] of Object.entries(type.fields)) {
45-
if (isRelationField(field) && encoded[propertyName]) {
46-
for (const toEntityId of encoded[propertyName]) {
47-
const relationId = generateId();
48-
relations[relationId] = {
49-
from: entityId,
50-
to: toEntityId,
51-
fromTypeName: typeName,
52-
fromPropertyName: propertyName,
53-
__deleted: false,
54-
};
55-
}
56-
// we create the relation object in the repo, so we don't need it in the entity
57-
delete encoded[propertyName];
58-
}
59-
}
60-
61-
// apply changes to the repo -> adds the entity to the repo entities document
62-
handle.change((doc) => {
63-
doc.entities ??= {};
64-
doc.entities[entityId] = {
65-
...encoded,
66-
'@@types@@': [typeName],
67-
__deleted: false,
68-
__version: '',
69-
};
70-
doc.relations ??= {};
71-
// merge relations with existing relations
72-
for (const [relationId, relation] of Object.entries(relations)) {
73-
doc.relations[relationId] = relation;
74-
}
75-
});
76-
77-
return findOne(handle, type)(entityId) as Entity<S>;
78-
};
79-
};
80-
81-
export const createNew = <const S extends Schema.Schema.AnyNoContext>(handle: DocHandle<DocumentContent>, type: S) => {
28+
export const create = <const S extends Schema.Schema.AnyNoContext>(handle: DocHandle<DocumentContent>, type: S) => {
8229
return (data: Readonly<WithRelationsAsStringArrays<Schema.Schema.Type<S>>>): Entity<S> => {
8330
const entityId = generateId();
8431
const encoded = encodeToGrc20Json(type, { ...data, id: entityId });

packages/hypergraph/src/entity/findMany.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,6 @@ export function subscribeToFindMany<const S extends Schema.Schema.AnyNoContext>(
444444
return query.data;
445445
};
446446

447-
// const allTypes = new Set<S>();
448-
// for (const [_key, field] of Object.entries(type.fields)) {
449-
// if (isRelationField(field)) {
450-
// allTypes.add(field as S);
451-
// }
452-
// }
453-
454447
const subscribe = (callback: () => void) => {
455448
let cacheEntry = decodedEntitiesCache.get(canonicalize(typeIds));
456449

packages/hypergraph/src/entity/getEntityRelations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as SchemaAST from 'effect/SchemaAST';
44
import { PropertyIdSymbol, RelationSchemaSymbol } from '../constants.js';
55
import { isRelation } from '../utils/isRelation.js';
66
import { decodeFromGrc20Json } from './entity.js';
7+
import { hasValidTypesProperty } from './hasValidTypesProperty.js';
78
import type { DocumentContent, Entity } from './types.js';
89

910
export const getEntityRelations = <const S extends Schema.Schema.AnyNoContext>(
@@ -38,8 +39,7 @@ export const getEntityRelations = <const S extends Schema.Schema.AnyNoContext>(
3839

3940
const relationEntity = doc.entities?.[relation.to];
4041
const decodedRelationEntity = { ...decodeFromGrc20Json(schema.value, { ...relationEntity, id: relation.to }) };
41-
// TODO: should we check if the relation entity is valid?
42-
// if (!hasValidTypesProperty(relationEntity)) continue;
42+
if (!hasValidTypesProperty(relationEntity)) continue;
4343

4444
relationEntities.push({ ...decodedRelationEntity, id: relation.to, _relation: { id: relationId } });
4545
}

packages/hypergraph/src/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ export * from './base58.js';
44
export * from './generateId.js';
55
export * from './hexBytesAddressUtils.js';
66
export * from './isRelation.js';
7-
export * from './isRelationField.js';
87
export * from './jsc.js';
98
export * from './stringToUint8Array.js';

packages/hypergraph/src/utils/isRelationField.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)