Litestone guide / Protected data
Encryption
@encrypted stores a column as AES-256-GCM ciphertext and hides it from every non-system read. Give createClient a 32-byte key as 64 hex characters and the rest is transparent.
model Patient {
id Int
ssn String?
(0)
}
await db.patient.create({ data: { id: 1, ssn: '123-45-6789' } })
const seen = await db.patient.findUnique({ where: { id: 1 } })
if ('ssn' in seen) throw new Error('a non-system read carries no @encrypted column')
const plain = await db.asSystem().patient.findUnique({ where: { id: 1 } })
if (plain.ssn !== '123-45-6789') throw new Error('the system reads the plaintext')
The key comes from the environment: createClient({ path, encryptionKey: process.env.ENCRYPTION_KEY }). A non-system caller may still write the column, because whoever supplies a secret is routinely not the system.
What else it can do
@encrypted(deterministic: true)derives the IV from the value, so an equalitywhereworks and the value reads back- Deterministic mode leaves equal values visibly equal in the file; plain
@encryptedcannot be filtered at all equals,not,in,notInand a bare value work on a deterministic column, andcontainsor sorting are refused by name@uniqueover a plain@encryptedcolumn is refused at parse, because a random IV makes it never fire@guardedbeside@encryptedmakes a column no caller may write either- Every stored value names the key that wrote it, so a wrong key raises instead of reading as
null $rotateKey(newKey)re-encrypts every key-reversible column under a new key and swaps the client's keypreviousEncryptionKeyskeeps the old keys readable, which makes a rotation resumablelitestone rekeyis the same rotation from a shell- A caller cannot send a value that merely looks like ciphertext
encryptionKeycan be a function of the tenant for a key per tenant- A deterministic or hashed column can be compared in a policy, and a plain
@encryptedone is refused
In depth: Encryption, § @encrypted(deterministic: true), § Key rotation, Access control § Encryption keys, and rotating one, § Comparing an encrypted column