summaryrefslogtreecommitdiffstats
path: root/node_modules/mongodb/lib/operations/rename.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
commite06ec920f7a5d784e674c4c4b4e6d1da3dc7391d (patch)
tree55713f725f77b44ebfec86e4eec3ce33e71458ca /node_modules/mongodb/lib/operations/rename.js
downloadwebsite_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.gz
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.bz2
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.zip
api, login, auth
Diffstat (limited to 'node_modules/mongodb/lib/operations/rename.js')
-rw-r--r--node_modules/mongodb/lib/operations/rename.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/node_modules/mongodb/lib/operations/rename.js b/node_modules/mongodb/lib/operations/rename.js
new file mode 100644
index 0000000..8098fe6
--- /dev/null
+++ b/node_modules/mongodb/lib/operations/rename.js
@@ -0,0 +1,61 @@
+'use strict';
+
+const OperationBase = require('./operation').OperationBase;
+const applyWriteConcern = require('../utils').applyWriteConcern;
+const checkCollectionName = require('../utils').checkCollectionName;
+const executeDbAdminCommand = require('./db_ops').executeDbAdminCommand;
+const handleCallback = require('../utils').handleCallback;
+const loadCollection = require('../dynamic_loaders').loadCollection;
+const toError = require('../utils').toError;
+
+class RenameOperation extends OperationBase {
+ constructor(collection, newName, options) {
+ super(options);
+
+ this.collection = collection;
+ this.newName = newName;
+ }
+
+ execute(callback) {
+ const coll = this.collection;
+ const newName = this.newName;
+ const options = this.options;
+
+ let Collection = loadCollection();
+ // Check the collection name
+ checkCollectionName(newName);
+ // Build the command
+ const renameCollection = coll.namespace;
+ const toCollection = coll.s.namespace.withCollection(newName).toString();
+ const dropTarget = typeof options.dropTarget === 'boolean' ? options.dropTarget : false;
+ const cmd = { renameCollection: renameCollection, to: toCollection, dropTarget: dropTarget };
+
+ // Decorate command with writeConcern if supported
+ applyWriteConcern(cmd, { db: coll.s.db, collection: coll }, options);
+
+ // Execute against admin
+ executeDbAdminCommand(coll.s.db.admin().s.db, cmd, options, (err, doc) => {
+ if (err) return handleCallback(callback, err, null);
+ // We have an error
+ if (doc.errmsg) return handleCallback(callback, toError(doc), null);
+ try {
+ return handleCallback(
+ callback,
+ null,
+ new Collection(
+ coll.s.db,
+ coll.s.topology,
+ coll.s.namespace.db,
+ newName,
+ coll.s.pkFactory,
+ coll.s.options
+ )
+ );
+ } catch (err) {
+ return handleCallback(callback, toError(err), null);
+ }
+ });
+ }
+}
+
+module.exports = RenameOperation;