Skip to content

Commit dab9a09

Browse files
committed
feat(shell-api): provide lazily-connecting Mongo ctor
1 parent 862bfdf commit dab9a09

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/shell-api/src/shell-api.ts

+37
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,43 @@ export default class ShellApi extends ShellApiClass {
263263
return mongo;
264264
}
265265

266+
@returnType('Mongo')
267+
@platforms(['CLI'])
268+
public LazyMongo(
269+
uri?: string,
270+
fleOptions?: ClientSideFieldLevelEncryptionOptions,
271+
otherOptions?: { api?: ServerApi | ServerApiVersion }
272+
): Mongo {
273+
assertCLI(
274+
this._instanceState.initialServiceProvider.platform,
275+
'new Mongo connections'
276+
);
277+
const mongo = new Mongo(this._instanceState, uri, fleOptions, otherOptions);
278+
const connectPromise = mongo.connect();
279+
connectPromise.catch(() => {});
280+
this._instanceState.mongos.push(mongo);
281+
282+
return new Proxy(mongo, {
283+
get: (target, prop) => {
284+
if (typeof (Mongo.prototype as any)[prop] === 'function') {
285+
return function (...args: any[]) {
286+
return Object.assign(
287+
(async () => {
288+
await connectPromise;
289+
return await (target as any)[prop](...args);
290+
})(),
291+
{
292+
[Symbol.for('@@mongosh.syntheticPromise')]: true,
293+
}
294+
);
295+
};
296+
} else {
297+
return (target as any)[prop];
298+
}
299+
},
300+
});
301+
}
302+
266303
@returnsPromise
267304
@returnType('Database')
268305
@platforms(['CLI'])

0 commit comments

Comments
 (0)