Skip to content

Instantly share code, notes, and snippets.

@htkcodes
Last active March 21, 2018 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htkcodes/3813ca5388daabe08dfed1da56b1d729 to your computer and use it in GitHub Desktop.
Save htkcodes/3813ca5388daabe08dfed1da56b1d729 to your computer and use it in GitHub Desktop.
PouchDB CRUD COMMANDS
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//Database information
//Preparing the document
doc = {
_id : '001',
name: 'Raju',
age : 23,
designation : 'Designer'
}
//Inserting Document
db.put(doc, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Document created Successfully");
}
});
//Reading the contents of a document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
//deleting database
db.destroy(function (err, response) {
if (err) {
return console.log(err);
} else {
console.log("Database Deleted");
}
});
//Preparing the document for update
doc = {
_ic:'<id here>',
age: 26,
_rev: '<rev number here>',
}
//Inserting Document
db.put(doc);
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
//Deleting an existing document
db.remove('<id>', '<rev number>', function(err) {
if (err) {
return console.log(err);
} else {
console.log("Document deleted successfully");
}
});
//BULK ADDITION
//Preparing the documents array
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}
docs = [doc1, doc2, doc3]
//Inserting Documents
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Documents created Successfully");
}
});
//Retrieve all documents
//Retrieving all the documents in PouchDB
db.allDocs(function(err, docs) {
if (err) {
return console.log(err);
} else {
console.log (docs.rows);
}
});
//---UPDATE BULK---
//Preparing the document
docs = [{_id : '<id>', _rev: '<rev id>', age : 25, },
{_id : '002', _rev: '1-b5e49db7e984841bf12a13e3ee548125', age : 26, },
{_id : '003', _rev: '1-a7b342786ecc707aa91f3b321a177b51', age : 27 }]
//Updating the documents in bulk
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Documents Updated Successfully");
}
});
//DELETION BULK
//Preparing the document
docs = [{_id : '001', _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d', _deleted : true },
{_id : '002', _rev: '2-43966007568ce9567c96422195fcfa0d', _deleted : true },
{_id : '003', _rev: '2-6c5349652527f4f39583ff14f23cd677',_deleted : true }]
//Deleting Documents
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log(response+"Documents deleted Successfully");
}
});
//---ATTACHMENT----
//Preparing the attachment
var my_attachment = new Buffer(['Welcome to tutorialspoint'], {type: 'text/plain'});
//Adding attachment to a document
db.putAttachment('001', 'att_1.txt', my_attachment, 'text/plain', function(err, res) {
if (err) {
return console.log(err);
} else {
console.log(res+"Attachment added successfully")
}
});
//Reading Attachment
//Reading the Document
db.get('001',{attachments: true}, function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
if (err) {
return console.log(err);
} else {
console.log(blob_buffer);
}
});
//Deleting Attachment
db.removeAttachment('<id>', 'att_1.txt', '<rev id>',
function(err, res) {
if (err) {
return console.log(err);
} else {
console.log(res+"Attachment Deleted successfully")
}
});
//sync
//Creating local database object
var localDB = new PouchDB('local_database');
//Creating remote database object
var remoteDB = new PouchDB('http://localhost:5984/remote_database');
//Synchronising both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successfully");
//altenate
//Synchronising Remote and local databases
localDB.sync(remoteDB, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log(response);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment