-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
130 lines (117 loc) · 2.88 KB
/
script.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { PrismaClient } from '@prisma/client'
import { fieldEncryptionExtension } from 'prisma-field-encryption'
const prisma = new PrismaClient().$extends(fieldEncryptionExtension())
async function main() {
// Play with the Prisma API here, and report back:
// https://github.com/47ng/prisma-field-encryption/issues/new
//
// Caveats & Limitations: you cannot filter on encrypted fields, eg:
// prisma.user.findUnique({ where: { name: 'Super secret' }}) won't work.
// Also, raw database access with $executeRaw and $queryRaw is not supported.
const USER_EMAIL = 'secret.spy@cia.gov'
// Clean slate
try {
process.env.PRISMA_FIELD_ENCRYPTION_LOG = 'false'
await prisma.user.delete({ where: { email: USER_EMAIL } })
await prisma.post.deleteMany()
} catch {}
// Un-comment this line to see internal operations:
// process.env.PRISMA_FIELD_ENCRYPTION_LOG = 'true'
await prisma.user.create({
data: {
email: USER_EMAIL,
name: 'Super secret',
},
})
const superSecretSpy = await prisma.user.findUnique({
where: { email: USER_EMAIL },
})
await prisma.user.update({
where: { email: USER_EMAIL },
data: {
name: 'Under cover',
},
})
const underCoverSpy = await prisma.user.findFirst({
where: { email: USER_EMAIL },
})
const report = await prisma.post.create({
data: {
author: {
connect: {
email: underCoverSpy?.email,
},
},
title: 'Secret report',
content: 'I have infiltrated the enemy base.',
},
})
const assignmentPre = await prisma.post.create({
data: {
author: {
connect: {
email: underCoverSpy?.email,
},
},
title: 'Your mission if you choose to accept it...',
content: 'This encrypted field will self-destruct in the next operation',
},
})
const assignmentPost = await prisma.post.update({
where: {
id: assignmentPre.id,
},
data: {
content: null,
},
})
const userUpdate = await prisma.user.update({
where: { email: USER_EMAIL },
data: {
name: 'I am the enemy now',
posts: {
update: {
where: {
id: report.id,
},
data: {
content: 'I hereby resign from my position, effective immediately.',
},
},
},
},
include: {
posts: {
select: {
title: true,
content: true,
},
},
},
})
const usersAndTheirPosts = await prisma.user.findMany({
include: {
posts: {
select: {
title: true,
content: true,
},
},
},
})
console.dir(
{
superSecretSpy,
underCoverSpy,
report,
assignmentPre,
assignmentPost,
userUpdate,
usersAndTheirPosts,
},
{ depth: Infinity }
)
}
main().finally(async () => {
await prisma.$disconnect()
})