collections.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const OperationBase = require('./operation').OperationBase;
  3. const handleCallback = require('../utils').handleCallback;
  4. let collection;
  5. function loadCollection() {
  6. if (!collection) {
  7. collection = require('../collection');
  8. }
  9. return collection;
  10. }
  11. class CollectionsOperation extends OperationBase {
  12. constructor(db, options) {
  13. super(options);
  14. this.db = db;
  15. }
  16. execute(callback) {
  17. const db = this.db;
  18. let options = this.options;
  19. let Collection = loadCollection();
  20. options = Object.assign({}, options, { nameOnly: true });
  21. // Let's get the collection names
  22. db.listCollections({}, options).toArray((err, documents) => {
  23. if (err != null) return handleCallback(callback, err, null);
  24. // Filter collections removing any illegal ones
  25. documents = documents.filter(doc => {
  26. return doc.name.indexOf('$') === -1;
  27. });
  28. // Return the collection objects
  29. handleCallback(
  30. callback,
  31. null,
  32. documents.map(d => {
  33. return new Collection(
  34. db,
  35. db.s.topology,
  36. db.databaseName,
  37. d.name,
  38. db.s.pkFactory,
  39. db.s.options
  40. );
  41. })
  42. );
  43. });
  44. }
  45. }
  46. module.exports = CollectionsOperation;