From 0af96636187d96fd2ceea3bbe22f0964a7026504 Mon Sep 17 00:00:00 2001 From: missprogrammer Date: Tue, 31 Jan 2017 06:17:23 +0330 Subject: [PATCH 1/2] Added support for the creation of views (db.createView) --- lib/db.js | 55 +++++++++++++++++++++++++++++++++++++ test/functional/db_tests.js | 25 +++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/lib/db.js b/lib/db.js index 3d10dc3f6b0..4b900eaa55b 100644 --- a/lib/db.js +++ b/lib/db.js @@ -594,6 +594,61 @@ Db.prototype.createCollection = function(name, options, callback) { define.classMethod('createCollection', {callback: true, promise:true}); +/** + * Create view on MongoDB 3.4 or higher + * + * @method + * @param {string} name Name of view + * @param {string} viewOn The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create on MongoDB 3.4 or higher. + * @param {array} pipeline An array that consists of the aggregation pipeline stage. create creates the view by applying the specified pipeline to the viewOn collection or view on MongoDB 3.4 or higher. + * @param {object} [options=null] Optional settings. + * @param {Db~resultCallback} [callback] The results callback + * @return {Promise} returns Promise if no callback passed + */ +Db.prototype.createView = function(name, viewOn, pipeline, options, callback) { + var self = this; + if(typeof pipeline == 'function') callback = pipeline, pipeline = []; + if(typeof options == 'function') callback = options, options = {}; + options = options || {}; + + // Create view command + var cmd = {'create': name}; + + if (viewOn == undefined) { + throw Error("Must specify a backing view or collection"); + } + + if (pipeline != undefined) { + if (!Array.isArray(pipeline)) { + pipeline = [pipeline]; + } + } + + cmd.pipeline = pipeline; + cmd.viewOn = viewOn; + + // Check if the callback is in fact a string + if(typeof callback == 'function') return this.command(cmd, options, function(err, result) { + // Did the user destroy the topology + if(callback == null) return; + if(err) return handleCallback(callback, err, null); + handleCallback(callback, null, result.ok ? true : false); + }); + + // Execute the command + return new this.s.promiseLibrary(function(resolve, reject) { + // Execute command + self.command(cmd, options, function(err, result) { + // Did the user destroy the topology + if(err) return reject(err); + if(result.ok) return resolve(true); + resolve(false); + }); + }); +} + +define.classMethod('createView', {callback: true, promise:true}); + /** * Get all the db statistics. * diff --git a/test/functional/db_tests.js b/test/functional/db_tests.js index 0bf9029d66a..e843c717351 100644 --- a/test/functional/db_tests.js +++ b/test/functional/db_tests.js @@ -539,3 +539,28 @@ exports['should correctly execute close function in order'] = { }); } } + +/** + * @ignore + */ +exports.shouldCorrectlyGetErrorCreatingExistingView = { + metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } }, + + // The actual test we wish to run + test: function(configuration, test) { + var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1, auto_reconnect:false}); + + // Establish connection to db + db.open(function(err, db) { + var _db = db.db("nonexistingdb"); + // Let's create the view + _db.createView('test_create_view', 'test_resave_dbref', function(err, result) { + test.equal(null, err); + test.equal(true, result); + + db.close(); + test.done(); + }); + }); + } +} From ca429d4d91ea0c6eeec34c8ccd07b27093e97528 Mon Sep 17 00:00:00 2001 From: missprogrammer Date: Fri, 3 Feb 2017 01:18:32 +0330 Subject: [PATCH 2/2] Fixed typo string to function Callback should be a function to enter this scope. so it's can't be null --- lib/db.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/db.js b/lib/db.js index 4b900eaa55b..3e1fd1382d5 100644 --- a/lib/db.js +++ b/lib/db.js @@ -627,10 +627,9 @@ Db.prototype.createView = function(name, viewOn, pipeline, options, callback) { cmd.pipeline = pipeline; cmd.viewOn = viewOn; - // Check if the callback is in fact a string + // Check if the callback is in fact a function if(typeof callback == 'function') return this.command(cmd, options, function(err, result) { // Did the user destroy the topology - if(callback == null) return; if(err) return handleCallback(callback, err, null); handleCallback(callback, null, result.ok ? true : false); });