Skip to content

Commit

Permalink
add discriminator support
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Jan 24, 2025
1 parent d02257a commit 2926a92
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
28 changes: 20 additions & 8 deletions lib/drivers/node-mongodb-native/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pkg = require('../../../package.json');
const processConnectionOptions = require('../../helpers/processConnectionOptions');
const setTimeout = require('../../helpers/timers').setTimeout;
const utils = require('../../utils');
const { Schema } = require('../../mongoose');

/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
Expand Down Expand Up @@ -357,20 +358,31 @@ NativeConnection.prototype.createClient = async function createClient(uri, optio
* options.
*/
NativeConnection.prototype._buildEncryptionSchemas = function() {
const schemaMap = Object.values(this.models).filter(model => model.schema.encryptionType() === 'csfle').reduce(
(schemaMap, model) => {
const { schema, collection: { collectionName } } = model;
const namespace = `${this.$dbName}.${collectionName}`;
const qeMappings = {};
const csfleMappings = {};

for (const model of Object.values(this.models)) {
const { schema, collection: { collectionName } } = model;
const namespace = `${this.$dbName}.${collectionName}`;
if (schema.encryptionType() === 'csfle') {
csfleMappings[namespace] ??= new Schema({}, { encryptionType: 'csfle' });
csfleMappings[namespace].add(schema);
} else if (schema.encryptionType() === 'queryableEncryption') {
qeMappings[namespace] ??= new Schema({}, { encryptionType: 'queryableEncryption' });
qeMappings[namespace].add(schema);
}
}

const schemaMap = Object.entries(csfleMappings).reduce(
(schemaMap, [namespace, schema]) => {
schemaMap[namespace] = schema._buildSchemaMap();
return schemaMap;
},
{}
);

const encryptedFieldsMap = Object.values(this.models).filter(model => model.schema.encryptionType() === 'qe').reduce(
(encryptedFieldsMap, model) => {
const { schema, collection: { collectionName } } = model;
const namespace = `${this.$dbName}.${collectionName}`;
const encryptedFieldsMap = Object.entries(qeMappings).reduce(
(encryptedFieldsMap, [namespace, schema]) => {
encryptedFieldsMap[namespace] = schema._buildEncryptedFields();
return encryptedFieldsMap;
},
Expand Down
22 changes: 11 additions & 11 deletions test/encryption/encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ describe('ci', () => {
it(`${name} encrypts and decrypts`, test);
});

describe('QE', function() {
describe('queryableEncryption', function() {
beforeEach(async function() {
schema = new Schema({
field: {
type, encrypt: { keyId: keyId }
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});

connection = createConnection();
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});

connection = createConnection();
Expand Down Expand Up @@ -231,12 +231,12 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});
const schema = new Schema({
a: nestedSchema
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});

connection = createConnection();
Expand Down Expand Up @@ -307,7 +307,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});

connection = createConnection();
Expand Down Expand Up @@ -398,7 +398,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
});

connection = createConnection();
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
}));
const model2 = connection.model('Model2', new Schema({
b: {
Expand All @@ -491,7 +491,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
}));

return { model1, model2 };
Expand Down Expand Up @@ -551,7 +551,7 @@ describe('ci', () => {
}
}
}, {
encryptionType: 'qe'
encryptionType: 'queryableEncryption'
}));
const model2 = connection.model('Model2', new Schema({
b: {
Expand Down Expand Up @@ -684,7 +684,7 @@ describe('ci', () => {
});


describe('qe', function() {
describe('queryableEncryption', function() {
beforeEach(async function() {
connection = createConnection();

Expand Down

0 comments on commit 2926a92

Please sign in to comment.