Databases
Database Conceptsβ
Databases created on Couchbase Lite can share the same hierarchical structure as Capella databases. This makes it easier to sync data between mobile applications and applications built using Capella.
Although the terminology is different, the structure can be mapped to relational database terms:
| Relational database | Couchbase |
|---|---|
| Database | Database |
| Schema | Scope |
| Table | Collection |
This structure gives you plenty of choices when it comes to partitioning your data. The most basic structure is to use the single default scope with a single default collection; or you could opt for a structure that allow you to split your collections into logical scopes.
You may not need to sync all the data related for a particular application. You can set up a scope that syncs data, and a second scope that doesn't.
One reason for doing this is to store local configuration data (such as the preferred screen orientation or keyboard layout). Since this information only relates to a particular device, there is no need to sync it:
| DataΒ Scope | Description |
|---|---|
| local | Contains information pertaining to the device. |
| syncing | Contains information pertaining to the user, which can be synced back to the cloud for use on the web or another device. |
Create or Open Databaseβ
You can create a new database and-or open an existing database, using the
Database class. The database class provides both synchronous and
asynchronous methods for opening and closing databases (see
explanation for Synchronous and Asynchronous APIs).
Just pass in a database name and optionally a DatabaseConfiguration - see
Example 1.
Things to watch for include:
-
If the named database does not exist in the specified, or default, location then a new one is created.
-
The database is created in a default location unless you specify a directory for it β see:
DatabaseConfiguration.directory.tipBest Practice is to always specify the path to the database explicitly.
See Supported Platforms for the default location for each platform.
- Async
- Sync
final database = await Database.openAsync('my-database');
final database = Database.openSync('my-database');
Close Databaseβ
You are advised to incorporate the closing of all open databases into your application workflow.
Closing a database is simple, just use Database.close. See Example 2.
This also closes active replications, listeners, and-or live queries connected
to the database.
Closing a database soon after starting a replication involving it can cause an exception as the asynchronous replicator start may not yet be connected.
await database.close();
Database Full Syncβ
Database Full Sync prevents the loss of transactional data due to an unexpected
system crash or loss of power. This feature is not enabled by default and must
be manually set in your database configuration using
DatabaseConfiguration.fullSync.
Database Full Sync is a safe method to prevent data loss but will incur a significant degradation of performance.
final config = DatabaseConfiguration(fullSync: true);
final database = await Database.openAsync('my-database', config);
Once a database is created, its configuration is immutable β modifying the
DatabaseConfiguration afterwards has no effect on the existing instance.
Database Encryptionβ
This feature is an Enterprise Edition feature.
Couchbase Lite includes the ability to encrypt Couchbase Lite databases. This allows mobile applications to secure data at rest, when it is being stored on the device. The algorithm used to encrypt the database is 256-bit AES.
Enablingβ
To enable encryption, use DatabaseConfiguration.encryptionKey to set the
encryption key of your choice. Provide this encryption key every time the
database is opened - see Example 4.
final key = await EncryptionKey.passwordAsync('secret password');
final config = DatabaseConfiguration(encryptionKey: key);
final database = await Database.openAsync('my-database', config);
Persistingβ
Couchbase Lite does not persist the key. It is the application's responsibility to manage the key and store it in a platform specific secure store such as Apple's Keychain or Android's Keystore.
Openingβ
An encrypted database can only be opened with the same language SDK that was used to encrypt it in the first place. So a database encrypted using the Dart SDK, and then exported, is readable only by the Dart or C SDK.
Changingβ
To change an existing encryption key, open the database using its existing
encryption-key and use Database.changeEncryptionKey to set the required
new encryption-key value.
Removingβ
To remove encryption, open the database using its existing encryption-key and
use Database.changeEncryptionKey with a null value as the encryption
key.
Database Maintenanceβ
From time to time it may be necessary to perform certain maintenance activities on your database, for example to compact the database file, removing unused documents and blobs no longer referenced by any documents.
Couchbase Lite's API provides the Database.performMaintenance method. The
available maintenance operations, including compact are as shown in the enum
MaintenanceType to accomplish this.
This is a resource intensive operation and is not performed automatically. It should be run on-demand using the API. If in doubt, consult Couchbase support.
Command Line Toolβ
cblite is a command-line tool for inspecting and querying Couchbase Lite databases.
You can download and build it from the couchbaselabs GitHub repository. Note that the cblite tool is not supported by the Couchbase Support Policy.
Couchbase Lite for VSCodeβ
Couchbase Lite for VSCode is a Visual Studio Code extension that provides a user interface for inspecting and querying Couchbase Lite databases. You can find more information about this extension from it's GitHub repository.
Troubleshootingβ
You should use console logs as your first source of diagnostic information. If
the information in the default logging level is insufficient you can focus it on
database errors and generate more verbose messages (see LogLevel) β
see: Example 5.
Database.log.console.level = LogLevel.verbose;