Skip to content

Commit 4dead0a

Browse files
committed
Merge branch '7.x'
2 parents 6b78ba1 + d31310a commit 4dead0a

File tree

11 files changed

+221
-103
lines changed

11 files changed

+221
-103
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
7.6.6 / 2023-11-27
2+
==================
3+
* perf: avoid double-running setter logic when calling `push()` #14120 #11380
4+
* fix(populate): set populated docs in correct order when populating virtual underneath doc array with justOne #14105 #14018
5+
* fix: bump mongodb driver -> 5.9.1 #14084 #13829 [lorand-horvath](https://github.com/lorand-horvath)
6+
* types: allow defining document array using [{ prop: String }] syntax #14095 #13424
7+
* types: correct types for when includeResultMetadata: true is set #14078 #13987 [prathamVaidya](https://github.com/prathamVaidya)
8+
* types(query): base filters and projections off of RawDocType instead of DocType so autocomplete doesn't show populate #14118 #14077
9+
* types: make property names show up in intellisense for UpdateQuery #14123 #14090
10+
* types(model): support calling Model.validate() with pathsToSkip option #14088 #14003
11+
* docs: remove "DEPRECATED" warning mistakenly added to read() tags param #13980
12+
113
8.0.1 / 2023-11-15
214
==================
315
* fix: retain key order with aliases when creating indexes with alias #14042 [meabed](https://github.com/meabed)

