learning_ai_common_plat/packages/cosmos/src/containers.ts
saravanakumardb1 90b9cf93d8 fix(common): configure ESLint 9 and fix lint issues
- Added @eslint/js dependency
- Updated eslint.config.js for ESLint 9 compatibility
- Added required globals (crypto, localStorage, React, etc.)
- Fixed unused imports and variables
- Disabled sort-imports temporarily
- Formatted all files with Prettier
2026-02-12 16:37:30 -08:00

68 lines
2.0 KiB
TypeScript

/**
* Container registry for dashboards that need partition key validation
* and createIfNotExists support.
*/
import { Container, PartitionKeyDefinition } from '@azure/cosmos';
import { getCosmosClient, getDatabase } from './client.js';
import type { ContainerConfig } from './types.js';
const _registry: Map<string, ContainerConfig> = new Map();
const _containerCache: Map<string, Container> = new Map();
/**
* Register containers with their partition key configuration.
* Call once at app startup before any getRegisteredContainer() calls.
*/
export function registerContainers(definitions: Record<string, ContainerConfig>): void {
for (const [name, config] of Object.entries(definitions)) {
_registry.set(name, config);
}
}
/**
* Get a container that was previously registered.
* Throws if the container name is unknown.
*/
export function getRegisteredContainer(name: string): Container {
if (!_registry.has(name)) {
throw new Error(`Unknown container '${name}'. Valid: ${[..._registry.keys()].join(', ')}`);
}
let container = _containerCache.get(name);
if (!container) {
container = getDatabase().container(name);
_containerCache.set(name, container);
}
return container;
}
/**
* Create all registered containers if they don't exist.
* Call from a seed script or on first deploy.
*/
export async function initializeAllContainers(): Promise<void> {
const client = getCosmosClient();
const dbId = process.env.COSMOS_DATABASE || 'lysnrai';
const { database } = await client.databases.createIfNotExists({ id: dbId });
for (const [name, config] of _registry.entries()) {
await database.containers.createIfNotExists({
id: name,
partitionKey: {
paths: [config.partitionKeyPath],
} as PartitionKeyDefinition,
...(config.defaultTtl != null && { defaultTtl: config.defaultTtl }),
});
}
}
/**
* Reset the registry (useful for testing).
* @internal
*/
export function _resetRegistry(): void {
_registry.clear();
_containerCache.clear();
}