Initial commit
This commit is contained in:
164
src/storage/storage-memory.ts
Normal file
164
src/storage/storage-memory.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
import { BaseStorage, FindOptions } from './base-storage.js';
|
||||
|
||||
/**
|
||||
* Implementation of BaseStore using Memory as the storage backend.
|
||||
*
|
||||
* @remarks
|
||||
* This implementation can be used testing and caching of expensive operations.
|
||||
*/
|
||||
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> {
|
||||
return new StorageMemory<T>();
|
||||
}
|
||||
|
||||
private store: Map<string, T>;
|
||||
private children: Map<string, StorageMemory<any>>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.store = new Map();
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
private matchesFilter(item: T, filter?: Partial<T>): boolean {
|
||||
if (!filter || Object.keys(filter).length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(filter)) {
|
||||
if (item[key] !== value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
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[]> {
|
||||
const results: T[] = [];
|
||||
for (const [, value] of this.store) {
|
||||
if (this.matchesFilter(value, filter)) {
|
||||
results.push(value);
|
||||
}
|
||||
}
|
||||
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>,
|
||||
options: Partial<FindOptions> = {},
|
||||
): Promise<number> {
|
||||
let updated = 0;
|
||||
const itemsToUpdate: Array<[string, T]> = [];
|
||||
|
||||
// Collect all matching items
|
||||
for (const [key, value] of this.store) {
|
||||
if (this.matchesFilter(value, filter)) {
|
||||
itemsToUpdate.push([key, value]);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply skip and limit
|
||||
const startIndex = options.skip || 0;
|
||||
const endIndex = options.limit
|
||||
? startIndex + options.limit
|
||||
: itemsToUpdate.length;
|
||||
const itemsToProcess = itemsToUpdate.slice(startIndex, endIndex);
|
||||
|
||||
// Update items
|
||||
for (const [key, oldValue] of itemsToProcess) {
|
||||
const updatedValue = { ...oldValue, ...update };
|
||||
this.store.set(key, updatedValue);
|
||||
this.emit('update', { key, 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[] = [];
|
||||
|
||||
// Collect all matching keys
|
||||
for (const [key, value] of this.store) {
|
||||
if (this.matchesFilter(value, filter)) {
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply skip and limit
|
||||
const startIndex = options.skip || 0;
|
||||
const endIndex = options.limit
|
||||
? startIndex + options.limit
|
||||
: keysToDelete.length;
|
||||
const keysToProcess = keysToDelete.slice(startIndex, endIndex);
|
||||
|
||||
// Delete items
|
||||
for (const key of keysToProcess) {
|
||||
this.store.delete(key);
|
||||
this.emit('delete', { key });
|
||||
deleted++;
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
deriveChild<C>(path: string): BaseStorage<C> {
|
||||
if (!this.children.has(path)) {
|
||||
this.children.set(path, new StorageMemory<C>());
|
||||
}
|
||||
return this.children.get(path) as StorageMemory<C>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user