|
| 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