Skip to content

Commit f560522

Browse files
authored
feat(shell-api): support commitQuorum parameter in createIndexes MONGOSH-1121 (#1220)
1 parent 4d2fc32 commit f560522

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

packages/shell-api/src/collection.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,17 @@ describe('Collection', () => {
785785
{ name: 'index-1' }
786786
);
787787
});
788-
});
788+
it('should allow commitQuorum parameter', async() => {
789+
await collection.createIndexes([{ x: 1 }], { name: 'index-1' }, 3);
789790

791+
expect(serviceProvider.createIndexes).to.have.been.calledWith(
792+
'db1',
793+
'coll1',
794+
[{ key: { x: 1 }, name: 'index-1' }],
795+
{ name: 'index-1', commitQuorum: 3 }
796+
);
797+
});
798+
});
790799
context('when options is not an object', () => {
791800
it('throws an error', async() => {
792801
const error = await collection.createIndexes(
@@ -829,6 +838,16 @@ describe('Collection', () => {
829838
{ name: 'index-1' }
830839
);
831840
});
841+
it('should allow commitQuorum parameter', async() => {
842+
await collection[method]({ x: 1 }, { name: 'index-1' }, 3);
843+
844+
expect(serviceProvider.createIndexes).to.have.been.calledWith(
845+
'db1',
846+
'coll1',
847+
[{ key: { x: 1 }, name: 'index-1' }],
848+
{ name: 'index-1', commitQuorum: 3 }
849+
);
850+
});
832851
});
833852

834853
context('when options is not an object', () => {

packages/shell-api/src/collection.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -981,23 +981,21 @@ export default class Collection extends ShellApiWithMongoClass {
981981
}
982982
);
983983
}
984-
985984
/**
986-
* Create indexes for a collection
987-
*
985+
* Internal function which calls the Service Provider createIndexes function.
986+
* This function is used also by createIndex and ensureIndex
987+
*
988988
* @param {Document} keyPatterns - An array of documents that contains
989989
* the field and value pairs where the field is the index key and the
990990
* value describes the type of index for that field.
991991
* @param {Document} options - createIndexes options (
992992
* name, background, sparse ...)
993993
* @return {Promise}
994994
*/
995-
@returnsPromise
996-
@serverVersions(['3.2.0', ServerVersions.latest])
997-
@apiVersions([1])
998-
async createIndexes(
995+
async _createIndexes(
999996
keyPatterns: Document[],
1000-
options: CreateIndexesOptions = {}
997+
options: CreateIndexesOptions = {},
998+
commitQuorum?: number | string
1001999
): Promise<string[]> {
10021000
assertArgsDefinedType([keyPatterns], [true], 'Collection.createIndexes');
10031001
if (typeof options !== 'object' || Array.isArray(options)) {
@@ -1006,15 +1004,39 @@ export default class Collection extends ShellApiWithMongoClass {
10061004
CommonErrors.InvalidArgument
10071005
);
10081006
}
1009-
10101007
const specs = keyPatterns.map((pattern) => ({
10111008
...options, key: pattern
10121009
}));
1013-
1014-
this._emitCollectionApiCall('createIndexes', { specs });
1015-
1010+
const createIndexesOptions: CreateIndexesOptions = { ...await this._database._baseOptions(), ...options };
1011+
if (undefined !== commitQuorum) {
1012+
createIndexesOptions.commitQuorum = commitQuorum;
1013+
}
10161014
return await this._mongo._serviceProvider.createIndexes(
1017-
this._database._name, this._name, specs, { ...await this._database._baseOptions(), ...options });
1015+
this._database._name, this._name, specs, createIndexesOptions);
1016+
}
1017+
/**
1018+
* Create indexes for a collection
1019+
*
1020+
* @param {Document} keyPatterns - An array of documents that contains
1021+
* the field and value pairs where the field is the index key and the
1022+
* value describes the type of index for that field.
1023+
* @param {Document} options - createIndexes options (
1024+
* name, background, sparse ...)
1025+
* @return {Promise}
1026+
*/
1027+
@returnsPromise
1028+
@serverVersions(['3.2.0', ServerVersions.latest])
1029+
@apiVersions([1])
1030+
async createIndexes(
1031+
keyPatterns: Document[],
1032+
options: CreateIndexesOptions = {},
1033+
commitQuorum?: number | string
1034+
): Promise<string[]> {
1035+
const specs = keyPatterns.map((pattern) => ({
1036+
...options, key: pattern
1037+
}));
1038+
this._emitCollectionApiCall('createIndexes', { specs });
1039+
return this._createIndexes(keyPatterns, options, commitQuorum)
10181040
}
10191041

10201042
/**
@@ -1032,7 +1054,8 @@ export default class Collection extends ShellApiWithMongoClass {
10321054
@apiVersions([1])
10331055
async createIndex(
10341056
keys: Document,
1035-
options: CreateIndexesOptions = {}
1057+
options: CreateIndexesOptions = {},
1058+
commitQuorum?: number | string
10361059
): Promise<string> {
10371060
assertArgsDefinedType([keys], [true], 'Collection.createIndex');
10381061
if (typeof options !== 'object' || Array.isArray(options)) {
@@ -1042,10 +1065,7 @@ export default class Collection extends ShellApiWithMongoClass {
10421065
);
10431066
}
10441067
this._emitCollectionApiCall('createIndex', { keys, options });
1045-
1046-
const spec = { key: keys, ...options }; // keep options for java
1047-
const names = await this._mongo._serviceProvider.createIndexes(
1048-
this._database._name, this._name, [spec], { ...await this._database._baseOptions(), ...options });
1068+
const names = await this._createIndexes([ keys ], options, commitQuorum);
10491069
if (!Array.isArray(names) || names.length !== 1) {
10501070
throw new MongoshInternalError(
10511071
`Expected createIndexes() to return array of length 1, saw ${names}`);
@@ -1068,19 +1088,11 @@ export default class Collection extends ShellApiWithMongoClass {
10681088
@apiVersions([1])
10691089
async ensureIndex(
10701090
keys: Document,
1071-
options: CreateIndexesOptions = {}
1091+
options: CreateIndexesOptions = {},
1092+
commitQuorum?: number | string
10721093
): Promise<Document> {
1073-
assertArgsDefinedType([keys], [true], 'Collection.ensureIndex');
1074-
if (typeof options !== 'object' || Array.isArray(options)) {
1075-
throw new MongoshInvalidInputError(
1076-
'The "options" argument must be an object.',
1077-
CommonErrors.InvalidArgument
1078-
);
1079-
}
10801094
this._emitCollectionApiCall('ensureIndex', { keys, options });
1081-
1082-
const spec = { key: keys, ...options };
1083-
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], { ...await this._database._baseOptions(), ...options });
1095+
return await this._createIndexes([ keys ], options, commitQuorum);
10841096
}
10851097

10861098
/**

0 commit comments

Comments
 (0)