node-json-db
    Preparing search index...

    Implements

    Index

    Constructors

    • Parameters

      • filename: string
      • saveOnPush: boolean = true
      • humanReadable: boolean = false
      • separator: string = '/'
      • syncOnSave: boolean = false

      Returns Config

    Properties

    adapter: IAdapter<any>
    humanReadable: boolean
    saveOnPush: boolean
    separator: string
    syncOnSave: boolean

    Accessors

    Methods

    • 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.

      Parameters

      Returns void

      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);
    • 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.

      Parameters

      • cipherKey: 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)

      Returns void

      If the key is asymmetric (not supported)

      If the key length is not exactly 32 bytes

      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