|
| 1 | +--- |
| 2 | +description: Working with Couchbase Lite databases with Dart. |
| 3 | +related_content: |
| 4 | + - name: Blobs |
| 5 | + url: /blob |
| 6 | + - name: Documents |
| 7 | + url: /documents |
| 8 | + - name: Indexing |
| 9 | + url: /indexing |
| 10 | +--- |
| 11 | + |
| 12 | +import Tabs from '@theme/Tabs' |
| 13 | +import TabItem from '@theme/TabItem' |
| 14 | + |
| 15 | +# Databases |
| 16 | + |
| 17 | +## Create or Open Database |
| 18 | + |
| 19 | +You can create a new database and-or open an existing database, using the |
| 20 | +`api|Database` class. The database class provides both synchronous and |
| 21 | +asynchronous methods for opening and closing databases (see |
| 22 | +[explanation for Synchronous and Asynchronous APIs](/key-concepts.mdx#sync-and-async-apis)). |
| 23 | +Just pass in a database name and optionally a `api|DatabaseConfiguration` - see |
| 24 | +[Example 1](#). |
| 25 | + |
| 26 | +Things to watch for include: |
| 27 | + |
| 28 | +- If the named database does not exist in the specified, or default, location |
| 29 | + then a new one is created. |
| 30 | +- The database is created in a default location unless you specify a directory |
| 31 | + for it — see: `api|DatabaseConfiguration.directory`. |
| 32 | + |
| 33 | + :::tip |
| 34 | + |
| 35 | + Best Practice is to always specify the path to the database explicitly. |
| 36 | + |
| 37 | + ::: |
| 38 | + |
| 39 | + See [Supported Platforms](./supported-platforms.mdx) for the default location |
| 40 | + for each platform. |
| 41 | + |
| 42 | +<CodeExample id={1} title="Opening a Database"> |
| 43 | + |
| 44 | +<APITabs> |
| 45 | +<APITab api="Async"> |
| 46 | + |
| 47 | +```dart |
| 48 | +final database = await Database.openAsync('my-database'); |
| 49 | +``` |
| 50 | + |
| 51 | +</APITab> |
| 52 | +<APITab api="Sync"> |
| 53 | + |
| 54 | +```dart |
| 55 | +final database = Database.openSync('my-database'); |
| 56 | +``` |
| 57 | + |
| 58 | +</APITab> |
| 59 | +</APITabs> |
| 60 | + |
| 61 | +</CodeExample> |
| 62 | + |
| 63 | +## Close Database |
| 64 | + |
| 65 | +You are advised to incorporate the closing of all open database into your |
| 66 | +application workflow. |
| 67 | + |
| 68 | +Closing a database is simple, just call use `api|Database.close`. See |
| 69 | +[Example 2](#). This also closes active replications, listeners, and-or live |
| 70 | +queries connected to the database. |
| 71 | + |
| 72 | +<CodeExample id={2} title="Closing a Database"> |
| 73 | + |
| 74 | +```dart |
| 75 | +await database.close(); |
| 76 | +``` |
| 77 | + |
| 78 | +</CodeExample> |
| 79 | + |
| 80 | +## Database Encryption |
| 81 | + |
| 82 | +<EnterpriseFeatureCallout /> |
| 83 | + |
| 84 | +Couchbase Lite includes the ability to encrypt Couchbase Lite databases. This |
| 85 | +allows mobile applications to secure data at rest, when it is being stored on |
| 86 | +the device. The algorithm used to encrypt the database is 256-bit AES. |
| 87 | + |
| 88 | +### Enabling |
| 89 | + |
| 90 | +To enable encryption, use `api|DatabaseConfiguration.encryptionKey` to set the |
| 91 | +encryption key of your choice. Provide this encryption key every time the |
| 92 | +database is opened - see [Example 3](#). |
| 93 | + |
| 94 | +<CodeExample id={3} title="Opening an Encrypted Database"> |
| 95 | + |
| 96 | +```dart |
| 97 | +final key = await EncryptionKey.passwordAsync('secret password'); |
| 98 | +final config = DatabaseConfiguration(encryptionKey: key); |
| 99 | +final database = await Database.openAsync('my-database', config); |
| 100 | +``` |
| 101 | + |
| 102 | +</CodeExample> |
| 103 | + |
| 104 | +### Persiting |
| 105 | + |
| 106 | +Couchbase Lite does not persist the key. It is the application's responsibility |
| 107 | +to manage the key and store it in a platform specific secure store such as |
| 108 | +Apple's |
| 109 | +[Keychain](https://developer.apple.com/documentation/security/keychain_services) |
| 110 | +or Android's |
| 111 | +[Keystore](https://developer.android.com/training/articles/keystore). |
| 112 | + |
| 113 | +### Opening |
| 114 | + |
| 115 | +An encrypted database can only be opened with the same language SDK that was |
| 116 | +used to encrypt it in the first place. So a database encrypted using the Dart |
| 117 | +SDK, and then exported, is readable only by the Dart or C SDK. |
| 118 | + |
| 119 | +### Changing |
| 120 | + |
| 121 | +To change an existing encryption key, open the database using its existing |
| 122 | +encryption-key and use `api|Database.changeEncryptionKey` to set the required |
| 123 | +new encryption-key value. |
| 124 | + |
| 125 | +### Removing |
| 126 | + |
| 127 | +To remove encryption, open the database using its existing encryption-key and |
| 128 | +use `api|Database.changeEncryptionKey` with a `null` value as the encryption |
| 129 | +key. |
| 130 | + |
| 131 | +## Database Maintenance |
| 132 | + |
| 133 | +From time to time it may be necessary to perform certain maintenance activities |
| 134 | +on your database, for example to compact the database file, removing unused |
| 135 | +documents and blobs no longer referenced by any documents. |
| 136 | + |
| 137 | +Couchbase Lite's API provides the `api|Database.performMaintenance` method. The |
| 138 | +available maintenance operations, including compact are as shown in the enum |
| 139 | +`api|enum:MaintenanceType` to accomplish this. |
| 140 | + |
| 141 | +This is a resource intensive operation and is not performed automatically. It |
| 142 | +should be run on-demand using the API. If in doubt, consult Couchbase support. |
| 143 | + |
| 144 | +## Command Line Tool |
| 145 | + |
| 146 | +cblite is a command-line tool for inspecting and querying Couchbase Lite |
| 147 | +databases. |
| 148 | + |
| 149 | +You can download and build it from the couchbaselabs |
| 150 | +[GitHub repository](https://github.com/couchbaselabs/couchbase-mobile-tools/blob/master/README.cblite.md). |
| 151 | +Note that the cblite tool is not supported by the |
| 152 | +[Couchbase Support Policy](https://www.couchbase.com/support-policy). |
| 153 | + |
| 154 | +## Couchbase Lite for VSCode |
| 155 | + |
| 156 | +Couchbase Lite for VSCode is a Visual Studio Code extension that provides a user |
| 157 | +interface for inspecting and querying Couchbase Lite databases. You can find |
| 158 | +more information about this extension from it's |
| 159 | +[GitHub repository](https://github.com/couchbaselabs/vscode-cblite). |
| 160 | + |
| 161 | +## Troubleshooting |
| 162 | + |
| 163 | +You should use console logs as your first source of diagnostic information. If |
| 164 | +the information in the default logging level is insufficient you can focus it on |
| 165 | +database errors and generate more verbose messages (see `api|enum:LogLevel`) |
| 166 | +— see: [Example 4](#). |
| 167 | + |
| 168 | +<CodeExample id={4} title="Increase level of Database Log Messages"> |
| 169 | + |
| 170 | +<EmbedderTabs> |
| 171 | +<EmbedderTab embedder="Dart"> |
| 172 | + |
| 173 | +```dart |
| 174 | +Database.log.console.level = LogLevel.verbose; |
| 175 | +``` |
| 176 | + |
| 177 | +:::note |
| 178 | + |
| 179 | +On standalone Dart, logging is configured to directly log to stdandard output |
| 180 | +and does not go through Dart. |
| 181 | + |
| 182 | +::: |
| 183 | + |
| 184 | +</EmbedderTab> |
| 185 | +<EmbedderTab embedder="Flutter"> |
| 186 | + |
| 187 | +```dart |
| 188 | +Database.log.custom!.level = LogLevel.verbose; |
| 189 | +``` |
| 190 | + |
| 191 | +:::note |
| 192 | + |
| 193 | +On Flutter, logging is configured to use a custom logger that calls Dart's |
| 194 | +`api|dart:core|fn:print` function. Console logging is disabled. This is because |
| 195 | +not all Flutter platforms (e.g. Android) support console logging. |
| 196 | + |
| 197 | +::: |
| 198 | + |
| 199 | +</EmbedderTab> |
| 200 | +</EmbedderTabs> |
| 201 | + |
| 202 | +</CodeExample> |
0 commit comments