PouchDB – Database Info

PouchDB – Database Info ”; Previous Next You can get the basic information about the database using the method named info() Syntax Following is the syntax of using the info() method of PouchDB. This method accepts a callback function. db.info([callback]) Example Following is an example of retrieving database information using the info() method. Here, we are displaying the information of the database named my_database. In case of error, the error will be displayed on the console. //Requiring the package var PouchDB = require(”PouchDB”); //Creating the database object var db = new PouchDB(”my_database”); //Database information db.info(function(err, info) { if (err) { return console.log(err); } else { console.log(info); } }); Save the above code in a file with the name Database_info.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples>node Database_info.js This will display the info of the specified database as follows. { doc_count: 0, update_seq: 0, backend_adapter: ”LevelDOWN”, db_name: ”my_database”, auto_compaction: false, adapter: ”leveldb” } Remote Database Info In the same way, you get the information of a database that is saved remotely on the server (CouchDB). To do so, instead of database name, you need to pass the path to the required database in CouchDB. Example Following is an example of retrieving information of a database that is saved in the CouchDB server. This code gives you information of a database named my_database. //Requiring the package var PouchDB = require(”PouchDB”); //Creating the database object var db = new PouchDB(”http://localhost:5984/my_database”); //Database information db.info(function(err, info) { if (err) { return console.log(err); } else { console.log(info); } }); Save the above code in a file with the name Database_ Remote_info.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples>node Database_Remote_info.js This will display the info of the specified database as follows. { db_name: ”my_database”, doc_count: 0, doc_del_count: 0, update_seq: 0, purge_seq: 0, compact_running: false, disk_size: 79, data_size: 0, instance_start_time: ”1458209191708486”, disk_format_version: 6, committed_update_seq: 0, host: ”http://localhost:5984/my_database/”, auto_compaction: false, adapter: ”http” } Print Page Previous Next Advertisements ”;

PouchDB – Create Document

PouchDB – Create Document ”; Previous Next You can create a document in PouchDB using the db.put() method. Syntax Following is the syntax of using the db.put() method of PouchDB. You can store the document that is to be created in PouchDB, in a variable and pass as a parameter to this method. In addition, this method also accepts a callback (optional) function as a parameter. db.put(document, callback) Example Following is an example of creating a document in PouchDB using the put() method. The document we create should be of JSON format, a set of key-value pairs separated by comma (,) and enclosed within curly braces ({}). //Requiring the package var PouchDB = require(”PouchDB”); //Creating the database object var db = new PouchDB(”my_database”); //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”); } }); Save the above code in a file with name Create_Document.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples >node Create_Document.js This creates the given document in PouchDB database named my_database, which is stored locally, displaying the following message. Document created Successfully Inserting a Document in a Remote Database You can also insert a document in the database that is stored remotely on the server (CouchDB). To do so, instead of database name you need to pass the path to the database where you want to create documents in CouchDB. Example Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot. Now, if you click on the database named my_database, you will find an empty database as shown in the following screenshot. Following is an example of inserting a document in a database named my_database that is saved in the CouchDB server. //Requiring the package var PouchDB = require(”PouchDB”); //Creating the database object var db = new PouchDB(”http://localhost:5984/my_database”); //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”); } }); Save the above code in a file with the name Remote_Create_Document.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples >node Remote_Create_Document.js This creates the given document in PouchDB database named my_database which is stored in CouchDB, displaying the following message. Document created Successfully Verification After executing the above program, if you visit the my_database again, you can observe the document created as shown in the following screenshot. Print Page Previous Next Advertisements ”;

PouchDB – Delete Database

PouchDB – Delete Database ”; Previous Next You can delete a database in PouchDB using the db.destroy() method. Syntax Following is the syntax of using the db.destroy() method. This method accepts a callback function as a parameter. db.destroy() Example Following is an example of deleting a database in PouchDB using the destroy() method. Here, we are deleting the database named my_database, created in the previous chapters. //Requiring the package var PouchDB = require(”PouchDB”); //Creating the database object var db = new PouchDB(”my_database”); //deleting database db.destroy(function (err, response) { if (err) { return console.log(err); } else { console.log (“Database Deleted”); } }); Save the above code in a file with the name Delete_Database.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples >node Delete_Database.js This will delete the database named my_database which is stored locally displaying the following message. Database Deleted Deleting a Remote Database In the same way, you can delete a database that is stored remotely on the server (CouchDB). To do so, instead of a database name, you need to pass the path to the database that is required to be deleted, in CouchDB. Example Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot. Following is an example of deleting a database named my_database that is saved in the CouchDB server. //Requiring the package var PouchDB = require(”pouchdb”); //Creating the database object var db = new PouchDB(”http://localhost:5984/my_database”); //deleting database db.destroy(function (err, response) { if (err) { return console.log(err); } else { console.log(“Database Deleted”); } }); Save the above code in a file with the name Remote_Database_Delete.js. Open the command prompt and execute the JavaScript file using node as shown below. C:PouchDB_Examples >Remote_Database_Delete.js This deletes the specified database from PouchDB displaying the following message. Database Deleted Verification After executing the above program, if you visit the URL again, you will get the following screenshot. Here you can observe only two databases since my_database was deleted. Print Page Previous Next Advertisements ”;