Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 30, 2022
2 parents 48556a3 + a73234f commit de77099
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 45 deletions.
81 changes: 45 additions & 36 deletions benchmarks/benchjs/casting.js
Expand Up @@ -18,7 +18,6 @@ const utils = require('../../lib/utils.js');
* These are all the benchmark tests for casting stuff
*/


const Comments = new Schema();

Comments.add({
Expand Down Expand Up @@ -48,6 +47,7 @@ const BlogPost = new Schema({
default: 'kandinsky'
}
});

const commentData = {
title: 'test comment',
date: new Date(),
Expand Down Expand Up @@ -78,6 +78,7 @@ const blogData10 = utils.clone(blogData);
const blogData100 = utils.clone(blogData);
const blogData1000 = utils.clone(blogData);
const blogData10000 = utils.clone(blogData);

for (let i = 0; i < 10; i++) {
blogData10.comments.push(commentData);
}
Expand All @@ -90,47 +91,54 @@ for (let i = 0; i < 1000; i++) {
for (let i = 0; i < 10000; i++) {
blogData10000.comments.push(commentData);
}

mongoose.model('BlogPost', BlogPost);

suite.add('Casting - Embedded Docs - 0 Docs', {
fn: function() {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData);
}
}).add('Casting - Embedded Docs - 10 Docs', {
fn: function() {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData10);
}
}).add('Casting - Embedded Docs - 100 Docs', {
fn: function() {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData100);
}
}).add('Casting - Embedded Docs - 1000 Docs', {
fn: function() {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData1000);
}
}).add('Casting - Embedded Docs - 10000 Docs', {
fn: function() {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData10000);
}
})
.on('cycle', function(evt) {
suite
.add('Casting - Embedded Docs - 0 Docs', {
fn: function () {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData);
}
})
.add('Casting - Embedded Docs - 10 Docs', {
fn: function () {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData10);
}
})
.add('Casting - Embedded Docs - 100 Docs', {
fn: function () {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData100);
}
})
.add('Casting - Embedded Docs - 1000 Docs', {
fn: function () {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData1000);
}
})
.add('Casting - Embedded Docs - 10000 Docs', {
fn: function () {
const BlogPost = mongoose.model('BlogPost');
const bp = new BlogPost();
bp.init(blogData10000);
}
})
.on('cycle', function (evt) {
if (process.env.MONGOOSE_DEV || process.env.PULL_REQUEST) {
console.log(String(evt.target));
}
}).on('complete', function() {
})
.on('complete', function () {
if (!process.env.MONGOOSE_DEV && !process.env.PULL_REQUEST) {
const outObj = {};
this.forEach(function(item) {
this.forEach(function (item) {
const out = {};
out.stats = item.stats;
delete out.stats.sample;
Expand All @@ -139,4 +147,5 @@ suite.add('Casting - Embedded Docs - 0 Docs', {
});
console.dir(outObj, { depth: null, colors: true });
}
}).run({ async: true });
})
.run({ async: true });
4 changes: 2 additions & 2 deletions docs/typescript/subdocuments.md
@@ -1,7 +1,7 @@
# Handling Subdocuments in TypeScript

Subdocuments are tricky in TypeScript.
By default, Mongoose treats object properties in document interfaces as _nested propertes_ rather than subdocuments.
By default, Mongoose treats object properties in document interfaces as _nested properties_ rather than subdocuments.

```ts
// Setup
Expand Down Expand Up @@ -81,4 +81,4 @@ const UserModel = model<User, UserModelType>('User', new Schema<User, UserModelT

const doc = new UserModel({});
doc.names[0].ownerDocument(); // Works!
```
```
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -1223,7 +1223,7 @@ Mongoose.prototype._promiseOrCallback = function(callback, fn, ee) {
/**
* Use this function in `pre()` middleware to skip calling the wrapped function.
*
* ####Example:
* #### Example:
*
* schema.pre('save', function() {
* // Will skip executing `save()`, but will execute post hooks as if
Expand All @@ -1241,7 +1241,7 @@ Mongoose.prototype.skipMiddlewareFunction = Kareem.skipWrappedFunction;
/**
* Use this function in `post()` middleware to replace the result
*
* ####Example:
* #### Example:
*
* schema.post('find', function(res) {
* // Normally you have to modify `res` in place. But with
Expand Down
6 changes: 3 additions & 3 deletions lib/schema.js
Expand Up @@ -477,7 +477,7 @@ Schema.prototype.defaultOptions = function(options) {
* Inherit a Schema by applying a discriminator on an existing Schema.
*
*
* ####Example:
* #### Example:
*
* const options = { discriminatorKey: 'kind' };
*
Expand Down Expand Up @@ -648,7 +648,7 @@ Schema.prototype.add = function add(obj, prefix) {
* removeIndex only removes indexes from your schema object. Does **not** affect the indexes
* in MongoDB.
*
* ####Example:
* #### Example:
*
* const ToySchema = new Schema({ name: String, color: String, price: Number });
*
Expand Down Expand Up @@ -701,7 +701,7 @@ Schema.prototype.removeIndex = function removeIndex(index) {
* clearIndexes only removes indexes from your schema object. Does **not** affect the indexes
* in MongoDB.
*
* ####Example:
* #### Example:
*
* const ToySchema = new Schema({ name: String, color: String, price: Number });
* ToySchema.index({ name: 1 });
Expand Down
18 changes: 17 additions & 1 deletion test/types/schema.test.ts
Expand Up @@ -9,7 +9,8 @@ import {
InferSchemaType,
SchemaType,
Query,
HydratedDocument
HydratedDocument,
SchemaOptions
} from 'mongoose';
import { expectType, expectError, expectAssignable } from 'tsd';

Expand Down Expand Up @@ -559,6 +560,7 @@ function gh11828() {
}
});
}

function gh11997() {
interface IUser {
name: string;
Expand All @@ -569,3 +571,17 @@ function gh11997() {
});
userSchema.index({ name: 1 }, { weights: { name: 1 } });
}

function gh12003() {
const baseSchemaOptions: SchemaOptions = {
versionKey: false
};

const BaseSchema = new Schema({
name: String
}, baseSchemaOptions);

type BaseSchemaType = InferSchemaType<typeof BaseSchema>;

expectType<{ name?: string }>({} as BaseSchemaType);
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -166,7 +166,7 @@ declare module 'mongoose' {
/**
* Create a new schema
*/
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, DocType, TInstanceMethods, TQueryHelpers, TStaticMethods>);
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods>);

/** Adds key path / schema type pairs to this schema. */
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
Expand Down
7 changes: 7 additions & 0 deletions types/utility.d.ts
Expand Up @@ -12,4 +12,11 @@ declare module 'mongoose' {

type MergeType<A, B> = Omit<A, keyof B> & B;

/**
* @summary Converts Unions to one record "object".
* @description It makes intellisense dialog box easier to read as a single object instead of showing that in multiple object unions.
* @param {T} T The type to be converted.
*/
type FlatRecord<T> = { [K in keyof T]: T[K] };

}

0 comments on commit de77099

Please sign in to comment.