Skip to content

Commit edfbdfa

Browse files
documentation
1 parent cda0e26 commit edfbdfa

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Diff for: docs/field-level-encryption.md

+55
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,58 @@ Encrypted schemas must be registered on a connection, not the Mongoose global:
161161
const connection = mongoose.createConnection();
162162
const UserModel = connection.model('User', encryptedUserSchema);
163163
```
164+
165+
### Connecting and configuring encryption options
166+
167+
CSFLE/QE in Mongoose work by generating the encryption schema that the MongoDB driver expects for each encrypted model on the connection. This happens automatically the model's connection is established.
168+
169+
Queryable encryption and csfle requires all the same configuration as outlined in <>, except for the schemaMap or encryptedFieldsMap options.
170+
171+
```javascript
172+
const keyVaultNamespace = 'client.encryption';
173+
const kmsProviders = { local: { key } };
174+
await connection.openUri(`mongodb://localhost:27017`, {
175+
// Configure auto encryption
176+
autoEncryption: {
177+
keyVaultNamespace: 'datakeys.datakeys',
178+
kmsProviders
179+
}
180+
});
181+
```
182+
183+
Once the connection is established, Mongoose's operations will work as usual. Writes are encrypted automatically by the MongoDB driver prior to sending them to the server and reads are decrypted by the driver after fetching documents from the server.
184+
185+
### Discriminators
186+
187+
Discriminators are supported for encrypted models as well:
188+
189+
```javascript
190+
const connection = createConnection();
191+
192+
const schema = new Schema({
193+
name: {
194+
type: String, encrypt: { keyId }
195+
}
196+
}, {
197+
encryptionType: 'queryableEncryption'
198+
});
199+
200+
const Model = connection.model('BaseUserModel', schema);
201+
const ModelWithAge = model.discriminator('ModelWithAge', new Schema({
202+
age: {
203+
type: Int32, encrypt: { keyId: keyId2 }
204+
}
205+
}, {
206+
encryptionType: 'queryableEncryption'
207+
}));
208+
209+
const ModelWithBirthday = model.discriminator('ModelWithBirthday', new Schema({
210+
dob: {
211+
type: Int32, encrypt: { keyId: keyId3 }
212+
}
213+
}, {
214+
encryptionType: 'queryableEncryption'
215+
}));
216+
```
217+
218+
When generating encryption schemas, Mongoose merges all discriminators together for the all discriminators declared on the same namespace. As a result, discriminators that declare the same key with different types are not supported. Furthermore, all discriminators must share the same encryption type - it is not possible to configure discriminators on the same model for both CSFLE and QE.

0 commit comments

Comments
 (0)