var saveDoc = function(doc) { var deferred = q.defer(); db.get(doc._id).then(function(old_doc) { doc._rev = old_doc._rev; return db.put(doc); }).then(function(response) { // handle response //console.log('update response: ' + JSON.stringify(response)); deferred.resolve(response); }).catch(function (err) { if (err.status === 404) { db.put(doc).then(function(response){ //console.log('new doc response: ' + JSON.stringify(response)); deferred.resolve(response); }).catch(function (err) { //console.log('new doc error: ' + JSON.stringify(err)); err.step = 'create'; deferred.reject(err); }); } else { //console.log('update error:' + JSON.stringify(err)); err.step = 'update'; deferred.reject(err); } }); return deferred.promise; };
handler: function(req, reply) { var authHeader = req.headers.authorization.split(' '); var signature = authHeader[0]; var auth = new Buffer(authHeader[1], 'base64').toString('ascii'); var check = hash('secretgoeshere' + check); console.log(signature, check, signature === check); var user = JSON.parse(auth).user; user.type = 'user'; var id = user.id; delete user.id; db.allDocs({include_docs: true}, function(err, res) { }); db.get(id, function(err, doc) { if(doc) { console.log(doc); db.put(user, id, doc._rev, function(err, res) { if(err) throw err; }); } else { db.put(user, id, function(err, res) { if(err) throw err; }); } db.query({map: threadPosts}, {include_docs: true}, function(err, res) { res.rows reply(res.rows); }); }); }
it('saves changes in pouchdb', function(done) { db.get('a', function(err) { expect(err).to.be.an.object(); expect(err.message).to.equal('missing'); done(); }); });
app.post('/addVocab', function(req, res){ //add to database vocabDB.get(req.body.word.toLowerCase()).then(function(doc) { console.log("doc", doc); }).then(function(response) { // handle response }).catch(function (err) { vocabDB.put({ _id: req.body.word.toLowerCase(), word: req.body.word, definition: req.body.definition, example: req.body.example, category: [req.body.category] }).then(function (response) { doc.message = 'Success!!'; // handle response }).catch(function (err) { console.log(err); doc.message = 'Error!!'; }); }); // add success or error message to view... res.redirect('admin'); })
var docs = db.allDocs(function(err, response) { val = response.total_rows; console.log("val response total rows", val); var details = ""; var cloudantDocId=[]; j=0; for(i=0; i < val; i++) { db.get(response.rows[i].id, function (err,doc){ j++; details= details + JSON.stringify(doc.ImageID) + " -- " + JSON.stringify(doc.Title) + "\n"; // Kludge because of Node.js asynchronous handling. To be fixed - T V Ganesh if(j == val) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.write("The content of the cloudant DB - is displayed \n"); res.write(details); res.end(); console.log("details=",details); } }); // End db.get } //End for }); // End db.allDocs
app.get('/list', function(req, res){ if (process.env.VCAP_SERVICES) { // Running on Bluemix. Parse out the port and host that we've been assigned. var env = JSON.parse(process.env.VCAP_SERVICES); var host = process.env.VCAP_APP_HOST; var port = process.env.VCAP_APP_PORT; console.log('VCAP_SERVICES: %s', process.env.VCAP_SERVICES); // Also parse out Cloudant settings. cloudant = env['cloudantNoSQLDB'][0]['credentials']; } var db = new pouchdb('meeting'), remote =cloudant.url + '/meeting'; opts = { continuous: true }; console.log(remote); db.replicate.to(remote, opts); db.replicate.from(remote, opts); var docs = db.allDocs(function(err, result){ console.log("result" + result); res.write(result.toString()); }); db.get('meetingRoom', function(err, response){ console.log("response" + response); // var details = JSON.stringify(doc.data); // res.write(details); // res.end(); // console.log(details); }); });
it('making changes in pouchdb...', function(done) { db.get('b', function(err, doc) { expect(err).to.equal(null); doc.text = 'wash some of the dishes'; db.put(doc, done); }); });
var docs = db.allDocs(function(err, response) { val = response.total_rows; var details = ""; j=0; var booklist = new Array(val); console.log(response); for(i=0; i < val; i++) { //booklist[i] = new Array(2); db.get(response.rows[i].id, function (err,doc){ j++; booklist[j] = new Array(2); booklist[j][0] = doc.Title; booklist[j][1] = doc.author; details= details + JSON.stringify(doc.Title) + " " + JSON.stringify(doc.author) + "\n"; // Kludge because of Node.js asynchronous handling. To be fixed - T V Ganesh if(j == val) { res.render('userlist', { "userlist" : booklist }); console.log("Details = " + details); console.log("2" + booklist); } }); // End db.get } //End for }); // End db.allDocs
.get(function(req, res) { db.get(req.params.bear_id, function(err, bear) { if(err) res.send(err); res.json(bear); }); })
}).then(function (info) { info.rows.should.have.length(0); return db.get('_local/godzilla').then(function () { throw new Error('expected 404'); }, function (err) { should.exist(err); }); });
db.get(book).then(function (doc) { refDb.get('refChunks').then(function (chunkDoc) { console.log(doc.chapters[parseInt(chapter,10)-1].verses.length); currentBook = doc; createRefSelections(); createVerseInputs(doc.chapters[parseInt(chapter,10)-1].verses, chunkDoc.chunks[parseInt(book,10)-1], chapter); }); }).catch(function (err) {
const updateTask = task => { return db.get(task._id) .then(res => { return db.put(task) }).then(res => { return res }).catch(err => console.log(err)) }
async function requestPersist (repoName, hookId, type) { if (type === 'add') { const doc = await db.get(username) doc.repos.push({ name: repoName, hook: hookId }) db.put(doc) } else { const doc = await db.get(username) const matching = doc.repos.map(repo => repo.name).indexOf(repoName) if (matching > -1) { deleteHook(repoName, doc.repos[matching].hook) doc.repos.splice(matching, 1) } db.put(doc) } }
test('reopen', function (t) { t.plan(1); var dbName = 'one'; var db = new PouchDB(dbName, {db: memdown}); db.crypto('password'); db.get('baz').then(function (resp) { t.equals(resp.foo, 'bar', 'decrypts data'); }); });
user.remove(function (err) { (err === null).should.be.true; pouch.get(user.primary(), function (err, doc) { (doc === undefined).should.be.true; err.should.be.ok; err.should.have.property('message', 'deleted'); done(); }); });
return remote.bulkDocs(docs).then(function (response) { assert.equal(response.length, docCount, response); response.forEach(function (row) { assert(!row.error); }); // ensure we can retrieve what we inserted return remote.get(docs[0]._id); }).then(function (doc) {
.then(function(result) { sessionKey = result.token; sessionPass = result.password; firstExpires = result.expires; expect(sessionKey).to.be.a('string'); expect(result.userDBs.usertest).to.equal('https://' + sessionKey + ':' + sessionPass + '@' + 'mydb.example.com/test_usertest$superuser'); return(userDB.get(testUserForm.username)); })
const removeTask = task => { return db.get(task._id) .then(doc => { return db.remove(doc) }).then(res => { return res }).catch(err => { console.log(err) }) }
user.save(function (err, res) { user.primary().should.be.ok; res.should.be.ok; pouch.get(user.primary(), function (err, doc) { doc.should.be.ok; doc.should.have.property('name', 'bar'); doc.should.have.property('nick', 'baz'); done(); }); });
complete: function(){ db.get(id, function(err, doc){ if(err) { if(err.status === 404) return initializeRecord(); return console.error(err) } emitter.emit('db-ready') }) }
function create(episodeNum) { var key = 'episode-' + episodeNum; return db.get(key) .catch(function(error) { // console.log('--expected error'); return { _id: key } }) }
wordoftheday.get('wordoftheday').then(function(doc) { vocabDB.get(doc.word_id).then(function(d){ result.wordoftheday = d; result.view = function(){ return 'main'; } result.title = 'DreamAction Vocab - รวมคำศัพท์ทางสถาปัตย์เพื่อสถาปนิก'; res.render('index', result); }) }).catch(console.log.bind(console));
app.get('/term/:id', function(req, res){ vocabDB.get(req.params.id).then(function(doc){ doc.view = function(){ return 'vocab'; } doc.title = `ความหมายของ ${req.params.id} - Dream Action`; res.render('index', doc); }) })
}).then(function (v2) { v2.v.should.equal(2); return db.get('doc', {rev: revs[0]}).then(function () { // the v1 revision is not in the db anymore done(new Error('v1 should be missing')); }).catch(function (error) { error.message.should.equal('missing'); done(); }); }).catch(done);
.delete(function(req, res) { db.get(req.params.bear_id, function(err, doc) { db.remove(doc, function(err, bear) { if(err) res.send(err); res.json({ message: 'Successfully deleted' }); }); }); });
sync.once('change', function() { serverDB.get('A', function(err, doc) { expect(err).to.equal(null); expect(doc).to.deep.equal({ _id: 'A', a: 1, b: 2, _rev: reply.rev, }); done(); }); });
db.bulkDocs({docs: docs, new_edits: false}, function () { db.get('foo', {open_revs: 'all'}, function (err, res) { res.sort(function (a, b) { return a.ok._rev < b.ok._rev ? -1 : a.ok._rev > b.ok._rev ? 1 : 0; }); res.length.should.equal(2); res[0].ok._rev.should.equal('2-x', 'doc1 ok'); res[1].ok._rev.should.equal('2-y', 'doc2 ok'); done(); }); });
var getDoc = function(_id) { var deferred = q.defer(); db.get(_id).then(function (doc) { // handle doc //console.log(JSON.stringify(doc)); deferred.resolve(doc); }).catch(function (err) { //console.log('getDoc error: ' + JSON.stringify(err)); deferred.reject(err); }); return deferred.promise; };
function getPendingTxs(callback){ db.get('pendingTxs', function(err, doc){ if(err) { if(err.status === 404) { return callback(null, []) } return callback(err); } callback(null, doc.txs) }) }
exports.list = function(req, res) { //Parse the process.env for the port and host that we've been assigned if (process.env.VCAP_SERVICES) { // Running on Bluemix. Parse the port and host that we've been assigned. var env = JSON.parse(process.env.VCAP_SERVICES); var host = process.env.VCAP_APP_HOST; var port = process.env.VCAP_APP_PORT; console.log('VCAP_SERVICES: %s', process.env.VCAP_SERVICES); // Also parse Cloudant settings. var cloudant = env['cloudantNoSQLDB'][0]['credentials']; } var db = new pouchdb('books'), remote =cloudant.url + '/books'; opts = { continuous: true }; // Replicate the DB to remote console.log(remote); db.replicate.to(remote, opts); db.replicate.from(remote, opts); // Get our form values. These rely on the "name" attributes var DocId = req.body.docid; var Title = req.body.title; var Author = req.body.author; // Update book3 db.get(DocId, function(err, response) { console.log("*******************value= " + response._id + " " + response._rev + " " + Author + " " + Title); db.put({ _id: DocId, _rev: response._rev, author: Author, Title : Title, }, function(err, response) { if (err) { // If it failed, return error res.send("There was a problem updating the information to the database."); } else { // If it worked, redirect to display books res.location("booklist"); // And forward to success page res.redirect("booklist"); } }); }); };