Skip to content

refactor!: CreateIndexOp returns string, CreateIndexesOp returns array #2666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,23 @@ export class IndexesOperation extends AbstractOperation<Document> {
export class CreateIndexesOperation extends CommandOperation<Document> {
options: CreateIndexesOptions;
collectionName: string;
onlyReturnNameOfCreatedIndex?: boolean;
singular?: boolean;
indexes: IndexDescription[];

constructor(
parent: OperationParent,
collectionName: string,
indexes: IndexDescription[],
options?: CreateIndexesOptions
options?: CreateIndexesOptions,
singular?: boolean
) {
super(parent, options);

this.options = options ?? {};
this.singular = singular ?? false;
this.collectionName = collectionName;

this.indexes = indexes;
if (indexes.length === 1) {
this.onlyReturnNameOfCreatedIndex = true;
}
}

execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
Expand Down Expand Up @@ -219,13 +218,14 @@ export class CreateIndexesOperation extends CommandOperation<Document> {
// collation is set on each index, it should not be defined at the root
this.options.collation = undefined;

super.executeCommand(server, session, cmd, (err, result) => {
super.executeCommand(server, session, cmd, err => {
if (err) {
callback(err);
return;
}

callback(undefined, this.onlyReturnNameOfCreatedIndex ? indexes[0].name : result);
const names = indexes.map(index => index.name);
callback(undefined, (this.singular ? names[0] : names) as Document);
});
}
}
Expand All @@ -244,7 +244,7 @@ export class CreateIndexOperation extends CreateIndexesOperation {
// coll.createIndex([['a', 1]]);
// createIndexes is always called with an array of index spec objects

super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options, true);
}
}

Expand Down
40 changes: 35 additions & 5 deletions test/functional/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,8 @@ describe('Indexes', function () {
}
});

it('should correctly execute createIndexes', {
metadata: {
requires: { mongodb: '>=2.6.0', topology: ['single', 'ssl', 'heap', 'wiredtiger'] }
},
it('should correctly execute createIndexes with multiple indexes', {
metadata: { requires: { mongodb: '>=2.6.0', topology: ['single'] } },

test: function (done) {
var configuration = this.configuration;
Expand All @@ -845,7 +843,7 @@ describe('Indexes', function () {
[{ key: { a: 1 } }, { key: { b: 1 }, name: 'hello1' }],
function (err, r) {
expect(err).to.not.exist;
test.equal(3, r.numIndexesAfter);
expect(r).to.deep.equal(['a_1', 'hello1']);

db.collection('createIndexes')
.listIndexes()
Expand All @@ -868,6 +866,38 @@ describe('Indexes', function () {
}
});

it('should correctly execute createIndexes with one index', {
metadata: { requires: { mongodb: '>=2.6.0', topology: ['single'] } },

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
db.collection('createIndexes').createIndexes([{ key: { a: 1 } }], function (err, r) {
expect(err).to.not.exist;
expect(r).to.deep.equal(['a_1']);

db.collection('createIndexes')
.listIndexes()
.toArray(function (err, docs) {
expect(err).to.not.exist;
var keys = {};

for (var i = 0; i < docs.length; i++) {
keys[docs[i].name] = true;
}

test.ok(keys['a_1']);
test.ok(keys['hello1']);

client.close(done);
});
});
});
}
});

it('shouldCorrectlyCreateTextIndex', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
Expand Down