Skip to content

Commit 115281b

Browse files
committed
remove _typeLaunder()
1 parent 26451e2 commit 115281b

File tree

14 files changed

+61
-96
lines changed

14 files changed

+61
-96
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ describe('Collection', function () {
7070
describe('toShellResult', function () {
7171
const mongo = sinon.spy();
7272
const db = new Database(mongo as any, 'myDB');
73-
const coll = new Collection(
74-
mongo as any,
75-
db._typeLaunder(),
76-
'myCollection'
77-
);
73+
const coll = new Collection(mongo as any, db, 'myCollection');
7874
it('toShellResult', async function () {
7975
expect((await toShellResult(coll)).type).to.equal('Collection');
8076
expect((await toShellResult(coll)).printable).to.equal(
@@ -89,11 +85,7 @@ describe('Collection', function () {
8985
{ _instanceState: { emitApiCallWithArgs: (): void => {} } } as any,
9086
'db1'
9187
);
92-
const coll: any = new Collection(
93-
{} as any,
94-
database._typeLaunder(),
95-
'coll'
96-
);
88+
const coll: any = new Collection({} as any, database, 'coll');
9789
expect(coll.someCollection).to.have.instanceOf(Collection);
9890
expect(coll.someCollection._name).to.equal('coll.someCollection');
9991
});
@@ -177,7 +169,7 @@ describe('Collection', function () {
177169
ServerSchema,
178170
ServerSchema['db1'],
179171
ServerSchema['db1']['coll1']
180-
>(mongo, database._typeLaunder(), 'coll1');
172+
>(mongo, database, 'coll1');
181173
});
182174
describe('aggregate', function () {
183175
let serviceProviderCursor: StubbedInstance<ServiceProviderAggregationCursor>;
@@ -2881,7 +2873,7 @@ describe('Collection', function () {
28812873
);
28822874
collection = new Collection(
28832875
mongo1,
2884-
database._typeLaunder(),
2876+
database,
28852877
'collfle2' as StringKey<ServerSchema['db1']>
28862878
);
28872879
mongo2 = new Mongo(

packages/shell-api/src/collection.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import type {
7373
AggregateOptions,
7474
SearchIndexDescription,
7575
} from '@mongosh/service-provider-core';
76-
import type { RunCommandCursor, DatabaseWithSchema } from './index';
76+
import type { RunCommandCursor, Database, DatabaseWithSchema } from './index';
7777
import {
7878
AggregationCursor,
7979
BulkWriteResult,
@@ -123,14 +123,14 @@ export class Collection<
123123
_database: DatabaseWithSchema<M, D>;
124124
_name: N;
125125

126-
_typeLaunder(): CollectionWithSchema<M, D> {
127-
return this as CollectionWithSchema<M, D>;
128-
}
129-
130-
constructor(mongo: Mongo<M>, database: DatabaseWithSchema<M, D>, name: N) {
126+
constructor(
127+
mongo: Mongo<M>,
128+
database: DatabaseWithSchema<M, D> | Database<M, D>,
129+
name: N
130+
) {
131131
super();
132132
this._mongo = mongo;
133-
this._database = database;
133+
this._database = database as DatabaseWithSchema<M, D>;
134134
this._name = name;
135135
const proxy = new Proxy(this, {
136136
get: (target, prop): any => {
@@ -1637,7 +1637,7 @@ export class Collection<
16371637
explain(verbosity: ExplainVerbosityLike = 'queryPlanner'): Explainable {
16381638
verbosity = validateExplainableVerbosity(verbosity);
16391639
this._emitCollectionApiCall('explain', { verbosity });
1640-
return new Explainable(this._mongo, this._typeLaunder(), verbosity);
1640+
return new Explainable(this._mongo, this, verbosity);
16411641
}
16421642

16431643
/**
@@ -2008,7 +2008,7 @@ export class Collection<
20082008
true,
20092009
await this._database._baseOptions()
20102010
);
2011-
return new Bulk(this._typeLaunder(), innerBulk, true);
2011+
return new Bulk(this, innerBulk, true);
20122012
}
20132013

20142014
@returnsPromise
@@ -2022,14 +2022,14 @@ export class Collection<
20222022
false,
20232023
await this._database._baseOptions()
20242024
);
2025-
return new Bulk(this._typeLaunder(), innerBulk);
2025+
return new Bulk(this, innerBulk);
20262026
}
20272027

20282028
@returnType('PlanCache')
20292029
@apiVersions([])
20302030
getPlanCache(): PlanCache {
20312031
this._emitCollectionApiCall('getPlanCache');
2032-
return new PlanCache(this._typeLaunder());
2032+
return new PlanCache(this);
20332033
}
20342034

20352035
@returnsPromise
@@ -2341,15 +2341,15 @@ export class Collection<
23412341
@apiVersions([1])
23422342
async hideIndex(index: string | Document): Promise<Document> {
23432343
this._emitCollectionApiCall('hideIndex');
2344-
return setHideIndex(this._typeLaunder(), index, true);
2344+
return setHideIndex(this, index, true);
23452345
}
23462346

23472347
@serverVersions(['4.4.0', ServerVersions.latest])
23482348
@returnsPromise
23492349
@apiVersions([1])
23502350
async unhideIndex(index: string | Document): Promise<Document> {
23512351
this._emitCollectionApiCall('unhideIndex');
2352-
return setHideIndex(this._typeLaunder(), index, false);
2352+
return setHideIndex(this, index, false);
23532353
}
23542354

23552355
@serverVersions(['7.0.0', ServerVersions.latest])

packages/shell-api/src/database.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ export class Database<
9393
_cachedCollectionNames: StringKey<D>[] = [];
9494
_cachedHello: Document | null = null;
9595

96-
_typeLaunder(): DatabaseWithSchema<M, D> {
97-
return this as DatabaseWithSchema<M, D>;
98-
}
99-
10096
constructor(mongo: Mongo<M>, name: StringKey<M>, session?: Session) {
10197
super();
10298
this._mongo = mongo;
@@ -107,7 +103,7 @@ export class Database<
107103
> = Object.create(null);
108104
this._collections = collections;
109105
this._session = session;
110-
const proxy = new Proxy(this._typeLaunder(), {
106+
const proxy = new Proxy(this, {
111107
get: (target, prop): any => {
112108
if (prop in target) {
113109
return (target as any)[prop];
@@ -122,7 +118,11 @@ export class Database<
122118
}
123119

124120
if (!collections[prop]) {
125-
collections[prop] = new Collection(mongo, proxy, prop)._typeLaunder();
121+
collections[prop] = new Collection<M, D>(
122+
mongo,
123+
proxy,
124+
prop
125+
) as CollectionWithSchema<M, D>;
126126
}
127127

128128
return collections[prop];
@@ -522,11 +522,11 @@ export class Database<
522522
._collections;
523523

524524
if (!collections[coll]) {
525-
collections[coll] = new Collection(
525+
collections[coll] = new Collection<M, D>(
526526
this._mongo,
527-
this._typeLaunder(),
527+
this,
528528
coll
529-
)._typeLaunder();
529+
) as CollectionWithSchema<M, D>;
530530
}
531531

532532
return collections[coll] as CollectionWithSchema<M, D, D[K], K>;
@@ -1535,7 +1535,7 @@ export class Database<
15351535
async printShardingStatus(verbose = false): Promise<CommandResult> {
15361536
this._emitDatabaseApiCall('printShardingStatus', { verbose });
15371537
const result = await getPrintableShardStatus(
1538-
await getConfigDB(this._typeLaunder()),
1538+
await getConfigDB(this),
15391539
verbose
15401540
);
15411541
return new CommandResult('StatsResult', result);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Explainable', function () {
5050
describe('metadata', function () {
5151
const mongo: any = { _instanceState: { emitApiCallWithArgs: sinon.spy() } };
5252
const db = new Database(mongo, 'myDB');
53-
const coll = new Collection(mongo, db._typeLaunder(), 'myCollection');
53+
const coll = new Collection(mongo, db, 'myCollection');
5454
const explainable = new Explainable(mongo, coll, 'queryPlannerExtended');
5555
it('toShellResult', async function () {
5656
const result = await toShellResult(explainable);
@@ -81,7 +81,7 @@ describe('Explainable', function () {
8181
serviceProvider
8282
);
8383
database = new Database(mongo, 'db1');
84-
collection = new Collection(mongo, database._typeLaunder(), 'coll1');
84+
collection = new Collection(mongo, database, 'coll1');
8585
explainable = new Explainable(mongo, collection, 'queryPlanner');
8686
});
8787
describe('getCollection', function () {

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

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ describe('getPrintableShardStatus', function () {
217217
configDatabase.getSiblingDB = getSiblingDB;
218218
configDatabase._maybeCachedHello = stub().returns({ msg: 'isdbgrid' });
219219

220-
const status = await getPrintableShardStatus(
221-
configDatabase._typeLaunder(),
222-
false
223-
);
220+
const status = await getPrintableShardStatus(configDatabase, false);
224221
expect(status.shardingVersion.clusterId).to.be.instanceOf(bson.ObjectId);
225222
expect(status.shards.map(({ host }: { host: string }) => host)).to.include(
226223
'shard01/localhost:27018,localhost:27019,localhost:27020'
@@ -251,10 +248,7 @@ describe('getPrintableShardStatus', function () {
251248
'upgradeState',
252249
]) {
253250
it(`does not show ${hiddenField} in shardingVersion`, async function () {
254-
const status = await getPrintableShardStatus(
255-
configDatabase._typeLaunder(),
256-
false
257-
);
251+
const status = await getPrintableShardStatus(configDatabase, false);
258252
expect((status.shardingVersion as any)[hiddenField]).to.equal(
259253
undefined
260254
);
@@ -265,28 +259,19 @@ describe('getPrintableShardStatus', function () {
265259
it('returns whether the balancer is currently running', async function () {
266260
{
267261
inBalancerRound = true;
268-
const status = await getPrintableShardStatus(
269-
configDatabase._typeLaunder(),
270-
true
271-
);
262+
const status = await getPrintableShardStatus(configDatabase, true);
272263
expect(status.balancer['Currently running']).to.equal('yes');
273264
}
274265

275266
{
276267
inBalancerRound = false;
277-
const status = await getPrintableShardStatus(
278-
configDatabase._typeLaunder(),
279-
true
280-
);
268+
const status = await getPrintableShardStatus(configDatabase, true);
281269
expect(status.balancer['Currently running']).to.equal('no');
282270
}
283271
});
284272

285273
it('returns an object with verbose sharding information if requested', async function () {
286-
const status = await getPrintableShardStatus(
287-
configDatabase._typeLaunder(),
288-
true
289-
);
274+
const status = await getPrintableShardStatus(configDatabase, true);
290275
expect((status['most recently active mongoses'][0] as any).up).to.be.a(
291276
'number'
292277
);
@@ -300,10 +285,7 @@ describe('getPrintableShardStatus', function () {
300285
_id: 'balancer',
301286
activeWindow: { start: '00:00', stop: '23:59' },
302287
});
303-
const status = await getPrintableShardStatus(
304-
configDatabase._typeLaunder(),
305-
false
306-
);
288+
const status = await getPrintableShardStatus(configDatabase, false);
307289
expect(status.balancer['Balancer active window is set between']).to.equal(
308290
'00:00 and 23:59 server local time'
309291
);
@@ -319,10 +301,7 @@ describe('getPrintableShardStatus', function () {
319301
what: 'balancer.round',
320302
ns: '',
321303
});
322-
const status = await getPrintableShardStatus(
323-
configDatabase._typeLaunder(),
324-
false
325-
);
304+
const status = await getPrintableShardStatus(configDatabase, false);
326305
expect(
327306
status.balancer['Failed balancer rounds in last 5 attempts']
328307
).to.equal(1);
@@ -336,10 +315,7 @@ describe('getPrintableShardStatus', function () {
336315
ts: new bson.ObjectId('5fce116c579db766a198a176'),
337316
when: new Date('2020-12-07T11:26:36.803Z'),
338317
});
339-
const status = await getPrintableShardStatus(
340-
configDatabase._typeLaunder(),
341-
false
342-
);
318+
const status = await getPrintableShardStatus(configDatabase, false);
343319
expect(
344320
status.balancer['Collections with active migrations']
345321
).to.have.lengthOf(1);
@@ -354,10 +330,7 @@ describe('getPrintableShardStatus', function () {
354330
what: 'moveChunk.from',
355331
details: { from: 'shard0', to: 'shard1', note: 'success' },
356332
});
357-
const status = await getPrintableShardStatus(
358-
configDatabase._typeLaunder(),
359-
false
360-
);
333+
const status = await getPrintableShardStatus(configDatabase, false);
361334
expect(
362335
status.balancer['Migration Results for the last 24 hours']
363336
).to.deep.equal({ 1: 'Success' });
@@ -369,10 +342,7 @@ describe('getPrintableShardStatus', function () {
369342
what: 'moveChunk.from',
370343
details: { from: 'shard0', to: 'shard1', errmsg: 'oopsie' },
371344
});
372-
const status = await getPrintableShardStatus(
373-
configDatabase._typeLaunder(),
374-
false
375-
);
345+
const status = await getPrintableShardStatus(configDatabase, false);
376346

377347
expect(
378348
status.balancer['Migration Results for the last 24 hours']
@@ -382,7 +352,7 @@ describe('getPrintableShardStatus', function () {
382352
it('fails when config.version is empty', async function () {
383353
await configDatabase.getCollection('version').drop();
384354
try {
385-
await getPrintableShardStatus(configDatabase._typeLaunder(), false);
355+
await getPrintableShardStatus(configDatabase, false);
386356
} catch (err: any) {
387357
expect(err.name).to.equal('MongoshInvalidInputError');
388358
return;

packages/shell-api/src/mongo-errors.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('mongo-errors', function () {
9999
serviceProvider
100100
);
101101
database = new Database(mongo, 'db1');
102-
collection = new Collection(mongo, database._typeLaunder(), 'coll1');
102+
collection = new Collection(mongo, database, 'coll1');
103103
});
104104

105105
it('on collection.find error', async function () {

packages/shell-api/src/mongo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export default class Mongo<
267267
}
268268

269269
if (!(name in this._databases)) {
270-
this._databases[name] = new Database(this, name)._typeLaunder();
270+
this._databases[name] = new Database(this, name) as DatabaseWithSchema<
271+
M,
272+
M[K]
273+
>;
271274
}
272275
return this._databases[name] as DatabaseWithSchema<M, M[K]>;
273276
}

packages/shell-api/src/replica-set.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('ReplicaSet', function () {
114114
serviceProvider
115115
);
116116
db = new Database(mongo, 'testdb');
117-
rs = new ReplicaSet(db._typeLaunder());
117+
rs = new ReplicaSet(db);
118118
});
119119

120120
describe('initiate', function () {
@@ -855,7 +855,7 @@ describe('ReplicaSet', function () {
855855
);
856856
instanceState = new ShellInstanceState(serviceProvider);
857857
db = instanceState.currentDb;
858-
rs = new ReplicaSet(db._typeLaunder());
858+
rs = new ReplicaSet(db);
859859

860860
// check replset uninitialized
861861
try {
@@ -1132,7 +1132,7 @@ describe('ReplicaSet', function () {
11321132

11331133
const instanceState = new ShellInstanceState(serviceProvider);
11341134
const db = instanceState.currentDb;
1135-
const rs = new ReplicaSet(db._typeLaunder());
1135+
const rs = new ReplicaSet(db);
11361136
const addArbWithRetry = createRetriableMethod(rs, 'addArb');
11371137
/**
11381138
* Small hack warning:

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { redactURICredentials } from '@mongosh/history';
88
import type { Document } from '@mongosh/service-provider-core';
99
import type Mongo from './mongo';
10-
import type { DatabaseWithSchema } from './database';
10+
import type { Database, DatabaseWithSchema } from './database';
1111
import {
1212
deprecated,
1313
returnsPromise,
@@ -42,9 +42,9 @@ export default class ReplicaSet<
4242
> extends ShellApiWithMongoClass {
4343
_database: DatabaseWithSchema<M, D>;
4444

45-
constructor(database: DatabaseWithSchema<M, D>) {
45+
constructor(database: DatabaseWithSchema<M, D> | Database<M, D>) {
4646
super();
47-
this._database = database;
47+
this._database = database as DatabaseWithSchema<M, D>;
4848
}
4949

5050
get _mongo(): Mongo<M> {

0 commit comments

Comments
 (0)