Skip to content

Commit b9ad94e

Browse files
TS support
1 parent 36bbc33 commit b9ad94e

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

test/types/schema.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ import {
2121
Types,
2222
Query,
2323
model,
24-
ValidateOpts,
25-
BufferToBinary
24+
ValidateOpts
2625
} from 'mongoose';
27-
import { Binary } from 'mongodb';
28-
import { IsPathRequired } from '../../types/inferschematype';
26+
import { Binary, BSON } from 'mongodb';
2927
import { expectType, expectError, expectAssignable } from 'tsd';
3028
import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype';
3129

@@ -591,6 +589,16 @@ const batchSchema2 = new Schema({ name: String }, { discriminatorKey: 'kind', st
591589
} } });
592590
batchSchema2.discriminator('event', eventSchema2);
593591

592+
593+
function encryptionType() {
594+
const keyId = new BSON.UUID();
595+
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'newFakeEncryptionType' }));
596+
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 1 }));
597+
598+
expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'queryableEncryption' }));
599+
expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'csfle' }));
600+
}
601+
594602
function gh11828() {
595603
interface IUser {
596604
name: string;

test/types/schemaTypeOptions.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BSON } from 'mongodb';
12
import {
23
AnyArray,
34
Schema,
@@ -74,3 +75,37 @@ function defaultOptions() {
7475
expectType<Record<string, any>>(new Schema.Types.Subdocument('none').defaultOptions);
7576
expectType<Record<string, any>>(new Schema.Types.UUID('none').defaultOptions);
7677
}
78+
79+
function encrypt() {
80+
const uuid = new BSON.UUID();
81+
const binary = new BSON.Binary();
82+
83+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' };
84+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' };
85+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, algorithm: undefined };
86+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: [uuid], algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' };
87+
88+
// qe + valid queries
89+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, queries: 'equality' };
90+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, queries: 'range' };
91+
new SchemaTypeOptions<string>()['encrypt'] = { keyId: uuid, queries: undefined };
92+
93+
// empty object
94+
expectError<SchemaTypeOptions<string>['encrypt']>({});
95+
96+
// invalid keyId
97+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: 'fakeId' });
98+
99+
// missing keyId
100+
expectError<SchemaTypeOptions<string>['encrypt']>({ queries: 'equality' });
101+
expectError<SchemaTypeOptions<string>['encrypt']>({ algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' });
102+
103+
// invalid algorithm
104+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: uuid, algorithm: 'SHA_FAKE_ALG' });
105+
106+
// invalid queries
107+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: uuid, queries: 'fakeQueryOption' });
108+
109+
// invalid input option
110+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: uuid, invalidKey: 'fakeKeyOption' });
111+
}

types/schemaoptions.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ declare module 'mongoose' {
258258
* @default false
259259
*/
260260
overwriteModels?: boolean;
261+
262+
/**
263+
* Required when the schema is encrypted.
264+
*/
265+
encryptionType?: 'csfle' | 'queryableEncryption';
261266
}
262267

263268
interface DefaultSchemaOptions {

types/schematypes.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as BSON from 'bson';
2+
13
declare module 'mongoose' {
24

35
/** The Mongoose Date [SchemaType](/docs/schematypes.html). */
@@ -207,6 +209,11 @@ declare module 'mongoose' {
207209
maxlength?: number | [number, string] | readonly [number, string];
208210

209211
[other: string]: any;
212+
213+
/**
214+
* If set, configures the field for automatic encryption.
215+
*/
216+
encrypt?: EncryptSchemaTypeOptions;
210217
}
211218

212219
interface Validator<DocType = any> {
@@ -218,6 +225,28 @@ declare module 'mongoose' {
218225

219226
type ValidatorFunction<DocType = any> = (this: DocType, value: any, validatorProperties?: Validator) => any;
220227

228+
interface QueryEncryptionEncryptOptions {
229+
/** The id of the dataKey to use for encryption. Must be a BSON binary subtype 4 (UUID). */
230+
keyId: BSON.Binary;
231+
232+
/**
233+
* Specifies the type of queries that the field can be queried on the encrypted field.
234+
*/
235+
queries?: 'equality' | 'range';
236+
}
237+
238+
interface ClientSideEncryptionEncryptOptions {
239+
/** The id of the dataKey to use for encryption. Must be a BSON binary subtype 4 (UUID). */
240+
keyId: [BSON.Binary] | BSON.Binary;
241+
242+
/**
243+
* The algorithm to use for encryption.
244+
*/
245+
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random';
246+
}
247+
248+
export type EncryptSchemaTypeOptions = QueryEncryptionEncryptOptions | ClientSideEncryptionEncryptOptions;
249+
221250
class SchemaType<T = any, DocType = any> {
222251
/** SchemaType constructor */
223252
constructor(path: string, options?: AnyObject, instance?: string);

0 commit comments

Comments
 (0)