leaf-db
Version:
Small file-based database for node.js
65 lines (62 loc) • 1.7 kB
TypeScript
type Join<T extends Array<string | number | Symbol>> = T[number];
type DeepPartial<T> = Partial<{
[K in keyof T]?: T[K] extends object ? Update<T[K]> : T[K];
}>;
type Json = string | number | boolean | null | Json[] | {
[key: string]: Json;
};
type JsonObject = {
[key: string]: Json;
};
type Operators = {
$gt: number;
$gte: number;
$lt: number;
$lte: number;
$text: string;
$regex: RegExp;
$has: Json;
$size: number;
$not: Json;
};
type Draft = {
_id?: string;
} & {
[key: string]: Json;
[key: `$${string}`]: never;
[key: `__${string}`]: never;
};
type Doc<T extends Draft> = T & {
readonly _id: string;
readonly __deleted?: boolean;
};
type Query<T> = Partial<{
[K in keyof T]: T[K] extends JsonObject ? Query<T[K]> : T[K] | Partial<Operators>;
}>;
type Update<T> = Omit<DeepPartial<T>, '__deleted' | '_id'>;
type LeafDBOptions = {
storage?: string | {
root: string;
name?: string;
};
};
declare class LeafDB<T extends Draft> {
static id(): string;
private readonly _memory;
private readonly _storage?;
private _set;
private _delete;
constructor(options?: LeafDBOptions);
open(): {
raw: string;
err: unknown;
}[];
close(): void;
insert(drafts: T[]): Doc<T>[];
select(...queries: Array<Query<Doc<T>>>): Doc<T>[];
selectById(...ids: string[]): Doc<T>[];
update(update: Update<Doc<T>>, ...queries: Array<Query<Doc<T>>>): Doc<T>[];
delete(...queries: Array<Query<Doc<T>>>): number;
drop(): void;
}
export { DeepPartial, Doc, Draft, Join, Json, JsonObject, LeafDBOptions, Operators, Query, Update, LeafDB as default };