Skip to content

Commit 40a8727

Browse files
addaleaxlerouxb
authored andcommitted
WIP
1 parent ed0f87c commit 40a8727

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

packages/service-provider-core/src/admin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
AutoEncryptionOptions,
1414
Collection,
1515
} from './all-transport-types';
16-
import type { ConnectionExtraInfo } from './index';
16+
import type { ConnectionExtraInfo, ServiceProvider } from './index';
1717
import type { ReplPlatform } from './platform';
1818
import type {
1919
AWSEncryptionKeyOptions,
@@ -90,7 +90,10 @@ export default interface Admin {
9090
* @param uri
9191
* @param options
9292
*/
93-
getNewConnection(uri: string, options: MongoClientOptions): Promise<any>; // returns the ServiceProvider instance
93+
getNewConnection(
94+
uri: string,
95+
options: MongoClientOptions
96+
): Promise<ServiceProvider>;
9497

9598
/**
9699
* Return the URI for the current connection, if this ServiceProvider is connected.

packages/service-provider-core/src/cursors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ export interface ServiceProviderChangeStream<TSchema = Document>
6767
next(): Promise<TSchema>;
6868
readonly resumeToken: ResumeToken;
6969
}
70+
71+
export type ServiceProviderAnyCursor<TSchema = Document> =
72+
| ServiceProviderAggregationCursor<TSchema>
73+
| ServiceProviderFindCursor<TSchema>
74+
| ServiceProviderRunCommandCursor<TSchema>
75+
| ServiceProviderFindCursor<TSchema>
76+
| ServiceProviderChangeStream<TSchema>;

packages/service-provider-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export {
2424
ServiceProviderFindCursor,
2525
ServiceProviderRunCommandCursor,
2626
ServiceProviderChangeStream,
27+
ServiceProviderAnyCursor,
2728
} from './cursors';
2829

2930
export {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import type {
2+
ServiceProvider,
3+
ServiceProviderAnyCursor,
4+
ServiceProviderAbstractCursor,
5+
} from '@mongosh/service-provider-core';
6+
import { ServiceProviderCore } from '@mongosh/service-provider-core';
7+
8+
export class DeepInspectServiceProviderWrapper
9+
extends ServiceProviderCore
10+
implements ServiceProvider
11+
{
12+
_sp: ServiceProvider;
13+
14+
constructor(sp: ServiceProvider) {
15+
super(sp.bsonLibrary);
16+
this._sp = sp;
17+
18+
for (const prop of Object.keys(this)) {
19+
if (typeof (this as any)[prop] === 'function' && !(prop in sp)) {
20+
(this as any)[prop] = undefined;
21+
}
22+
}
23+
}
24+
25+
aggregate = cursorMethod('aggregate');
26+
aggregateDb = cursorMethod('aggregateDb');
27+
count = bsonMethod('count');
28+
estimatedDocumentCount = bsonMethod('estimatedDocumentCount');
29+
countDocuments = bsonMethod('countDocuments');
30+
distinct = bsonMethod('distinct');
31+
find = cursorMethod('find');
32+
findOneAndDelete = bsonMethod('findOneAndDelete');
33+
findOneAndReplace = bsonMethod('findOneAndReplace');
34+
findOneAndUpdate = bsonMethod('findOneAndUpdate');
35+
getTopology = forwardedMethod('getTopology');
36+
getIndexes = bsonMethod('getIndexes');
37+
listCollections = bsonMethod('listCollections');
38+
readPreferenceFromOptions = forwardedMethod('readPreferenceFromOptions');
39+
watch = cursorMethod('watch');
40+
getSearchIndexes = bsonMethod('getSearchIndexes');
41+
runCommand = bsonMethod('runCommand');
42+
runCommandWithCheck = bsonMethod('runCommandWithCheck');
43+
runCursorCommand = cursorMethod('runCursorCommand');
44+
dropDatabase = bsonMethod('dropDatabase');
45+
dropCollection = bsonMethod('dropCollection');
46+
bulkWrite = bsonMethod('bulkWrite');
47+
deleteMany = bsonMethod('deleteMany');
48+
updateMany = bsonMethod('updateMany');
49+
updateOne = bsonMethod('updateOne');
50+
deleteOne = bsonMethod('deleteOne');
51+
createIndexes = bsonMethod('createIndexes');
52+
insertMany = bsonMethod('insertMany');
53+
insertOne = bsonMethod('insertOne');
54+
replaceOne = bsonMethod('replaceOne');
55+
initializeBulkOp = bsonMethod('initializeBulkOp');
56+
createSearchIndexes = bsonMethod('createSearchIndexes');
57+
close = forwardedMethod('close');
58+
suspend = forwardedMethod('suspend');
59+
renameCollection = bsonMethod('renameCollection');
60+
dropSearchIndex = bsonMethod('dropSearchIndex');
61+
updateSearchIndex = bsonMethod('updateSearchIndex');
62+
listDatabases = bsonMethod('listDatabases');
63+
authenticate = bsonMethod('authenticate');
64+
createCollection = bsonMethod('createCollection');
65+
getReadPreference = forwardedMethod('getReadPreference');
66+
getReadConcern = forwardedMethod('getReadConcern');
67+
getWriteConcern = forwardedMethod('getWriteConcern');
68+
69+
get platform() {
70+
return this._sp.platform;
71+
}
72+
get initialDb() {
73+
return this._sp.initialDb;
74+
}
75+
getURI = forwardedMethod('getURI');
76+
getConnectionInfo = forwardedMethod('getConnectionInfo');
77+
resetConnectionOptions = forwardedMethod('resetConnectionOptions');
78+
startSession = forwardedMethod('startSession');
79+
getRawClient = forwardedMethod('getRawClient');
80+
createClientEncryption = forwardedMethod('createClientEncryption');
81+
getFleOptions = forwardedMethod('getFleOptions');
82+
createEncryptedCollection = forwardedMethod('createEncryptedCollection');
83+
84+
async getNewConnection(
85+
...args: Parameters<ServiceProvider['getNewConnection']>
86+
): Promise<ServiceProvider> {
87+
return new DeepInspectServiceProviderWrapper(
88+
await this._sp.getNewConnection(...args)
89+
);
90+
}
91+
}
92+
93+
const cursorBsonMethods: (keyof Partial<ServiceProviderAnyCursor>)[] = [
94+
'next',
95+
'tryNext',
96+
'readBufferedDocuments',
97+
'toArray',
98+
'',
99+
];
100+
101+
type PickMethodsByReturnType<T, R> = {
102+
[k in keyof T as NonNullable<T[k]> extends (...args: any[]) => R
103+
? k
104+
: never]: T[k];
105+
};
106+
107+
function cursorMethod<
108+
K extends keyof PickMethodsByReturnType<
109+
ServiceProvider,
110+
ServiceProviderAnyCursor
111+
>
112+
>(
113+
key: K
114+
): (
115+
...args: Parameters<Required<ServiceProvider>[K]>
116+
) => ReturnType<Required<ServiceProvider>[K]> {
117+
return function (
118+
this: ServiceProvider,
119+
...args: Parameters<ServiceProvider[K]>
120+
): ReturnType<ServiceProvider[K]> {
121+
return this[key](...args);
122+
};
123+
}
124+
125+
function bsonMethod<
126+
K extends keyof PickMethodsByReturnType<ServiceProvider, any>
127+
>(
128+
key: K
129+
): (
130+
...args: Parameters<Required<ServiceProvider>[K]>
131+
) => ReturnType<Required<ServiceProvider>[K]> {
132+
return function (
133+
this: ServiceProvider,
134+
...args: Parameters<Required<ServiceProvider>[K]>
135+
): ReturnType<Required<ServiceProvider>[K]> {
136+
return this[key](...args);
137+
};
138+
}
139+
140+
function forwardedMethod<
141+
K extends keyof PickMethodsByReturnType<ServiceProvider, any>
142+
>(
143+
key: K
144+
): (
145+
...args: Parameters<Required<ServiceProvider>[K]>
146+
) => ReturnType<Required<ServiceProvider>[K]> {
147+
return function (
148+
this: ServiceProvider,
149+
...args: Parameters<Required<ServiceProvider>[K]>
150+
): ReturnType<Required<ServiceProvider>[K]> {
151+
return this[key](...args);
152+
};
153+
}

0 commit comments

Comments
 (0)