Skip to content

Create database if does not exist #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ function createApp (doc, url, cb) {
doc._attachments = copy(app.doc._attachments)
delete doc.__attachments;
var body = JSON.stringify(doc)
console.log('PUT '+url.replace(/^(https?:\/\/[^@:]+):[^@]+@/, '$1:******@'))
request({uri:url, method:'PUT', body:body, headers:h}, function (err, resp, body) {
console.log('PUT '+design.replace(/^(https?:\/\/[^@:]+):[^@]+@/, '$1:******@'))
request({uri:design, method:'PUT', body:body, headers:h}, function (err, resp, body) {
if (err) throw err;
if (resp.statusCode !== 201) throw new Error("Could not push document\n"+body)
app.doc._rev = JSON.parse(body).rev
console.log('Finished push. '+app.doc._rev)
playSound();
request({uri:url, headers:h}, function (err, resp, body) {
request({uri:design, headers:h}, function (err, resp, body) {
body = JSON.parse(body);
app.doc._attachments = body._attachments;
if (callback) callback()
Expand Down Expand Up @@ -208,16 +208,23 @@ function createApp (doc, url, cb) {
})
}

var _id = doc.app ? doc.app._id : doc._id
// Get current couchapp design document
var design = url +'/'+ doc._id;
request({uri: design, headers:h}, function (err, resp, body) {

if (url.slice(url.length - _id.length) !== _id) url += '/' + _id;

request({uri:url, headers:h}, function (err, resp, body) {
if (err) throw err;
if (resp.statusCode == 404) app.current = {};
if (resp.statusCode == 404) {
// Create database if does not exist
request({method:'PUT', uri:url}, function(){
app.current = {};
cb(app);
})
}
else if (resp.statusCode !== 200) throw new Error("Failed to get doc\n"+body)
else app.current = JSON.parse(body)
cb(app)
else {
app.current = JSON.parse(body);
cb(app);
}
})
}

Expand Down