The storage provider is only used when you call anonymizer.session(). Direct anonymize() calls return the PII map but don’t save it:
// ❌ Storage NOT used - you must handle the PII map yourselfconst result = await anonymizer.anonymize('Hello John!');// result.piiMap is returned but NOT saved to storage// ✅ Storage IS used - auto-saves and auto-loadsconst session = anonymizer.session('conversation-123');const result = await session.anonymize('Hello John!');// result.piiMap is automatically saved to storage
import { createAnonymizer, InMemoryKeyProvider, SQLitePIIStorageProvider, // or IndexedDBPIIStorageProvider for browser} from 'rehydra';// 1. Setup storage (once at app start)const storage = new SQLitePIIStorageProvider('./pii-maps.db');await storage.initialize();// 2. Create anonymizer with storage and key providerconst anonymizer = createAnonymizer({ ner: { mode: 'quantized' }, keyProvider: new InMemoryKeyProvider(), piiStorageProvider: storage,});await anonymizer.initialize();// 3. Create a session for each conversationconst session = anonymizer.session('conversation-123');// 4. Anonymize - auto-saves to storageconst result = await session.anonymize('Hello John Smith from Acme Corp!');console.log(result.anonymizedText);// "Hello <PII type="PERSON" id="1"/> from <PII type="ORG" id="1"/>!"// 5. Later (even after app restart): rehydrate - auto-loads and decryptsconst translated = await translateAPI(result.anonymizedText);const original = await session.rehydrate(translated);console.log(original);// "Hello John Smith from Acme Corp!"
Each session ID maps to a separate stored PII map:
// Different chat sessionsconst aliceChat = anonymizer.session('user-alice-chat');const bobChat = anonymizer.session('user-bob-chat');await aliceChat.anonymize('Alice: Contact me at alice@example.com');await bobChat.anonymize('Bob: My number is +49 30 123456');// Each session has independent storageawait aliceChat.rehydrate(translatedText1); // Uses Alice's PII mapawait bobChat.rehydrate(translatedText2); // Uses Bob's PII map
Within a session, entity IDs are consistent across multiple anonymize() calls:
const session = anonymizer.session('chat-123');// Message 1: User provides contact infoconst msg1 = await session.anonymize('Contact me at user@example.com');// → "Contact me at <PII type="EMAIL" id="1"/>"// Message 2: References same email + new one const msg2 = await session.anonymize('CC: user@example.com and admin@example.com');// → "CC: <PII type="EMAIL" id="1"/> and <PII type="EMAIL" id="2"/>"// ↑ Same ID (reused) ↑ New ID// All messages can be rehydrated correctlyawait session.rehydrate(msg1.anonymizedText); // ✓await session.rehydrate(msg2.anonymizedText); // ✓
import { SQLitePIIStorageProvider } from 'rehydra';// File-based databaseconst storage = new SQLitePIIStorageProvider('./data/pii-maps.db');await storage.initialize();// Or in-memory for testingconst testStorage = new SQLitePIIStorageProvider(':memory:');await testStorage.initialize();
Dependencies:
Bun: Uses built-in bun:sqlite (no additional install)