@@ -4,7 +4,7 @@ import { typesData } from '../data/typesData';
44// Type definitions for the data structure
55interface Property {
66 id : string ;
7- dataType : 'TEXT' | 'NUMBER' | 'RELATION' | 'CHECKBOX' | 'DATE' | 'POINT' | 'URL' ;
7+ dataType : string ;
88 relationValueTypes : Array < {
99 id : string ;
1010 name : string ;
@@ -40,6 +40,15 @@ interface MappingEntry {
4040 relations ?: Record < string , string > ;
4141}
4242
43+ interface TypeWithSchemaAndMapping {
44+ id : string ;
45+ name : string ;
46+ className : string ;
47+ properties : Property [ ] ;
48+ schema : string ;
49+ mapping : string ;
50+ }
51+
4352// Helper function to convert dataType to Type
4453function dataTypeToType (
4554 dataType : string ,
@@ -93,6 +102,21 @@ function createClassName(typeName: string): string {
93102 . join ( '' ) ;
94103}
95104
105+ // Helper function to convert string to camelCase
106+ function toCamelCase ( str : string ) : string {
107+ return str
108+ . split ( ' ' )
109+ . map ( ( word , index ) => {
110+ if ( index === 0 ) {
111+ // First word should be lowercase
112+ return word . toLowerCase ( ) ;
113+ }
114+ // Subsequent words should be capitalized
115+ return word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) . toLowerCase ( ) ;
116+ } )
117+ . join ( '' ) ;
118+ }
119+
96120export function convertTypesDataToSchemaAndMapping ( ) {
97121 const schemaEntries : string [ ] = [ ] ;
98122 const mappingEntries : Record < string , MappingEntry > = { } ;
@@ -117,7 +141,7 @@ export function convertTypesDataToSchemaAndMapping() {
117141 const mappingRelations : Record < string , string > = { } ;
118142
119143 for ( const property of type . properties ) {
120- const propertyName = property . entity . name . toLowerCase ( ) . replace ( / \s + / g , '' ) ;
144+ const propertyName = toCamelCase ( property . entity . name ) ;
121145
122146 if ( property . dataType === 'RELATION' ) {
123147 const targetClassName = getRelationTargetClassName ( property . relationValueTypes ) ;
@@ -204,3 +228,89 @@ ${mappingEntries}
204228};
205229` ;
206230}
231+
232+ // Function to generate schema for a single type
233+ export function generateSchemaForType ( type : TypeData ) : string {
234+ const className = createClassName ( type . name ) ;
235+
236+ const properties : string [ ] = [ ] ;
237+
238+ for ( const property of type . properties ) {
239+ const propertyName = toCamelCase ( property . entity . name ) ;
240+
241+ if ( property . dataType === 'RELATION' ) {
242+ const targetClassName = getRelationTargetClassName ( property . relationValueTypes ) ;
243+ if ( targetClassName ) {
244+ const targetClass = createClassName ( targetClassName ) ;
245+ properties . push ( ` ${ propertyName } : Type.Relation(${ targetClass } )` ) ;
246+ }
247+ } else {
248+ const typeInstance = dataTypeToType ( property . dataType ) ;
249+ const typeName = typeInstance . name . endsWith ( '$' ) ? typeInstance . name . slice ( 0 , - 1 ) : typeInstance . name ;
250+ properties . push ( ` ${ propertyName } : Type.${ typeName } ` ) ;
251+ }
252+ }
253+
254+ return `export class ${ className } extends Entity.Class<${ className } >('${ className } ')({
255+ ${ properties . join ( ',\n' ) }
256+ }) {}` ;
257+ }
258+
259+ // Function to generate mapping for a single type
260+ export function generateMappingForType ( type : TypeData ) : string {
261+ const className = createClassName ( type . name ) ;
262+
263+ const mappingProperties : Record < string , string > = { } ;
264+ const mappingRelations : Record < string , string > = { } ;
265+
266+ for ( const property of type . properties ) {
267+ const propertyName = toCamelCase ( property . entity . name ) ;
268+
269+ if ( property . dataType === 'RELATION' ) {
270+ const targetClassName = getRelationTargetClassName ( property . relationValueTypes ) ;
271+ if ( targetClassName ) {
272+ mappingRelations [ propertyName ] = `Id.Id('${ property . id } ')` ;
273+ }
274+ } else {
275+ mappingProperties [ propertyName ] = `Id.Id('${ property . id } ')` ;
276+ }
277+ }
278+
279+ const properties = Object . entries ( mappingProperties )
280+ . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ` )
281+ . join ( ',\n' ) ;
282+
283+ const relations = Object . entries ( mappingRelations )
284+ . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ` )
285+ . join ( ',\n' ) ;
286+
287+ return ` ${ className } : {
288+ typeIds: [Id.Id('${ type . id } ')],
289+ properties: {
290+ ${ properties }
291+ },
292+ ${
293+ relations
294+ ? ` relations: {
295+ ${ relations }
296+ },`
297+ : ''
298+ }
299+ }` ;
300+ }
301+
302+ // Function to get all types with their individual schema and mapping
303+ export function getTypesWithSchemaAndMapping ( ) : TypeWithSchemaAndMapping [ ] {
304+ return typesData . types . map ( ( type ) => {
305+ const className = createClassName ( type . name ) ;
306+
307+ return {
308+ id : type . id ,
309+ name : type . name ,
310+ className,
311+ properties : type . properties ,
312+ schema : generateSchemaForType ( type ) ,
313+ mapping : generateMappingForType ( type ) ,
314+ } ;
315+ } ) ;
316+ }
0 commit comments