Skip to content
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

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

Merged
merged 4 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,10 @@ export abstract class BulkOperationBase {
}

/** An internal helper method. Do not invoke directly. Will be going away in the future */
execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<void> | void {
execute(
options?: BulkWriteOptions,
callback?: Callback<BulkWriteResult>
): Promise<BulkWriteResult> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};

Expand Down Expand Up @@ -1292,7 +1295,7 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
function handleEarlyError(
err?: AnyError,
callback?: Callback<BulkWriteResult>
): Promise<void> | void {
): Promise<BulkWriteResult> | void {
const Promise = PromiseProvider.get();
if (typeof callback === 'function') {
callback(err);
Expand Down
28 changes: 14 additions & 14 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,19 +727,19 @@ export class Collection implements OperationParent {
* await collection.createIndex(['j', ['k', -1], { l: '2d' }])
* ```
*/
createIndex(indexSpec: IndexSpecification): Promise<Document>;
createIndex(indexSpec: IndexSpecification, callback: Callback<Document>): void;
createIndex(indexSpec: IndexSpecification, options: CreateIndexesOptions): Promise<Document>;
createIndex(indexSpec: IndexSpecification): Promise<string>;
createIndex(indexSpec: IndexSpecification, callback: Callback<string>): void;
createIndex(indexSpec: IndexSpecification, options: CreateIndexesOptions): Promise<string>;
createIndex(
indexSpec: IndexSpecification,
options: CreateIndexesOptions,
callback: Callback<Document>
callback: Callback<string>
): void;
createIndex(
indexSpec: IndexSpecification,
options?: CreateIndexesOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: CreateIndexesOptions | Callback<string>,
callback?: Callback<string>
): Promise<string> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down Expand Up @@ -781,19 +781,19 @@ export class Collection implements OperationParent {
* ]);
* ```
*/
createIndexes(indexSpecs: IndexDescription[]): Promise<Document>;
createIndexes(indexSpecs: IndexDescription[], callback: Callback<Document>): void;
createIndexes(indexSpecs: IndexDescription[], options: CreateIndexesOptions): Promise<Document>;
createIndexes(indexSpecs: IndexDescription[]): Promise<string[]>;
createIndexes(indexSpecs: IndexDescription[], callback: Callback<string[]>): void;
createIndexes(indexSpecs: IndexDescription[], options: CreateIndexesOptions): Promise<string[]>;
createIndexes(
indexSpecs: IndexDescription[],
options: CreateIndexesOptions,
callback: Callback<Document>
callback: Callback<string[]>
): void;
createIndexes(
indexSpecs: IndexDescription[],
options?: CreateIndexesOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: CreateIndexesOptions | Callback<string[]>,
callback?: Callback<string[]>
): Promise<string[]> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ? Object.assign({}, options) : {};
if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
Expand Down
14 changes: 7 additions & 7 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,25 +581,25 @@ export class Db implements OperationParent {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
createIndex(name: string, indexSpec: IndexSpecification): Promise<Document>;
createIndex(name: string, indexSpec: IndexSpecification, callback?: Callback<Document>): void;
createIndex(name: string, indexSpec: IndexSpecification): Promise<string>;
createIndex(name: string, indexSpec: IndexSpecification, callback?: Callback<string>): void;
createIndex(
name: string,
indexSpec: IndexSpecification,
options: CreateIndexesOptions
): Promise<Document>;
): Promise<string>;
createIndex(
name: string,
indexSpec: IndexSpecification,
options: CreateIndexesOptions,
callback: Callback<Document>
callback: Callback<string>
): void;
createIndex(
name: string,
indexSpec: IndexSpecification,
options?: CreateIndexesOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: CreateIndexesOptions | Callback<string>,
callback?: Callback<string>
): Promise<string> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down
23 changes: 14 additions & 9 deletions src/operations/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ export class IndexesOperation extends AbstractOperation<Document> {
}

/** @internal */
export class CreateIndexesOperation extends CommandOperation<Document> {
export class CreateIndexesOperation<
T extends string | string[] = string[]
> extends CommandOperation<T> {
options: CreateIndexesOptions;
collectionName: string;
onlyReturnNameOfCreatedIndex?: boolean;
indexes: IndexDescription[];

constructor(
Expand All @@ -168,12 +169,9 @@ export class CreateIndexesOperation extends CommandOperation<Document> {
this.collectionName = collectionName;

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

execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
execute(server: Server, session: ClientSession, callback: Callback<T>): void {
const options = this.options;
const indexes = this.indexes;

Expand Down Expand Up @@ -219,19 +217,20 @@ 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 indexNames = indexes.map(index => index.name || '');
callback(undefined, indexNames as T);
});
}
}

/** @internal */
export class CreateIndexOperation extends CreateIndexesOperation {
export class CreateIndexOperation extends CreateIndexesOperation<string> {
constructor(
parent: OperationParent,
collectionName: string,
Expand All @@ -246,6 +245,12 @@ export class CreateIndexOperation extends CreateIndexesOperation {

super(parent, collectionName, [makeIndexSpec(indexSpec, options)], options);
}
execute(server: Server, session: ClientSession, callback: Callback<string>): void {
super.execute(server, session, (err, indexNames) => {
if (err || !indexNames) return callback(err);
return callback(undefined, indexNames[0]);
});
}
}

/** @internal */
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