11import type { DocHandle } from '@automerge/automerge-repo' ;
22import * as Option from 'effect/Option' ;
3- import * as Schema from 'effect/Schema' ;
3+ import type * as Schema from 'effect/Schema' ;
44import * as SchemaAST from 'effect/SchemaAST' ;
55import { PropertyIdSymbol } from '../constants.js' ;
66import { generateId } from '../utils/generateId.js' ;
77import { isRelation } from '../utils/isRelation.js' ;
8- import { isRelationField } from '../utils/isRelationField.js' ;
98import { encodeToGrc20Json } from './entity.js' ;
109import { 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 } ) ;
0 commit comments