@aequum/in-memory
Version:
aequum in-memory stuff
112 lines (111 loc) • 3.87 kB
TypeScript
/**
* In memory (**volatile**) repository, implements basic
* input/output operations.
*
* **NOTE**: If you want to change the primary key field
* you must set the `primaryKeyField` property and pass the
* `PrimeryKeyField` type parameter.
*
* @typeParam Model - The model type, must extend `AnyObject`
* @typeParam PrimeryKeyField - The primary key field name, defaults to 'id'
* if this changes you must set the `primaryKeyField` property
*/
export declare class InMemoryRepository<Model extends {
[key in PrimeryKeyField]: any;
}, PrimeryKeyField extends string = 'id'> {
protected data: Model[];
protected primaryKeyField: PrimeryKeyField;
/**
* Primary Key generator, by default uses `randomUUID()`
* to generate a unique identifier.
*/
genPrimaryKey(): Model[PrimeryKeyField];
/**
* Check if an item matches the filter.
*
* @param item Repository item
* @param filter Custom filter to match against the item
*/
protected matchesFilter(item: Model, filter: Partial<Model>): boolean;
/**
* Find indexes of items that match the filter.
*
* @param filter Filter to match against the items
*/
protected matchIndexes(filter: Partial<Model>): number[];
/**
* Dot notation find, allows to find nested properties
* in the item.
*/
protected dotNotationFind(item: Model, property: string): any;
/**
* Delete an item from the repository.
*
* @param filter
*/
delete(filter: Partial<Model>): Promise<void>;
/**
* Delete all items that match the filter.
*
* @param filter
*/
deleteMany(filter: Partial<Model>): Promise<void>;
/**
* Find items in the repository that match the filter.
*
* @param filter
*/
find(filter: Partial<Model>): Promise<Model[]>;
/**
* Get an item from the repository that matches the filter.
*
* @param filter
*/
getOne(filter: Partial<Model>): Promise<Model>;
/**
* Get an item by its primary key.
*
* @param id
*/
getOneById(id: any): Promise<Model>;
/**
* Put an item into the repository.
*/
put(data: Model & {
[key in PrimeryKeyField]?: Model[PrimeryKeyField];
}): Promise<Model>;
/**
* Put many items into the repository.
*/
putMany(data: Model[]): Promise<Model[]>;
/**
* Update one or more items in the repository that match the filter.
*
* @param filter Filter to match
* @param data Data to update the items with
*/
update(filter: Partial<Model>, data: Partial<Model>): Promise<Model | Model[]>;
/**
* Push a value into an array property of items that match the filter.
*
* @param filter Filter to match the items
* @param property The property to push the value into, can be a dot notation
* string to access nested properties.
* For example: 'nested.property.array'
* @param value The value to push into the array property
* @throws NotFoundException if no items match the filter
* @throws ValidationException if the property is not a valid string
* @throws NotFoundException if the property is not found in the item
*/
pushOnArrayProperty(filter: Partial<Model>, property: string, value: any): Promise<Model | Model[]>;
/**
* Pull a value from an array property of items that match the filter.
*
* @param filter Filter to match the items
* @param property The property
* string to access nested properties.
* For example: 'nested.property.array'
* @param value The value to push into the array property
*/
pullFromArrayProperty(filter: Partial<Model>, property: string, value: any): Promise<Model | Model[]>;
}