session.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var inherits = require('util').inherits
  3. , f = require('util').format
  4. , EventEmitter = require('events').EventEmitter;
  5. /**
  6. * Creates a new Authentication Session
  7. * @class
  8. * @param {object} [options] Options for the session
  9. * @param {{Server}|{ReplSet}|{Mongos}} topology The topology instance underpinning the session
  10. */
  11. var Session = function(options, topology) {
  12. this.options = options;
  13. this.topology = topology;
  14. // Add event listener
  15. EventEmitter.call(this);
  16. }
  17. inherits(Session, EventEmitter);
  18. /**
  19. * Execute a command
  20. * @method
  21. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  22. * @param {object} cmd The command hash
  23. * @param {object} [options.readPreference] Specify read preference if command supports it
  24. * @param {object} [options.connection] Specify connection object to execute command against
  25. * @param {opResultCallback} callback A callback function
  26. */
  27. Session.prototype.command = function(ns, cmd, options, callback) {
  28. this.topology.command(ns, cmd, options, callback);
  29. }
  30. /**
  31. * Insert one or more documents
  32. * @method
  33. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  34. * @param {array} ops An array of documents to insert
  35. * @param {boolean} [options.ordered=true] Execute in order or out of order
  36. * @param {object} [options.writeConcern={}] Write concern for the operation
  37. * @param {opResultCallback} callback A callback function
  38. */
  39. Session.prototype.insert = function(ns, ops, options, callback) {
  40. this.topology.insert(ns, ops, options, callback);
  41. }
  42. /**
  43. * Perform one or more update operations
  44. * @method
  45. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  46. * @param {array} ops An array of updates
  47. * @param {boolean} [options.ordered=true] Execute in order or out of order
  48. * @param {object} [options.writeConcern={}] Write concern for the operation
  49. * @param {opResultCallback} callback A callback function
  50. */
  51. Session.prototype.update = function(ns, ops, options, callback) {
  52. this.topology.update(ns, ops, options, callback);
  53. }
  54. /**
  55. * Perform one or more remove operations
  56. * @method
  57. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  58. * @param {array} ops An array of removes
  59. * @param {boolean} [options.ordered=true] Execute in order or out of order
  60. * @param {object} [options.writeConcern={}] Write concern for the operation
  61. * @param {opResultCallback} callback A callback function
  62. */
  63. Session.prototype.remove = function(ns, ops, options, callback) {
  64. this.topology.remove(ns, ops, options, callback);
  65. }
  66. /**
  67. * Perform one or more remove operations
  68. * @method
  69. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  70. * @param {{object}|{Long}} cmd Can be either a command returning a cursor or a cursorId
  71. * @param {object} [options.batchSize=0] Batchsize for the operation
  72. * @param {array} [options.documents=[]] Initial documents list for cursor
  73. * @param {boolean} [options.tailable=false] Tailable flag set
  74. * @param {boolean} [options.oplogReply=false] oplogReply flag set
  75. * @param {boolean} [options.awaitdata=false] awaitdata flag set
  76. * @param {boolean} [options.exhaust=false] exhaust flag set
  77. * @param {boolean} [options.partial=false] partial flag set
  78. * @param {opResultCallback} callback A callback function
  79. */
  80. Session.prototype.cursor = function(ns, cmd, options) {
  81. return this.topology.cursor(ns, cmd, options);
  82. }
  83. module.exports = Session;