HospitalRun v2.0.0 uses CouchDB v3.0.0.
CouchDB is an opensource, secure and scalable DataBase. PouchDB, that implements CouchDB's replication protocol, is used on the HospitalRun FE for its offline-first capabilities: CouchDB and PouchDB are automatically synced.
Since HospitalRun is a multi-tenant app, one of the most important things to consider for us is a proper data isolation between tenants. In order to achieve it we flow one DB per tenant approach. Every DB has the newly introduced partitioned flag set to true, so it can be sharded and scaled horizontally in a better way: the partiion key is the hospital name.
Every database has a unique name, made by organization's name and a randomly generated shortid. The organization's name has to be slugified before being used:
For example, if an ONG is called "cure" the database assigned to it will be
As said, hospital name will be used as db's parition key. For example if the name is
AIC-CURE it will be
This implementation has two main benefits:
- On CouchDB side, we can leverage the new 3.0.0 sharding features and ensure that every data from the same hospital will be saved "nearby".
- On PouchDB side, we can allow the ONG admin to see data from all hospitals of its organization. In fact, we allow superadmins to create new hospitals, users (accross ONG hospitals) and ONG admins.
Since CouchDB uses map-reduce design documents for querying, we need to make their ergonomics better and make developer expierence more pleasant:
- HospitalRun Design Document interface
- [x] CLI to auto compile TypeScript design documents to json files used by CouchDB
- HospitalRun ddoc cli
- [ ] user creation : every new user and tenant will be created on the CouchDB side, using a Fastify exposed API. For an admin, it is possibile to create new user only when online: we perform checks and assign correct permissions on the server side. No local pouchdb changes to the core dbs (_users, _sessions etc) are allowed.