eval.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EvalOperation = void 0;
  4. const bson_1 = require("../bson");
  5. const error_1 = require("../error");
  6. const read_preference_1 = require("../read_preference");
  7. const command_1 = require("./command");
  8. /** @internal */
  9. class EvalOperation extends command_1.CommandOperation {
  10. constructor(db, code, parameters, options) {
  11. super(db, options);
  12. this.options = options !== null && options !== void 0 ? options : {};
  13. this.code = code;
  14. this.parameters = parameters;
  15. // force primary read preference
  16. Object.defineProperty(this, 'readPreference', {
  17. value: read_preference_1.ReadPreference.primary,
  18. configurable: false,
  19. writable: false
  20. });
  21. }
  22. execute(server, session, callback) {
  23. let finalCode = this.code;
  24. let finalParameters = [];
  25. // If not a code object translate to one
  26. if (!(finalCode && finalCode._bsontype === 'Code')) {
  27. finalCode = new bson_1.Code(finalCode);
  28. }
  29. // Ensure the parameters are correct
  30. if (this.parameters != null && typeof this.parameters !== 'function') {
  31. finalParameters = Array.isArray(this.parameters) ? this.parameters : [this.parameters];
  32. }
  33. // Create execution selector
  34. const cmd = { $eval: finalCode, args: finalParameters };
  35. // Check if the nolock parameter is passed in
  36. if (this.options.nolock) {
  37. cmd.nolock = this.options.nolock;
  38. }
  39. // Execute the command
  40. super.executeCommand(server, session, cmd, (err, result) => {
  41. if (err)
  42. return callback(err);
  43. if (result && result.ok === 1) {
  44. return callback(undefined, result.retval);
  45. }
  46. if (result) {
  47. callback(new error_1.MongoServerError({ message: `eval failed: ${result.errmsg}` }));
  48. return;
  49. }
  50. callback(err, result);
  51. });
  52. }
  53. }
  54. exports.EvalOperation = EvalOperation;
  55. //# sourceMappingURL=eval.js.map