drop.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const CommandOperation = require('./command');
  4. const defineAspects = require('./operation').defineAspects;
  5. const handleCallback = require('../utils').handleCallback;
  6. class DropOperation extends CommandOperation {
  7. constructor(db, options) {
  8. const finalOptions = Object.assign({}, options, db.s.options);
  9. if (options.session) {
  10. finalOptions.session = options.session;
  11. }
  12. super(db, finalOptions);
  13. }
  14. execute(callback) {
  15. super.execute((err, result) => {
  16. if (err) return handleCallback(callback, err);
  17. if (result.ok) return handleCallback(callback, null, true);
  18. handleCallback(callback, null, false);
  19. });
  20. }
  21. }
  22. defineAspects(DropOperation, Aspect.WRITE_OPERATION);
  23. class DropCollectionOperation extends DropOperation {
  24. constructor(db, name, options) {
  25. super(db, options);
  26. this.name = name;
  27. this.namespace = `${db.namespace}.${name}`;
  28. }
  29. _buildCommand() {
  30. return { drop: this.name };
  31. }
  32. }
  33. class DropDatabaseOperation extends DropOperation {
  34. _buildCommand() {
  35. return { dropDatabase: 1 };
  36. }
  37. }
  38. module.exports = {
  39. DropOperation,
  40. DropCollectionOperation,
  41. DropDatabaseOperation
  42. };