lib/aggregate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ Aggregate.prototype.unionWith = function(options) {
665665
* await Model.aggregate(pipeline).read('primaryPreferred');
666666
*
667667
* @param {String|ReadPreference} pref one of the listed preference options or their aliases
668-
* @param {Array} [tags] optional tags for this query. DEPRECATED
668+
* @param {Array} [tags] optional tags for this query.
669669
* @return {Aggregate} this
670670
* @api public
671671
* @see mongodb https://www.mongodb.com/docs/manual/applications/replication/#read-preference

lib/stateMachine.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ StateMachine.ctor = function() {
6565
*/
6666

6767
StateMachine.prototype._changeState = function _changeState(path, nextState) {
68-
const prevBucket = this.states[this.paths[path]];
68+
const prevState = this.paths[path];
69+
if (prevState === nextState) {
70+
return;
71+
}
72+
const prevBucket = this.states[prevState];
6973
if (prevBucket) delete prevBucket[path];
7074

7175
this.paths[path] = nextState;

lib/types/array/methods/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,15 @@ const methods = {
374374
if (val != null && utils.hasUserDefinedProperty(val, '$each')) {
375375
atomics.$push = val;
376376
} else {
377-
atomics.$push.$each = atomics.$push.$each.concat(val);
377+
if (val.length === 1) {
378+
atomics.$push.$each.push(val[0]);
379+
} else if (val.length < 10000) {
380+
atomics.$push.$each.push(...val);
381+
} else {
382+
for (const v of val) {
383+
atomics.$push.$each.push(v);
384+
}
385+
}
378386
}
379387
} else {
380388
atomics[op] = val;
@@ -403,8 +411,7 @@ const methods = {
403411
addToSet() {
404412
_checkManualPopulation(this, arguments);
405413

406-
let values = [].map.call(arguments, this._mapCast, this);
407-
values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
414+
const values = [].map.call(arguments, this._mapCast, this);
408415
const added = [];
409416
let type = '';
410417
if (values[0] instanceof ArraySubdocument) {
@@ -415,7 +422,7 @@ const methods = {
415422
type = 'ObjectId';
416423
}
417424

418-
const rawValues = utils.isMongooseArray(values) ? values.__array : this;
425+
const rawValues = utils.isMongooseArray(values) ? values.__array : values;
419426
const rawArray = utils.isMongooseArray(this) ? this.__array : this;
420427

421428
rawValues.forEach(function(v) {
@@ -682,10 +689,7 @@ const methods = {
682689

683690
_checkManualPopulation(this, values);
684691

685-
const parent = this[arrayParentSymbol];
686692
values = [].map.call(values, this._mapCast, this);
687-
values = this[arraySchemaSymbol].applySetters(values, parent, undefined,
688-
undefined, { skipDocumentArrayCast: true });
689693
let ret;
690694
const atomics = this[arrayAtomicsSymbol];
691695
this._markModified();
@@ -711,7 +715,7 @@ const methods = {
711715
'with different `$position`');
712716
}
713717
atomic = values;
714-
ret = [].push.apply(arr, values);
718+
ret = _basePush.apply(arr, values);
715719
}
716720

717721
this._registerAtomic('$push', atomic);
@@ -917,7 +921,6 @@ const methods = {
917921
values = arguments;
918922
} else {
919923
values = [].map.call(arguments, this._cast, this);
920-
values = this[arraySchemaSymbol].applySetters(values, this[arrayParentSymbol]);
921924
}
922925

923926
const arr = utils.isMongooseArray(this) ? this.__array : this;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
"docs:merge:6x": "git merge 6.x",
9494
"docs:test": "npm run docs:generate && npm run docs:generate:search",
9595
"docs:view": "node ./scripts/static.js",
96-
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
96+
"docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:generate && npm run docs:generate:search",
9797
"docs:prepare:publish:5x": "npm run docs:checkout:5x && npm run docs:merge:5x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:5x",
9898
"docs:prepare:publish:6x": "npm run docs:checkout:6x && npm run docs:merge:6x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && npm run docs:move:6x:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:6x",
99-
"docs:prepare:publish:7x": "git checkout 7.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/7.x ./tmp && npm run docs:checkout:gh-pages && rimraf ./docs/7.x && ncp ./tmp ./docs/7.x",
99+
"docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && npm run docs:checkout:gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
100100
"docs:check-links": "blc http://127.0.0.1:8089 -ro",
101101
"lint": "eslint .",
102102
"lint-js": "eslint . --ext .js --ext .cjs",

scripts/website.js

Lines changed: 111 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Error.stackTraceLimit = Infinity;
44

55
const acquit = require('acquit');
66
const fs = require('fs');
7+
const fsextra = require('fs-extra');
78
const path = require('path');
89
const pug = require('pug');
910
const pkg = require('../package.json');
@@ -31,7 +32,7 @@ require('acquit-ignore')();
3132
const { marked: markdown } = require('marked');
3233
const highlight = require('highlight.js');
3334
const { promisify } = require("util");
34-
const renderer = {
35+
markdown.use({
3536
heading: function(text, level, raw, slugger) {
3637
const slug = slugger.slug(raw);
3738
return `<h${level} id="${slug}">
@@ -40,7 +41,7 @@ const renderer = {
4041
</a>
4142
</h${level}>\n`;
4243
}
43-
};
44+
});
4445
markdown.setOptions({
4546
highlight: function(code, language) {
4647
if (!language) {
@@ -52,30 +53,100 @@ markdown.setOptions({
5253
return highlight.highlight(code, { language }).value;
5354
}
5455
});
55-
markdown.use({ renderer });
56-
57-
const testPath = path.resolve(cwd, 'test')
58-
59-
const tests = [
60-
...acquit.parse(fs.readFileSync(path.join(testPath, 'geojson.test.js')).toString()),
61-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/transactions.test.js')).toString()),
62-
...acquit.parse(fs.readFileSync(path.join(testPath, 'schema.alias.test.js')).toString()),
63-
...acquit.parse(fs.readFileSync(path.join(testPath, 'model.middleware.test.js')).toString()),
64-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/date.test.js')).toString()),
65-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/lean.test.js')).toString()),
66-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/cast.test.js')).toString()),
67-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/findoneandupdate.test.js')).toString()),
68-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/custom-casting.test.js')).toString()),
69-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/getters-setters.test.js')).toString()),
70-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/virtuals.test.js')).toString()),
71-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/defaults.test.js')).toString()),
72-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/discriminators.test.js')).toString()),
73-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/promises.test.js')).toString()),
74-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schematypes.test.js')).toString()),
75-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/validation.test.js')).toString()),
76-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())
56+
57+
const testPath = path.resolve(cwd, 'test');
58+
59+
/** additional test files to scan, relative to `test/` */
60+
const additionalTestFiles = [
61+
'geojson.test.js',
62+
'schema.alias.test.js'
63+
];
64+
/** ignored files from `test/docs/` */
65+
const ignoredTestFiles = [
66+
// ignored because acquit does not like "for await"
67+
'asyncIterator.test.js'
7768
];
7869

70+
/**
71+
* Load all test file contents with acquit
72+
* @returns {Object[]} acquit ast array
73+
*/
74+
function getTests() {
75+
const testDocs = path.resolve(testPath, 'docs');
76+
const filesToScan = [
77+
...additionalTestFiles.map(v => path.join(testPath, v)),
78+
...fs.readdirSync(testDocs).filter(v => !ignoredTestFiles.includes(v)).map(v => path.join(testDocs, v))
79+
];
80+
81+
const retArray = [];
82+
83+
for (const file of filesToScan) {
84+
try {
85+
retArray.push(acquit.parse(fs.readFileSync(file).toString()));
86+
} catch (err) {
87+
// add a file path to a acquit error, for better debugging
88+
err.filePath = file;
89+
throw err;
90+
}
91+
}
92+
93+
return retArray.flat();
94+
}
95+
96+
function deleteAllHtmlFiles() {
97+
try {
98+
console.log('Delete', path.join(versionObj.versionedPath, 'index.html'));
99+
fs.unlinkSync(path.join(versionObj.versionedPath, 'index.html'));
100+
} catch (err) {
101+
if (err.code !== 'ENOENT') {
102+
throw err;
103+
}
104+
}
105+
const foldersToClean = [
106+
path.join('.', versionObj.versionedPath, 'docs'),
107+
path.join('.', versionObj.versionedPath, 'docs', 'tutorials'),
108+
path.join('.', versionObj.versionedPath, 'docs', 'typescript'),
109+
path.join('.', versionObj.versionedPath, 'docs', 'api'),
110+
path.join('.', versionObj.versionedPath, 'docs', 'source', '_docs'),
111+
'./tmp'
112+
];
113+
for (const folder of foldersToClean) {
114+
let files = [];
115+
116+
try {
117+
files = fs.readdirSync(folder);
118+
} catch (err) {
119+
if (err.code === 'ENOENT') {
120+
continue;
121+
}
122+
}
123+
for (const file of files) {
124+
if (file.endsWith('.html')) {
125+
console.log('Delete', path.join(folder, file));
126+
fs.unlinkSync(path.join(folder, file));
127+
}
128+
}
129+
}
130+
}
131+
132+
function moveDocsToTemp() {
133+
if (!versionObj.versionedPath) {
134+
throw new Error('Cannot move unversioned deploy to /tmp');
135+
}
136+
try {
137+
fs.rmSync('./tmp', { recursive: true });
138+
} catch (err) {
139+
if (err.code !== 'ENOENT') {
140+
throw err;
141+
}
142+
}
143+
const folder = versionObj.versionedPath.replace(/^\//, '');
144+
const directory = fs.readdirSync(folder);
145+
for (const file of directory) {
146+
fsextra.moveSync(`${folder}/${file}`, `./tmp/${file}`);
147+
}
148+
}
149+
79150
/**
80151
* Array of array of semver numbers, sorted with highest number first
81152
* @example
@@ -227,7 +298,7 @@ const versionObj = (() => {
227298
getLatestVersionOf(5),
228299
]
229300
};
230-
const versionedDeploy = process.env.DOCS_DEPLOY === "true" ? !(base.currentVersion.listed === base.latestVersion.listed) : false;
301+
const versionedDeploy = !!process.env.DOCS_DEPLOY ? !(base.currentVersion.listed === base.latestVersion.listed) : false;
231302

232303
const versionedPath = versionedDeploy ? `/docs/${base.currentVersion.path}` : '';
233304

@@ -364,7 +435,7 @@ async function pugify(filename, options, isReload = false) {
364435
let contents = fs.readFileSync(path.resolve(cwd, inputFile)).toString();
365436

366437
if (options.acquit) {
367-
contents = transform(contents, tests);
438+
contents = transform(contents, getTests());
368439

369440
contents = contents.replaceAll(/^```acquit$/gmi, "```javascript");
370441
}
@@ -423,7 +494,7 @@ async function pugify(filename, options, isReload = false) {
423494
});
424495
}
425496

426-
// extra function to start watching for file-changes, without having to call this file directly with "watch"
497+
/** extra function to start watching for file-changes, without having to call this file directly with "watch" */
427498
function startWatch() {
428499
Object.entries(docsFilemap.fileMap).forEach(([file, fileValue]) => {
429500
let watchPath = path.resolve(cwd, file);
@@ -491,7 +562,7 @@ const pathsToCopy = [
491562
'docs/js',
492563
'docs/css',
493564
'docs/images'
494-
]
565+
];
495566

496567
/** Copy all static files when versionedDeploy is used */
497568
async function copyAllRequiredFiles() {
@@ -500,7 +571,6 @@ async function copyAllRequiredFiles() {
500571
return;
501572
}
502573

503-
const fsextra = require('fs-extra');
504574
await Promise.all(pathsToCopy.map(async v => {
505575
const resultPath = path.resolve(cwd, path.join('.', versionObj.versionedPath, v));
506576
await fsextra.copy(v, resultPath);
@@ -517,8 +587,16 @@ exports.cwd = cwd;
517587

518588
// only run the following code if this file is the main module / entry file
519589
if (isMain) {
520-
console.log(`Processing ~${files.length} files`);
521-
Promise.all([pugifyAllFiles(), copyAllRequiredFiles()]).then(() => {
522-
console.log("Done Processing");
523-
})
590+
(async function main() {
591+
console.log(`Processing ~${files.length} files`);
592+
593+
await deleteAllHtmlFiles();
594+
await pugifyAllFiles();
595+
await copyAllRequiredFiles();
596+
if (!!process.env.DOCS_DEPLOY && !!versionObj.versionedPath) {
597+
await moveDocsToTemp();
598+
}
599+
600+
console.log('Done Processing');
601+
})();
524602
}

test/query.test.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ describe('Query', function() {
13701370
});
13711371

13721372
describe('setOptions', function() {
1373-
it('works', async function() {
1373+
it('works', function() {
13741374
const q = new Query();
13751375
q.setOptions({ thing: 'cat' });
13761376
q.setOptions({ populate: ['fans'] });
@@ -1394,16 +1394,6 @@ describe('Query', function() {
13941394
assert.equal(q.options.hint.index2, -1);
13951395
assert.equal(q.options.readPreference.mode, 'secondary');
13961396
assert.equal(q.options.readPreference.tags[0].dc, 'eu');
1397-
1398-
const Product = db.model('Product', productSchema);
1399-
const [, doc2] = await Product.create([
1400-
{ numbers: [3, 4, 5] },
1401-
{ strings: 'hi there'.split(' '), w: 'majority' }
1402-
]);
1403-
1404-
const docs = await Product.find().setOptions({ limit: 1, sort: { _id: -1 }, read: 'n' });
1405-
assert.equal(docs.length, 1);
1406-
assert.equal(docs[0].id, doc2.id);
14071397
});
14081398

14091399
it('populate as array in options (gh-4446)', function() {

test/types.array.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,4 +1915,26 @@ describe('types array', function() {
19151915
}
19161916
});
19171917
});
1918+
1919+
it('calls array setters (gh-11380)', function() {
1920+
let called = 0;
1921+
const Test = db.model('Test', new Schema({
1922+
intArr: [{
1923+
type: Number,
1924+
set: v => {
1925+
++called;
1926+
return Math.floor(v);
1927+
}
1928+
}]
1929+
}));
1930+
1931+
assert.equal(called, 0);
1932+
const doc = new Test({ intArr: [3.14] });
1933+
assert.deepStrictEqual(doc.intArr, [3]);
1934+
assert.equal(called, 1);
1935+
1936+
doc.intArr.push(2.718);
1937+
assert.deepStrictEqual(doc.intArr, [3, 2]);
1938+
assert.equal(called, 2);
1939+
});
19181940
});

test/types/models.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,11 @@ async function gh14072() {
845845

846846
await M.bulkWrite(bulkWriteArray);
847847
}
848+
849+
async function gh14003() {
850+
const schema = new Schema({ name: String });
851+
const TestModel = model('Test', schema);
852+
853+
await TestModel.validate({ name: 'foo' }, ['name']);
854+
await TestModel.validate({ name: 'foo' }, { pathsToSkip: ['name'] });
855+
}

0 commit comments

Comments
 (0)