Crypto TypeORM
    Preparing search index...

    Class EncryptionTransformer

    A TypeORM ValueTransformer that transparently encrypts column values before they are persisted and decrypts them when they are loaded.

    Uses crypto-lib's secretbox (XChaCha20-Poly1305) under the hood: each write generates a fresh random nonce, and the sealed output is stored as a Base64 string in the database.

    import { EncryptionTransformer } from "@sebastienrousseau/crypto-typeorm";

    const transformer = new EncryptionTransformer({
    key: process.env.COLUMN_ENCRYPTION_KEY!,
    });

    @Entity()
    class User {
    @Column({ type: "text", transformer })
    ssn!: string;
    }

    Implements

    • ValueTransformer
    Index
    • Encrypt a value before it is written to the database.

      • null / undefined values pass through unchanged.
      • Non-string values are JSON-serialised before encryption.
      • Returns a Base64-encoded sealed box (nonce + ciphertext + tag).

      Parameters

      • value: unknown

      Returns string | null

    • Decrypt a value after it is read from the database.

      • null / undefined values pass through unchanged.
      • Returns the original plaintext string.

      Parameters

      • value: unknown

      Returns string | null