@@ -58,6 +58,124 @@ describe('Encrypted test', () => {
58
58
expect ( read . encrypted_value ) . toBe ( 'abc123' ) ;
59
59
expect ( sudoRead . encrypted_value ) . not . toBe ( 'abc123' ) ;
60
60
expect ( rawRead . encrypted_value ) . not . toBe ( 'abc123' ) ;
61
+
62
+ // update
63
+ const updated = await db . user . update ( {
64
+ where : { id : '1' } ,
65
+ data : { encrypted_value : 'abc234' } ,
66
+ } ) ;
67
+ expect ( updated . encrypted_value ) . toBe ( 'abc234' ) ;
68
+ await expect ( db . user . findUnique ( { where : { id : '1' } } ) ) . resolves . toMatchObject ( {
69
+ encrypted_value : 'abc234' ,
70
+ } ) ;
71
+ await expect ( prisma . user . findUnique ( { where : { id : '1' } } ) ) . resolves . not . toMatchObject ( {
72
+ encrypted_value : 'abc234' ,
73
+ } ) ;
74
+
75
+ // upsert with create
76
+ const upsertCreate = await db . user . upsert ( {
77
+ where : { id : '2' } ,
78
+ create : {
79
+ id : '2' ,
80
+ encrypted_value : 'abc345' ,
81
+ } ,
82
+ update : {
83
+ encrypted_value : 'abc456' ,
84
+ } ,
85
+ } ) ;
86
+ expect ( upsertCreate . encrypted_value ) . toBe ( 'abc345' ) ;
87
+ await expect ( db . user . findUnique ( { where : { id : '2' } } ) ) . resolves . toMatchObject ( {
88
+ encrypted_value : 'abc345' ,
89
+ } ) ;
90
+ await expect ( prisma . user . findUnique ( { where : { id : '2' } } ) ) . resolves . not . toMatchObject ( {
91
+ encrypted_value : 'abc345' ,
92
+ } ) ;
93
+
94
+ // upsert with update
95
+ const upsertUpdate = await db . user . upsert ( {
96
+ where : { id : '2' } ,
97
+ create : {
98
+ id : '2' ,
99
+ encrypted_value : 'abc345' ,
100
+ } ,
101
+ update : {
102
+ encrypted_value : 'abc456' ,
103
+ } ,
104
+ } ) ;
105
+ expect ( upsertUpdate . encrypted_value ) . toBe ( 'abc456' ) ;
106
+ await expect ( db . user . findUnique ( { where : { id : '2' } } ) ) . resolves . toMatchObject ( {
107
+ encrypted_value : 'abc456' ,
108
+ } ) ;
109
+ await expect ( prisma . user . findUnique ( { where : { id : '2' } } ) ) . resolves . not . toMatchObject ( {
110
+ encrypted_value : 'abc456' ,
111
+ } ) ;
112
+
113
+ // createMany
114
+ await db . user . createMany ( {
115
+ data : [
116
+ { id : '3' , encrypted_value : 'abc567' } ,
117
+ { id : '4' , encrypted_value : 'abc678' } ,
118
+ ] ,
119
+ } ) ;
120
+ await expect ( db . user . findUnique ( { where : { id : '3' } } ) ) . resolves . toMatchObject ( {
121
+ encrypted_value : 'abc567' ,
122
+ } ) ;
123
+ await expect ( prisma . user . findUnique ( { where : { id : '3' } } ) ) . resolves . not . toMatchObject ( {
124
+ encrypted_value : 'abc567' ,
125
+ } ) ;
126
+
127
+ // createManyAndReturn
128
+ await expect (
129
+ db . user . createManyAndReturn ( {
130
+ data : [
131
+ { id : '5' , encrypted_value : 'abc789' } ,
132
+ { id : '6' , encrypted_value : 'abc890' } ,
133
+ ] ,
134
+ } )
135
+ ) . resolves . toEqual (
136
+ expect . arrayContaining ( [
137
+ { id : '5' , encrypted_value : 'abc789' } ,
138
+ { id : '6' , encrypted_value : 'abc890' } ,
139
+ ] )
140
+ ) ;
141
+ await expect ( db . user . findUnique ( { where : { id : '5' } } ) ) . resolves . toMatchObject ( {
142
+ encrypted_value : 'abc789' ,
143
+ } ) ;
144
+ await expect ( prisma . user . findUnique ( { where : { id : '5' } } ) ) . resolves . not . toMatchObject ( {
145
+ encrypted_value : 'abc789' ,
146
+ } ) ;
147
+ } ) ;
148
+
149
+ it ( 'Works with nullish values' , async ( ) => {
150
+ const { enhance, prisma } = await loadSchema (
151
+ `
152
+ model User {
153
+ id String @id @default(cuid())
154
+ encrypted_value String? @encrypted()
155
+ }` ,
156
+ {
157
+ enhancements : [ 'encryption' ] ,
158
+ enhanceOptions : {
159
+ encryption : { encryptionKey } ,
160
+ } ,
161
+ }
162
+ ) ;
163
+
164
+ const db = enhance ( ) ;
165
+ await expect ( db . user . create ( { data : { id : '1' , encrypted_value : '' } } ) ) . resolves . toMatchObject ( {
166
+ encrypted_value : '' ,
167
+ } ) ;
168
+ await expect ( prisma . user . findUnique ( { where : { id : '1' } } ) ) . resolves . toMatchObject ( { encrypted_value : '' } ) ;
169
+
170
+ await expect ( db . user . create ( { data : { id : '2' } } ) ) . resolves . toMatchObject ( {
171
+ encrypted_value : null ,
172
+ } ) ;
173
+ await expect ( prisma . user . findUnique ( { where : { id : '2' } } ) ) . resolves . toMatchObject ( { encrypted_value : null } ) ;
174
+
175
+ await expect ( db . user . create ( { data : { id : '3' , encrypted_value : null } } ) ) . resolves . toMatchObject ( {
176
+ encrypted_value : null ,
177
+ } ) ;
178
+ await expect ( prisma . user . findUnique ( { where : { id : '3' } } ) ) . resolves . toMatchObject ( { encrypted_value : null } ) ;
61
179
} ) ;
62
180
63
181
it ( 'Decrypts nested fields' , async ( ) => {
0 commit comments