Skip to content
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
17 changes: 16 additions & 1 deletion packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,22 @@ const translations: Catalog = {
shardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.shardCollection',
description: 'Enables sharding for a collection. Uses the shardCollection command',
example: 'sh.shardCollection(namesapce, key, unique?, options?)',
example: 'sh.shardCollection(namespace, key, unique?, options?)',
},
reshardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.reshardCollection',
description: 'Enables sharding for a collection. Uses the reshardCollection command',
example: 'sh.reshardCollection(namespace, key, unique?, options?)',
},
commitReshardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.commitReshardCollection',
description: 'Commits the current reshardCollection on a given collection',
example: 'sh.commitReshardCollection(namespace)',
},
abortReshardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortReshardCollection',
description: 'Abort the current reshardCollection on a given collection',
example: 'sh.abortReshardCollection(namespace)',
},
status: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.status',
Expand Down
92 changes: 92 additions & 0 deletions packages/shell-api/src/shard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,98 @@ describe('Shard', () => {
expect(catchedError).to.equal(expectedError);
});
});
describe('reshardCollection', () => {
it('calls serviceProvider.runCommandWithCheck without optional args', async() => {
await shard.reshardCollection('db.coll', { key: 1 } );
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
reshardCollection: 'db.coll',
key: { key: 1 }
}
);
});

it('calls serviceProvider.runCommandWithCheck with optional args', async() => {
await shard.reshardCollection('db.coll', { key: 1 }, true, { option1: 1 });
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
reshardCollection: 'db.coll',
key: { key: 1 },
unique: true,
option1: 1
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.reshardCollection('db.coll', { key: 1 });
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const catchedError = await shard.reshardCollection('db.coll', { key: 1 })
.catch(e => e);
expect(catchedError).to.equal(expectedError);
});
});
describe('commitReshardCollection', () => {
it('calls serviceProvider.runCommandWithCheck', async() => {
await shard.commitReshardCollection('db.coll');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
commitReshardCollection: 'db.coll'
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.commitReshardCollection('db.coll');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const catchedError = await shard.commitReshardCollection('db.coll')
.catch(e => e);
expect(catchedError).to.equal(expectedError);
});
});
describe('abortReshardCollection', () => {
it('calls serviceProvider.runCommandWithCheck', async() => {
await shard.abortReshardCollection('db.coll');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
abortReshardCollection: 'db.coll'
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.abortReshardCollection('db.coll');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const catchedError = await shard.abortReshardCollection('db.coll')
.catch(e => e);
expect(catchedError).to.equal(expectedError);
});
});
describe('addShard', () => {
it('calls serviceProvider.runCommandWithCheck with arg', async() => {
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });
Expand Down
47 changes: 43 additions & 4 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,51 @@ export default class Shard extends ShellApiWithMongoClass {
}

@returnsPromise
async shardCollection(namespace: string, key: Document, unique?: boolean, options?: Document): Promise<Document> {
assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean'], [undefined, 'object']], 'Shard.shardCollection');
this._emitShardApiCall('shardCollection', { namespace, key, unique, options });
@serverVersions(['5.0.0', ServerVersions.latest])
async commitReshardCollection(namespace: string): Promise<Document> {
assertArgsDefinedType([namespace], ['string'], 'Shard.commitReshardCollection');
this._emitShardApiCall('commitReshardCollection', { namespace });
return await this._database._runAdminCommand({
commitReshardCollection: namespace
});
}

@returnsPromise
@serverVersions(['5.0.0', ServerVersions.latest])
async abortReshardCollection(namespace: string): Promise<Document> {
assertArgsDefinedType([namespace], ['string'], 'Shard.abortReshardCollection');
this._emitShardApiCall('abortReshardCollection', { namespace });
return await this._database._runAdminCommand({
abortReshardCollection: namespace
});
}

@returnsPromise
async shardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise<Document> {
return await this._runShardCollection('shardCollection', namespace, key, unique, options);
}

@returnsPromise
@serverVersions(['5.0.0', ServerVersions.latest])
async reshardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise<Document> {
return await this._runShardCollection('reshardCollection', namespace, key, unique, options);
}

async _runShardCollection(
command: 'shardCollection' | 'reshardCollection',
namespace: string,
key: Document,
unique?: boolean | Document,
options?: Document): Promise<Document> {
assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean', 'object'], [undefined, 'object']], `Shard.${command}`);
this._emitShardApiCall(command, { namespace, key, unique, options });
if (typeof unique === 'object' && unique !== null) {
options = unique;
unique = undefined;
}

const cmd = {
shardCollection: namespace,
[command]: namespace,
key: key
} as any;
if (unique !== undefined) {
Expand Down