Remove ID from storage methods
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user