102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
import { EventEmitter } from 'src/utils/index.js';
|
|
|
|
/**
|
|
* Query options for find operations
|
|
*/
|
|
export type FindOptions = {
|
|
limit: number;
|
|
skip: number;
|
|
sort: { [key: string]: 1 | -1 };
|
|
};
|
|
|
|
export type StorageEvent<T = Record<string, any>> = {
|
|
insert: {
|
|
key: string;
|
|
value: T;
|
|
};
|
|
update: {
|
|
key: string;
|
|
value: T;
|
|
};
|
|
delete: {
|
|
key: string;
|
|
};
|
|
clear: undefined;
|
|
};
|
|
|
|
export abstract class BaseStorage<
|
|
T extends Record<string, any> = Record<string, any>,
|
|
> 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>;
|
|
|
|
/**
|
|
* Insert multiple documents into the store
|
|
* @param documents Map of ID to document
|
|
*/
|
|
abstract insertMany(documents: Map<string, 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>;
|
|
|
|
/**
|
|
* Find all documents that match the filter
|
|
* @param filter MongoDB-like query filter
|
|
* @param options Query options (limit, skip, sort)
|
|
*/
|
|
abstract find(filter?: Partial<T>, options?: FindOptions): Promise<T[]>;
|
|
|
|
/**
|
|
* Update a document that matches the filter
|
|
* @param filter Query to match the document to update
|
|
* @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>;
|
|
|
|
/**
|
|
* Update all documents that match the filter
|
|
* @param filter Query to match documents to update
|
|
* @param update Document or fields to update
|
|
* @param options Query options (limit, skip, sort)
|
|
* @returns Number of documents updated
|
|
*/
|
|
abstract updateMany(
|
|
filter: Partial<T>,
|
|
update: Partial<T>,
|
|
options: Partial<FindOptions>,
|
|
): Promise<number>;
|
|
|
|
/**
|
|
* Delete a document that matches the filter
|
|
* @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>;
|
|
|
|
/**
|
|
* Delete all documents that match the filter with options
|
|
* @param filter Query to match documents to delete
|
|
* @param options Query options (limit, skip, sort)
|
|
* @returns Number of documents deleted
|
|
*/
|
|
abstract deleteMany(
|
|
filter: Partial<T>,
|
|
options: Partial<FindOptions>,
|
|
): Promise<number>;
|
|
|
|
/**
|
|
* Derive a child storage from the current storage
|
|
* @param path The path to derive the child storage from
|
|
* @returns The child storage
|
|
*/
|
|
abstract deriveChild<C>(path: string): BaseStorage<C>;
|
|
}
|