mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 08:00:59 +00:00
Add getAllFromIndex method for efficient sorted queries
- Add getAllFromIndex to StorageBackend interface for index-based queries - Implement getAllFromIndex in IndexedDBStorageBackend using cursor API - Update SessionsStore.getAllMetadata to use lastModified index - Enables database-native sorting instead of fetching all keys and sorting in JS Benefits: - Much faster for large datasets (uses IndexedDB cursor with direction) - Reduces memory usage (no need to load all keys first) - Leverages existing indices for optimal performance
This commit is contained in:
parent
b129154cc8
commit
46c1da9826
3 changed files with 38 additions and 5 deletions
|
|
@ -96,6 +96,34 @@ export class IndexedDBStorageBackend implements StorageBackend {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getAllFromIndex<T = unknown>(
|
||||||
|
storeName: string,
|
||||||
|
indexName: string,
|
||||||
|
direction: "asc" | "desc" = "asc",
|
||||||
|
): Promise<T[]> {
|
||||||
|
const db = await this.getDB();
|
||||||
|
const tx = db.transaction(storeName, "readonly");
|
||||||
|
const store = tx.objectStore(storeName);
|
||||||
|
const index = store.index(indexName);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const results: T[] = [];
|
||||||
|
const request = index.openCursor(null, direction === "desc" ? "prev" : "next");
|
||||||
|
|
||||||
|
request.onsuccess = () => {
|
||||||
|
const cursor = request.result;
|
||||||
|
if (cursor) {
|
||||||
|
results.push(cursor.value as T);
|
||||||
|
cursor.continue();
|
||||||
|
} else {
|
||||||
|
resolve(results);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async clear(storeName: string): Promise<void> {
|
async clear(storeName: string): Promise<void> {
|
||||||
const db = await this.getDB();
|
const db = await this.getDB();
|
||||||
const tx = db.transaction(storeName, "readwrite");
|
const tx = db.transaction(storeName, "readwrite");
|
||||||
|
|
|
||||||
|
|
@ -42,11 +42,8 @@ export class SessionsStore extends Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllMetadata(): Promise<SessionMetadata[]> {
|
async getAllMetadata(): Promise<SessionMetadata[]> {
|
||||||
const keys = await this.getBackend().keys("sessions-metadata");
|
// Use the lastModified index to get sessions sorted by most recent first
|
||||||
const metadata = await Promise.all(
|
return this.getBackend().getAllFromIndex<SessionMetadata>("sessions-metadata", "lastModified", "desc");
|
||||||
keys.map((key) => this.getBackend().get<SessionMetadata>("sessions-metadata", key)),
|
|
||||||
);
|
|
||||||
return metadata.filter((m): m is SessionMetadata => m !== null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string): Promise<void> {
|
async delete(id: string): Promise<void> {
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,14 @@ export interface StorageBackend {
|
||||||
*/
|
*/
|
||||||
keys(storeName: string, prefix?: string): Promise<string[]>;
|
keys(storeName: string, prefix?: string): Promise<string[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all values from a specific store, ordered by an index.
|
||||||
|
* @param storeName - The store to query
|
||||||
|
* @param indexName - The index to use for ordering
|
||||||
|
* @param direction - Sort direction ("asc" or "desc")
|
||||||
|
*/
|
||||||
|
getAllFromIndex<T = unknown>(storeName: string, indexName: string, direction?: "asc" | "desc"): Promise<T[]>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all data from a specific store.
|
* Clear all data from a specific store.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue