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
4 changes: 3 additions & 1 deletion packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,11 @@ export default class Collection extends ShellApiWithMongoClass {
*
* @returns {Cursor} The promise of the cursor.
*/
// eslint-disable-next-line @typescript-eslint/require-await
@returnType('Cursor')
@apiVersions([1])
find(query?: Document, projection?: Document, options: FindOptions = {}): Cursor {
@returnsPromise
async find(query?: Document, projection?: Document, options: FindOptions = {}): Promise<Cursor> {
if (projection) {
options.projection = projection;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,8 @@ export default class Database extends ShellApiWithMongoClass {
result.usedMB = olStats.size / (1024 * 1024);
result.usedMB = Math.ceil(result.usedMB * 100) / 100;

const first = await ol.find().sort({ $natural: 1 }).limit(1).tryNext();
const last = await ol.find().sort({ $natural: -1 }).limit(1).tryNext();
const first = await (await ol.find()).sort({ $natural: 1 }).limit(1).tryNext();
const last = await (await ol.find()).sort({ $natural: -1 }).limit(1).tryNext();
if (first === null || last === null) {
throw new MongoshRuntimeError(
'objects not found in local.oplog.$main -- is this a new and empty db instance?',
Expand Down
8 changes: 4 additions & 4 deletions packages/shell-api/src/explainable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Explainable', () => {
it('attributes', () => {
expect(signatures.Explainable.attributes.find).to.deep.equal({
type: 'function',
returnsPromise: false,
returnsPromise: true,
deprecated: false,
returnType: 'ExplainableCursor',
platforms: ALL_PLATFORMS,
Expand Down Expand Up @@ -103,15 +103,15 @@ describe('Explainable', () => {
describe('find', () => {
let cursorStub;
let explainResult;
beforeEach(() => {
beforeEach(async() => {
explainResult = { ok: 1 };

const cursorSpy = {
explain: sinon.spy(() => explainResult)
} as unknown;
collection.find = sinon.spy(() => (cursorSpy as Cursor));
collection.find = sinon.spy(() => Promise.resolve(cursorSpy as Cursor));

cursorStub = explainable.find(
cursorStub = await explainable.find(
{ query: 1 },
{ projection: 1 }
);
Expand Down
5 changes: 3 additions & 2 deletions packages/shell-api/src/explainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ export default class Explainable extends ShellApiWithMongoClass {

@returnType('ExplainableCursor')
@apiVersions([1])
find(query?: Document, projection?: Document): ExplainableCursor {
@returnsPromise
async find(query?: Document, projection?: Document): Promise<ExplainableCursor> {
this._emitExplainableApiCall('find', { query, projection });

const cursor = this._collection.find(query, projection);
const cursor = await this._collection.find(query, projection);
return new ExplainableCursor(this._mongo, cursor, this._verbosity);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/shell-api/src/field-level-encryption.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,29 +287,29 @@ describe('Field Level Encryption', () => {
});
});
describe('getKey', () => {
it('calls find on key coll', () => {
it('calls find on key coll', async() => {
const c = { cursor: 1 } as any;
sp.find.returns(c);
const result = keyVault.getKey(KEY_ID);
const result = await keyVault.getKey(KEY_ID);
expect(sp.find).to.have.been.calledOnceWithExactly(DB, COLL, { _id: KEY_ID }, {});
expect(result._cursor).to.deep.equal(c);
});
});
describe('getKeyByAltName', () => {
it('calls find on key coll', () => {
it('calls find on key coll', async() => {
const c = { cursor: 1 } as any;
const keyaltname = 'abc';
sp.find.returns(c);
const result = keyVault.getKeyByAltName(keyaltname);
const result = await keyVault.getKeyByAltName(keyaltname);
expect(sp.find).to.have.been.calledOnceWithExactly(DB, COLL, { keyAltNames: keyaltname }, {});
expect(result._cursor).to.deep.equal(c);
});
});
describe('getKeys', () => {
it('calls find on key coll', () => {
it('calls find on key coll', async() => {
const c = { cursor: 1 } as any;
sp.find.returns(c);
const result = keyVault.getKeys();
const result = await keyVault.getKeys();
expect(sp.find).to.have.been.calledOnceWithExactly(DB, COLL, {}, {});
expect(result._cursor).to.deep.equal(c);
});
Expand Down
9 changes: 6 additions & 3 deletions packages/shell-api/src/field-level-encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,24 @@ export class KeyVault extends ShellApiWithMongoClass {

@returnType('Cursor')
@apiVersions([1])
getKey(keyId: BinaryType): Cursor {
@returnsPromise
async getKey(keyId: BinaryType): Promise<Cursor> {
assertArgsDefinedType([keyId], [true], 'KeyVault.getKey');
return this._keyColl.find({ '_id': keyId });
}

@returnType('Cursor')
@apiVersions([1])
getKeyByAltName(keyAltName: string): Cursor {
@returnsPromise
async getKeyByAltName(keyAltName: string): Promise<Cursor> {
assertArgsDefinedType([keyAltName], ['string'], 'KeyVault.getKeyByAltName');
return this._keyColl.find({ 'keyAltNames': keyAltName });
}

@returnType('Cursor')
@apiVersions([1])
getKeys(): Cursor {
@returnsPromise
async getKeys(): Promise<Cursor> {
return this._keyColl.find({});
}

Expand Down
78 changes: 36 additions & 42 deletions packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function processDigestPassword(
* @param configDB
* @param verbose
*/
export async function getPrintableShardStatus(db: Database, verbose: boolean): Promise<any> {
export async function getPrintableShardStatus(db: Database, verbose: boolean): Promise<Document> {
const result = {} as any; // use array to maintain order

// configDB is a DB object that contains the sharding metadata of interest.
Expand All @@ -185,8 +185,8 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P

const [ version, shards, mostRecentMongos ] = await Promise.all([
versionColl.findOne(),
shardsColl.find().sort({ _id: 1 }).toArray(),
mongosColl.find().sort({ ping: -1 }).limit(1).tryNext()
shardsColl.find().then(cursor => cursor.sort({ _id: 1 }).toArray()),
mongosColl.find().then(cursor => cursor.sort({ ping: -1 }).limit(1).tryNext())
]);
if (version === null) {
throw new MongoshInvalidInputError(
Expand Down Expand Up @@ -228,8 +228,8 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P
};

if (verbose) {
result[mongosAdjective] = await mongosColl
.find(recentMongosQuery)
result[mongosAdjective] = await (await mongosColl
.find(recentMongosQuery))
.sort({ ping: -1 })
.toArray();
} else {
Expand Down Expand Up @@ -277,7 +277,7 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P
(async(): Promise<void> => {
// Output the list of active migrations
type Lock = { _id: string; when: Date };
const activeLocks: Lock[] = await configDB.getCollection('locks').find({ state: { $eq: 2 } }).toArray() as Lock[];
const activeLocks: Lock[] = await (await configDB.getCollection('locks').find({ state: { $eq: 2 } })).toArray() as Lock[];
if (activeLocks?.length > 0) {
balancerRes['Collections with active migrations'] = activeLocks.map((lock) => {
return `${lock._id} started at ${lock.when}`;
Expand All @@ -300,7 +300,7 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P

if (versionHasActionlog) {
// Review config.actionlog for errors
const balErrs = await configDB.getCollection('actionlog').find({ what: 'balancer.round' }).sort({ time: -1 }).limit(5).toArray();
const balErrs = await (await configDB.getCollection('actionlog').find({ what: 'balancer.round' })).sort({ time: -1 }).limit(5).toArray();
const actionReport = { count: 0, lastErr: '', lastTime: ' ' };
if (balErrs !== null) {
balErrs.forEach((r: any) => {
Expand Down Expand Up @@ -393,7 +393,7 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P
const dbRes: any[] = [];
result.databases = dbRes;

const databases = await configDB.getCollection('databases').find().sort({ name: 1 }).toArray();
const databases = await (await configDB.getCollection('databases').find()).sort({ name: 1 }).toArray();

// Special case the config db, since it doesn't have a record in config.databases.
databases.push({ '_id': 'config', 'primary': 'config', 'partitioned': true });
Expand All @@ -405,8 +405,8 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P
const escapeRegex = (string: string): string => {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
const colls = await configDB.getCollection('collections')
.find({ _id: new RegExp('^' + escapeRegex(db._id) + '\\.') })
const colls = await (await configDB.getCollection('collections')
.find({ _id: new RegExp('^' + escapeRegex(db._id) + '\\.') }))
.sort({ _id: 1 })
.toArray();

Expand Down Expand Up @@ -441,45 +441,39 @@ export async function getPrintableShardStatus(db: Database, verbose: boolean): P

// NOTE: this will return the chunk info as a string, and will print ugly BSON
if (totalChunks < 20 || verbose) {
(await chunksColl.find(chunksCollMatch)
.sort({ min: 1 }).toArray())
.forEach((chunk: any) => {
const c = {
min: chunk.min,
max: chunk.max,
'on shard': chunk.shard,
'last modified': chunk.lastmod
} as any;
// Displaying a full, multi-line output for each chunk is a bit verbose,
// even if there are only a few chunks. Where supported, we use a custom
// inspection function to inspect a copy of this object with an unlimited
// line break length (i.e. all objects on a single line).
Object.defineProperty(c, Symbol.for('nodejs.util.inspect.custom'), {
value: function(depth: number, options: any): string {
return inspect({ ...this }, { ...options, breakLength: Infinity });
},
writable: true,
configurable: true
});
if (chunk.jumbo) c.jumbo = 'yes';
chunksRes.push(c);
for await (const chunk of (await chunksColl.find(chunksCollMatch)).sort({ min: 1 })) {
const c = {
min: chunk.min,
max: chunk.max,
'on shard': chunk.shard,
'last modified': chunk.lastmod
} as any;
// Displaying a full, multi-line output for each chunk is a bit verbose,
// even if there are only a few chunks. Where supported, we use a custom
// inspection function to inspect a copy of this object with an unlimited
// line break length (i.e. all objects on a single line).
Object.defineProperty(c, Symbol.for('nodejs.util.inspect.custom'), {
value: function(depth: number, options: any): string {
return inspect({ ...this }, { ...options, breakLength: Infinity });
},
writable: true,
configurable: true
});
if (chunk.jumbo) c.jumbo = 'yes';
chunksRes.push(c);
}
} else {
chunksRes.push('too many chunks to print, use verbose if you want to force print');
}

const tagsRes: any[] = [];
(await configDB.getCollection('tags')
.find(chunksCollMatch)
.sort({ min: 1 })
.toArray())
.forEach((tag: any) => {
tagsRes.push({
tag: tag.tag,
min: tag.min,
max: tag.max
});
for await (const tag of (await configDB.getCollection('tags').find(chunksCollMatch)).sort({ min: 1 })) {
tagsRes.push({
tag: tag.tag,
min: tag.min,
max: tag.max
});
}
collRes.chunks = chunksRes;
collRes.tags = tagsRes;
return [coll._id, collRes];
Expand Down
Loading