feat: add adapter factory + registry pattern for queue, storage, memory
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
18
packages/memory/src/factory.ts
Normal file
18
packages/memory/src/factory.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { MemoryAdapter, MemoryConfig } from './types.js';
|
||||
|
||||
type MemoryType = MemoryConfig['type'];
|
||||
|
||||
const registry = new Map<MemoryType, (config: MemoryConfig) => MemoryAdapter>();
|
||||
|
||||
export function registerMemoryAdapter(
|
||||
type: MemoryType,
|
||||
factory: (config: MemoryConfig) => MemoryAdapter,
|
||||
): void {
|
||||
registry.set(type, factory);
|
||||
}
|
||||
|
||||
export function createMemoryAdapter(config: MemoryConfig): MemoryAdapter {
|
||||
const factory = registry.get(config.type);
|
||||
if (!factory) throw new Error(`No adapter registered for type: ${config.type}`);
|
||||
return factory(config);
|
||||
}
|
||||
@@ -20,3 +20,4 @@ export type {
|
||||
Insight as AdapterInsight,
|
||||
InsightSearchResult,
|
||||
} from './types.js';
|
||||
export { createMemoryAdapter, registerMemoryAdapter } from './factory.js';
|
||||
|
||||
18
packages/queue/src/factory.ts
Normal file
18
packages/queue/src/factory.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { QueueAdapter, QueueConfig } from './types.js';
|
||||
|
||||
type QueueType = QueueConfig['type'];
|
||||
|
||||
const registry = new Map<QueueType, (config: QueueConfig) => QueueAdapter>();
|
||||
|
||||
export function registerQueueAdapter(
|
||||
type: QueueType,
|
||||
factory: (config: QueueConfig) => QueueAdapter,
|
||||
): void {
|
||||
registry.set(type, factory);
|
||||
}
|
||||
|
||||
export function createQueueAdapter(config: QueueConfig): QueueAdapter {
|
||||
const factory = registry.get(config.type);
|
||||
if (!factory) throw new Error(`No adapter registered for type: ${config.type}`);
|
||||
return factory(config);
|
||||
}
|
||||
@@ -8,3 +8,4 @@ export {
|
||||
} from './queue.js';
|
||||
|
||||
export { type QueueAdapter, type QueueConfig as QueueAdapterConfig } from './types.js';
|
||||
export { createQueueAdapter, registerQueueAdapter } from './factory.js';
|
||||
|
||||
18
packages/storage/src/factory.ts
Normal file
18
packages/storage/src/factory.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { StorageAdapter, StorageConfig } from './types.js';
|
||||
|
||||
type StorageType = StorageConfig['type'];
|
||||
|
||||
const registry = new Map<StorageType, (config: StorageConfig) => StorageAdapter>();
|
||||
|
||||
export function registerStorageAdapter(
|
||||
type: StorageType,
|
||||
factory: (config: StorageConfig) => StorageAdapter,
|
||||
): void {
|
||||
registry.set(type, factory);
|
||||
}
|
||||
|
||||
export function createStorageAdapter(config: StorageConfig): StorageAdapter {
|
||||
const factory = registry.get(config.type);
|
||||
if (!factory) throw new Error(`No adapter registered for type: ${config.type}`);
|
||||
return factory(config);
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export type { StorageAdapter, StorageConfig } from './types.js';
|
||||
export { createStorageAdapter, registerStorageAdapter } from './factory.js';
|
||||
|
||||
Reference in New Issue
Block a user