Skip to content

Commit 713b92d

Browse files
committed
pass sessions into execute
1 parent 68a550e commit 713b92d

36 files changed

+225
-153
lines changed

src/operations/add_user.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Callback, getTopology } from '../utils';
66
import type { Document } from '../bson';
77
import type { Server } from '../sdam/server';
88
import type { Db } from '../db';
9+
import type { ClientSession } from '../sessions';
910

1011
/** @public */
1112
export interface AddUserOptions extends CommandOperationOptions {
@@ -39,7 +40,7 @@ export class AddUserOperation extends CommandOperation<Document> {
3940
this.options = options || {};
4041
}
4142

42-
execute(server: Server, callback: Callback<Document>): void {
43+
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
4344
const db = this.db;
4445
const username = this.username;
4546
const password = this.password;
@@ -101,7 +102,7 @@ export class AddUserOperation extends CommandOperation<Document> {
101102
command.pwd = userPassword;
102103
}
103104

104-
super.executeCommand(server, command, callback);
105+
super.executeCommand(server, session, command, callback);
105106
}
106107
}
107108

src/operations/aggregate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Callback } from '../utils';
77
import type { Document } from '../bson';
88
import type { Server } from '../sdam/server';
99
import type { CollationOptions } from '../cmap/wire_protocol/write_command';
10+
import type { ClientSession } from '../sessions';
1011

1112
/** @internal */
1213
export const DB_AGGREGATE_COLLECTION = 1 as const;
@@ -84,7 +85,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
8485
this.pipeline.push(stage);
8586
}
8687

87-
execute(server: Server, callback: Callback<T>): void {
88+
execute(server: Server, session: ClientSession, callback: Callback<T>): void {
8889
const options: AggregateOptions = this.options;
8990
const serverWireVersion = maxWireVersion(server);
9091
const command: Document = { aggregate: this.target, pipeline: this.pipeline };
@@ -116,7 +117,7 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
116117
command.cursor.batchSize = options.batchSize;
117118
}
118119

119-
super.executeCommand(server, command, callback);
120+
super.executeCommand(server, session, command, callback);
120121
}
121122
}
122123

src/operations/bulk_write.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { applyRetryableWrites, Callback } from '../utils';
21
import { Aspect, defineAspects, AbstractOperation } from './operation';
2+
import type { Callback } from '../utils';
33
import type { Collection } from '../collection';
44
import type {
55
BulkOperationBase,
@@ -8,6 +8,7 @@ import type {
88
AnyBulkWriteOperation
99
} from '../bulk/common';
1010
import type { Server } from '../sdam/server';
11+
import type { ClientSession } from '../sessions';
1112

1213
/** @internal */
1314
export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
@@ -26,10 +27,10 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
2627
this.operations = operations;
2728
}
2829

