Remove ID from storage methods

This commit is contained in:
2026-02-25 00:46:34 +11:00
parent 91df32c786
commit fe68cf2459
5 changed files with 77 additions and 140 deletions

View File

@@ -11,15 +11,13 @@ export type FindOptions = {
export type StorageEvent<T = Record<string, any>> = {
insert: {
key: string;
value: T;
};
update: {
key: string;
value: T;
};
delete: {
key: string;
value: T;
};
clear: undefined;
};
@@ -29,22 +27,26 @@ export abstract class BaseStorage<
> extends EventEmitter<StorageEvent<T>> {
/**
* Insert a document into the store
* @param id Unique identifier for the document
* @param document The document to insert
*/
abstract insertOne(id: string, document: T): Promise<void>;
async insertOne(document: T): Promise<void> {
await this.insertMany([document]);
}
/**
* Insert multiple documents into the store
* @param documents Map of ID to document
* @param documents Array of documents to insert
*/
abstract insertMany(documents: Map<string, T>): Promise<void>;
abstract insertMany(documents: Array<T>): Promise<void>;
/**
* Find a single document that matches the filter
* @param filter MongoDB-like query filter
*/
abstract findOne(filter?: Partial<T>): Promise<T | null>;
async findOne(filter?: Partial<T>): Promise<T | null> {
const results = await this.find(filter);
return results.length > 0 ? results[0] : null;
}
/**
* Find all documents that match the filter
@@ -59,7 +61,10 @@ export abstract class BaseStorage<
* @param update Document or fields to update
* @returns True if a document was updated, false otherwise
*/
abstract updateOne(filter: Partial<T>, update: Partial<T>): Promise<boolean>;
async updateOne(filter: Partial<T>, update?: Partial<T>): Promise<boolean> {
const results = await this.updateMany(filter, update, { limit: 1 });
return results > 0;
}
/**
* Update all documents that match the filter
@@ -71,7 +76,7 @@ export abstract class BaseStorage<
abstract updateMany(
filter: Partial<T>,
update: Partial<T>,
options: Partial<FindOptions>,
options?: Partial<FindOptions>,
): Promise<number>;
/**
@@ -79,7 +84,10 @@ export abstract class BaseStorage<
* @param filter Query to match the document to delete
* @returns True if a document was deleted, false otherwise
*/
abstract deleteOne(filter: Partial<T>): Promise<boolean>;
async deleteOne(filter: Partial<T>): Promise<boolean> {
const results = await this.deleteMany(filter, { limit: 1 });
return results > 0;
}
/**
* Delete all documents that match the filter with options

View File

@@ -8,7 +8,7 @@ import { BaseStorage, type FindOptions } from './base-storage.js';
export class EncryptedStorage<
T extends Record<string, any> = Record<string, any>,
> extends BaseStorage<T> {
static from<T>(storage: BaseStorage, key: AESKey) {
static from<T>(storage: BaseStorage<Record<string, string>>, key: AESKey) {
return new EncryptedStorage<T>(storage, key);
}
@@ -18,24 +18,34 @@ export class EncryptedStorage<
});
constructor(
private readonly storage: BaseStorage,
private readonly storage: BaseStorage<Record<string, string>>,
private readonly key: AESKey,
) {
super();
// Forward events from the underlying storage, decrypting the data
this.storage.on('insert', async (event) => {
// De-crypt the value before emitting the event.
const decryptedValue = await this.convertToDecrypted(event.value as Record<string, string>);
this.emit('insert', { key: event.key, value: decryptedValue });
// Re-emit the insert event with the original payload.
this.emit('insert', { value: decryptedValue });
});
this.storage.on('update', async (event) => {
// De-crypt the value before emitting the event.
const decryptedValue = await this.convertToDecrypted(event.value as Record<string, string>);
this.emit('update', { key: event.key, value: decryptedValue });
// Re-emit the update event with the original payload.
this.emit('update', { value: decryptedValue });
});
this.storage.on('delete', (event) => {
this.emit('delete', event);
this.storage.on('delete', async (event) => {
// De-crypt the value before emitting the event.
const decryptedValue = await this.convertToDecrypted(event.value as Record<string, string>);
// Re-emit the delete event with the original payload.
this.emit('delete', { value: decryptedValue });
});
this.storage.on('clear', (event) => {
@@ -43,43 +53,22 @@ export class EncryptedStorage<
});
}
async insertOne(id: string, document: T): Promise<void> {
const encrypted = await this.convertToEncrypted(document);
await this.storage.insertOne(id, encrypted);
}
async insertMany(documents: Map<string, T>): Promise<void> {
const encrypted = new Map<string, Record<string, string>>();
for (const [key, value] of documents.entries()) {
encrypted.set(key, await this.convertToEncrypted(value));
async insertMany(documents: Array<T>): Promise<void> {
const encrypted = [];
for (const document of documents) {
encrypted.push(await this.convertToEncrypted(document));
}
await this.storage.insertMany(encrypted);
}
async findOne(filter: Partial<T>): Promise<T | null> {
async find(filter?: Partial<T>, options?: FindOptions): Promise<T[]> {
const encryptedFilter = await this.convertToEncrypted(filter);
console.log('encryptedFilter', encryptedFilter);
const document = await this.storage.findOne(encryptedFilter);
if (!document) return null;
return this.convertToDecrypted(document);
}
async find(filter: Partial<T>): Promise<T[]> {
const encryptedFilter = await this.convertToEncrypted(filter);
const documents = await this.storage.find(encryptedFilter);
const documents = await this.storage.find(encryptedFilter, options);
return Promise.all(
documents.map(async (document) => this.convertToDecrypted(document)),
);
}
async updateOne(filter: Partial<T>, update: Partial<T>): Promise<boolean> {
const encryptedFilter = await this.convertToEncrypted(filter);
const encryptedUpdate = await this.convertToEncrypted(update);
return this.storage.updateOne(encryptedFilter, encryptedUpdate);
}
async updateMany(
filter: Partial<T>,
update: Partial<T>,
@@ -90,11 +79,6 @@ export class EncryptedStorage<
return this.storage.updateMany(encryptedFilter, encryptedUpdate, options);
}
async deleteOne(filter: Partial<T>): Promise<boolean> {
const encryptedFilter = await this.convertToEncrypted(filter);
return this.storage.deleteOne(encryptedFilter);
}
async deleteMany(
filter: Partial<T>,
options: Partial<FindOptions> = {},

View File

@@ -15,34 +15,34 @@ export class StorageMemorySynced<T extends Record<string, any> = Record<string,
// 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 });
await this.inMemoryCache.insertOne(payload.value);
this.emit('insert', payload);
});
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 });
const filter = {
id: payload.value.id,
} as unknown as Partial<T>
// Update the document in memory by ID.
await this.inMemoryCache.updateOne(filter, payload.value);
this.emit('update', payload);
});
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 });
await this.inMemoryCache.deleteOne(payload.value);
// Re-emit the delete event with the original payload.
this.emit('delete', payload);
});
this.store.on('clear', async () => {
// Clear all documents from memory cache
await this.inMemoryCache.deleteMany({} as Partial<T>);
await this.inMemoryCache.deleteMany({});
// Re-emit the clear event with the original payload.
this.emit('clear', undefined);
});
}
@@ -57,35 +57,21 @@ export class StorageMemorySynced<T extends Record<string, any> = Record<string,
// 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);
}
await inMemoryCache.insertOne(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> {
async insertMany(documents: Array<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>,
@@ -94,10 +80,6 @@ export class StorageMemorySynced<T extends Record<string, any> = Record<string,
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);
}

View File

@@ -10,7 +10,7 @@ export class StorageMemory<
T extends Record<string, any> = Record<string, any>,
> extends BaseStorage<T> {
// TODO: Eventually this may accept indexes as an argument.
static from<T>(): StorageMemory<T> {
static from<T extends Record<string, any>>(): StorageMemory<T> {
return new StorageMemory<T>();
}
@@ -24,15 +24,10 @@ export class StorageMemory<
this.children = new Map();
}
async insertOne(id: string, document: T): Promise<void> {
this.store.set(id, document);
this.emit('insert', { key: id, value: document });
}
async insertMany(documents: Map<string, T>): Promise<void> {
for (const [key, value] of documents.entries()) {
this.store.set(key, value);
this.emit('insert', { key, value });
async insertMany(documents: Array<T>): Promise<void> {
for (const document of documents) {
this.store.set(document.id, document);
this.emit('insert', { value: document });
}
}
@@ -49,16 +44,7 @@ export class StorageMemory<
return true;
}
async findOne(filter?: Partial<T>): Promise<T | null> {
for (const [, value] of this.store) {
if (this.matchesFilter(value, filter)) {
return value;
}
}
return null;
}
async find(filter?: Partial<T>): Promise<T[]> {
async find(filter?: Partial<T>, options?: FindOptions): Promise<T[]> {
const results: T[] = [];
for (const [, value] of this.store) {
if (this.matchesFilter(value, filter)) {
@@ -68,18 +54,6 @@ export class StorageMemory<
return results;
}
async updateOne(filter: Partial<T>, update: Partial<T>): Promise<boolean> {
for (const [key, value] of this.store) {
if (this.matchesFilter(value, filter)) {
const updated = { ...value, ...update };
this.store.set(key, updated);
this.emit('update', { key, value: updated });
return true;
}
}
return false;
}
async updateMany(
filter: Partial<T>,
update: Partial<T>,
@@ -106,35 +80,24 @@ export class StorageMemory<
for (const [key, oldValue] of itemsToProcess) {
const updatedValue = { ...oldValue, ...update };
this.store.set(key, updatedValue);
this.emit('update', { key, value: updatedValue });
this.emit('update', { value: updatedValue });
updated++;
}
return updated;
}
async deleteOne(filter: Partial<T>): Promise<boolean> {
for (const [key, value] of this.store) {
if (this.matchesFilter(value, filter)) {
this.store.delete(key);
this.emit('delete', { key });
return true;
}
}
return false;
}
async deleteMany(
filter: Partial<T>,
options: Partial<FindOptions> = {},
): Promise<number> {
let deleted = 0;
const keysToDelete: string[] = [];
const rowsToDelete: Array<T> = [];
// Collect all matching keys
for (const [key, value] of this.store) {
if (this.matchesFilter(value, filter)) {
keysToDelete.push(key);
rowsToDelete.push(value);
}
}
@@ -142,13 +105,13 @@ export class StorageMemory<
const startIndex = options.skip || 0;
const endIndex = options.limit
? startIndex + options.limit
: keysToDelete.length;
const keysToProcess = keysToDelete.slice(startIndex, endIndex);
: rowsToDelete.length;
const rowsToProcess = rowsToDelete.slice(startIndex, endIndex);
// Delete items
for (const key of keysToProcess) {
this.store.delete(key);
this.emit('delete', { key });
for (const row of rowsToProcess) {
this.store.delete(row.id);
this.emit('delete', { value: row });
deleted++;
}