Skip to content

Commit

Permalink
refactor(NODE-5473): remove unused callback inheritance in operations…
Browse files Browse the repository at this point in the history
… layer (#3793)
  • Loading branch information
malikj2000 committed Aug 3, 2023
1 parent 42d05ae commit afad9af
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/operations/eval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Code, type Document } from '../bson';
import type { Collection } from '../collection';
import type { Db } from '../db';
import { MongoServerError } from '../error';
import { ReadPreference } from '../read_preference';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { CommandOperation, type CommandOperationOptions } from './command';

/** @public */
export interface EvalOptions extends CommandOperationOptions {
nolock?: boolean;
}

/** @internal */
export class EvalOperation extends CommandOperation<Document> {
override options: EvalOptions;
code: Code;
parameters?: Document | Document[];

constructor(
db: Db | Collection,
code: Code,
parameters?: Document | Document[],
options?: EvalOptions
) {
super(db, options);

this.options = options ?? {};
this.code = code;
this.parameters = parameters;
// force primary read preference
Object.defineProperty(this, 'readPreference', {
value: ReadPreference.primary,
configurable: false,
writable: false
});
}

override async execute(server: Server, session: ClientSession | undefined): Promise<Document> {
let finalCode = this.code;
let finalParameters: Document[] = [];

// If not a code object translate to one
if (!(finalCode && (finalCode as unknown as { _bsontype: string })._bsontype === 'Code')) {
finalCode = new Code(finalCode as never);
}

// Ensure the parameters are correct
if (this.parameters != null && typeof this.parameters !== 'function') {
finalParameters = Array.isArray(this.parameters) ? this.parameters : [this.parameters];
}

// Create execution selector
const cmd: Document = { $eval: finalCode, args: finalParameters };

// Check if the nolock parameter is passed in
if (this.options.nolock) {
cmd.nolock = this.options.nolock;
}

// Execute the command
const result = await super.executeCommand(server, session, cmd);
if (result && result.ok === 1) {
return result.retval;
}

if (result) {
throw new MongoServerError({ message: `eval failed: ${result.errmsg}` });
}

return result;
}
}

0 comments on commit afad9af

Please sign in to comment.