Skip to content

Commit 97cf472

Browse files
committed
implemented executeCallback
executeCallbackCommand now invokes executeCommand
1 parent 94380fd commit 97cf472

File tree

3 files changed

+13
-50
lines changed

3 files changed

+13
-50
lines changed

src/operations/command.ts

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
111111
server: Server,
112112
session: ClientSession | undefined,
113113
cmd: Document
114-
): Promise<any> {
114+
): Promise<Document> {
115115
// TODO: consider making this a non-enumerable property
116116
this.server = server;
117117

@@ -169,46 +169,9 @@ export abstract class CommandCallbackOperation<T = any> extends CommandOperation
169169
cmd: Document,
170170
callback: Callback
171171
): void {
172-
this.server = server;
173-
174-
const options = {
175-
...this.options,
176-
...this.bsonOptions,
177-
readPreference: this.readPreference,
178-
session
179-
};
180-
181-
const serverWireVersion = maxWireVersion(server);
182-
const inTransaction = this.session && this.session.inTransaction();
183-
184-
if (this.readConcern && commandSupportsReadConcern(cmd) && !inTransaction) {
185-
Object.assign(cmd, { readConcern: this.readConcern });
186-
}
187-
188-
if (this.trySecondaryWrite && serverWireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION) {
189-
options.omitReadPreference = true;
190-
}
191-
192-
if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) {
193-
Object.assign(cmd, { writeConcern: this.writeConcern });
194-
}
195-
196-
if (
197-
options.collation &&
198-
typeof options.collation === 'object' &&
199-
!this.hasAspect(Aspect.SKIP_COLLATION)
200-
) {
201-
Object.assign(cmd, { collation: options.collation });
202-
}
203-
204-
if (typeof options.maxTimeMS === 'number') {
205-
cmd.maxTimeMS = options.maxTimeMS;
206-
}
207-
208-
if (this.hasAspect(Aspect.EXPLAINABLE) && this.explain) {
209-
cmd = decorateWithExplain(cmd, this.explain);
210-
}
211-
212-
server.command(this.ns, cmd, options, callback);
172+
super.executeCommand(server, session, cmd).then(
173+
res => callback(undefined, res),
174+
err => callback(err, undefined)
175+
);
213176
}
214177
}

src/sdam/server.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
118118
pool: ConnectionPool;
119119
serverApi?: ServerApi;
120120
hello?: Document;
121-
commandAsync: (
122-
ns: MongoDBNamespace,
123-
cmd: Document,
124-
options: CommandOptions
125-
) => Promise<Document | undefined>;
121+
commandAsync: (ns: MongoDBNamespace, cmd: Document, options: CommandOptions) => Promise<Document>;
126122
[kMonitor]: Monitor | null;
127123

128124
/** @event */
@@ -151,7 +147,8 @@ export class Server extends TypedEventEmitter<ServerEvents> {
151147
ns: MongoDBNamespace,
152148
cmd: Document,
153149
options: CommandOptions,
154-
callback: Callback<Document>
150+
// callback type defines Document result because result is never nullish when it succeeds, otherwise promise rejects
151+
callback: (error: Error | undefined, result: Document) => void
155152
) => this.command(ns, cmd, options, callback as any)
156153
);
157154

test/unit/operations/abstract_command.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class ConcreteCommand<T> extends CommandOperation<T> {
3131
session: ClientSession | undefined,
3232
callback: Callback<any>
3333
) {
34-
return [server, session, callback];
34+
super.execute(server, session).then(
35+
res => callback(undefined, res),
36+
err => callback(err, undefined)
37+
);
3538
}
3639
}
3740

@@ -50,7 +53,7 @@ describe('class CommandOperation', () => {
5053
const operation = new ConcreteCommand<any>();
5154
const serverSpy = sinon.stub(server, 'commandAsync');
5255
const commandPromise = operation.executeCommand(server, undefined, { ping: 1 });
53-
expect(commandPromise).to.be.instanceOf(Promise<any>);
56+
expect(commandPromise).to.be.instanceOf(Promise);
5457
await commandPromise;
5558
expect(serverSpy).to.have.been.calledOnce;
5659
});

0 commit comments

Comments
 (0)