Kysely adapters for Emmett Crypto Shredding, providing PostgreSQL implementations for key storage and policy storage.
👉 View Complete Documentation →
This package provides Kysely-specific database adapters for:
- Key Storage - PostgreSQL implementation of the
KeyStorageinterface - Policy Storage - PostgreSQL implementation of the
PolicyStorageinterface - Policy Management - Utilities for managing encryption policies in the database
The actual encryption/decryption logic, key rotation, and crypto shredding functionality are provided by @wataruoguchi/emmett-crypto-shredding.
npm install @wataruoguchi/emmett-crypto-shredding-kysely @wataruoguchi/emmett-crypto-shredding kysely pgCopy the migration file from database/migrations/1761627233034_crypto_shredding.ts to your migrations directory and run it.
import { createPolicies } from '@wataruoguchi/emmett-crypto-shredding-kysely';
// Example: During tenant onboarding
await createPolicies(db, [
{
policyId: `${tenantId}-generator`,
partition: tenantId,
streamTypeClass: 'generator', // Stream type to encrypt
encryptionAlgorithm: 'AES-GCM',
keyRotationIntervalDays: 180,
keyScope: 'stream', // 'stream' or 'type'
},
]);Or use default policies:
import { createDefaultPolicies } from '@wataruoguchi/emmett-crypto-shredding-kysely';
// Creates policies for 'user-data' and 'audit-log' stream types
await createDefaultPolicies(db, tenantId);Wire up the crypto event store in your module:
import {
createCryptoEventStore,
createWebCryptoProvider,
} from '@wataruoguchi/emmett-crypto-shredding';
import {
createKeyManagement,
createPolicyResolver,
} from '@wataruoguchi/emmett-crypto-shredding-kysely';
import { getKyselyEventStore } from '@wataruoguchi/emmett-event-store-kysely';
export function createMyModule({ db, logger }) {
const eventStore = createCryptoEventStore(
getKyselyEventStore({ db, logger }),
{
policy: createPolicyResolver(db, logger),
keys: createKeyManagement(db),
crypto: createWebCryptoProvider(),
buildAAD: ({ partition, streamId }) =>
new TextEncoder().encode(`${partition}:${streamId}`),
logger,
},
);
// Use eventStore with your event handlers...
}Creates a key management service backed by PostgreSQL.
Creates a lower-level key storage adapter (if you need direct access).
Creates a policy resolver backed by PostgreSQL.
Creates a lower-level policy storage adapter.
Create multiple encryption policies in a single batch operation.
Create default encryption policies for a partition using the default policy configuration.
updatePolicy(db: Kysely<any> | any, policyId: string, partition: string, updates: {...}): Promise<void>
Update an existing encryption policy.
Delete an encryption policy.
List all policies for a partition.
MIT