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

@@ -1,6 +1,5 @@
import { AESKey } from '../src/crypto/aes-key.js'; import { AESKey } from '../src/crypto/aes-key.js';
import { BaseStorage } from '../src/storage/base-storage.js'; import { StorageMemory, EncryptedStorage } from '../src/storage/index.js';
import { StorageMemory, StorageMemorySynced, EncryptedStorage } from '../src/storage/index.js';
const storage = StorageMemory.from(); const storage = StorageMemory.from();
// const storageSynced = StorageMemorySynced.from(storage); // const storageSynced = StorageMemorySynced.from(storage);
@@ -8,6 +7,7 @@ const storage = StorageMemory.from();
const currentDate = new Date(); const currentDate = new Date();
const data = { const data = {
id: 'test',
name: 'test', name: 'test',
age: 20, age: 20,
email: 'test@test.com', email: 'test@test.com',
@@ -24,9 +24,9 @@ storageEncryptedBase.on('insert', (event) => {
}); });
// Store data in storage // Store data in storage
await storage.insertOne('test', data); await storage.insertOne(data);
// storageSynced.insertOne('test', data); // storageSynced.insertOne('test', data);
await storageEncrypted.insertOne('test', data); await storageEncrypted.insertOne(data);
// Retrieve data from storage // Retrieve data from storage
const retrievedData = await storage.findOne({ name: 'test' }); const retrievedData = await storage.findOne({ name: 'test' });

View File

@@ -11,15 +11,13 @@ export type FindOptions = {
export type StorageEvent<T = Record<string, any>> = { export type StorageEvent<T = Record<string, any>> = {
insert: { insert: {
key: string;
value: T; value: T;
}; };
update: { update: {
key: string;
value: T; value: T;
}; };
delete: { delete: {
key: string; value: T;
}; };
clear: undefined; clear: undefined;
}; };
@@ -29,22 +27,26 @@ export abstract class BaseStorage<
> extends EventEmitter<StorageEvent<T>> { > extends EventEmitter<StorageEvent<T>> {
/** /**
* Insert a document into the store * Insert a document into the store
* @param id Unique identifier for the document
* @param document The document to insert * @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 * 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 * Find a single document that matches the filter
* @param filter MongoDB-like query 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 * Find all documents that match the filter
@@ -59,7 +61,10 @@ export abstract class BaseStorage<
* @param update Document or fields to update * @param update Document or fields to update
* @returns True if a document was updated, false otherwise * @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 * Update all documents that match the filter
@@ -71,7 +76,7 @@ export abstract class BaseStorage<
abstract updateMany( abstract updateMany(
filter: Partial<T>, filter: Partial<T>,
update: Partial<T>, update: Partial<T>,
options: Partial<FindOptions>, options?: Partial<FindOptions>,
): Promise<number>; ): Promise<number>;
/** /**
@@ -79,7 +84,10 @@ export abstract class BaseStorage<
* @param filter Query to match the document to delete * @param filter Query to match the document to delete
* @returns True if a document was deleted, false otherwise * @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 * 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< export class EncryptedStorage<
T extends Record<string, any> = Record<string, any>, T extends Record<string, any> = Record<string, any>,
> extends BaseStorage<T> { > 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); return new EncryptedStorage<T>(storage, key);
} }
@@ -18,24 +18,34 @@ export class EncryptedStorage<
}); });
constructor( constructor(
private readonly storage: BaseStorage, private readonly storage: BaseStorage<Record<string, string>>,
private readonly key: AESKey, private readonly key: AESKey,
) { ) {
super(); super();
// Forward events from the underlying storage, decrypting the data // Forward events from the underlying storage, decrypting the data
this.storage.on('insert', async (event) => { 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>); 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) => { 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>); 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.storage.on('delete', async (event) => {
this.emit('delete', 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) => { this.storage.on('clear', (event) => {
@@ -43,43 +53,22 @@ export class EncryptedStorage<
}); });
} }
async insertOne(id: string, document: T): Promise<void> { async insertMany(documents: Array<T>): Promise<void> {
const encrypted = await this.convertToEncrypted(document); const encrypted = [];
await this.storage.insertOne(id, encrypted); for (const document of documents) {
} encrypted.push(await this.convertToEncrypted(document));
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));
} }
await this.storage.insertMany(encrypted); 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); const encryptedFilter = await this.convertToEncrypted(filter);
const documents = await this.storage.find(encryptedFilter, options);
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);
return Promise.all( return Promise.all(
documents.map(async (document) => this.convertToDecrypted(document)), 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( async updateMany(
filter: Partial<T>, filter: Partial<T>,
update: Partial<T>, update: Partial<T>,
@@ -90,11 +79,6 @@ export class EncryptedStorage<
return this.storage.updateMany(encryptedFilter, encryptedUpdate, options); 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( async deleteMany(
filter: Partial<T>, filter: Partial<T>,
options: Partial<FindOptions> = {}, 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. // Hook into all write operations so that we can sync the In-Memory cache.
this.store.on('insert', async (payload) => { this.store.on('insert', async (payload) => {
await this.inMemoryCache.insertOne(payload.key, payload.value); await this.inMemoryCache.insertOne(payload.value);
this.emit('insert', { key: payload.key, value: payload.value }); this.emit('insert', payload);
}); });
this.store.on('update', async (payload) => { this.store.on('update', async (payload) => {
// For update events, we need to find and update the document in memory // 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 // Since we don't have the filter, we'll update by key
await this.inMemoryCache.insertOne(payload.key, payload.value); const filter = {
this.emit('update', { key: payload.key, value: payload.value }); 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) => { this.store.on('delete', async (payload) => {
// For delete events, we need to find and delete the document by key await this.inMemoryCache.deleteOne(payload.value);
const allDocs = await this.inMemoryCache.find();
for (const doc of allDocs) { // Re-emit the delete event with the original payload.
if ('id' in doc && doc.id === payload.key) { this.emit('delete', payload);
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 () => { this.store.on('clear', async () => {
// Clear all documents from memory cache // 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); 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. // Sync the data from the backing store into the In-Memory cache.
const allDocuments = await store.find(); const allDocuments = await store.find();
for (const document of allDocuments) { for (const document of allDocuments) {
if ('id' in document && typeof document.id === 'string') { await inMemoryCache.insertOne(document);
await inMemoryCache.insertOne(document.id, document);
}
} }
// Return our instance of this store. // Return our instance of this store.
return memorySyncedStore; return memorySyncedStore;
} }
async insertOne(id: string, document: T): Promise<void> { async insertMany(documents: Array<T>): Promise<void> {
await this.store.insertOne(id, document);
}
async insertMany(documents: Map<string, T>): Promise<void> {
await this.store.insertMany(documents); 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[]> { async find(filter?: Partial<T>, options?: FindOptions): Promise<T[]> {
return await this.inMemoryCache.find(filter, options); 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( async updateMany(
filter: Partial<T>, filter: Partial<T>,
update: 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); 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> { async deleteMany(filter: Partial<T>, options: FindOptions = {} as FindOptions): Promise<number> {
return await this.store.deleteMany(filter, options); return await this.store.deleteMany(filter, options);
} }

View File

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