Skip to content

Commit 8821dd3

Browse files
support discriminators
1 parent 3e4ba3f commit 8821dd3

File tree

85 files changed

+4449
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+4449
-582
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = {
1515
'!.*',
1616
'node_modules',
1717
'.git',
18-
'data'
18+
'data',
19+
'.config'
1920
],
2021
overrides: [
2122
{

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
with:
2727
fetch-depth: 0
2828
- name: Setup node
29-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
29+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
3030
with:
3131
node-version: 22
3232

.github/workflows/documentation.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3232

3333
- name: Setup node
34-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
34+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
3535
with:
3636
node-version: 22
3737

@@ -52,7 +52,7 @@ jobs:
5252
- run: git fetch --depth=1 --tags # download all tags for documentation
5353

5454
- name: Setup node
55-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
55+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
5656
with:
5757
node-version: 22
5858

.github/workflows/encryption-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
env:
2525
FORCE_COLOR: true
2626
steps:
27-
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
27+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2828
- name: Setup node
29-
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
29+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
3030
with:
3131
node-version: 22
3232
- name: Install Dependencies

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2626

2727
- name: Setup node
28-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
28+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
2929
with:
3030
node-version: 22
3131

@@ -61,7 +61,7 @@ jobs:
6161
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6262

6363
- name: Setup node
64-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
64+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
6565
with:
6666
node-version: ${{ matrix.node }}
6767

@@ -96,7 +96,7 @@ jobs:
9696
steps:
9797
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
9898
- name: Setup node
99-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
99+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
100100
with:
101101
node-version: 22
102102
- name: Load MongoDB binary cache
@@ -124,7 +124,7 @@ jobs:
124124
steps:
125125
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
126126
- name: Setup node
127-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
127+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
128128
with:
129129
node-version: 22
130130
- run: npm install

.github/workflows/tidelift-alignment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Checkout
1818
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1919
- name: Setup node
20-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
20+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
2121
with:
2222
node-version: 22
2323
- name: Alignment

.github/workflows/tsd.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2424

2525
- name: Setup node
26-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
26+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
2727
with:
2828
node-version: 22
2929

@@ -41,7 +41,7 @@ jobs:
4141
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
4242

4343
- name: Setup node
44-
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
44+
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
4545
with:
4646
node-version: 22
4747

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ list.out
7070

7171
data
7272
*.pid
73+
mo-expansion*

CHANGELOG.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
1+
8.12.1 / 2025-03-04
2+
===================
3+
* fix: match bson version with mongodb's bson version #15297 [hasezoey](https://github.com/hasezoey)
4+
5+
8.12.0 / 2025-03-03
6+
===================
7+
* feat: bump mongodb driver to 6.14
8+
* feat: expose "SchemaTypeOptions" in browser #15277 [hasezoey](https://github.com/hasezoey)
9+
* docs: update field-level-encryption.md #15272 [dphrag](https://github.com/dphrag)
10+
11+
8.11.0 / 2025-02-26
12+
===================
13+
* feat(model): make bulkWrite results include MongoDB bulk write errors as well as validation errors #15271 #15265
14+
* feat(document): add schemaFieldsOnly option to toObject() and toJSON() #15259 #15218
15+
* feat: introduce populate ordered option for populating in series rather than in parallel for transactions #15239 #15231 #15210
16+
* fix(bigint): throw error when casting BigInt that's outside of the bounds of what MongoDB can safely store #15230 #15200
17+
18+
8.10.2 / 2025-02-25
19+
===================
20+
* fix(model+connection): return MongoDB BulkWriteResult instance even if no valid ops #15266 #15265
21+
* fix(debug): avoid printing trusted symbol in debug output #15267 #15263
22+
* types: make type inference logic resilient to no Buffer type due to missing @types/node #15261
23+
24+
8.10.1 / 2025-02-14
25+
===================
26+
* perf(document): only call undoReset() 1x/document #15257 #15255
27+
* perf(schema): clear childSchemas when overwriting existing path to avoid performance degradations #15256 #15253
28+
* perf: some more micro optimizations for find() and findOne() #14906 #15250
29+
* fix(model): avoid adding timeout on Model.init() buffering to avoid unintentional dangling open handles #15251 #15241
30+
* fix: avoid connection buffering on init if autoCreate: false #15247 #15241
31+
* fix: infer discriminator key if set in $set with overwriteDiscriminatorKey #15243 #15218
32+
* types(middleware): make this in document middleware the hydrated doc type, not raw doc type #15246 #15242
33+
* types(schema): support options parameter to Schema.prototype.discriminator() #15249 #15244
34+
* types(schema): allow calling Schema.prototype.number() with no message arg #15237 #15236
35+
* docs(typescript): recommend using HydratedSingleSubdocument over Types.Subdocument #15240 #15211
36+
37+
8.10.0 / 2025-02-05
38+
===================
39+
* feat(schema+schematype): add toJSONSchema() method to convert schemas and schematypes to JSON schema #15184 #11162
40+
* feat(connection): make connection helpers respect bufferTimeoutMS #15229 #15201
41+
* feat(document): support schematype-level transform option #15163 #15084
42+
* feat(model): add insertOne() function to insert a single doc #15162 #14843
43+
* feat(connection): support Connection.prototype.aggregate() for db-level aggregations #15153
44+
* feat(model): make syncIndexes() not call createIndex() on indexes that already exist #15175 #12250
45+
* feat(model): useConnection(connection) function #14802
46+
* fix(model): disallow updateMany(update) and fix TypeScript types re: updateMany() #15199 #15190
47+
* fix(collection): avoid buffering if creating a collection during a connection interruption #15187 #14971
48+
* fix(model): throw error if calling create() with multiple docs in a transaction unless ordered: true #15100
49+
* fix(model): skip createCollection() in syncIndexes() if autoCreate: false #15155
50+
* fix(model): make `hydrate()` handle hydrating deeply nested populated docs with hydratedPopulatedDocs #15130
51+
* types(document): make sure toObject() and toJSON() apply versionKey __v #15097
52+
* ci(NODE-6505): CI Setup for Encryption Support #15139 [aditi-khare-mongoDB](https://github.com/aditi-khare-mongoDB)
53+
54+
8.9.7 / 2025-02-04
55+
==================
56+
* fix: avoid applying defaults on map embedded paths #15217 #15196
57+
* types: add missing $median operator to aggregation types #15233 #15209
58+
* docs(document): clarify that toObject() returns a POJO that may contain non-POJO values #15232 #15208
59+
60+
8.9.6 / 2025-01-31
61+
==================
62+
* fix(document): allow setting values to undefined with set(obj) syntax with strict: false #15207 #15192
63+
* fix(schema): improve reason for UUID cast error, currently a TypeError #15215 #15202
64+
* fix(aggregate): improve error when calling near() with invalid coordinates #15206 #15188
65+
166
7.8.6 / 2025-01-20
267
===================
368
* chore: remove coverage output from bundle
@@ -94,8 +159,6 @@
94159
* fix: disallow using $where in match
95160
* perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties #15055 #15029
96161
* fix(model+query): support overwriteDiscriminatorKey for bulkWrite updateOne and updateMany, allow inferring discriminator key from update #15046 #15040
97-
=======
98-
>>>>>>> 7.x
99162

100163
7.8.3 / 2024-11-26
101164
==================

docs/field-level-encryption.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ Keep in mind that the following example is a simple example to help you get star
3939
The encryption key in the following example is insecure; MongoDB recommends using a [KMS](https://www.mongodb.com/docs/v5.0/core/security-client-side-encryption-key-management/).
4040

4141
```javascript
42-
const { ClientEncryption } = require('mongodb-client-encryption');
42+
const { ClientEncryption } = require('mongodb');
4343
const mongoose = require('mongoose');
44-
const { Binary } = require('mongodb');
4544

4645
run().catch(err => console.log(err));
4746

@@ -66,12 +65,14 @@ async function run() {
6665
kmsProviders
6766
}
6867
}).asPromise();
69-
const encryption = new ClientEncryption(conn.client, {
68+
const encryption = new ClientEncryption(conn.getClient(), {
7069
keyVaultNamespace,
7170
kmsProviders,
7271
});
7372

74-
const _key = await encryption.createDataKey('local');
73+
const _key = await encryption.createDataKey('local', {
74+
keyAltNames: ['exampleKeyName'],
75+
});
7576
}
7677
```
7778

@@ -112,3 +113,51 @@ With the above connection, if you create a model named 'Test' that uses the 'tes
112113
const Model = mongoose.model('Test', mongoose.Schema({ name: String }));
113114
await Model.create({ name: 'super secret' });
114115
```
116+
117+
## Automatic FLE in Mongoose
118+
119+
Mongoose supports the declaration of encrypted schemas - schemas that, when connected to a model, utilize MongoDB's Client Side
120+
Field Level Encryption or Queryable Encryption under the hood. Mongoose automatically generates either an `encryptedFieldsMap` or a
121+
`schemaMap` when instantiating a MongoClient and encrypts fields on write and decrypts fields on reads.
122+
123+
### Encryption types
124+
125+
MongoDB has two different automatic encryption implementations: client side field level encryption (CSFLE) and queryable encryption (QE).
126+
See [choosing an in-use encryption approach](https://www.mongodb.com/docs/v7.3/core/queryable-encryption/about-qe-csfle/#choosing-an-in-use-encryption-approach).
127+
128+
### Declaring Encrypted Schemas
129+
130+
The following schema declares two properties, `name` and `ssn`. `ssn` is encrypted using queryable encryption, and
131+
is configured for equality queries:
132+
133+
```javascript
134+
const encryptedUserSchema = new Schema({
135+
name: String,
136+
ssn: {
137+
type: String,
138+
// 1
139+
encrypt: {
140+
keyId: '<uuid string of key id>',
141+
queries: 'equality'
142+
}
143+
}
144+
// 2
145+
}, { encryptionType: 'queryableEncryption' });
146+
```
147+
148+
To declare a field as encrypted, you must:
149+
150+
1. Annotate the field with encryption metadata in the schema definition
151+
2. Choose an encryption type for the schema and configure the schema for the encryption type
152+
153+
Not all schematypes are supported for CSFLE and QE. For an overview of valid schema types, refer to MongoDB's documentation.
154+
155+
### Registering Models
156+
157+
Encrypted schemas must be registered on a connection, not the Mongoose global:
158+
159+
```javascript
160+
161+
const connection = mongoose.createConnection();
162+
const UserModel = connection.model('User', encryptedUserSchema);
163+
```

0 commit comments

Comments
 (0)