Skip to content

Commit

Permalink
fix(schema): support 'ascending', 'asc', 'descending', 'desc' for ind…
Browse files Browse the repository at this point in the history
…ex direction

Fix #13725
  • Loading branch information
vkarpov15 committed Aug 22, 2023
1 parent b2e7315 commit 2591f9f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
10 changes: 9 additions & 1 deletion lib/helpers/schema/getIndexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ module.exports = function getIndexes(schema) {
field[prefix + key] = 'text';
delete options.text;
} else {
const isDescendingIndex = Number(index) === -1;
let isDescendingIndex = false;
if (index === 'descending' || index === 'desc') {
isDescendingIndex = true;
} else if (index === 'ascending' || index === 'asc') {
isDescendingIndex = false;
} else {
isDescendingIndex = Number(index) === -1;
}

field[prefix + key] = isDescendingIndex ? -1 : 1;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,14 @@ Schema.prototype.index = function(fields, options) {
utils.expires(options);
}

for (const field of Object.keys(fields)) {
if (fields[field] === 'ascending' || fields[field] === 'asc') {
fields[field] = 1;
} else if (fields[field] === 'descending' || fields[field] === 'desc') {
fields[field] = -1;
}
}

this._indexes.push([fields, options]);
return this;
};
Expand Down
17 changes: 15 additions & 2 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,21 @@ describe('schema', function() {
assert.deepEqual(indexes[1][0], { prop2: 1 });
});

it('using "ascending" and "descending" for order (gh-13725)', function() {
const testSchema = new Schema({
prop1: { type: String, index: 'ascending' },
prop2: { type: Number, index: 'descending' },
prop3: { type: String }
});
testSchema.index({ prop3: 'desc' }, { unique: true });

const indexes = testSchema.indexes();
assert.equal(indexes.length, 3);
assert.deepEqual(indexes[0][0], { prop1: 1 });
assert.deepEqual(indexes[1][0], { prop2: -1 });
assert.deepEqual(indexes[2][0], { prop3: -1 });
});

it('with single nested doc (gh-6113)', function() {
const pointSchema = new Schema({
type: {
Expand All @@ -873,8 +888,6 @@ describe('schema', function() {
assert.deepEqual(schema.indexes(), [
[{ point: '2dsphere' }, { background: true }]
]);


});

it('with embedded discriminator (gh-6485)', function() {
Expand Down
5 changes: 0 additions & 5 deletions test/types/mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import * as mongoose from 'mongoose';
import { expectType } from 'tsd';
import * as bson from 'bson';

<<<<<<< HEAD
import GridFSBucket = mongoose.mongo.GridFSBucket;

=======
>>>>>>> 6.x
function gh12537() {
const schema = new mongoose.Schema({ test: String });
const model = mongoose.model('Test', schema);
Expand Down
4 changes: 0 additions & 4 deletions types/augmentations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ declare module 'bson' {
/** Mongoose automatically adds a conveniency "_id" getter on the base ObjectId class */
_id: this;
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 6.x
4 changes: 2 additions & 2 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare module 'mongoose' {
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)]: ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
};

/**
/**
* @summary Obtains document schema type from Schema instance.
* @param {Schema} TSchema `typeof` a schema instance.
* @example
Expand All @@ -39,7 +39,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down

0 comments on commit 2591f9f

Please sign in to comment.