|
| 1 | +import { Type } from '@graphprotocol/hypergraph'; |
| 2 | +import { typesData } from '../data/typesData'; |
| 3 | + |
| 4 | +// Type definitions for the data structure |
| 5 | +interface Property { |
| 6 | + id: string; |
| 7 | + dataType: 'TEXT' | 'NUMBER' | 'RELATION' | 'CHECKBOX' | 'DATE' | 'POINT' | 'URL'; |
| 8 | + relationValueTypes: Array<{ |
| 9 | + id: string; |
| 10 | + name: string; |
| 11 | + description: string | null; |
| 12 | + properties: Array<{ |
| 13 | + id: string; |
| 14 | + dataType: string; |
| 15 | + entity: { |
| 16 | + id: string; |
| 17 | + name: string; |
| 18 | + }; |
| 19 | + }>; |
| 20 | + }>; |
| 21 | + entity: { |
| 22 | + id: string; |
| 23 | + name: string; |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +interface TypeData { |
| 28 | + id: string; |
| 29 | + name: string; |
| 30 | + properties: Property[]; |
| 31 | +} |
| 32 | + |
| 33 | +interface TypesData { |
| 34 | + types: TypeData[]; |
| 35 | +} |
| 36 | + |
| 37 | +interface MappingEntry { |
| 38 | + typeIds: string[]; |
| 39 | + properties: Record<string, string>; |
| 40 | + relations?: Record<string, string>; |
| 41 | +} |
| 42 | + |
| 43 | +// Helper function to convert dataType to Type |
| 44 | +function dataTypeToType( |
| 45 | + dataType: string, |
| 46 | +): |
| 47 | + | typeof Type.Text |
| 48 | + | typeof Type.Number |
| 49 | + | typeof Type.Relation |
| 50 | + | typeof Type.Checkbox |
| 51 | + | typeof Type.Date |
| 52 | + | typeof Type.Point |
| 53 | + | typeof Type.Url { |
| 54 | + switch (dataType) { |
| 55 | + case 'TEXT': |
| 56 | + return Type.Text; |
| 57 | + case 'NUMBER': |
| 58 | + return Type.Number; |
| 59 | + case 'RELATION': |
| 60 | + return Type.Relation; // This will need to be handled specially |
| 61 | + case 'CHECKBOX': |
| 62 | + return Type.Checkbox; |
| 63 | + case 'DATE': |
| 64 | + return Type.Date; |
| 65 | + case 'POINT': |
| 66 | + return Type.Point; |
| 67 | + case 'URL': |
| 68 | + return Type.Url; |
| 69 | + default: |
| 70 | + return Type.Text; // fallback |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Helper function to get relation target class name |
| 75 | +function getRelationTargetClassName( |
| 76 | + relationValueTypes: Array<{ |
| 77 | + id: string; |
| 78 | + name: string; |
| 79 | + description: string | null; |
| 80 | + properties: Array<{ id: string; dataType: string; entity: { id: string; name: string } }>; |
| 81 | + }>, |
| 82 | +): string | null { |
| 83 | + if (relationValueTypes.length === 0) return null; |
| 84 | + return relationValueTypes[0].name; |
| 85 | +} |
| 86 | + |
| 87 | +// Helper function to create a class name from type name |
| 88 | +function createClassName(typeName: string): string { |
| 89 | + // Convert to PascalCase and handle special cases |
| 90 | + return typeName |
| 91 | + .split(' ') |
| 92 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) |
| 93 | + .join(''); |
| 94 | +} |
| 95 | + |
| 96 | +export function convertTypesDataToSchemaAndMapping() { |
| 97 | + const schemaEntries: string[] = []; |
| 98 | + const mappingEntries: Record<string, MappingEntry> = {}; |
| 99 | + |
| 100 | + // Create a map of type names to their IDs for relation handling |
| 101 | + const typeNameToId = new Map<string, string>(); |
| 102 | + const typeIdToName = new Map<string, string>(); |
| 103 | + |
| 104 | + // First pass: collect all type names and IDs |
| 105 | + for (const type of typesData.types) { |
| 106 | + typeNameToId.set(type.name, type.id); |
| 107 | + typeIdToName.set(type.id, type.name); |
| 108 | + } |
| 109 | + |
| 110 | + // Second pass: generate schema and mapping |
| 111 | + for (const type of typesData.types) { |
| 112 | + const className = createClassName(type.name); |
| 113 | + |
| 114 | + // Generate schema entry |
| 115 | + const properties: string[] = []; |
| 116 | + const mappingProperties: Record<string, string> = {}; |
| 117 | + const mappingRelations: Record<string, string> = {}; |
| 118 | + |
| 119 | + for (const property of type.properties) { |
| 120 | + const propertyName = property.entity.name.toLowerCase().replace(/\s+/g, ''); |
| 121 | + |
| 122 | + if (property.dataType === 'RELATION') { |
| 123 | + const targetClassName = getRelationTargetClassName(property.relationValueTypes); |
| 124 | + if (targetClassName) { |
| 125 | + const targetClass = createClassName(targetClassName); |
| 126 | + properties.push(` ${propertyName}: Type.Relation(${targetClass})`); |
| 127 | + mappingRelations[propertyName] = `Id.Id('${property.id}')`; |
| 128 | + } |
| 129 | + } else { |
| 130 | + const typeInstance = dataTypeToType(property.dataType); |
| 131 | + const typeName = typeInstance.name.endsWith('$') ? typeInstance.name.slice(0, -1) : typeInstance.name; |
| 132 | + properties.push(` ${propertyName}: Type.${typeName}`); |
| 133 | + mappingProperties[propertyName] = `Id.Id('${property.id}')`; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Generate schema class |
| 138 | + const schemaClass = `export class ${className} extends Entity.Class<${className}>('${className}')({ |
| 139 | +${properties.join(',\n')} |
| 140 | +}) {}`; |
| 141 | + |
| 142 | + schemaEntries.push(schemaClass); |
| 143 | + |
| 144 | + // Generate mapping entry |
| 145 | + mappingEntries[className] = { |
| 146 | + typeIds: [`Id.Id('${type.id}')`], |
| 147 | + properties: mappingProperties, |
| 148 | + ...(Object.keys(mappingRelations).length > 0 && { relations: mappingRelations }), |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + return { |
| 153 | + schema: schemaEntries.join('\n\n'), |
| 154 | + mapping: mappingEntries, |
| 155 | + }; |
| 156 | +} |
| 157 | + |
| 158 | +// Function to generate the complete schema file content |
| 159 | +export function generateSchemaFile(): string { |
| 160 | + const { schema } = convertTypesDataToSchemaAndMapping(); |
| 161 | + return `import { Entity, Type } from '@graphprotocol/hypergraph'; |
| 162 | +
|
| 163 | +${schema} |
| 164 | +`; |
| 165 | +} |
| 166 | + |
| 167 | +// Function to generate the complete mapping file content |
| 168 | +export function generateMappingFile(): string { |
| 169 | + const { mapping } = convertTypesDataToSchemaAndMapping(); |
| 170 | + |
| 171 | + const mappingEntries = Object.entries(mapping) |
| 172 | + .map(([className, mappingData]) => { |
| 173 | + const properties = Object.entries(mappingData.properties || {}) |
| 174 | + .map(([key, value]) => ` ${key}: ${value}`) |
| 175 | + .join(',\n'); |
| 176 | + |
| 177 | + const relations = mappingData.relations |
| 178 | + ? Object.entries(mappingData.relations) |
| 179 | + .map(([key, value]) => ` ${key}: ${value}`) |
| 180 | + .join(',\n') |
| 181 | + : ''; |
| 182 | + |
| 183 | + return ` ${className}: { |
| 184 | + typeIds: [${mappingData.typeIds.join(', ')}], |
| 185 | + properties: { |
| 186 | +${properties} |
| 187 | + }, |
| 188 | +${ |
| 189 | + relations |
| 190 | + ? ` relations: { |
| 191 | +${relations} |
| 192 | + },` |
| 193 | + : '' |
| 194 | +} |
| 195 | + }`; |
| 196 | + }) |
| 197 | + .join(',\n\n'); |
| 198 | + |
| 199 | + return `import { Id } from '@graphprotocol/grc-20'; |
| 200 | +import type { Mapping } from '@graphprotocol/hypergraph'; |
| 201 | +
|
| 202 | +export const mapping: Mapping = { |
| 203 | +${mappingEntries} |
| 204 | +}; |
| 205 | +`; |
| 206 | +} |
0 commit comments