Skip to content

Commit cc38a26

Browse files
committed
types: allow [true, string] for unique
1 parent ff18818 commit cc38a26

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

test/types/schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ movieSchema.index({ title: 'text' }, {
101101
});
102102
movieSchema.index({ rating: -1 });
103103
movieSchema.index({ title: 1 }, { unique: true });
104+
movieSchema.index({ title: 1 }, { unique: [true, 'Title must be unique'] as const });
104105
movieSchema.index({ tile: 'ascending' });
105106
movieSchema.index({ tile: 'asc' });
106107
movieSchema.index({ tile: 'descending' });

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ declare module 'mongoose' {
309309
eachPath(fn: (path: string, type: SchemaType) => void): this;
310310

311311
/** Defines an index (most likely compound) for this schema. */
312-
index(fields: IndexDefinition, options?: IndexOptions): this;
312+
index(fields: IndexDefinition, options?: Omit<IndexOptions, 'unique'> & { unique?: boolean | undefined | [true, string] }): this;
313313

314314
/**
315315
* Define a search index for this schema.

types/indexes.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ declare module 'mongoose' {
6363
type ConnectionSyncIndexesResult = Record<string, OneCollectionSyncIndexesResult>;
6464
type OneCollectionSyncIndexesResult = Array<string> & mongodb.MongoServerError;
6565

66-
interface IndexOptions extends mongodb.CreateIndexesOptions {
66+
type IndexOptions = Omit<mongodb.CreateIndexesOptions, 'expires' | 'weights' | 'unique'> & {
6767
/**
6868
* `expires` utilizes the `ms` module from [guille](https://github.com/guille/) allowing us to use a friendlier syntax:
6969
*
@@ -86,7 +86,9 @@ declare module 'mongoose' {
8686
*/
8787
expires?: number | string;
8888
weights?: Record<string, number>;
89-
}
89+
90+
unique?: boolean | [true, string]
91+
};
9092

9193
type SearchIndexDescription = mongodb.SearchIndexDescription;
9294
}

types/schematypes.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ declare module 'mongoose' {
111111
* will build a unique index on this path when the
112112
* model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
113113
*/
114-
unique?: boolean | number;
114+
unique?: boolean | number | [true, string];
115115

116116
/**
117117
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will

0 commit comments

Comments
 (0)