Skip to content

Commit d96d870

Browse files
committed
feat: add in-memory storage fallback for user database service
1 parent 6c767bc commit d96d870

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/agent-api/src/user-db-service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export class UserDbService {
1111
private database: Database | undefined = undefined;
1212
private usersContainer: Container | undefined = undefined;
1313
private isCosmosDbInitialized = false;
14+
private inMemoryStorage: Map<string, any> = new Map();
15+
private useInMemoryStorage = false;
1416

1517
static async getInstance(): Promise<UserDbService> {
1618
if (!UserDbService.instance) {
@@ -24,7 +26,8 @@ export class UserDbService {
2426
try {
2527
const endpoint = process.env.AZURE_COSMOSDB_NOSQL_ENDPOINT;
2628
if (!endpoint) {
27-
console.warn('Cosmos DB endpoint not found in environment variables.');
29+
console.warn('Cosmos DB endpoint not found in environment variables. Falling back to in-memory storage.');
30+
this.useInMemoryStorage = true;
2831
return;
2932
}
3033
const credential = new DefaultAzureCredential();
@@ -45,6 +48,10 @@ export class UserDbService {
4548
}
4649

4750
async getUserById(id: string): Promise<any | undefined> {
51+
if (this.useInMemoryStorage) {
52+
return this.inMemoryStorage.get(id);
53+
}
54+
4855
if (!this.isCosmosDbInitialized) return undefined;
4956
try {
5057
const resource = await this.usersContainer!.item(id).read();
@@ -56,11 +63,17 @@ export class UserDbService {
5663
}
5764

5865
async createUser(id: string): Promise<any> {
59-
if (!this.isCosmosDbInitialized) throw new Error('Cosmos DB not initialized');
6066
const user = {
6167
id,
6268
createdAt: new Date().toISOString()
6369
};
70+
71+
if (this.useInMemoryStorage) {
72+
this.inMemoryStorage.set(id, user);
73+
return user;
74+
}
75+
76+
if (!this.isCosmosDbInitialized) throw new Error('Cosmos DB not initialized');
6477
const { resource } = await this.usersContainer!.items.create(user);
6578
return resource;
6679
}

0 commit comments

Comments
 (0)