@kyvrixon/json-db
Version:
A simple, feature rich JSON database solution. Designed for Bun
171 lines • 5.4 kB
TypeScript
import { z } from 'zod';
/**
* Database configuration options.
*/
export interface DatabaseOptions {
/**
* Create directory if it does not exist.
*/
createDirectory?: boolean;
/**
* Enable or disable validation of data on read operations.
*/
validateOnRead?: boolean;
}
/**
* Represents a set of query operators for filtering data.
* All operators are properly typed to match the field type T.
*
* @template T - The type of the value being queried.
*/
export interface QueryOperators<T> {
/**
* Matches values that are equal to the specified value.
*/
$equals?: T;
/**
* Matches values that are not equal to the specified value.
*/
$notEquals?: T;
/**
* Matches values that are greater than the specified value.
* Only available for number, string, and Date types.
*/
$greaterThan?: T extends number | string | Date ? T : never;
/**
* Matches values that are greater than or equal to the specified value.
* Only available for number, string, and Date types.
*/
$greaterThanOrEqual?: T extends number | string | Date ? T : never;
/**
* Matches values that are less than the specified value.
* Only available for number, string, and Date types.
*/
$lessThan?: T extends number | string | Date ? T : never;
/**
* Matches values that are less than or equal to the specified value.
* Only available for number, string, and Date types.
*/
$lessThanOrEqual?: T extends number | string | Date ? T : never;
/**
* Matches values that are included in the specified array.
* For array fields, matches if any element is in the provided array.
* For non-array fields, matches if the value is in the provided array.
*/
$in?: T extends readonly (infer U)[] ? U[] : T[];
/**
* Matches values that are not included in the specified array.
* For array fields, matches if no element is in the provided array.
* For non-array fields, matches if the value is not in the provided array.
*/
$notIn?: T extends readonly (infer U)[] ? U[] : T[];
/**
* Checks if the field exists (true) or does not exist (false).
*/
$exists?: boolean;
/**
* Matches string values using the specified regular expression.
* Only available for string types.
*/
$regex?: T extends string ? RegExp : never;
/**
* Matches arrays with the specified number of elements.
* Only available for array types.
*/
$arraySize?: T extends readonly any[] ? number : never;
}
/**
* Main database filter type with full autocomplete support for field names
* and proper typing for each field's operators.
*/
export type DatabaseFilter<T> = {
[K in keyof T]?: T[K] | QueryOperators<T[K]>;
} & {
/**
* Logical AND - all conditions must be true
*/
$and?: DatabaseFilter<T>[];
/**
* Logical OR - at least one condition must be true
*/
$or?: DatabaseFilter<T>[];
/**
* Logical NOT - the condition must be false
*/
$not?: DatabaseFilter<T>;
};
export default class Database {
private basePath;
private options;
private lockedFiles;
constructor(basePath: string, options?: DatabaseOptions);
/**
* Ensures a directory exists, creating it if necessary
*/
private ensureDirectoryExists;
/**
* Acquires a memory-only lock for file operations
*/
private acquireLock;
/**
* Releases a memory-only lock for file operations
*/
private releaseLock;
/**
* Writes data to a JSON file atomically with memory lock
*/
private writeJSONFile;
/**
* Reads and parses a JSON file safely
*/
private readJSONFile;
/**
* Validates data against a Zod schema
*/
validate<T>(data: unknown, schema: z.ZodSchema<T>): T;
/**
* Writes a document to a path-based location
* Example: await db.write("users/123", myData) creates this.basePath/users/123.json
*/
write<T>(filePath: string, data: T, schema?: z.ZodSchema<T>): Promise<void>;
/**
* Reads a document from a path-based location
*/
read<T>(filePath: string, schema?: z.ZodSchema<T>): Promise<T | null>;
/**
* Reads all documents from a directory
*/
readAll<T>(dirPath: string, schema?: z.ZodSchema<T>): Promise<Map<string, T>>;
/**
* Finds documents matching a filter in a directory
*/
find<T>(dirPath: string, filter?: DatabaseFilter<T>, schema?: z.ZodSchema<T>): Promise<Map<string, T>>;
/**
* Finds the first document matching a filter
*/
findOne<T>(dirPath: string, filter?: DatabaseFilter<T>, schema?: z.ZodSchema<T>): Promise<{
id: string;
data: T;
} | null>;
/**
* Checks if a value matches a filter condition
*/
private matchesFilter;
/**
* Checks if a document matches a database filter
*/
private matchesDocument;
/**
* Deletes a document from a path-based location
*/
delete(filePath: string): Promise<boolean>;
/**
* Deletes a whole directory and all its contents
*/
drop(dirPath: string): Promise<boolean>;
/**
* Creates an empty directory (and parents if needed)
*/
create(dirPath: string): Promise<void>;
}
//# sourceMappingURL=json-db.d.ts.map