@@ -12,7 +12,6 @@ const pkg = require('../../../package.json');
1212const processConnectionOptions = require ( '../../helpers/processConnectionOptions' ) ;
1313const setTimeout = require ( '../../helpers/timers' ) . setTimeout ;
1414const utils = require ( '../../utils' ) ;
15- const { inferBSONType } = require ( '../../encryption_utils' ) ;
1615
1716/**
1817 * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
@@ -305,8 +304,7 @@ NativeConnection.prototype.createClient = async function createClient(uri, optio
305304 } ;
306305 }
307306
308-
309- const { schemaMap, encryptedFieldsMap } = this . _buildEncryptionSchemas ( options ) ;
307+ const { schemaMap, encryptedFieldsMap } = this . _buildEncryptionSchemas ( ) ;
310308
311309 if ( Object . keys ( schemaMap ) . length > 0 ) {
312310 options . autoEncryption . schemaMap = schemaMap ;
@@ -349,91 +347,28 @@ NativeConnection.prototype.createClient = async function createClient(uri, optio
349347 */
350348NativeConnection . prototype . _buildEncryptionSchemas = function ( ) {
351349 const schemaMap = Object . values ( this . models ) . filter ( model => model . schema . encryptionType ( ) === 'csfle' ) . reduce (
352- schemaMapReducer . bind ( this ) ,
350+ ( schemaMap , model ) => {
351+ const { schema, collection : { collectionName } } = model ;
352+ const namespace = `${ this . $dbName } .${ collectionName } ` ;
353+ schemaMap [ namespace ] = schema . _buildSchemaMap ( ) ;
354+ return schemaMap ;
355+ } ,
353356 { }
354357 ) ;
358+
355359 const encryptedFieldsMap = Object . values ( this . models ) . filter ( model => model . schema . encryptionType ( ) === 'qe' ) . reduce (
356- encryptedFieldsMapReducer . bind ( this ) ,
360+ ( encryptedFieldsMap , model ) => {
361+ const { schema, collection : { collectionName } } = model ;
362+ const namespace = `${ this . $dbName } .${ collectionName } ` ;
363+ encryptedFieldsMap [ namespace ] = schema . _buildEncryptedFields ( ) ;
364+ return encryptedFieldsMap ;
365+ } ,
357366 { }
358367 ) ;
359368
360369 return {
361370 schemaMap, encryptedFieldsMap
362371 } ;
363-
364- /**
365- * `schemaMap`s are JSON schemas, which use the following structure to represent objects:
366- * { field: { bsonType: 'object', properties: { ... } } }
367- *
368- * for example, a schema that looks like this `{ a: { b: int32 } }` would be encoded as
369- * `{ a: { bsonType: 'object', properties: { b: < encryption configuration > } } }`
370- *
371- * This function takes an array of path segments, an output object (that gets mutated) and
372- * a value to associated with the full path, and constructs a valid CSFLE JSON schema path for
373- * the object. This works for deeply nested properties as well.
374- *
375- * @param {string[] } path array of path components
376- * @param {object } object the object in which to build a JSON schema of `path`'s properties
377- * @param {object } value the value to associate with the path in object
378- */
379- function buildNestedPath ( path , object , value ) {
380- let i = 0 , component = path [ i ] ;
381- for ( ; i < path . length - 1 ; ++ i , component = path [ i ] ) {
382- object [ component ] = object [ component ] == null ? {
383- bsonType : 'object' ,
384- properties : { }
385- } : object [ component ] ;
386- object = object [ component ] . properties ;
387- }
388- object [ component ] = value ;
389- }
390-
391- /**
392- * @param {object } schemaMap the accumulation schemaMap
393- * @param {Model } the model
394- * @returns
395- */
396- function schemaMapReducer ( schemaMap , model ) {
397- const { schema, collection : { collectionName } } = model ;
398- const namespace = `${ this . $dbName } .${ collectionName } ` ;
399-
400- function schemaMapPropertyReducer ( accum , [ path , propertyConfig ] ) {
401- const bsonType = inferBSONType ( schema , path ) ;
402- const pathComponents = path . split ( '.' ) ;
403- const configuration = { encrypt : { ...propertyConfig , bsonType } } ;
404- buildNestedPath ( pathComponents , accum , configuration ) ;
405- return accum ;
406- }
407- const properties = Object . entries ( schema . encryptedFields ) . reduce (
408- schemaMapPropertyReducer ,
409- { } ) ;
410-
411- schemaMap [ namespace ] = {
412- bsonType : 'object' ,
413- properties
414- } ;
415- return schemaMap ;
416- }
417-
418- /**
419- *
420- * @param {object } encryptedFieldsMap the accumulation encryptedFieldsMap
421- * @param {Model } the model
422- * @returns
423- */
424- function encryptedFieldsMapReducer ( encryptedFieldsMap , { schema, collection : { collectionName } } ) {
425- const namespace = `${ this . $dbName } .${ collectionName } ` ;
426- const fields = Object . entries ( schema . encryptedFields ) . map (
427- ( [ path , config ] ) => {
428- const bsonType = inferBSONType ( schema , path ) ;
429- // { path, bsonType, keyId, queries? }
430- return { path, bsonType, ...config } ;
431- } ) ;
432-
433- encryptedFieldsMap [ namespace ] = { fields } ;
434-
435- return encryptedFieldsMap ;
436- }
437372} ;
438373
439374/*!
0 commit comments