@@ -21,140 +21,72 @@ type RecursiveQueryEntity = {
2121 point : string ;
2222 } [ ] ;
2323 relationsList ?: {
24+ id : string ;
2425 toEntity : RecursiveQueryEntity ;
2526 typeId : string ;
2627 } [ ] ;
2728} ;
2829
29- export const convertRelations = < S extends Schema . Schema . AnyNoContext > ( queryEntity : RecursiveQueryEntity , type : S ) => {
30+ export const convertRelations = < S extends Schema . Schema . AnyNoContext > (
31+ queryEntity : RecursiveQueryEntity ,
32+ ast : SchemaAST . TypeLiteral ,
33+ ) => {
3034 const rawEntity : Record < string , string | boolean | number | unknown [ ] | Date > = { } ;
3135
32- const ast = type . ast as SchemaAST . TypeLiteral ;
33-
34- // console.log('queryEntity', queryEntity);
35-
3636 for ( const prop of ast . propertySignatures ) {
3737 const result = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( prop . type ) ;
3838
39- if ( isRelation ( prop . type ) ) {
39+ if ( isRelation ( prop . type ) && Option . isSome ( result ) ) {
4040 rawEntity [ String ( prop . name ) ] = [ ] ;
4141
42- if ( ! queryEntity . valuesList ) {
42+ const relationTransformation = prop . type . rest ?. [ 0 ] ?. type ;
43+ const typeIds : string [ ] = SchemaAST . getAnnotation < string [ ] > ( TypeIdsSymbol ) ( relationTransformation ) . pipe (
44+ Option . getOrElse ( ( ) => [ ] ) ,
45+ ) ;
46+ if ( typeIds . length === 0 ) {
4347 continue ;
4448 }
4549
46- if ( Option . isSome ( result ) ) {
47- const relationTransformation = prop . type . rest ?. [ 0 ] ?. type ;
48- const typeIds : string [ ] = SchemaAST . getAnnotation < string [ ] > ( TypeIdsSymbol ) ( relationTransformation ) . pipe (
49- Option . getOrElse ( ( ) => [ ] ) ,
50- ) ;
51- if ( typeIds . length === 0 ) {
52- continue ;
53- }
54-
55- const allRelationsWithTheCorrectPropertyTypeId = queryEntity . relationsList ?. filter (
56- ( a ) => a . typeId === result . value ,
57- ) ;
58- if ( allRelationsWithTheCorrectPropertyTypeId ) {
59- for ( const relationEntry of allRelationsWithTheCorrectPropertyTypeId ) {
60- const nestedRawEntity :
61- | Record < string , string | boolean | number | unknown [ ] | Date >
62- | { _relation : { id : string } } = {
63- id : relationEntry . toEntity . id ,
64- _relation : {
65- id : 'TODO: relation id' ,
66- } ,
67- } ;
68-
69- for ( const nestedProp of relationTransformation . propertySignatures ) {
70- const nestedResult = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( nestedProp . type ) ;
71- if ( Option . isSome ( nestedResult ) ) {
72- const value = relationEntry . toEntity . valuesList ?. find ( ( a ) => a . propertyId === nestedResult . value ) ;
73- if ( ! value ) {
74- continue ;
75- }
76- const rawValue = convertPropertyValue ( value , nestedProp . type ) ;
77- if ( rawValue ) {
78- nestedRawEntity [ String ( nestedProp . name ) ] = rawValue ;
79- }
50+ const allRelationsWithTheCorrectPropertyTypeId = queryEntity . relationsList ?. filter (
51+ ( a ) => a . typeId === result . value ,
52+ ) ;
53+ if ( allRelationsWithTheCorrectPropertyTypeId ) {
54+ for ( const relationEntry of allRelationsWithTheCorrectPropertyTypeId ) {
55+ let nestedRawEntity :
56+ | Record < string , string | boolean | number | unknown [ ] | Date >
57+ | { _relation : { id : string } } = {
58+ id : relationEntry . toEntity . id ,
59+ _relation : {
60+ id : relationEntry . id ,
61+ } ,
62+ } ;
63+
64+ const relationsForRawNestedEntity = convertRelations ( relationEntry . toEntity , relationTransformation ) ;
65+
66+ nestedRawEntity = {
67+ ...nestedRawEntity ,
68+ ...relationsForRawNestedEntity ,
69+ } ;
70+
71+ for ( const nestedProp of relationTransformation . propertySignatures ) {
72+ const nestedResult = SchemaAST . getAnnotation < string > ( PropertyIdSymbol ) ( nestedProp . type ) ;
73+ if ( Option . isSome ( nestedResult ) ) {
74+ const value = relationEntry . toEntity . valuesList ?. find ( ( a ) => a . propertyId === nestedResult . value ) ;
75+ if ( ! value ) {
76+ continue ;
77+ }
78+ const rawValue = convertPropertyValue ( value , nestedProp . type ) ;
79+ if ( rawValue ) {
80+ nestedRawEntity [ String ( nestedProp . name ) ] = rawValue ;
8081 }
81- // TODO: in the end every entry should be validated using the Schema?!?
82- rawEntity [ String ( prop . name ) ] = [ ...( rawEntity [ String ( prop . name ) ] as unknown [ ] ) , nestedRawEntity ] ;
8382 }
8483 }
84+ // TODO: in the end every entry should be validated using the Schema?!?
85+ rawEntity [ String ( prop . name ) ] = [ ...( rawEntity [ String ( prop . name ) ] as unknown [ ] ) , nestedRawEntity ] ;
8586 }
8687 }
8788 }
8889 }
8990
90- // for (const [key, relationId] of Object.entries(mappingEntry?.relations ?? {})) {
91- // const properties = (queryEntity.relationsList ?? []).filter((a) => a.typeId === relationId);
92- // if (properties.length === 0) {
93- // rawEntity[key] = [] as unknown[];
94- // continue;
95- // }
96-
97- // const field = type.fields[key];
98- // if (!field) {
99- // // @ts -expect-error TODO: properly access the type.name
100- // console.error(`Field ${key} not found in ${type.name}`);
101- // continue;
102- // }
103- // const relationTransformation = field.ast.rest?.[0];
104- // if (!relationTransformation) {
105- // console.error(`Relation transformation for ${key} not found`);
106- // continue;
107- // }
108-
109- // const identifierAnnotation = SchemaAST.getIdentifierAnnotation(relationTransformation.type.to);
110- // if (Option.isNone(identifierAnnotation)) {
111- // console.error(`Relation identifier for ${key} not found`);
112- // continue;
113- // }
114-
115- // const relationTypeName = identifierAnnotation.value;
116-
117- // const relationMappingEntry = mapping[relationTypeName];
118- // if (!relationMappingEntry) {
119- // console.error(`Relation mapping entry for ${relationTypeName} not found`);
120- // continue;
121- // }
122-
123- // const newRelationEntities = properties.map((propertyEntry) => {
124- // // @ts -expect-error TODO: properly access the type.name
125- // const type = field.value;
126-
127- // let rawEntity: Record<string, string | boolean | number | unknown[] | Date> = {
128- // id: propertyEntry.toEntity.id,
129- // name: propertyEntry.toEntity.name,
130- // // TODO: should be determined by the actual value
131- // __deleted: false,
132- // // TODO: should be determined by the actual value
133- // __version: '',
134- // };
135-
136- // // take the mappingEntry and assign the attributes to the rawEntity
137- // for (const [key, value] of Object.entries(relationMappingEntry?.properties ?? {})) {
138- // const property = propertyEntry.toEntity.valuesList?.find((a) => a.propertyId === value);
139- // if (property) {
140- // rawEntity[key] = convertPropertyValue(property, type);
141- // }
142- // }
143-
144- // rawEntity = {
145- // ...rawEntity,
146- // ...convertRelations(propertyEntry.toEntity, type, relationMappingEntry, mapping),
147- // };
148-
149- // return rawEntity;
150- // });
151-
152- // if (rawEntity[key]) {
153- // rawEntity[key] = [...(rawEntity[key] as unknown[]), ...newRelationEntities];
154- // } else {
155- // rawEntity[key] = newRelationEntities;
156- // }
157- // }
158-
15991 return rawEntity ;
16092} ;
0 commit comments