MCPcopy
hub / github.com/mongodb/node-mongodb-native / applySession

Function applySession

src/sessions.ts:1172–1251  ·  view source on GitHub ↗
(
  session: ClientSession,
  command: Document,
  options: CommandOptions
)

Source from the content-addressed store, hash-verified

1170 * @internal
1171 */
1172export function applySession(
1173 session: ClientSession,
1174 command: Document,
1175 options: CommandOptions
1176): MongoDriverError | undefined {
1177 if (session.hasEnded) {
1178 return new MongoExpiredSessionError();
1179 }
1180
1181 // May acquire serverSession here
1182 const serverSession = session.serverSession;
1183 if (serverSession == null) {
1184 return new MongoRuntimeError('Unable to acquire server session');
1185 }
1186
1187 if (options.writeConcern?.w === 0) {
1188 if (session && session.explicit) {
1189 // Error if user provided an explicit session to an unacknowledged write (SPEC-1019)
1190 return new MongoAPIError('Cannot have explicit session with unacknowledged writes');
1191 }
1192 return;
1193 }
1194
1195 // mark the last use of this session, and apply the `lsid`
1196 serverSession.lastUse = processTimeMS();
1197 command.lsid = serverSession.id;
1198
1199 const inTxnOrTxnCommand = session.inTransaction() || isTransactionCommand(command);
1200 const isRetryableWrite = !!options.willRetryWrite;
1201
1202 if (isRetryableWrite || inTxnOrTxnCommand) {
1203 serverSession.txnNumber += session.txnNumberIncrement;
1204 session.txnNumberIncrement = 0;
1205 // TODO(NODE-2674): Preserve int64 sent from MongoDB
1206 command.txnNumber = Long.fromNumber(serverSession.txnNumber);
1207 }
1208
1209 if (!inTxnOrTxnCommand) {
1210 if (session.transaction.state !== TxnState.NO_TRANSACTION) {
1211 session.transaction.transition(TxnState.NO_TRANSACTION);
1212 }
1213
1214 if (
1215 session.supports.causalConsistency &&
1216 session.operationTime &&
1217 commandSupportsAfterClusterTime(command)
1218 ) {
1219 command.readConcern = command.readConcern || {};
1220 Object.assign(command.readConcern, { afterClusterTime: session.operationTime });
1221 } else if (session.snapshotEnabled) {
1222 command.readConcern = command.readConcern || { level: ReadConcernLevel.snapshot };
1223 if (session.snapshotTime != null) {
1224 Object.assign(command.readConcern, { atClusterTime: session.snapshotTime });
1225 }
1226 }
1227
1228 return;
1229 }

Callers 2

prepareCommandMethod · 0.90
sessions.test.tsFile · 0.85

Calls 5

processTimeMSFunction · 0.90
isTransactionCommandFunction · 0.90
inTransactionMethod · 0.80
transitionMethod · 0.80

Tested by

no test coverage detected