29-
execute(server: Server, callback: Callback<BulkWriteResult>): void {
30+
execute(server: Server, session: ClientSession, callback: Callback<BulkWriteResult>): void {
3031
const coll = this.collection;
3132
const operations = this.operations;
32-
const options = { ...this.options, ...this.bsonOptions };
33+
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
3334

3435
// Create the bulk operation
3536
const bulk: BulkOperationBase =
@@ -46,11 +47,8 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
4647
return callback(err);
4748
}
4849

49-
let finalOptions = Object.assign({}, options);
50-
finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
51-
5250
// Execute the bulk
53-
bulk.execute(finalOptions, (err, r) => {
51+
bulk.execute({ ...options, session }, (err, r) => {
5452
// We have connection level error
5553
if (!r && err) {
5654
return callback(err);

src/operations/collections.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Db } from '../db';
66
// eslint-disable-next-line
77
import type { Collection } from '../collection';
88
import type { Server } from '../sdam/server';
9+
import type { ClientSession } from '../sessions';
910

1011
export interface CollectionsOptions extends OperationOptions {
1112
nameOnly?: boolean;
@@ -22,15 +23,15 @@ export class CollectionsOperation extends AbstractOperation<Collection[]> {
2223
this.db = db;
2324
}
2425

25-
execute(server: Server, callback: Callback<Collection[]>): void {
26+
execute(server: Server, session: ClientSession, callback: Callback<Collection[]>): void {
2627
const db = this.db;
27-
let options: CollectionsOptions = this.options;
28-
2928
const Collection = loadCollection();
3029

31-
options = Object.assign({}, options, { nameOnly: true });
3230
// Let's get the collection names
33-
db.listCollections({}, options).toArray((err, documents) => {
31+
db.listCollections(
32+
{},
33+
{ ...this.options, nameOnly: true, readPreference: this.readPreference, session }
34+
).toArray((err, documents) => {
3435
if (err || !documents) return callback(err);
3536
// Filter collections removing any illegal ones
3637
documents = documents.filter(doc => doc.name.indexOf('$') === -1);

src/operations/command.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ReadConcern } from '../read_concern';
33
import { WriteConcern, WriteConcernOptions } from '../write_concern';
44
import { maxWireVersion, MongoDBNamespace, Callback, decorateWithExplain } from '../utils';
55
import type { ReadPreference } from '../read_preference';
6-
import { commandSupportsReadConcern } from '../sessions';
6+
import { ClientSession, commandSupportsReadConcern } from '../sessions';
77
import { MongoError } from '../error';
88
import type { Logger } from '../logger';
99
import type { Server } from '../sdam/server';
@@ -97,13 +97,19 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
9797
return true;
9898
}
9999

100-
abstract execute(server: Server, callback: Callback<T>): void;
100+
abstract execute(server: Server, session: ClientSession, callback: Callback<T>): void;
101101

102-
executeCommand(server: Server, cmd: Document, callback: Callback): void {
102+
executeCommand(server: Server, session: ClientSession, cmd: Document, callback: Callback): void {
103103
// TODO: consider making this a non-enumerable property
104104
this.server = server;
105105

106-
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
106+
const options = {
107+
...this.options,
108+
...this.bsonOptions,
109+
readPreference: this.readPreference,
110+
session
111+
};
112+
107113
const serverWireVersion = maxWireVersion(server);
108114
const inTransaction = this.session && this.session.inTransaction();
109115

src/operations/count.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Callback, MongoDBNamespace } from '../utils';
44
import type { Document } from '../bson';
55
import type { Server } from '../sdam/server';
66
import type { Collection } from '../collection';
7+
import type { ClientSession } from '../sessions';
78

89
/** @public */
910
export interface CountOptions extends CommandOperationOptions {
@@ -31,7 +32,7 @@ export class CountOperation extends CommandOperation<number> {
3132
this.query = filter;
3233
}
3334

34-
execute(server: Server, callback: Callback<number>): void {
35+
execute(server: Server, session: ClientSession, callback: Callback<number>): void {
3536
const options = this.options;
3637
const cmd: Document = {
3738
count: this.collectionName,
@@ -54,7 +55,7 @@ export class CountOperation extends CommandOperation<number> {
5455
cmd.maxTimeMS = options.maxTimeMS;
5556
}
5657

57-
super.executeCommand(server, cmd, (err, result) => {
58+
super.executeCommand(server, session, cmd, (err, result) => {
5859
callback(err, result ? result.n : 0);
5960
});
6061
}

src/operations/count_documents.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Callback } from '../utils';
33
import type { Document } from '../bson';
44
import type { Server } from '../sdam/server';
55
import type { Collection } from '../collection';
6+
import type { ClientSession } from '../sessions';
67

78
/** @public */
89
export interface CountDocumentsOptions extends AggregateOptions {
@@ -31,8 +32,8 @@ export class CountDocumentsOperation extends AggregateOperation<number> {
3132
super(collection, pipeline, options);
3233
}
3334

34-
execute(server: Server, callback: Callback<number>): void {
35-
super.execute(server, (err, result) => {
35+
execute(server: Server, session: ClientSession, callback: Callback<number>): void {
36+
super.execute(server, session, (err, result) => {
3637
if (err || !result) {
3738
callback(err);
3839
return;

src/operations/create_collection.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { PkFactory } from '../mongo_client';
99

1010
// eslint-disable-next-line
1111
import type { Collection } from '../collection';
12+
import type { ClientSession } from '../sessions';
1213

1314
const ILLEGAL_COMMAND_FIELDS = new Set([
1415
'w',
@@ -77,7 +78,7 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
7778
this.name = name;
7879
}
7980

80-
execute(server: Server, callback: Callback<Collection>): void {
81+
execute(server: Server, session: ClientSession, callback: Callback<Collection>): void {
8182
const db = this.db;
8283
const name = this.name;
8384
const options = this.options;
@@ -103,7 +104,7 @@ export class CreateCollectionOperation extends CommandOperation<Collection> {
103104
}
104105

105106
// otherwise just execute the command
106-
super.executeCommand(server, cmd, done);
107+
super.executeCommand(server, session, cmd, done);
107108
}
108109
}
109110

src/operations/delete.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Server } from '../sdam/server';
88
import type { Collection } from '../collection';
99
import type { Connection } from '../cmap/connection';
1010
import type { WriteCommandOptions } from '../cmap/wire_protocol/write_command';
11+
import type { ClientSession } from '../sessions';
1112

1213
/** @public */
1314
export interface DeleteOptions extends CommandOperationOptions {
@@ -43,11 +44,11 @@ export class DeleteOperation extends AbstractOperation<Document> {
4344
return this.operations.every(op => (typeof op.limit !== 'undefined' ? op.limit > 0 : true));
4445
}
4546

46-
execute(server: Server, callback: Callback): void {
47+
execute(server: Server, session: ClientSession, callback: Callback): void {
4748
server.remove(
4849
this.ns.toString(),
4950
this.operations,
50-
this.options as WriteCommandOptions,
51+
{ ...this.options, readPreference: this.readPreference, session } as WriteCommandOptions,
5152
callback
5253
);
5354
}
@@ -66,10 +67,10 @@ export class DeleteOneOperation extends CommandOperation<DeleteResult> {
6667
this.filter = filter;
6768
}
6869

69-
execute(server: Server, callback: Callback<DeleteResult>): void {
70+
execute(server: Server, session: ClientSession, callback: Callback<DeleteResult>): void {
7071
const coll = this.collection;
7172
const filter = this.filter;
72-
const options = { ...this.options, ...this.bsonOptions };
73+
const options = { ...this.options, ...this.bsonOptions, session };
7374

7475
options.single = true;
7576
removeDocuments(server, coll, filter, options, (err, r) => {
@@ -102,10 +103,10 @@ export class DeleteManyOperation extends CommandOperation<DeleteResult> {
102103
this.filter = filter;
103104
}
104105

105-
execute(server: Server, callback: Callback<DeleteResult>): void {
106+
execute(server: Server, session: ClientSession, callback: Callback<DeleteResult>): void {
106107
const coll = this.collection;
107108
const filter = this.filter;
108-
const options = { ...this.options, ...this.bsonOptions };
109+
const options = { ...this.options, ...this.bsonOptions, session };
109110

110111
// a user can pass `single: true` in to `deleteMany` to remove a single document, theoretically
111112
if (typeof options.single !== 'boolean') {

src/operations/distinct.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Document } from '../bson';
55
import type { Server } from '../sdam/server';
66
import type { Collection } from '../collection';
77
import { MongoError } from '../error';
8+
import type { ClientSession } from '../sessions';
89

910
/** @public */
1011
export type DistinctOptions = CommandOperationOptions;
@@ -38,7 +39,7 @@ export class DistinctOperation extends CommandOperation<Document[]> {
3839
this.query = query;
3940
}
4041

41-
execute(server: Server, callback: Callback<Document[]>): void {
42+
execute(server: Server, session: ClientSession, callback: Callback<Document[]>): void {
4243
const coll = this.collection;
4344
const key = this.key;
4445
const query = this.query;
@@ -71,7 +72,7 @@ export class DistinctOperation extends CommandOperation<Document[]> {
7172
return;
7273
}
7374

74-
super.executeCommand(server, cmd, (err, result) => {
75+
super.executeCommand(server, session, cmd, (err, result) => {
7576
if (err) {
7677
callback(err);
7778
return;

src/operations/drop.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CommandOperation, CommandOperationOptions } from './command';
33
import type { Callback } from '../utils';
44
import type { Server } from '../sdam/server';
55
import type { Db } from '../db';
6+
import type { ClientSession } from '../sessions';
67

78
/** @public */
89
export type DropCollectionOptions = CommandOperationOptions;
@@ -18,8 +19,8 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
1819
this.name = name;
1920
}
2021

21-
execute(server: Server, callback: Callback<boolean>): void {
22-
super.executeCommand(server, { drop: this.name }, (err, result) => {
22+
execute(server: Server, session: ClientSession, callback: Callback<boolean>): void {
23+
super.executeCommand(server, session, { drop: this.name }, (err, result) => {
2324
if (err) return callback(err);
2425
if (result.ok) return callback(undefined, true);
2526
callback(undefined, false);
@@ -38,8 +39,8 @@ export class DropDatabaseOperation extends CommandOperation<boolean> {
3839
super(db, options);
3940
this.options = options;
4041
}
41-
execute(server: Server, callback: Callback<boolean>): void {
42-
super.executeCommand(server, { dropDatabase: 1 }, (err, result) => {
42+
execute(server: Server, session: ClientSession, callback: Callback<boolean>): void {
43+
super.executeCommand(server, session, { dropDatabase: 1 }, (err, result) => {
4344
if (err) return callback(err);
4445
if (result.ok) return callback(undefined, true);
4546
callback(undefined, false);

src/operations/estimated_document_count.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Callback } from '../utils';
44
import type { Document } from '../bson';
55
import type { Server } from '../sdam/server';
66
import type { Collection } from '../collection';
7+
import type { ClientSession } from '../sessions';
78

89
/** @public */
910
export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
@@ -38,7 +39,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
3839
}
3940
}
4041

41-
execute(server: Server, callback: Callback<number>): void {
42+
execute(server: Server, session: ClientSession, callback: Callback<number>): void {
4243
const options = this.options;
4344
const cmd: Document = { count: this.collectionName };
4445

@@ -58,7 +59,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
5859
cmd.hint = options.hint;
5960
}
6061

61-
super.executeCommand(server, cmd, (err, response) => {
62+
super.executeCommand(server, session, cmd, (err, response) => {
6263
if (err) {
6364
callback(err);
6465
return;

src/operations/eval.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { MongoError } from '../error';
55
import type { Callback } from '../utils';
66
import type { Server } from '../sdam/server';
77
import type { Db } from '../db';
8-
import type { Collection } from '..';
8+
import type { Collection } from '../collection';
9+
import type { ClientSession } from '../sessions';
910

1011
/** @public */
1112
export interface EvalOptions extends CommandOperationOptions {
@@ -37,7 +38,7 @@ export class EvalOperation extends CommandOperation<Document> {
3738
});
3839
}
3940

40-
execute(server: Server, callback: Callback<Document>): void {
41+
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
4142
let finalCode = this.code;
4243
let finalParameters: Document[] = [];
4344

@@ -60,7 +61,7 @@ export class EvalOperation extends CommandOperation<Document> {
6061
}
6162

6263
// Execute the command
63-
super.executeCommand(server, cmd, (err, result) => {
64+
super.executeCommand(server, session, cmd, (err, result) => {
6465
if (err) return callback(err);
6566
if (result && result.ok === 1) {
6667
return callback(undefined, result.retval);

0 commit comments

Comments
 (0)