Skip to content

[WIP] use mql #2478

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class Collection<
await cursor.hasNext();
}

this._mongo._instanceState.currentCursor = cursor as Cursor;
this._mongo._instanceState.currentCursor = cursor;
return cursor;
}

Expand Down
25 changes: 11 additions & 14 deletions packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Database<
> extends ShellApiWithMongoClass<M> {
_mongo: Mongo<M>;
_name: StringKey<M>;
_collections: Record<StringKey<D>, CollectionWithSchema<M, D>>;
_collections: { [k in StringKey<D>]: CollectionWithSchema<M, D, D[k], k> };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aah this was what I was looking for!

_session: Session<M> | undefined;
_cachedCollectionNames: StringKey<D>[] = [];
_cachedHello: Document | null = null;
Expand All @@ -96,10 +96,7 @@ export class Database<
super();
this._mongo = mongo;
this._name = name;
const collections: Record<
string,
CollectionWithSchema<M, D>
> = Object.create(null);
const collections: typeof this._collections = Object.create(null);
this._collections = collections;
this._session = session;
const proxy = new Proxy(this, {
Expand All @@ -117,7 +114,7 @@ export class Database<
}

if (!collections[prop]) {
collections[prop] = new Collection<M, D>(
collections[prop as StringKey<D>] = new Collection<M, D>(
mongo,
proxy as DatabaseWithSchema<M, D>,
prop
Expand Down Expand Up @@ -501,7 +498,7 @@ export class Database<
assertArgsDefinedType([db], ['string'], 'Database.getSiblingDB');
this._emitDatabaseApiCall('getSiblingDB', { db });
if (this._session) {
return this._session.getDatabase(db) as DatabaseWithSchema<M, M[K]>;
return this._session.getDatabase(db);
}
return this._mongo._getDb(db);
}
Expand All @@ -519,18 +516,17 @@ export class Database<
);
}

const collections: Record<StringKey<D>, CollectionWithSchema<M, D>> = this
._collections;
const collections = this._collections;

if (!collections[coll]) {
collections[coll] = new Collection<M, D>(
collections[coll] = new Collection<M, D, D[K], K>(
this._mongo,
this as DatabaseWithSchema<M, D>,
coll
) as CollectionWithSchema<M, D>;
) as CollectionWithSchema<M, D, D[K], K>;
}

return collections[coll] as CollectionWithSchema<M, D, D[K], K>;
return collections[coll];
}

@returnsPromise
Expand Down Expand Up @@ -836,6 +832,7 @@ export class Database<
);
}

// eslint-disable-next-line @typescript-eslint/require-await
@returnsPromise
@apiVersions([1])
async createEncryptedCollection(
Expand Down Expand Up @@ -1530,14 +1527,14 @@ export class Database<
return result.err || null;
}

async _getConfigDB(): Promise<DatabaseWithSchema<M, M[keyof M]>> {
async _getConfigDB(): Promise<DatabaseWithSchema<M, M['config']>> {
const helloResult = await this._maybeCachedHello();
if (helloResult.msg !== 'isdbgrid') {
await this._instanceState.printWarning(
'MongoshWarning: [SHAPI-10003] You are not connected to a mongos. This command may not work as expected.'
);
}
return this.getSiblingDB('config' as any);
return this.getSiblingDB('config');
}

@returnsPromise
Expand Down
5 changes: 2 additions & 3 deletions packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import type {
import type { ClientSideFieldLevelEncryptionOptions } from './field-level-encryption';
import type { AutoEncryptionOptions, Long, ObjectId, Timestamp } from 'mongodb';
import { shellApiType } from './enums';
import type { AbstractCursor } from './abstract-cursor';
import type ChangeStreamCursor from './change-stream-cursor';
import type { ShellBson } from './shell-bson';
import { inspect } from 'util';

Expand Down Expand Up @@ -851,7 +849,7 @@ export function addHiddenDataProperty<T = any>(

export async function iterate(
results: CursorIterationResult,
cursor: AbstractCursor<any, any> | ChangeStreamCursor,
cursor: { isClosed(): boolean; tryNext(): Promise<Document | null> },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to specify smaller types. Didn't consider that.

batchSize: number
): Promise<CursorIterationResult> {
if (cursor.isClosed()) {
Expand Down Expand Up @@ -1298,6 +1296,7 @@ export type GenericServerSideSchema = {
locks: GenericCollectionSchema;
databases: GenericCollectionSchema;
tags: GenericCollectionSchema;
actionlog: GenericCollectionSchema;
};
local: {
'system.version': GenericCollectionSchema;
Expand Down
5 changes: 3 additions & 2 deletions packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export default class Mongo<
M extends GenericServerSideSchema = GenericServerSideSchema
> extends ShellApiClass {
private __serviceProvider: ServiceProvider | null = null;
public readonly _databases: Record<StringKey<M>, DatabaseWithSchema<M>> =
Object.create(null);
public readonly _databases: {
[k in StringKey<M>]: DatabaseWithSchema<M, M[k]>;
} = Object.create(null);
private _connectionId: number;
public _instanceState: ShellInstanceState;
public _connectionInfo: ConnectionInfo;
Expand Down
6 changes: 3 additions & 3 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ export default class Shard<
}
}

async _getConfigDB(): Promise<DatabaseWithSchema<M, M[keyof M]>> {
async _getConfigDB(): Promise<DatabaseWithSchema<M, M['config']>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on exactly this change and I seemed to just trade one set of errors for another. Although I agree that M['config'] is more correct - it is what I tried first.

const helloResult = await this._database._maybeCachedHello();
if (helloResult.msg !== 'isdbgrid') {
await this._instanceState.printWarning(
'MongoshWarning: [SHAPI-10003] You are not connected to a mongos. This command may not work as expected.'
);
}
return this._database.getSiblingDB('config' as any);
return this._database.getSiblingDB('config');
}

@returnsPromise
@apiVersions([1])
async status(
verbose = false,
configDB?: DatabaseWithSchema<M, M[keyof M]>
configDB?: DatabaseWithSchema<M, M['config']>
): Promise<CommandResult<ShardingStatusResult>> {
const result = await getPrintableShardStatus(
configDB ?? (await this._getConfigDB()),
Expand Down