blob: 29d7c8c237b4cbe5080d25a8dc1d614f99bcc51e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Delete a view from design document in Cloudant database:
* https://docs.cloudant.com/design_documents.html
**/
var DESIGN_PREFIX = '_design/';
function main(message) {
var cloudantOrError = getCloudantAccount(message);
if (typeof cloudantOrError !== 'object') {
return Promise.reject(cloudantOrError);
}
var cloudant = cloudantOrError;
var dbName = message.dbname;
var docId = message.docid;
var viewName = message.viewname;
var params = {};
if(!dbName) {
return Promise.reject('dbname is required.');
}
if(!docId) {
return Promise.reject('docid is required.');
}
var cloudantDb = cloudant.use(dbName);
if(!viewName) {
return Promise.reject('viewname is required.');
}
if (typeof message.params === 'object') {
params = message.params;
} else if (typeof message.params === 'string') {
try {
params = JSON.parse(message.params);
} catch (e) {
return Promise.reject('params field cannot be parsed. Ensure it is valid JSON.');
}
}
return deleteViewFromDesignDoc(cloudantDb, docId, viewName, params);
}
function deleteViewFromDesignDoc(cloudantDb, docId, viewName, params) {
//Check that doc id contains _design prefix
if (docId.indexOf(DESIGN_PREFIX) !== 0) {
docId = DESIGN_PREFIX + docId;
}
return getDocument(cloudantDb, docId)
.then(function(document) {
console.log("Got document: " + document);
delete document.views[viewName];
//Update the design document after removing the view
return insert(cloudantDb, document, params);
});
}
function getDocument(cloudantDb, docId) {
return new Promise(function(resolve, reject) {
cloudantDb.get(docId, function(error, response) {
if (!error) {
console.log("Got response: " + response);
resolve(response);
} else {
console.log("Got error: " + error);
reject(error);
}
});
});
}
function insert(cloudantDb, doc, params) {
return new Promise(function(resolve, reject) {
cloudantDb.insert(doc, params, function(error, response) {
if (!error) {
console.log('success', response);
resolve(response);
} else {
console.log('error', error);
reject(error);
}
});
});
}
function getCloudantAccount(message) {
// full cloudant URL - Cloudant NPM package has issues creating valid URLs
// when the username contains dashes (common in Bluemix scenarios)
var cloudantUrl;
if (message.url) {
// use bluemix binding
cloudantUrl = message.url;
} else {
if (!message.host) {
return 'cloudant account host is required.';
}
if (!message.username) {
return 'cloudant account username is required.';
}
if (!message.password) {
return 'cloudant account password is required.';
}
cloudantUrl = "https://" + message.username + ":" + message.password + "@" + message.host;
}
return require('cloudant')({
url: cloudantUrl
});
}