React to Database Changes with OpenWhisk Actions

One of the best features of CouchDB is its change feed which allows us to get a feed of the changes happening on our database. It’s also possible to have a serverless function (examples are for IBM Cloud Functions but should also work for Apache OpenWhisk) that fires in response to activity on that change feed. I have a database of events that I want to react to when they happen, but the change feed doesn’t include the actual document that was added – but you can add one of the built-in functions to a sequence to make that happen. This post will show how to achieve this by wiring up a built-in action to fetch the document with another action of our own that then handles the data. Continue reading

Copying CouchDB Data Between Laptops

I’m a regular and happy user of Apache CouchDB, so much so in fact that I’m writing a library to talk to it from PHP. While working on tweaking a feature, I realised that the laptop I use for development didn’t have the right/enough data on it to test this particular thing – but that I had a suitable database on my other laptop. Copying data between CouchDB installations is very easy because it has an HTTP API, but usually when I do this at least one endpoint is web-accessible. Enter one of my favourite tools: ngrok.

Ngrok allows me to make the CouchDB on one machine visible to the world (with all the security caveats that this entails! It’s a random URL, never leave the tunnel open longer than you need it, etc) with a command like this:

ngrok http 5984

This opens a tunnel to my local machine on port 5984 which is CouchDB’s default port. I’m running a local dev instance that doesn’t need a username or password which makes this simpler if not exactly secure. I get a gobbledeegook ngrok URL that will allow anyone, anywhere to talk to my CouchDB.

Then I went ahead and on the other laptop, used the web interface to start replication from the sample products database on the local machine over to the one on the ngrok URL.

As soon as it starts, the first laptop shows that there’s traffic coming over the ngrok link – and a few minutes later I had the database I wanted and can go ahead and work on this feature.

Testing API calls in PHP with Guzzle Mocks

I’m working on a CouchDB library for PHP, and so I needed to write some tests for it. CouchDB has an HTTP API so I’m basically making web requests and while I could certainly set up a test database and run full-on integration tests, there are a few limitations with that approach. Firstly: it means I’m testing the database as well, which isn’t what I want and brings extra dependencies that make the tests harder to run. Also: I want to be able to test error cases, rate limiting and so on, that would be difficult to recreate reliably. Continue reading

Multiple Search Keys in CouchDB

I work quite a bit with CouchDB (Cloudant, a hosted CouchDB solution, is part of Bluemix, IBM’s cloud platform – and I work for IBM so I get to use this as much as I like) and today I found a feature I hadn’t seen before. I struggled to find the docs, so I thought I’d post my working example here in case anyone else is solving a similar problem: wanting to use more than one set of key ranges when filtering a CouchDB view. Continue reading