create_collection.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const defineAspects = require('./operation').defineAspects;
  4. const CommandOperation = require('./command');
  5. const applyWriteConcern = require('../utils').applyWriteConcern;
  6. const handleCallback = require('../utils').handleCallback;
  7. const loadCollection = require('../dynamic_loaders').loadCollection;
  8. const MongoError = require('../core').MongoError;
  9. const ReadPreference = require('../core').ReadPreference;
  10. // Filter out any write concern options
  11. const illegalCommandFields = [
  12. 'w',
  13. 'wtimeout',
  14. 'j',
  15. 'fsync',
  16. 'autoIndexId',
  17. 'strict',
  18. 'serializeFunctions',
  19. 'pkFactory',
  20. 'raw',
  21. 'readPreference',
  22. 'session',
  23. 'readConcern',
  24. 'writeConcern'
  25. ];
  26. class CreateCollectionOperation extends CommandOperation {
  27. constructor(db, name, options) {
  28. super(db, options);
  29. this.name = name;
  30. }
  31. _buildCommand() {
  32. const name = this.name;
  33. const options = this.options;
  34. // Create collection command
  35. const cmd = { create: name };
  36. // Add all optional parameters
  37. for (let n in options) {
  38. if (
  39. options[n] != null &&
  40. typeof options[n] !== 'function' &&
  41. illegalCommandFields.indexOf(n) === -1
  42. ) {
  43. cmd[n] = options[n];
  44. }
  45. }
  46. return cmd;
  47. }
  48. execute(callback) {
  49. const db = this.db;
  50. const name = this.name;
  51. const options = this.options;
  52. let Collection = loadCollection();
  53. // Did the user destroy the topology
  54. if (db.serverConfig && db.serverConfig.isDestroyed()) {
  55. return callback(new MongoError('topology was destroyed'));
  56. }
  57. let listCollectionOptions = Object.assign({}, options, { nameOnly: true });
  58. listCollectionOptions = applyWriteConcern(listCollectionOptions, { db }, listCollectionOptions);
  59. // Check if we have the name
  60. db.listCollections({ name }, listCollectionOptions)
  61. .setReadPreference(ReadPreference.PRIMARY)
  62. .toArray((err, collections) => {
  63. if (err != null) return handleCallback(callback, err, null);
  64. if (collections.length > 0 && listCollectionOptions.strict) {
  65. return handleCallback(
  66. callback,
  67. MongoError.create({
  68. message: `Collection ${name} already exists. Currently in strict mode.`,
  69. driver: true
  70. }),
  71. null
  72. );
  73. } else if (collections.length > 0) {
  74. try {
  75. return handleCallback(
  76. callback,
  77. null,
  78. new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
  79. );
  80. } catch (err) {
  81. return handleCallback(callback, err);
  82. }
  83. }
  84. // Execute command
  85. super.execute(err => {
  86. if (err) return handleCallback(callback, err);
  87. try {
  88. return handleCallback(
  89. callback,
  90. null,
  91. new Collection(db, db.s.topology, db.databaseName, name, db.s.pkFactory, options)
  92. );
  93. } catch (err) {
  94. return handleCallback(callback, err);
  95. }
  96. });
  97. });
  98. }
  99. }
  100. defineAspects(CreateCollectionOperation, Aspect.WRITE_OPERATION);
  101. module.exports = CreateCollectionOperation;