@@ -18,55 +18,123 @@ export interface ObjectConfig {
1818
1919/**
2020 * Schema Registry - manages object schemas
21+ * Uses a singleton pattern with static methods for global access
2122 */
2223export class SchemaRegistry {
23- private schemas = new Map < string , ObjectConfig > ( ) ;
24+ private static instance : SchemaRegistry ;
25+ private schemas = new Map < string , Map < string , any > > ( ) ;
2426
27+ private constructor ( ) { }
28+
29+ private static getInstance ( ) : SchemaRegistry {
30+ if ( ! SchemaRegistry . instance ) {
31+ SchemaRegistry . instance = new SchemaRegistry ( ) ;
32+ }
33+ return SchemaRegistry . instance ;
34+ }
35+
36+ /**
37+ * Register an item in the registry
38+ * @param type - The type of item (e.g., 'object', 'field', 'action')
39+ * @param item - The item to register
40+ * @param idField - The field to use as the ID (default: 'name')
41+ */
42+ static registerItem ( type : string , item : any , idField : string = 'name' ) : void {
43+ const registry = SchemaRegistry . getInstance ( ) ;
44+ if ( ! registry . schemas . has ( type ) ) {
45+ registry . schemas . set ( type , new Map ( ) ) ;
46+ }
47+ const typeMap = registry . schemas . get ( type ) ! ;
48+ const id = item [ idField ] ;
49+ typeMap . set ( id , item ) ;
50+ }
51+
52+ /**
53+ * Get an item from the registry
54+ */
55+ static getItem ( type : string , id : string ) : any | undefined {
56+ const registry = SchemaRegistry . getInstance ( ) ;
57+ const typeMap = registry . schemas . get ( type ) ;
58+ return typeMap ?. get ( id ) ;
59+ }
60+
61+ /**
62+ * List all items of a type
63+ */
64+ static listItems ( type : string ) : any [ ] {
65+ const registry = SchemaRegistry . getInstance ( ) ;
66+ const typeMap = registry . schemas . get ( type ) ;
67+ if ( ! typeMap ) return [ ] ;
68+ return Array . from ( typeMap . values ( ) ) ;
69+ }
70+
71+ /**
72+ * Check if an item exists
73+ */
74+ static hasItem ( type : string , id : string ) : boolean {
75+ const registry = SchemaRegistry . getInstance ( ) ;
76+ const typeMap = registry . schemas . get ( type ) ;
77+ return typeMap ?. has ( id ) || false ;
78+ }
79+
80+ /**
81+ * Delete an item
82+ */
83+ static deleteItem ( type : string , id : string ) : boolean {
84+ const registry = SchemaRegistry . getInstance ( ) ;
85+ const typeMap = registry . schemas . get ( type ) ;
86+ if ( ! typeMap ) return false ;
87+ return typeMap . delete ( id ) ;
88+ }
89+
90+ /**
91+ * Clear all items
92+ */
93+ static clear ( ) : void {
94+ const registry = SchemaRegistry . getInstance ( ) ;
95+ registry . schemas . clear ( ) ;
96+ }
97+
98+ // Instance methods for backward compatibility
2599 register ( name : string , schema : ObjectConfig ) : void {
26- this . schemas . set ( name , schema ) ;
100+ SchemaRegistry . registerItem ( 'object' , schema , 'name' ) ;
27101 }
28102
29103 get ( name : string ) : ObjectConfig | undefined {
30- return this . schemas . get ( name ) ;
104+ return SchemaRegistry . getItem ( 'object' , name ) ;
31105 }
32106
33107 list ( ) : ObjectConfig [ ] {
34- return Array . from ( this . schemas . values ( ) ) ;
108+ return SchemaRegistry . listItems ( 'object' ) ;
35109 }
36110
37111 has ( name : string ) : boolean {
38- return this . schemas . has ( name ) ;
112+ return SchemaRegistry . hasItem ( 'object' , name ) ;
39113 }
40114
41115 delete ( name : string ) : boolean {
42- return this . schemas . delete ( name ) ;
43- }
44-
45- clear ( ) : void {
46- this . schemas . clear ( ) ;
116+ return SchemaRegistry . deleteItem ( 'object' , name ) ;
47117 }
48118}
49119
50120/**
51121 * ObjectQL Engine - runtime query engine
52122 */
53123export class ObjectQL {
54- private schemaRegistry : SchemaRegistry ;
55-
56124 constructor ( ) {
57- this . schemaRegistry = new SchemaRegistry ( ) ;
125+ // Use the singleton SchemaRegistry
58126 }
59127
60- getSchemaRegistry ( ) : SchemaRegistry {
61- return this . schemaRegistry ;
128+ getSchemaRegistry ( ) : typeof SchemaRegistry {
129+ return SchemaRegistry ;
62130 }
63131
64132 registerSchema ( name : string , schema : ObjectConfig ) : void {
65- this . schemaRegistry . register ( name , schema ) ;
133+ SchemaRegistry . registerItem ( 'object' , schema , 'name' ) ;
66134 }
67135
68136 getSchema ( name : string ) : ObjectConfig | undefined {
69- return this . schemaRegistry . get ( name ) ;
137+ return SchemaRegistry . getItem ( 'object' , name ) ;
70138 }
71139}
72140
0 commit comments