Files
Hashpass-lib/src/storage/storage-memory-synced.ts
2025-08-16 16:11:33 +10:00

113 lines
3.9 KiB
TypeScript

import { BaseStorage, FindOptions } from './base-storage.js';
import { StorageMemory } from './storage-memory.js';
/**
* Storage Adapter that takes another Storage Adapter (e.g. IndexedDB) and "syncs" its contents to system memory for faster read access.
*
* All read operations will use system memory - all write operations will use the provided adapter.
*/
export class StorageMemorySynced<T extends Record<string, any> = Record<string, any>> extends BaseStorage<T> {
constructor(
private inMemoryCache: StorageMemory<T>,
private store: BaseStorage<T>,
) {
super();
// Hook into all write operations so that we can sync the In-Memory cache.
this.store.on('insert', async (payload) => {
await this.inMemoryCache.insertOne(payload.key, payload.value);
this.emit('insert', { key: payload.key, value: payload.value });
});
this.store.on('update', async (payload) => {
// For update events, we need to find and update the document in memory
// Since we don't have the filter, we'll update by key
await this.inMemoryCache.insertOne(payload.key, payload.value);
this.emit('update', { key: payload.key, value: payload.value });
});
this.store.on('delete', async (payload) => {
// For delete events, we need to find and delete the document by key
const allDocs = await this.inMemoryCache.find();
for (const doc of allDocs) {
if ('id' in doc && doc.id === payload.key) {
const filter = {} as Partial<T>;
(filter as any).id = payload.key;
await this.inMemoryCache.deleteOne(filter);
break;
}
}
this.emit('delete', { key: payload.key });
});
this.store.on('clear', async () => {
// Clear all documents from memory cache
await this.inMemoryCache.deleteMany({} as Partial<T>);
this.emit('clear', undefined);
});
}
static async create<T extends Record<string, any>>(store: BaseStorage<T>) {
// Instantiate in-memory cache and the backing store.
const inMemoryCache = new StorageMemory<T>();
// Create instance of this store.
const memorySyncedStore = new StorageMemorySynced<T>(inMemoryCache, store);
// Sync the data from the backing store into the In-Memory cache.
const allDocuments = await store.find();
for (const document of allDocuments) {
if ('id' in document && typeof document.id === 'string') {
await inMemoryCache.insertOne(document.id, document);
}
}
// Return our instance of this store.
return memorySyncedStore;
}
async insertOne(id: string, document: T): Promise<void> {
await this.store.insertOne(id, document);
}
async insertMany(documents: Map<string, T>): Promise<void> {
await this.store.insertMany(documents);
}
async findOne(filter?: Partial<T>): Promise<T | null> {
return await this.inMemoryCache.findOne(filter);
}
async find(filter?: Partial<T>, options?: FindOptions): Promise<T[]> {
return await this.inMemoryCache.find(filter, options);
}
async updateOne(filter: Partial<T>, update: Partial<T>): Promise<boolean> {
return await this.store.updateOne(filter, update);
}
async updateMany(
filter: Partial<T>,
update: Partial<T>,
options: FindOptions = {} as FindOptions
): Promise<number> {
return await this.store.updateMany(filter, update, options);
}
async deleteOne(filter: Partial<T>): Promise<boolean> {
return await this.store.deleteOne(filter);
}
async deleteMany(filter: Partial<T>, options: FindOptions = {} as FindOptions): Promise<number> {
return await this.store.deleteMany(filter, options);
}
deriveChild<C extends Record<string, any>>(path: string): BaseStorage<C> {
const childStore = this.store.deriveChild<C>(path);
const childMemory = this.inMemoryCache.deriveChild<C>(path);
// Create a new synced storage for the child
return new StorageMemorySynced<C>(childMemory as StorageMemory<C>, childStore);
}
}