Pouchdb 简明教程

PouchDB - Delete Database

您可以使用 db.destroy() 方法删除 PouchDB 中的数据库。

You can delete a database in PouchDB using the db.destroy() method.

Syntax

以下为 db.destroy() 方法的语法。此方法接受一个回调函数作为参数。

Following is the syntax of using the db.destroy() method. This method accepts a callback function as a parameter.

db.destroy()

Example

以下是使用 destroy() 方法删除 PouchDB 中数据库的示例。在此,我们删除了名为 my_database 的数据库,已在前一章节中创建。

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”);
   }
});

将以上代码保存在名为 Delete_Database.js. 的文件中。打开命令提示符并使用 node 来执行 JavaScript 文件,如下所示。

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

这会删除名为 my_database 并存储在本地数据库中的数据库,并显示以下消息。

This will delete the database named my_database which is stored locally displaying the following message.

Database Deleted

Deleting a Remote Database

同样的,您可以删除存储在服务器(CouchDB)远端上的数据库。

In the same way, you can delete a database that is stored remotely on the server (CouchDB).

做法是:使用 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

假设 CouchDB 服务器中有一个名为 my_database 的数据库。然后,如果您使用 URL http://127.0.0.1:5984/_utils/index.html 验证 CouchDB 中的数据库列表,您将获得以下屏幕截图。

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.

deleting remote database

以下是删除一个存储在 CouchDB 服务器中名为 my_database 的数据库的示例。

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");
   }
});

将以上代码保存在名为 Remote_Database_Delete.js 的文件中。打开命令提示符并使用 node 来执行 JavaScript 文件,如下所示。

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

这会从 PouchDB 中删除指定数据库,并显示以下消息。

This deletes the specified database from PouchDB displaying the following message.

Database Deleted

Verification

执行上述程序后,如果您再次打开 URL,您会看到以下截图。在此,您可以看到只有两个数据库,因为 my_database 已被删除。

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.

delete database verification