A Makefile-based CLI tool for managing CouchDB design documents. Simplifies pulling, pushing, building, and managing CouchDB design documents from a local directory structure. The directory structure integrates with git. This tool can be included as part of the user's Makefile environment or as a drop-in-replacement for couchapp.
Before using couchdex.mk, ensure you have the following tools installed:
make- GNU Makecurl- For HTTP requests to CouchDBjq- JSON processor
On Debian/Ubuntu systems:
sudo apt-get install make curl jqgit clone https://github.com/sklassen/couchdex
cd couchdex.mkcurl -o couchdex.mk https://raw.githubusercontent.com/sklassen/couchdex/main/couchdex.mkTo make couchdex.mk available system-wide:
sudo cp couchdex.mk /usr/local/include/Or add it to your project's include path.
couchdex.mk works best with the following directory organization:
$HOME/
βββ .couchdexrc # Global configuration file (optional)
βββ cdb/ # Root directory for all CouchDB projects
βββ dbname/ # Database name
βββ designname/ # Design document name
βββ Makefile # Project configuration (generated by init)
βββ language # Language setting (javascript/erlang)
βββ views/ # View definitions
βββ shows/ # Show functions
βββ lists/ # List functions
βββ updates/ # Update functions
βββ filters/ # Filter functions
This structure allows couchdex.mk to automatically infer:
- Database name from the parent directory (e.g.,
testdb) - Design document name from the current directory (e.g.,
myapp,products,users)
Is not specified the language is javascript. The language is erlang is also supported.
Configure CouchDB connection settings via environment variables or a ~/.couchdexrc file:
COUCH_SCHEME=http # http or https (default: http)
COUCH_HOST=127.0.0.1 # CouchDB host (default: 127.0.0.1)
COUCH_PORT=5984 # CouchDB port (default: 5984)
COUCH_ADMIN=admin # Admin username (default: admin)
COUCH_PASSWD=secret # Admin password (required)
COUCH_DB=mydb # Database name (auto-detected from parent dir)
COUCH_DESIGN=mydesign # Design document name (auto-detected from current dir)For convenience, create a ~/.couchdexrc file with your default settings:
COUCH_ADMIN=admin
COUCH_PASSWD=your_passwordThis file will be automatically loaded by couchdex.mk, so you don't need to specify credentials in each project's Makefile.
# Create the recommended directory structure
mkdir -p ~/cdb/testdb/mydesign
cd ~/cdb/testdb/mydesign
# Initialize - this generates a Makefile with auto-detected settings
make -f couchdex.mk initThe init target generates a Makefile in the current directory with database and design names auto-detected from your directory path. After initialization, you can simply run:
make [target]To pull an existing design document from CouchDB and create the local file structure:
# Create directory structure
mkdir -p ~/cdb/testdb/mydesign
cd ~/cdb/testdb/mydesign
# Clone (auto-detects testdb as COUCH_DB and mydesign as COUCH_DESIGN)
make -f couchdex.mk clone COUCH_PASSWD=secretThis will:
- Download the design document from CouchDB
- Create local files for views, shows, lists, updates, filters, etc.
- Extract the language setting
- Generate a
Makefilefor the project
Information & Help:
help- Display available commandsversion- Show couchdex.mk versionstatus- Show current design document statuscheck- Confirm couchdex.mk is installed correctly
Server & Database Management:
dbs- List all databases on the servercreate- Create a new databasesecurity- View database security settingscompactdb- Compact the databasecleanup- Run view cleanup
Design Document Management:
init- Generate a Makefile with auto-detected database and design namesfetch- Fetch (GET) the design document from CouchDBpull- Expands the design document into the file systempush- Push (PUT) the design document to CouchDBpush-force- Force push (overwrite) the design documentclone- Clone design document and create local file structurebuild- Build the design document JSON from local filesrevs- Show revision historykeys- Show all keys in the design document
Utility:
compact- Compact a specific viewclean- Remove generated files
# Create the recommended directory structure
mkdir -p ~/cdb/store/catalog
cd ~/cdb/store/catalog
# Initialize - generates Makefile (auto-detects store as DB, catalog as design doc)
make -f /usr/local/include/couchdex.mk init
# Edit the generated Makefile to add your COUCH_PASSWD (or use ~/.couchdexrc)
# Create your views and functions
mkdir -p views/by_category
echo 'function(doc) { emit(doc.category, null); }' > views/by_category/map.js
# Build and push to CouchDB
make push# Navigate to your design document directory
cd ~/cdb/testdb/mydesign
# Pull the current version
make pull
# Clone to local file structure (if not already done)
make clone
# Edit your views/functions
vim views/by_name/map.js
# Push changes back to CouchDB
make pushYou can override settings per command:
make push COUCH_DB=production COUCH_PASSWD=prod_secret
make pull COUCH_DB=development COUCH_PASSWD=dev_secretOr explicitly specify when not using the recommended structure:
make push COUCH_DB=mydb COUCH_DESIGN=mydesign COUCH_PASSWD=secretOnce initialized or cloned, your design document directory will contain:
~/cdb/dbname/designname/
βββ Makefile # Generated by init (project configuration)
βββ .design_<name>.json # Design document cache
βββ language # Language setting (javascript/erlang)
βββ validate_doc_update.js # Validation function (if present)
βββ rewrite.js # Rewrite rules (if present)
βββ views/ # View definitions
β βββ by_name/
β βββ map.js # Map function
β βββ reduce.js # Reduce function (optional)
βββ shows/ # Show functions
β βββ item.js
βββ lists/ # List functions
β βββ items.js
βββ updates/ # Update functions
β βββ item.js
βββ filters/ # Filter functions
βββ recent.js
For convenience, create an alias:
alias couchdex='make -f /usr/local/include/couchdex.mk'Then use:
couchdex status
couchdex pushAs an added convience you can also enable bash completion by adding the following snippet to your .bashrc file.
# Enable bash completion for make targets
_couchdex_targets() {
local cur prev words cword
_init_completion || return
# Use grep to find targets and format them for bash completion
COMPREPLY=($(compgen -W "$(grep -oE '^[a-zA-Z0-9_-]+:' /usr/local/include/couchdex.mk | sed 's/://')" -- "$cur"))
}
complete -F _couchdex_targets couchdexEnable verbose output to see all commands being executed:
make push COUCH_VERBOSE=1If you need to overwrite remote changes (use with caution):
make push-forceAfter running make -f couchdex.mk init in ~/cdb/testdb/mydesign/, a Makefile will be generated that looks like this:
COUCH_ADMIN=admin
#COUCH_PASSWD=PASSWD_HERE
COUCH_DB=testdb
COUCH_DESIGN=mydesign
include /usr/local/include/couchdex.mkThe init target automatically generates this file with COUCH_DB and COUCH_DESIGN values detected from your directory path. Edit this file to configure your project-specific settings, or use ~/.couchdexrc for global credentials.
Here's a complete workflow from start to finish:
# 1. Install couchdex.mk system-wide
sudo cp couchdex.mk /usr/local/include/
# 2. Create global configuration file
cat > ~/.couchdexrc << 'EOF'
COUCH_ADMIN=admin
COUCH_PASSWD=your_password
COUCH_HOST=127.0.0.1
COUCH_PORT=5984
EOF
# 3. Create an alias for convenience
echo "alias couchdex='make -f /usr/local/include/couchdex.mk'" >> ~/.bashrc
source ~/.bashrc
# 4. Create your CouchDB project structure
mkdir -p ~/cdb/store/products
cd ~/cdb/store/products
# 5. Initialize the project - this generates a Makefile
couchdex init
# 6. Create a view
mkdir -p views/by_price
cat > views/by_price/map.js << 'EOF'
function(doc) {
if (doc.type === 'product' && doc.price) {
emit(doc.price, doc.name);
}
}
EOF
# 7. Push to CouchDB
make push
# 8. Check status
make statusError: "COUCH_PASSWD is not set"
- Ensure you've set the
COUCH_PASSWDvariable in your Makefile or environment
Error: "No jq in PATH"
- Install jq:
sudo apt-get install jq
Error: "No curl in PATH"
- Install curl:
sudo apt-get install curl
Connection refused
- Check that CouchDB is running:
curl http://127.0.0.1:5984/ - Verify your
COUCH_HOSTandCOUCH_PORTsettings
Wrong database or design document detected
- Verify you're in the correct directory structure:
~/cdb/dbname/designname/ - Or explicitly specify:
make push COUCH_DB=testdb COUCH_DESIGN=mydesign
Want to see what init generates?
- Run
make -f couchdex.mk initin your design document directory - A
Makefilewill be created with auto-detected settings from your path
See LICENSE file for details.
Contributions are welcome! Please submit issues and pull requests on GitHub.