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

@@ -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++;
}