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.
Create or Open Database
You can create a new database and-or open an existing database, using the CBLDatabase class. 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 and CBLDatabaseConfiguration.directory()
Typically, the default location for C is the application sandbox or current working directory .
See also Finding a Database File.
// NOTE: No error handling, for brevity (see getting started)
CBLError err;
CBLDatabase* db = CBLDatabase_Open(FLSTR("my-database"), NULL, &err);
Close Database
You are advised to incorporate the closing of all open databases into your application workflow.
To close a database, use CBLDatabase_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 .
|
// NOTE: No error handling, for brevity (see getting started)
CBLError err;
CBLDatabase_Close(db, &err);
Database Full Sync
Database Full Sync will prevent 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.
Database Full Sync is a safe method to preserve data loss but will incur a significant degredation of performance. |
// this enables full sync
config.fullSync = true;
It is not possible to change the configuration of a Database after instantiating the Database with the configuration by updating its DatabaseConfiguration property.
|
Database Encryption
This is an Enterprise Edition feature. |
Couchbase Lite on C includes the ability to encrypt Couchbase Lite databases. This allows mobile applications to secure the 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 CBLDatabaseConfiguration.encryptionKey() to set the encryption key of your choice. Provide this encryption key every time the database is opened — see Example 4.
// NOTE: No error handling, for brevity (see getting started)
CBLDatabaseConfiguration config = CBLDatabaseConfiguration_Default();
// This returns a boolean, so check it in production code
CBLEncryptionKey_FromPassword(&config.encryptionKey, FLSTR("password"));
CBLError err;
CBLDatabase* db = CBLDatabase_Open(FLSTR("seekrit"), &config, &err);
// Change the encryption key (or add encryption if the DB is unencrypted)
CBLEncryptionKey betterKey;
CBLEncryptionKey_FromPassword(&betterKey, FLSTR("betterpassw0rd"));
CBLDatabase_ChangeEncryptionKey(db, &betterKey, &err);
// Remove encryption
CBLDatabase_ChangeEncryptionKey(db, NULL, &err);
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 Android’s Keystore.
Opening
An encrypted database can only be opened with the same platform that was used to encrypt it in the first place. So a database encrypted using the C SDK, and then exported, is readable only by the C SDK.
Changing
To change an existing encryption key, open the database using its existing encryption-key and use CBLDatabase_ChangeEncryptionKey() to set the required new encryption-key value.
Removing
To remove encryption, open the database using its existing encryption-key and use CBLDatabase_ChangeEncryptionKey() with a null value as the encryption key.
Upgrading
To upgrade an encrypted database see: Upgrade 1.x databases
Finding a Database File
When the application is running on the iOS simulator, you can locate the application’s sandbox directory using the SimPholders utility.
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 CBLDatabase_PerformMaintenance() method.
The available maintenance operations, including compact
are as shown in the enum CBLMaintenanceType 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.