Crypto TypeORM
    Preparing search index...

    Class EncryptionSubscriber

    A TypeORM EntitySubscriber that hooks into the entity lifecycle to encrypt and decrypt fields automatically. The set of fields to process is defined via EncryptionConfig.fields — a Map keyed by entity constructor name.

    This approach is useful when you want to keep your entity classes free from transformer configuration and centralise encryption policy.

    Each value is bound to EntityName.field as associated data, and a stored value that cannot be decrypted makes afterLoad throw a FieldDecryptionError (unless allowPlaintextFallback is set and the value is not a ciphertext at all).

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

    const ds = new DataSource({
    // ... connection options
    subscribers: [
    new EncryptionSubscriber({
    key: process.env.COLUMN_ENCRYPTION_KEY!,
    fields: new Map([
    ["User", ["ssn", "email"]],
    ["Payment", ["cardNumber"]],
    ]),
    }),
    ],
    });

    Implements

    • EntitySubscriberInterface
    Index
    • Encrypt configured fields before a new entity is inserted.

      Parameters

      • event: InsertEvent<Record<string, unknown>>

      Returns void

    • Encrypt configured fields before an existing entity is updated.

      Parameters

      • event: UpdateEvent<Record<string, unknown>>

      Returns void

    • Decrypt configured fields after an entity is loaded from the database.

      Parameters

      • entity: Record<string, unknown>
      • Optionalevent: LoadEvent<Record<string, unknown>>

      Returns void