node-json-db
Version:
Database using JSON file as storage for Node.JS
81 lines (80 loc) • 3.15 kB
TypeScript
import { IAdapter } from "../adapter/IAdapter";
import { ISerializer } from "../adapter/data/ISerializer";
import { CipherKey } from 'crypto';
export interface JsonDBConfig {
readonly adapter: IAdapter<any>;
readonly saveOnPush: boolean;
readonly separator: string;
}
export declare class Config implements JsonDBConfig {
adapter: IAdapter<any>;
private _filename;
saveOnPush: boolean;
separator: string;
syncOnSave: boolean;
humanReadable: boolean;
private _serializers;
private _cipherKey?;
get filename(): string;
constructor(filename: string, saveOnPush?: boolean, humanReadable?: boolean, separator?: string, syncOnSave?: boolean);
/**
* Add a custom serializer for handling additional types during JSON serialization.
*
* Custom serializers allow you to extend the built-in type support (Date, Set, Map, RegExp, BigInt)
* with your own types. Each serializer uses a `__type`/`__value` envelope pattern in the stored JSON.
*
* @param serializer - The serializer to add. Must implement the {@link ISerializer} interface.
*
* @example
* ```typescript
* import { Config, ISerializer } from 'node-json-db';
*
* const urlSerializer: ISerializer = {
* type: "URL",
* serialize: (value: URL) => value.href,
* deserialize: (value: string) => new URL(value),
* test: (value: any) => value instanceof URL,
* };
*
* const config = new Config('mydb');
* config.addSerializer(urlSerializer);
* ```
*/
addSerializer(serializer: ISerializer): void;
/**
* Enable encryption for the database using AES-256-GCM.
*
* When encryption is enabled, the database filename automatically changes to use the `.enc.json` extension.
* For example, `mydb.json` becomes `mydb.enc.json`. This prevents accidentally accessing encrypted
* databases without proper encryption settings.
*
* This method is idempotent - calling it multiple times won't keep changing the filename.
*
* @param cipherKey - The encryption key. Must be exactly 32 bytes. Can be:
* - A string of 32 characters
* - A Buffer of 32 bytes
* - A symmetric KeyObject with 256-bit key size (from Node.js crypto module)
*
* @throws {Error} If the key is asymmetric (not supported)
* @throws {Error} If the key length is not exactly 32 bytes
*
* @example
* ```typescript
* import { Config } from 'node-json-db';
* import { randomBytes } from 'crypto';
*
* const config = new Config('mydb', true);
* const key = randomBytes(32); // 32-byte encryption key
* config.setEncryption(key);
* // Database will now be stored in 'mydb.enc.json' with encrypted data
* ```
*/
setEncryption(cipherKey: CipherKey): void;
private _rebuildAdapter;
}
export declare class ConfigWithAdapter implements JsonDBConfig {
readonly adapter: IAdapter<any>;
readonly saveOnPush: boolean;
readonly separator: string;
constructor(adapter: IAdapter<any>, saveOnPush?: boolean, separator?: string);
}