Skip to content

Commit 117ecd3

Browse files
committed
feat: enhance SchemaRegistry to support composite keys for packageId and improve unregister logic
1 parent 259b7ad commit 117ecd3

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

packages/objectql/src/registry.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class SchemaRegistry {
2929
this.metadata.set(type, new Map());
3030
}
3131
const collection = this.metadata.get(type)!;
32-
const key = String(item[keyField]);
32+
const baseName = String(item[keyField]);
3333
// Tag item with owning package for scoped queries
3434
if (packageId) {
3535
(item as any)._packageId = packageId;
@@ -39,16 +39,20 @@ export class SchemaRegistry {
3939
try {
4040
this.validate(type, item);
4141
} catch (e: any) {
42-
console.error(`[Registry] Validation failed for ${type} ${key}: ${e.message}`);
42+
console.error(`[Registry] Validation failed for ${type} ${baseName}: ${e.message}`);
4343
// For now, warn but don't crash, allowing partial/legacy loads
4444
// throw e;
4545
}
4646

47-
if (collection.has(key)) {
48-
console.warn(`[Registry] Overwriting ${type}: ${key}`);
47+
// Use composite key (packageId:name) when packageId is provided,
48+
// so same-named items from different packages can coexist.
49+
const storageKey = packageId ? `${packageId}:${baseName}` : baseName;
50+
51+
if (collection.has(storageKey)) {
52+
console.warn(`[Registry] Overwriting ${type}: ${storageKey}`);
4953
}
50-
collection.set(key, item);
51-
console.log(`[Registry] Registered ${type}: ${key}`);
54+
collection.set(storageKey, item);
55+
console.log(`[Registry] Registered ${type}: ${storageKey}`);
5256
}
5357

5458
/**
@@ -73,22 +77,47 @@ export class SchemaRegistry {
7377

7478
/**
7579
* Universal Unregister Method
80+
* Supports both simple and composite keys.
7681
*/
7782
static unregisterItem(type: string, name: string) {
7883
const collection = this.metadata.get(type);
79-
if (collection && collection.has(name)) {
84+
if (!collection) {
85+
console.warn(`[Registry] Attempted to unregister non-existent ${type}: ${name}`);
86+
return;
87+
}
88+
// Direct key
89+
if (collection.has(name)) {
8090
collection.delete(name);
8191
console.log(`[Registry] Unregistered ${type}: ${name}`);
82-
} else {
83-
console.warn(`[Registry] Attempted to unregister non-existent ${type}: ${name}`);
92+
return;
8493
}
94+
// Scan composite keys
95+
for (const key of collection.keys()) {
96+
if (key.endsWith(`:${name}`)) {
97+
collection.delete(key);
98+
console.log(`[Registry] Unregistered ${type}: ${key}`);
99+
return;
100+
}
101+
}
102+
console.warn(`[Registry] Attempted to unregister non-existent ${type}: ${name}`);
85103
}
86104

87105
/**
88106
* Universal Get Method
107+
* Looks up by exact key first, then falls back to scanning by item name
108+
* to handle composite keys (packageId:name).
89109
*/
90110
static getItem<T>(type: string, name: string): T | undefined {
91-
return this.metadata.get(type)?.get(name) as T;
111+
const collection = this.metadata.get(type);
112+
if (!collection) return undefined;
113+
// Direct lookup (works for items registered without packageId)
114+
const direct = collection.get(name);
115+
if (direct) return direct as T;
116+
// Scan for composite keys ending with :name
117+
for (const [key, item] of collection) {
118+
if (key.endsWith(`:${name}`)) return item as T;
119+
}
120+
return undefined;
92121
}
93122

94123
/**

0 commit comments

Comments
 (0)