Kotlin Extensions
Couchbase Lite — Kotlin support
Introduction
In addition to implementing the full Couchbase Lite Java SDK API, Kotbase also provides the additional APIs available in the Couchbase Lite Android KTX SDK, which includes a number of Kotlin-specific extensions.
This includes:
- Configuration factories for the configuration of important Couchbase Lite objects such as Databases, Replicators, and Listeners.
- Change Flows that monitor key Couchbase Lite objects for change using Kotlin features such as, coroutines and Flows.
Additionally, while not available in the Java SDK, as Java doesn't support operator overloading, Kotbase adds support
for Fragment
subscript APIs, similar to Couchbase Lite Swift, Objective-C, and .NET.
Configuration Factories
Couchbase Lite provides a set of configuration factories. These allow use of named parameters to specify property settings.
This makes it simple to create variant configurations, by simply overriding named parameters:
Example of overriding configuration
Database
Use DatabaseConfigurationFactory
to create a
DatabaseConfiguration
object, overriding the receiver’s
values with the passed parameters.
Replication
Use ReplicatorConfigurationFactory
to create
a ReplicatorConfiguration
object, overriding the
receiver’s values with the passed parameters.
val ReplicatorConfigurationFactory: ReplicatorConfiguration? = null
public fun ReplicatorConfiguration?.newConfig(
target: Endpoint? = null,
collections: Map<out kotlin.collections.Collection<Collection>, CollectionConfiguration?>? = null,
type: ReplicatorType? = null,
continuous: Boolean? = null,
authenticator: Authenticator? = null,
headers: Map<String, String>? = null,
pinnedServerCertificate: ByteArray? = null,
maxAttempts: Int? = null,
maxAttemptWaitTime: Int? = null,
heartbeat: Int? = null,
enableAutoPurge: Boolean? = null,
acceptOnlySelfSignedServerCertificate: Boolean? = null,
acceptParentDomainCookies: Boolean? = null
): ReplicatorConfiguration
Full Text Search
Use FullTextIndexConfigurationFactory
to
create a FullTextIndexConfiguration
object,
overriding the receiver’s values with the passed parameters.
Indexing
Use ValueIndexConfigurationFactory
to create
a ValueIndexConfiguration
object, overriding the
receiver’s values with the passed parameters.
Logs
Use LogFileConfigurationFactory
to create a
LogFileConfiguration
object, overriding the receiver’s
values with the passed parameters.
Change Flows
These wrappers use Flows to monitor for changes.
Collection Change Flow
Use the Collection.collectionChangeFlow()
to monitor
collection change events.
Document Change Flow
Use Collection.documentChangeFlow()
to monitor changes to a document.
Replicator Change Flow
Use Replicator.replicatorChangeFlow()
to monitor
replicator changes.
Document Replicator Change Flow
Use Replicator.documentReplicationFlow()
to monitor document changes during replication.
Query Change Flow
Use Query.queryChangeFlow()
to monitor changes to a query.
Fragment Subscripts
Kotbase uses Kotlin's indexed access operator to implement Couchbase Lite's
Fragment
subscript APIs for Database
, Collection
, Document
,
Array
, Dictionary
, and Result
, for concise, type-safe, and null-safe access to arbitrary values in a nested JSON
object. MutableDocument
, MutableArray
, and MutableDictionary
also support the MutableFragment
APIs for mutating values.
Supported types can get Fragment
or MutableFragment
objects by either
index or key. Fragment
objects represent an arbitrary entry in a key path, themselves supporting subscript access to
nested values.
Finally, the typed optional value at the end of a key path can be accessed or set with the Fragment
properties, e.g. array
, dictionary
, string
, int
, date
, etc.
Subscript API examples
val db = Database("db")
val coll = db.defaultCollection
val doc = coll["doc-id"] // DocumentFragment
doc.exists // true or false
doc.document // "doc-id" Document from Database
doc["array"].array // Array value from "array" key
doc["array"][0].string // String value from first Array item
doc["dict"].dictionary // Dictionary value from "dict" key
doc["dict"]["num"].int // Int value from Dictionary "num" key
coll["milk"]["exp"].date // Instant value from "exp" key from "milk" Document
val newDoc = MutableDocument("new-id")
newDoc["name"].value = "Sally" // set "name" value