json-file-database
Version:
Lightweight Database on NodeJS by JSON Files
77 lines (76 loc) • 2.06 kB
TypeScript
import { Collection } from './collection';
import type { Comparator, Element } from './collection';
import type { DatabaseFile } from './database-file';
/**
* Options to create a collection.
* @template E the type of element.
*/
export interface CollectionOptions<E extends Element> {
/**
* The name of the collection.
*/
name: string;
/**
* The primary key.
*/
primaryKey: keyof E;
/**
* The comparator to compare the elements.
* It will compare elements using built-in operators by default.
*/
comparator?: Comparator<E>;
}
/**
* What the Database will operate. It must contain array-typed values.
*/
export interface JSONData {
[key: string]: readonly any[];
}
/**
* The options when creating a connection of database file.
*/
export interface DatabaseOptions {
/**
* The file to process.
* If it is a string, it will be seen as a path to the file.
*/
file: string | DatabaseFile;
/**
* The delay of each saving action.
* @default 0
*/
delay?: number;
/**
* If the database file does not exist,
* it will create a new file with this object in it.
* @default {}
*/
init?: JSONData;
/**
* Save the json in a beautified version.
* @default false
*/
beautify?: boolean;
/**
* After the database file is saved,
* this function will be called if it is not undefined.
* @default ()=>{}
*/
onSaved?: (this: undefined) => void;
}
/**
* Creates a collection by full information.
*
* @template E the type of elements.
* @param options the options to create the collection.
* @returns the collection.
*/
export interface Database {
<E extends Element>(options: CollectionOptions<E>): Collection<E>;
}
/**
* Connects the database synchronously.
* @param options the options for connection.
* @returns the database.
*/
export declare function connect(options: DatabaseOptions): Database;