@@ -117,6 +117,7 @@ export class SchemaGenerator extends Effect.Service<SchemaGenerator>()('/typesyn
117117 ) ,
118118 Effect . andThen ( ( ) => updatePackageJson ( app , directory ) ) ,
119119 Effect . andThen ( ( ) => fs . writeFileString ( path . join ( directory , 'src' , 'schema.ts' ) , buildSchemaFile ( app ) ) ) ,
120+ Effect . andThen ( ( ) => fs . writeFileString ( path . join ( directory , 'src' , 'mapping.ts' ) , buildMappingFile ( app ) ) ) ,
120121 Effect . andThen ( ( ) => cleanup ) ,
121122 ) ;
122123
@@ -290,9 +291,55 @@ ${fieldStrings.join(',\n')}
290291
291292function buildSchemaFile ( schema : Domain . InsertAppSchema ) {
292293 const importStatement = `import { Entity, Type } from '@graphprotocol/hypergraph';` ;
294+
293295 const typeDefinitions = schema . types
294296 . map ( typeDefinitionToString )
295297 . filter ( ( def ) => def != null )
296298 . join ( '\n\n' ) ;
297299 return [ importStatement , typeDefinitions ] . join ( '\n\n' ) ;
298300}
301+
302+ export function buildMappingFile ( schema : Domain . InsertAppSchema ) {
303+ const importStatement1 = `import { Id } from '@graphprotocol/grc-20';` ;
304+ const importStatement2 = `import type { Mapping } from '@graphprotocol/hypergraph';` ;
305+
306+ const typeMappings : string [ ] = [ ] ;
307+
308+ for ( const type of schema . types ) {
309+ const properties : string [ ] = [ ] ;
310+ const relations : string [ ] = [ ] ;
311+
312+ // Process properties and relations
313+ for ( const property of type . properties ) {
314+ if ( Domain . isDataTypeRelation ( property . dataType ) ) {
315+ // This is a relation
316+ relations . push ( ` ${ Utils . toCamelCase ( property . name ) } : Id.Id('${ property . knowledgeGraphId } ')` ) ;
317+ } else {
318+ // This is a regular property
319+ properties . push ( ` ${ Utils . toCamelCase ( property . name ) } : Id.Id('${ property . knowledgeGraphId } ')` ) ;
320+ }
321+ }
322+
323+ const typeMapping = ` ${ type . name } : {
324+ typeIds: [Id.Id('${ type . knowledgeGraphId } ')],
325+ properties: {
326+ ${ properties . join ( ',\n' ) } ,
327+ },${
328+ relations . length > 0
329+ ? `
330+ relations: {
331+ ${ relations . join ( ',\n' ) } ,
332+ },`
333+ : ''
334+ }
335+ }` ;
336+
337+ typeMappings . push ( typeMapping ) ;
338+ }
339+
340+ const mappingString = `export const mapping: Mapping = {
341+ ${ typeMappings . join ( ',\n' ) } ,
342+ };` ;
343+
344+ return [ importStatement1 , importStatement2 , '' , mappingString ] . join ( '\n' ) ;
345+ }
0 commit comments