Database & State Management
Relevant source files
The following files were used as context for generating this wiki page:
Budibase utilizes a robust database system for its internal state management, which includes storing application definitions, user data, configurations, and other core metadata. The primary internal database is CouchDB, chosen for its JSON document storage model which aligns well with the structure of Budibase applications. While Budibase can connect to a variety of external SQL and NoSQL databases (like PostgreSQL, MySQL, MongoDB) as data sources for applications, this document focuses specifically on the internal CouchDB instance used by the Budibase platform itself.
The architecture includes a database abstraction layer, which, while currently focused on CouchDB, provides a structure for potential future support of other database backends. This system is designed for self-hosting, with configuration and initialization managed through Docker environment variables and startup scripts, ensuring a configurable and scalable deployment.
Sources: README.md, packages/backend-core/src/db/db.ts
Architecture Overview
The database architecture in Budibase is centered around the backend-core package. A service within the Budibase backend that needs to interact with the database does not call the CouchDB driver directly. Instead, it uses a generic DB interface. A factory function provides a concrete implementation of this interface, which is currently the CouchDB class. This class acts as a wrapper around the nano library, which is a minimalist driver for CouchDB.
This diagram illustrates the high-level data flow from a Budibase service to the CouchDB database.
Maintenance Mode
The system has a maintenance mode that can be triggered if there are database-related issues. For example, if the application detects that it is running against an outdated version of CouchDB, it will display a maintenance page prompting the user to upgrade their installation and providing a link to migration guides.
Sources: packages/builder/src/pages/builder/maintenance/index.svelte:1-49
CouchDB Configuration and Initialization
For self-hosted deployments, Budibase uses Docker to run its services, including CouchDB. The configuration is managed via environment variables passed to the Docker container.
Environment Variables
The following environment variables are used to configure the CouchDB instance.
| Variable | Description | Default Value |
|---|---|---|
MAIN_PORT | The main port for the Budibase installation. | 10000 |
COUCH_DB_USER | The username for the CouchDB admin user. | budibase |
COUCH_DB_PASSWORD | The password for the CouchDB admin user. | change-me |
COUCH_DB_PORT | The port on which the CouchDB instance is exposed. | 4005 |
Sources: hosting/portainer/template.json:1-55, hosting/couchdb/docker-entrypoint.sh:1-5
Initialization Process
When the CouchDB Docker container starts, an entrypoint script (docker-entrypoint.sh) executes a series of initialization steps to prepare the database.
This flowchart shows the sequence of events during CouchDB initialization.
The script first configures the local.ini file with the provided admin username and password. After starting the CouchDB server, it waits until the server is responsive. It then uses curl to make HTTP API calls to create the admin user and necessary system databases (_users, _replicator, _global_changes). Finally, it sets security permissions on the _users database to restrict access.
A key part of the initialization is creating the admin user:
# hosting/couchdb/docker-entrypoint.sh:31-33
curl -X PUT "http://127.0.0.1:5984/_node/nonode@nohost/_config/admins/${COUCH_USER}" \
--data "\"${COUCH_PASSWORD}\"" \
--user "${COUCH_USER}:${COUCH_PASSWORD}"
Sources: hosting/couchdb/docker-entrypoint.sh:1-60
Database Abstraction Layer
To decouple the application logic from a specific database technology, Budibase uses a database abstraction layer defined in packages/backend-core/src/db/db.ts.
DB Interface and Factory
The core of this layer is the DB interface, which defines a standard set of methods for database operations (get, put, remove, allDocs, etc.).
The getDB function acts as a factory. It is responsible for returning a concrete implementation of the DB interface. Currently, it is hardcoded to return a CouchDB instance, but this design allows for easily swapping or adding other database backends in the future.
// packages/backend-core/src/db/db.ts:27-31
import { CouchDB } from "./couch"
export function getDB(dbName: string): DB {
return new CouchDB(dbName)
}
This class diagram shows the relationship between the DB interface and the CouchDB implementation.
CouchDB Implementation
The CouchDB class in packages/backend-core/src/db/couch/index.ts implements the DB interface by wrapping the nano library. Each method in the class maps directly to a corresponding nano function, handling the communication with the CouchDB HTTP API.
Key functions include:
| Function | Description |
|---|---|
get(id) | Fetches a single document by its _id. |
put(doc) | Creates or updates a document. It automatically adds createdAt and updatedAt timestamps. |
remove(docOrId, rev) | Deletes a document. |
allDocs(opts) | Retrieves all documents in the database, with options for pagination and filtering. |
bulkDocs(docs) | Performs multiple document creations, updates, or deletions in a single request. |
query(view, opts) | Executes a pre-defined MapReduce view query. |
Sources: packages/backend-core/src/db/couch/index.ts:1-213
The following sequence diagram shows the flow of a get request from a service to the database.
Data Structures
All documents stored in Budibase's internal database extend a common base structure defined in packages/types/src/documents/document.ts.
Base Document Structure
The BaseDocument interface defines the minimum set of properties that every document must have. The Document interface extends this with CouchDB-specific fields.
// packages/types/src/documents/document.ts:40-44
export interface BaseDocument {
_id?: string
createdAt?: string
updatedAt?: string
}
// packages/types/src/documents/document.ts:46-51
export interface Document extends BaseDocument {
_rev?: string
// tableId is a denormalised remnant, but useful for lookups
tableId?: string
}
| Field | Type | Description | Source Interface |
|---|---|---|---|
_id | string | The unique identifier for the document. | BaseDocument |
_rev | string | The revision identifier, used by CouchDB for optimistic locking. | Document |
createdAt | string | ISO 8601 timestamp of when the document was created. | BaseDocument |
updatedAt | string | ISO 8601 timestamp of the last update. | BaseDocument |
tableId | string | A denormalized field used for certain lookups. | Document |
The system also uses a DocumentType enum to distinguish between different types of documents stored in the database, such as apps, tables, roles, and users. This is typically part of the _id to allow for easy querying and identification.
Sources: packages/types/src/documents/document.ts:1-51
Summary
Budibase's state management relies on an internal CouchDB database, which stores all platform-related metadata as JSON documents. The architecture is designed for flexibility, featuring a database abstraction layer that isolates the core application from the specific database implementation. Configuration and setup are streamlined for self-hosted environments using Docker, with environment variables and initialization scripts ensuring a consistent and reliable deployment. This robust and decoupled design provides a solid foundation for managing the platform's state while allowing for future scalability and extensibility.