# CouchDB Notes
CouchDB is a database that completely embraces the web. Store your data with JSON documents. Access your documents with your web browser, via HTTP. Query, combine, and transform your documents with JavaScript.
# Eventual Consistency (opens new window)
- Consistency: All database clients see the same data, even with concurrent updates.
- Availability: All database clients are able to access some version of the data.
- Partition tolerance: The database can be split over multiple servers.
# Conflict resources
CouchDB uses an "optimistic concurrency" model. In the simplest terms, this just means that you send a document version along with your update, and CouchDB rejects the change if the current document version doesn't match what you've sent.
# Useful API
# Database
HEAD /{db}
- get infoPUT /{db}
- create databaseDELETE /{db}
- delete databaseGET /_all_dbs
- find databasesPOST /{db}/index
- create indexPUT /{db}/_revs_limit
- Sets the max no of document revisions that will be tracked by CouchDBGET /{db}/_changes
- polling changes
# Document
HEAD /{db}/${docid}
- get infoGET /{db}/${docid}
- get documentPUT /{db}/${docid}
- create / update documentDELETE /{db}/${docid}
- delete documentPOST /{db}/_bulk_docs
- upsert / delete documentsPOST /{db}/_find
- Find documents using a declarative JSON querying syntaxGET /{db}/_all_docs
- executes the built-in _all_docs view
# Basic usage from axios
const authToken = Buffer.from(`${configs.couchUser}:${configs.couchPass}`).toString("base64");
const axiosClient = axios.create({
baseURL: `${configs.couchHost}:${configs.couchPort}`,
headers: {
Authorization: `Basic ${authToken}`,
},
});
Create database & create index & set revision
const databaseName = `test_db`;
await axiosClient.put(`/${databaseName}`, {});
await axiosClient.post(`/${databaseName}/_index`, {
index: {
fields: ["createdAt"],
},
});
await axiosClient.put(`/${databaseName}/_revs_limit`, `20`);