@@ -2,46 +2,73 @@ import { ServiceObject, App } from '@objectstack/spec';
22
33/**
44 * Global Schema Registry
5+ * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.)
56 */
67export class SchemaRegistry {
7- private static objects = new Map < string , ServiceObject > ( ) ;
8- private static apps = new Map < string , App > ( ) ;
8+ // Nested Map: Type -> Name -> MetadataItem
9+ private static metadata = new Map < string , Map < string , any > > ( ) ;
910
1011 /**
11- * Register a new object schema
12+ * Universal Register Method
1213 */
13- static register ( schema : ServiceObject ) {
14- if ( this . objects . has ( schema . name ) ) {
15- console . warn ( `[Registry] Overwriting object: ${ schema . name } ` ) ;
14+ static registerItem < T extends { name : string } > ( type : string , item : T ) {
15+ if ( ! this . metadata . has ( type ) ) {
16+ this . metadata . set ( type , new Map ( ) ) ;
1617 }
17- this . objects . set ( schema . name , schema ) ;
18- console . log ( `[Registry] Registered object: ${ schema . name } ` ) ;
18+ const collection = this . metadata . get ( type ) ! ;
19+
20+ if ( collection . has ( item . name ) ) {
21+ console . warn ( `[Registry] Overwriting ${ type } : ${ item . name } ` ) ;
22+ }
23+ collection . set ( item . name , item ) ;
24+ console . log ( `[Registry] Registered ${ type } : ${ item . name } ` ) ;
25+ }
26+
27+ /**
28+ * Universal Get Method
29+ */
30+ static getItem < T > ( type : string , name : string ) : T | undefined {
31+ return this . metadata . get ( type ) ?. get ( name ) as T ;
32+ }
33+
34+ /**
35+ * Universal List Method
36+ */
37+ static listItems < T > ( type : string ) : T [ ] {
38+ return Array . from ( this . metadata . get ( type ) ?. values ( ) || [ ] ) as T [ ] ;
39+ }
40+
41+ // ==========================================
42+ // Typed Helper Methods (Shortcuts)
43+ // ==========================================
44+
45+ /**
46+ * Object Helpers
47+ */
48+ static registerObject ( schema : ServiceObject ) {
49+ this . registerItem ( 'object' , schema ) ;
1950 }
2051
21- static get ( name : string ) : ServiceObject | undefined {
22- return this . objects . get ( name ) ;
52+ static getObject ( name : string ) : ServiceObject | undefined {
53+ return this . getItem < ServiceObject > ( 'object' , name ) ;
2354 }
2455
25- static getAll ( ) : ServiceObject [ ] {
26- return Array . from ( this . objects . values ( ) ) ;
56+ static getAllObjects ( ) : ServiceObject [ ] {
57+ return this . listItems < ServiceObject > ( 'object' ) ;
2758 }
2859
2960 /**
30- * Register a new app schema
61+ * App Helpers
3162 */
3263 static registerApp ( app : App ) {
33- if ( this . apps . has ( app . name ) ) {
34- console . warn ( `[Registry] Overwriting app: ${ app . name } ` ) ;
35- }
36- this . apps . set ( app . name , app ) ;
37- console . log ( `[Registry] Registered app: ${ app . name } ` ) ;
64+ this . registerItem ( 'app' , app ) ;
3865 }
3966
4067 static getApp ( name : string ) : App | undefined {
41- return this . apps . get ( name ) ;
68+ return this . getItem < App > ( 'app' , name ) ;
4269 }
4370
4471 static getAllApps ( ) : App [ ] {
45- return Array . from ( this . apps . values ( ) ) ;
72+ return this . listItems < App > ( 'app' ) ;
4673 }
4774}
0 commit comments