@gecogvidanto/plugin-nedb
Version:
Nebd local database management plugin for ĞecoĞvidanto
82 lines (81 loc) • 2.41 kB
TypeScript
/// <reference types="node" />
import { Database, ModelList } from '@gecogvidanto/plugin';
import NedbPromiseDatastore from '../NedbPromiseDatastore';
interface NedbDatabase<T extends ModelList> extends Database<T> {
readonly models: T;
readonly dbs: {
[K in keyof T]: NedbPromiseDatastore;
};
}
export interface BinaryData {
type: '$$BinaryData$$';
hash: string;
}
export declare function isBinaryData(data: unknown): data is BinaryData;
export interface BinaryStoreDoc {
_id?: string;
hash: string;
usages: Array<{
storeName: string;
recordId: string;
}>;
}
/**
* A datastore managing binary data.
*/
export default class BinaryStore {
private readonly allDb;
private readonly db;
private readonly binaryDirectory;
private readonly compactInterval;
private nextCompact;
/**
* Create a new binary store.
*
* @param allDb - The full database system.
* @param db - The database (actually, the binary datastore).
* @param binaryDirectory - The directory where binary items will be saved.
* @param compactInterval - Interval between datastore cleaning.
*/
constructor(allDb: NedbDatabase<ModelList>, db: NedbPromiseDatastore, binaryDirectory: string, compactInterval: number);
/**
* Ensure store is ready.
*/
start(): Promise<void>;
/**
* Save a binary item.
*
* @param storeName - The name of the store having binary data to store.
* @param recordId - The identifier of the record containing binary data.
* @param hash - The hash of the data to store.
* @param data - The data to store.
*/
save(storeName: string, recordId: string, hash: string, data: Buffer): Promise<void>;
/**
* Read the binary data for a hash.
*
* @param hash - The hash for which to read data.
* @returns The read binary data.
*/
read(hash: string): Promise<Buffer>;
/**
* Clean the database.
*/
private compact;
/**
* Remove unused binaries.
*/
private checkUsage;
/**
* Check consistency between existing files and stored hashes.
*/
private checkConsistency;
/**
* Get the path of the file containing data for the binary hash.
*
* @param hash - The binary hash.
* @returns The path of the file.
*/
private pathFor;
}
export {};