Skip to content

Commit a6d4213

Browse files
authored
Merge branch 'master' into IslandRhythms/gh-13762
2 parents cf576fc + 71bbf64 commit a6d4213

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

lib/model.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,17 @@ Model.createCollection = async function createCollection(options) {
14511451
* // Will drop the 'age' index and create an index on `name`
14521452
* await Customer.syncIndexes();
14531453
*
1454+
* You should be careful about running `syncIndexes()` on production applications under heavy load,
1455+
* because index builds are expensive operations, and unexpected index drops can lead to degraded
1456+
* performance. Before running `syncIndexes()`, you can use the [`diffIndexes()` function](#Model.diffIndexes())
1457+
* to check what indexes `syncIndexes()` will drop and create.
1458+
*
1459+
* #### Example:
1460+
*
1461+
* const { toDrop, toCreate } = await Model.diffIndexes();
1462+
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
1463+
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
1464+
*
14541465
* @param {Object} [options] options to pass to `ensureIndexes()`
14551466
* @param {Boolean} [options.background=null] if specified, overrides each index's `background` property
14561467
* @return {Promise}
@@ -1483,9 +1494,14 @@ Model.syncIndexes = async function syncIndexes(options) {
14831494
/**
14841495
* Does a dry-run of `Model.syncIndexes()`, returning the indexes that `syncIndexes()` would drop and create if you were to run `syncIndexes()`.
14851496
*
1497+
* #### Example:
1498+
*
1499+
* const { toDrop, toCreate } = await Model.diffIndexes();
1500+
* toDrop; // Array of strings containing names of indexes that `syncIndexes()` will drop
1501+
* toCreate; // Array of strings containing names of indexes that `syncIndexes()` will create
1502+
*
14861503
* @param {Object} [options]
1487-
* @returns {Promise} which contains an object, {toDrop, toCreate}, which
1488-
* are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
1504+
* @return {Promise<Object>} contains the indexes that would be dropped in MongoDB and indexes that would be created in MongoDB as `{ toDrop: string[], toCreate: string[] }`.
14891505
*/
14901506

14911507
Model.diffIndexes = async function diffIndexes() {

test/types/schema.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1218,3 +1218,15 @@ function gh13800() {
12181218
expectError<string>(this.someOtherField);
12191219
});
12201220
}
1221+
1222+
async function gh13797() {
1223+
interface IUser {
1224+
name: string;
1225+
}
1226+
new Schema<IUser>({ name: { type: String, required: function() {
1227+
expectType<IUser>(this); return true;
1228+
} } });
1229+
new Schema<IUser>({ name: { type: String, default: function() {
1230+
expectType<IUser>(this); return '';
1231+
} } });
1232+
}

types/index.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ declare module 'mongoose' {
236236
/**
237237
* Create a new schema
238238
*/
239-
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
239+
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
240240

241241
/** Adds key path / schema type pairs to this schema. */
242242
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
@@ -300,7 +300,7 @@ declare module 'mongoose' {
300300
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
301301

302302
/** The original object passed to the schema constructor */
303-
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;
303+
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType>;
304304

305305
/** Gets/sets schema paths. */
306306
path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType;
@@ -484,26 +484,26 @@ declare module 'mongoose' {
484484
? DateSchemaDefinition
485485
: (Function | string);
486486

487-
export type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
488-
SchemaTypeOptions<T extends undefined ? any : T> |
487+
export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any> = SchemaDefinitionWithBuiltInClass<T> |
488+
SchemaTypeOptions<T extends undefined ? any : T, EnforcedDocType> |
489489
typeof SchemaType |
490490
Schema<any, any, any> |
491491
Schema<any, any, any>[] |
492-
SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
492+
SchemaTypeOptions<T extends undefined ? any : Unpacked<T>, EnforcedDocType>[] |
493493
Function[] |
494-
SchemaDefinition<T> |
495-
SchemaDefinition<Unpacked<T>>[] |
494+
SchemaDefinition<T, EnforcedDocType> |
495+
SchemaDefinition<Unpacked<T>, EnforcedDocType>[] |
496496
typeof Schema.Types.Mixed |
497-
MixedSchemaTypeOptions;
497+
MixedSchemaTypeOptions<EnforcedDocType>;
498498

499-
export type SchemaDefinition<T = undefined> = T extends undefined
499+
export type SchemaDefinition<T = undefined, EnforcedDocType = any> = T extends undefined
500500
? { [path: string]: SchemaDefinitionProperty; }
501-
: { [path in keyof T]?: SchemaDefinitionProperty<T[path]>; };
501+
: { [path in keyof T]?: SchemaDefinitionProperty<T[path], EnforcedDocType>; };
502502

503503
export type AnyArray<T> = T[] | ReadonlyArray<T>;
504504
export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
505505

506-
export interface MixedSchemaTypeOptions extends SchemaTypeOptions<Schema.Types.Mixed> {
506+
export interface MixedSchemaTypeOptions<EnforcedDocType> extends SchemaTypeOptions<Schema.Types.Mixed, EnforcedDocType> {
507507
type: typeof Schema.Types.Mixed;
508508
}
509509

types/schematypes.d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare module 'mongoose' {
3939

4040
type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
4141

42-
class SchemaTypeOptions<T> {
42+
class SchemaTypeOptions<T, EnforcedDocType = any> {
4343
type?:
4444
T extends string ? StringSchemaDefinition :
4545
T extends number ? NumberSchemaDefinition :
@@ -48,12 +48,12 @@ declare module 'mongoose' {
4848
T extends Map<any, any> ? SchemaDefinition<typeof Map> :
4949
T extends Buffer ? SchemaDefinition<typeof Buffer> :
5050
T extends Types.ObjectId ? ObjectIdSchemaDefinition :
51-
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
52-
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
53-
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
54-
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
55-
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
56-
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
51+
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType>> :
52+
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>>) :
53+
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string, EnforcedDocType>> :
54+
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number, EnforcedDocType>> :
55+
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean, EnforcedDocType>> :
56+
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>> :
5757
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
5858

5959
/** Defines a virtual with the given name that gets/sets this path. */
@@ -74,13 +74,13 @@ declare module 'mongoose' {
7474
* path cannot be set to a nullish value. If a function, Mongoose calls the
7575
* function and only checks for nullish values if the function returns a truthy value.
7676
*/
77-
required?: boolean | (() => boolean) | [boolean, string] | [() => boolean, string];
77+
required?: boolean | ((this: EnforcedDocType) => boolean) | [boolean, string] | [(this: EnforcedDocType) => boolean, string];
7878

7979
/**
8080
* The default value for this path. If a function, Mongoose executes the function
8181
* and uses the return value as the default.
8282
*/
83-
default?: DefaultType<T> | ((this: any, doc: any) => DefaultType<T>) | null;
83+
default?: DefaultType<T> | ((this: EnforcedDocType, doc: any) => DefaultType<T>) | null;
8484

8585
/**
8686
* The model that `populate()` should use if populating this path.

0 commit comments

Comments
 (0)