generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.ts
55 lines (49 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { analyseDMMF } from './dmmf'
import {
configureKeys,
decryptOnRead,
encryptOnWrite,
ConfigureKeysParams
} from './encryption'
import type { Configuration, Middleware, MiddlewareParams } from './types'
export function fieldEncryptionMiddleware(
config: Configuration = {}
): Middleware {
// This will throw if the encryption key is missing
// or if anything is invalid.
const configureKeysParams: ConfigureKeysParams = {
encryptionKey: config.encryptionKey,
decryptionKeys: config.decryptionKeys
}
const keys = configureKeys(configureKeysParams)
const models = analyseDMMF()
return async function fieldEncryptionMiddleware(
params: MiddlewareParams,
next: (params: MiddlewareParams) => Promise<any>
) {
if (!params.model) {
// Unsupported operation
return await next(params)
}
const operation = `${params.model}.${params.action}`
// Params are mutated in-place for modifications to occur.
// See https://github.com/prisma/prisma/issues/9522
const encryptedParams = encryptOnWrite(
params,
keys,
models,
operation,
config.encryptionFn
)
let result = await next(encryptedParams)
decryptOnRead(
encryptedParams,
result,
keys,
models,
operation,
config.decryptionFn
)
return result
}
}