Skip to content

Commit 81a4382

Browse files
Copilothotlong
andcommitted
feat: Implement SchemaRegistry with static singleton pattern
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 1c3f89e commit 81a4382

3 files changed

Lines changed: 97 additions & 19 deletions

File tree

packages/foundation/objectql/src/index.ts

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
2223
export 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
*/
53123
export 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

packages/foundation/objectstack-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export {
1111
ObjectKernel,
1212
ObjectStackKernel,
1313
type Plugin,
14+
type RuntimeContext as PluginContext,
1415
type RuntimeContext,
1516
type RuntimeAppConfig,
1617
type RuntimeDriver,

packages/foundation/types/tsconfig.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
{
2-
"extends": "../../../tsconfig.base.json",
32
"compilerOptions": {
3+
"target": "ES2019",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"declaration": true,
7+
"declarationMap": true,
8+
"composite": true,
49
"outDir": "./dist",
5-
"rootDir": "./src"
10+
"rootDir": "./src",
11+
"strict": true,
12+
"esModuleInterop": true,
13+
"skipLibCheck": true,
14+
"forceConsistentCasingInFileNames": true
615
},
716
"include": ["src/**/*"],
817
"references": []

0 commit comments

Comments
 (0)