- Fork and clone this repo.
cd
into the repo.npm install
to install dependencies.npm run dev
to start the Vite server that runs the React app.
*Note: The current CouchDB.dmg file (3.3.1) for Mac is missing a required dependency called spidermonkey. The below process may not work if that is still the case. Click here to see how we got around it.
- Download and install CouchDB by dragging it into your applications folder.
- Open CouchDB from your applications folder.
- Create an account when prompted.
- Log-in with your new account.
- Create a database named
bears
. - Click your newly created
bears
database. - Click the green Create Document button.
- This will create a JSON object that has a system-generated
"id"
value. - Copy and paste the
name
,color
, andisRealBear
properties into this new JSON document to make a bear. Should look like this: -
{ "_id": "16c0fe95d43829e068750a054c007aca", "name": "Wish Bear", "color": "Blue", "isRealBear": false }
- Note: You'll need to add a comma to the end of the
"_id"
line.
- This will create a JSON object that has a system-generated
- Make another bear document:
-
{ "_id": "16c0fe95d43829e068750a054c008852", "name": "Kodiak Bear", "color": "Brown", "isRealBear": true }
-
- Woo. That's enough.
- Yeah, it seems nifty. I followed these docs.
- This React app was generated with
npm create vite@latest
.
- It's a NoSQL database, obviously.
- Instead of tables that have columns and rows, CouchDB stores data as JSON documents inside a database.
- It provides pretty unique functionality. Rather than SQL transactions (ACID, remember?), CouchDB leverages the idea of revisions.
- Please consider this video to be required viewing before playing around with CouchDB:
- https://youtu.be/h9ZSEpv3d9g
- It's presented by a person who built a pg-style library that makes it easy for a Go app to interact with CouchDB, and he also maintains the CouchDB documentation.
- Those two 👆 screenshots are from this video.
- This is kinda like a basic
SELECT * FROM bears
, except it just gives us metadata about each bear document in ourbears
database:-
curl -XGET http://admin:admin@localhost:5984/bears/_all_docs
-
- This is the
SELECT * FROM bears
replacement: - Note: Need to wrap the URL in quotes if it includes query params.
-
curl -XGET 'http://admin:admin@localhost:5984/bears/_all_docs?include_docs=true'
-
- This is like a basic
SELECT * FROM bears WHERE id=someId
:-
curl -XGET http://admin:admin@localhost:5984/bears/16c0fe95d43829e068750a054c007aca
- 👆 `/bears/[_id value of a bear document goes herer]
-
- Here's how we can add Winnie the Pooh to our
bears
database via the command line. (cURL
is just a tool that lets us make HTTP requests from the command line. It's like the most minimal possible version of Postman.)-
curl -XPOST http://admin:admin@localhost:5984/bears/ -H "Content-Type: application/json" -d '{"name": "Winnie the Pooh","color": "Yellow","isRealBear": false}'
-
- Important: When doing an update, you can't just update a single property (like we can update a single column's value w/ SQL). When updating a document, you have to provide the entire desired "new revision" of that document.
- To update a document, you must provide both the
"_id"
and"_rev"
values for the document you're targeting. - Here's how we could bring Winnie the Pooh to life (by flipping
"isRealBear"
totrue
): *zsh curl -XPUT http://admin:admin@localhost:5984/bears/da39956cac2be9f8b907ccdc2b004560 -H "Content-Type: application/json" -d '{"_id": "da39956cac2be9f8b907ccdc2b004560","_rev": "1-89e5a855255b63f1b63afeb0cde8c4e9","name": "Winnie the Pooh","color": "Yellow","isRealBear": true}'
- To delete a document, you must provide both the
"_id"
(in the URL) and"_rev"
(in a query parameter) values for the document you're targeting. - It turns out that bringing Winnie the Pooh to life was a mistake. All the honey is gone. Here's how we delete Winnie the Pooh:
*
zsh curl -XDELETE 'http://admin:admin@localhost:5984/bears/da39956cac2be9f8b907ccdc2b004560?rev=2-5810eab404de7811d4778943ffbeabd3'
This is wild, but kinda nifty. We don't really "query" a CouchDB database. Instead, we send GET requests to endpoints whose job is to perform some combination of filtering/mapping/reducing all the documents in a given database.
What if we want to obtain only the real bears? (Only the bears whose "isRealBear"
property is true
.)
-
In SQL-land, that's a simple WHERE clauise!
SELECT * FROM "bears" WHERE "isRealBear" = true;
-
In CouchDB-land, we need to expose an endpoint whose job will always-and-forever be sending back just the real bears.
-
The endpoint itself will be something like:
/bears/_design/bearsViews/_view/realBears
/[databaseName]/_design/[nameOfViews]/_view/[nameOfView]
-
Creating the
realBears
view looks like this in Fauxton:- Take a look at the Map function 👆 there. That function will be executed on each object that is stored in a given database.
- The
emit
function inside of it is how you add a chunk of data to the view's output. It adds an entry to the result that the view will send as an HTTP response. - The
emit
function takes two arguments:- The
key
. - The
value
.
- The
- Read about it in the CouchDB docs. 🙂
- The
-
Hitting this endpoint with a GET request looks like:
Lastly! If you have 33 minutes to spare, I can't recommend this video highly enough. It does a wonderful job explaining views:
Note: CouchDB does expose an "ad-hoc" query language called Mango. It's based on the way queries are formed for MongoDB, which is another NoSQL database. But, from what I've read/watched so far, this query language is meant to serve the use case of making a one-time custom query...they aren't as performant as views.