Couchbase Lite-Abfrage-Builder-Engine mit Kotlin und Jetpack Compose

  • Learn how to query documents in Couchbase Lite
  • Explore the Query Builder Engine

Einführung

Couchbase Lite brings powerful querying and Full-Text-Search(FTS) capabilties to the edge. The query builder interface is based on SQL++, Couchbase’s declarative query language that extends SQL for JSON. If you are familiar with SQL, you will feel right at home with the semantics of the new API. The query builder API is designed using the Fluent API Design Pattern, and it uses method cascading to read to like a Domain Specific Language (DSL). This makes the interface very intuitive and easy to understand.

In diesem Schritt des Lernpfads werden Sie die Grundlagen von:

  • Using the QueryBuilder API
  • Using Live Queries with queryChangeFlow and Kotlin Co-Routine Flows
  • Using JSON Serialization to “deserialize” documents from the database in to Kotlin data class
  • Verwendung von Indizes zur Beschleunigung einer Abfrage
  • Using the count function with the QueryBuilder API
  • Verwendung des Operators LIKE zur Suche nach Dokumenten mit einem bestimmten Wert in einem Feld

App-Übersicht

Obwohl die Demo-App viele Funktionen bietet, werden Sie in diesem Schritt Schritt für Schritt durch die folgenden Schritte geführt:

  • Melden Sie sich in der Anwendung an
  • Scrolling the list of projects
  • Review the code for displaying projects

Hinweis: Dieser Schritt setzt voraus, dass Sie den vorherigen Schritt abgeschlossen haben. Batch-Verfahren that loaded the sample data into the application. This part of the learning path will not work if you don’t complete the previous steps.

Installation

App-Quellcode abrufen

Klonen von Quellcode
  • If you haven’t already cloned the repo from the previous steps, clone the Learn Couchbase Lite with Kotlin and Jetpack Compose Repository von GitHub.
git clone https://github.com/couchbase-examples/android-kotlin-cbl-learning-path.git

Probieren Sie es aus

  • Öffnen Sie mit Android Studio das Verzeichnis src/build.gradle.
  • Das Projekt erstellen und ausführen.
  • Melden Sie sich mit der App an “demo@example.com“ und “P@ssw0rd12” für die Felder „User-ID“ und „Passwort“ entsprechend.
  • Verify that you see sample projects on the screen. If you do not see sample projects, please complete the previous step Batch-Verfahren Um die Beispieldaten zu laden, bevor man fortfährt.

Project Listing,400

Data Model

A reminder that Couchbase Lite is a JSON Document Store. A Dokument is a logical collection of named fields and values. The values are any valid JSON types. In addition to the standard JSON types, Couchbase Lite supports Date und Blob data types. While it is not required or enforced, it is a recommended practice to include a “documentType” property that can serve as a namespace for related documents.

The Project Document

Die Beispiel-App wurde mit einer Sammlung von Dokument mit einem “documentType” Eigentum von “project” in the previous step. Each document represents a project that a team would would work on and have to complete before the due date based on a selected location, which is another document type.

Ein Beispiel für ein Dokument wäre:

{
	"team": "team1",
	"modifiedOn": "1656804469897",
	"documentType": "project",
	"createdBy": "demo@example.com",
	"dueDate": "1734415200000",
	"name": "Santa Clara Warehouse Audit",
	"description": "Audit of warehouse stock located in Santa Clara, CA.",
	"modifiedBy": "demo@example.com",
	"warehouse": {
		"documentType": "warehouse",
		"name": "Santa Clara Warehouse",
   	"shippingTo": [
			"AZ",
			"CA",
			"HI",
			"NV"
		],
		"warehouseId": "e1839e0b-57a0-472c-b29d-8d57e256ef32",
		"city": "Santa Clara",
		"address1": "3250 Dr Olcott Street",
		"postalCode": "95054",
		"latitude": 32.3803024,
		"state": "CA",
		"salesTax": 0.0913,
		"longitude": -121.9674197,
		"yearToDateBalance": 0.0
	},
	"projectId": "663953ba-9e4c-4090-9e07-642c1778d467",
	"createdOn": "1656804469897"
}
The Project Data Class

Wenn ein “project” is retreived from the database it is stored within an data class of type Project.

@Keep
@Serializable
data class Project (
    var projectId: String = "",
    var name: String = "",
    var description: String = "",
    var isComplete: Boolean = false,
    var documentType: String = "",
    @Serializable(with = DateSerializer::class)
    var dueDate: Date? = null,
    var warehouse: Warehouse? = null,

    //security tracking
    var team: String = "",
    var createdBy: String = "",
    var modifiedBy: String = "",
    @Serializable(with = DateSerializer::class)
    var createdOn: Date? = null,
    @Serializable(with = DateSerializer::class)
    var modifiedOn: Date? = null
)
The Warehouse Class

Wenn ein “warehouse” from a project is retreived from the database it is stored within a data class of type Warehouse.

@Keep
@Serializable
data class Warehouse(
    val warehouseId: String,
    val name: String,
    val address1: String,
    val address2: String? = "",
    val city: String,
    val state: String,
    val postalCode: String,
    val salesTax: Double,
    val yearToDateBalance: Double,
    val latitude: Double,
    val longitude: Double,
    val shippingTo: List<String>,
    val documentType: String
    )
)

Exploring the Query Builder API with the Count Function

The Query API in Couchbase Lite is extensive. On the Developer – Database Information screen we display the number of warehouse documents found in the warehouse database. The QueryBuilder Für die Berechnung dieser Zahl wurde die API zusammen mit der Count-Funktion verwendet. Um den Entwicklerinformationsbildschirm anzuzeigen:

  • Starten Sie die Invenory-Anwendung auf Ihrem Emulator.
  • Login mit dem Benutzernamen demo@example.com und Passwort P@ssw0rd12
  • Klicken Sie auf das Symbol des Schubladenmenüs (manchmal auch als „Hamburger“-Symbol bezeichnet) und tippen Sie auf „Entwickler“.
  • Tippen Sie auf die Schaltfläche „Datenbankinformationen“

Zählung der Anzahl der Dokumente nach Typ

The “DevDatabaseInfoView” screen displays the count of how many documents are in the warehouse database with the type set to ‘warehouse’.

Developer - Database Information

The DevDatabaseInfoView function obtains this information from the DevDatabaseInfoViewModel class, which tracks this in a mutableStateOf variable. The view model calls the WarehouseRepositoryDb which runs the query to calculate the count.

private suspend fun updateWarehouseCount() {
 viewModelScope.launch(Dispatchers.IO) {
  val locationCount = warehouseRepository.warehouseCount()
   if (locationCount > 0) {
    withContext(Dispatchers.Main) {
     numberOfWarehouses.value = locationCount
    }
   }
  }
}
override suspend fun warehouseCount(): Int {
  • Wir erstellen die Abfrage mithilfe der QueryBuilder API, der nach Dokumenten sucht, die den angegebenen Kriterien entsprechen.
var resultCount = 0
val countAliasName = "count"
try {
 val db = databaseResources.warehouseDatabase
 db?.let {
  val query = QueryBuilder  // <1>
   .select(SelectResult.expression(Function.count(Expression.string("*"))).`as`(countAliasName)) // <2>
   .from(DataSource.database(it)) // <3>
   .where(Expression.property(typeAttributeName).equalTo(Expression.string(documentType))) // <4>
  val results = query.execute().allResults()  // <5>
  resultCount = results[0].getInt(countAliasName)  // <6>
  }
 } catch (e: Exception){
  Log.e(e.message, e.stackTraceToString())
 }
  1. Call the Query Builder API to create a Query
  2. The select function will run the calculation from the count function. Note we have to use the ` as an escape character because as is a reserved Kotlin keyword.
  3. Die DataSource.database(it) specified the Data Source which is the warehouse database
  4. We include the Wo? clauses that uses the equalTo operator to look for the “warehouse” string in the “Typ” property.
  5. Wir führen die Abfrage durch, indem wir die execute().allResults() method on the Query that was constructed in the previous step
  6. We get the results by looking at the first result in the collection and getting an integer value for the key countAlaisName which we defined in the Wählen Sie Klausel

Further Exploring the Query Builder API – return List of Projects Documents

The Query Builder API in Couchbase Lite is extensive. In our second example, we will be using the QueryBuilder API to make a simple Mustererkennung query using the equalTo operator. This example will use Live Query with Kotlin coroutine flows.

Fetching Project Documents

From the “Login” screen, when a user logs in with the proper credentials they are presented with a list of projects there team is assigned to work on.

  • Öffnen Sie die ProjectListView.kt Datei öffnen und die ProjectListView Funktion.

  • The live query is collected as state from the Flow<List> repositoryFlow property that the ProjectListViewModel exposes. Flows do not emit values until they are collected. This is often referred to as a cold stream.

Tipp: Anytime documents change or are added to the database that satisify this query, the flow would stream to the ProjectListViewModel and the ProjectListView would collect it if it’s the current composable on the screen. The Live-Abfrage API via the queryChangeFlow function can dynmamically update your UI without the need for end user interaction, which will come in handy in future sections of the learning path when we review replication.

val projectList by viewModel.repositoryFlow.collectAsState(initial = listOf())
var repositoryFlow: Flow<List<Project>> = repository.getDocuments(currentUser.team)
  • The view model exposes the flow which is a list of projects from the repository getDocument function. The getDocument function takes in the current user’s team in order to filter out results to only the projects associated with that team.

  • To see how to build a flow, open the ProjectRepositoryDb.kt Datei öffnen und die Funktion getDocuments suchen.

override fun getDocuments(team: String): Flow<List<Project>>
  • We first build a Query using the QueryBuilder API, der nach Dokumenten sucht, die den angegebenen Kriterien entsprechen.
 val query = QueryBuilder // <1>
  .select(SelectResult.all()) // <2>
  .from(DataSource.database(database).`as`("item")) // <3>
  .where( //4
    Expression.property("documentType")
      .equalTo(Expression.string(projectType)) // <4>
    .and(Expression.property("team")
      .equalTo(Expression.string(team)))) // <4>
  1. Build a query using the QueryBuilder API
  2. Die SelectResult.all() specifies that we are interested in all properties in Documents that match the specified criteria
  3. Die DataSource.database(database).\as("item") specified the Data Source. Note we have to use escape character in order to use the as function
  4. Die Expression builds a QueryExpression used to find documents where the Typ property and the Team properties are equal to the value passed in
  • Once the query has been created we can define the flow that will be returned.
val flow = query        // <1>
  .queryChangeFlow()  // <1> 
  .map { qc -> mapQueryChangeToProject(qc) } // <2>
  .flowOn(Dispatchers.IO)  // <3>
query.execute()  // <4>
return flow  // <5>
  1. The query varible from the previous step is used to call the queryChangeFlow function to return a flow
  2. Die intermediate operator map is used to change the results coming back using the mapQueryChangeToProject function passing in the QueryChange. This is used to deserialize the results from JSON to the Project data class.
  3. The flowOn function is used to make sure that the flow runs on the Dispatcher.IO context to keep the code from running on the main UI thread.
  4. The query executes. Remember this step MUST be done before returning the flow.
  5. Return the flow

REMINDER: Kotlin Co Routine Flows emit new values into the stream of data and are not returned until they are collected.

Indexieren der Abfrage
  • Die Erstellung von Indizes für nicht-FTS-basierte Abfragen ist optional. Um Abfragen zu beschleunigen, können Sie jedoch Indizes für die Eigenschaften erstellen, auf die Sie abfragen möchten. Die Indexierung wird aktiv durchgeführt.

  • In dem DatabaseManager.kt Datei, suchen Sie die createTeamTypeIndex Funktion.

  • Wir erstellen einen Index auf der Typ und Team Eigenschaften der Dokumente in der Bestandsaufnahme Datenbank.

private fun createTeamTypeIndex(){
 try {
  inventoryDatabase?.let {  // <1>
   if (!it.indexes.contains(teamIndexName)) {
    // create index for ProjectListView to only return documents with
    // the type attribute set to project and the team attribute set to the
    // logged in users team
    it.createIndex( // <2>
     teamIndexName, // <3>
     IndexBuilder.valueIndex(   // <4>
      ValueIndexItem.property(documentTypeAttributeName), // <5>
      ValueIndexItem.property(teamAttributeName)) // <5>
     )
    }
   }
 } catch (e: Exception){
  android.util.Log.e(e.message, e.stackTraceToString())
 }
}
  1. The let keyword is used to unbox the database since it can be null.
  2. Call the database createIndex function which takes in a name of the index and then the properites to index
  3. The name of the index in string format. TeamIndexName is defined toward the top of the class with the value idxTeam
  4. The IndexBuilder’s valueIndex function can be used to create the index
  5. The ValueIndexItem.property factory method can be used to create a ValueIndexItem by passing in the property of which to index.

Query Builder API – Searching for Warehouses with the LIKE Operator

The Query Builder API supports serveral operators including the LIKE which can be used for string matching. We use the LIKE operator on the data editor screen for Projects to find a warehouse to add to a project. Let’s review the code.

HinweisDer Gleicheinheitsoperator führt auch geschwindigkeitsabhängige Übereinstimmungen aus. Um geschwindigkeitsunabhängige Übereinstimmungen zu erzielen, verwenden Sie Klein- oder Großbuchstabenfunktionen, um sicherzustellen, dass alle Vergleichswerte denselben Buchstaben haben und damit das Problem des Buchstabenbedarfs beseitigt wird.

On the Project Editor screen we provide a link to the Warehouse Section screen. To see the Project Editor screen:

  • Starten Sie die Invenory-Anwendung auf Ihrem Emulator.
  • Login mit dem Benutzernamen demo@example.com und Passwort P@ssw0rd12
  • Click the + icon to add a Project
  • Type in a name for the project on the Name field.
  • Tippen Sie auf die Schaltfläche No Warehouse Selected

Project Editor screen

  • In the City box enter Sa
  • In the State box enter Ca
  • Klicken Sie auf die Schaltfläche „Suchen“
  • A listing of warehouses should return that match these results

Select Project screen

Let’s review the code for the Warehouse Section screen.

Warehouse Selection

  • Öffnen Sie die WarehouseSelectionView.kt Datei öffnen und die WarehouseSelectionView Funktion.

  • The WarehouseSelectionView exposes the viewModel’s onSearch which the WarehouseSector function uses with the Search button.

WarehouseSelector(
  searchCity = viewModel.searchCity.value,
  searchState = viewModel.searchState.value,
  onSearchCityChanged = viewModel.onSearchCityChanged,
  onSearchStateChanged = viewModel.onSearchCountryChanged,
  onSearch = viewModel.onSearch,
  warehouseStatusMessage = viewModel.locationStatusMessage.value,
  warehouses = viewModel.warehousesState,
  onWarehouseSelected = onWarehouseSelected
)
  • This is defined as OnSearch in the WarehouseSelector function
fun WarehouseSelector(
    searchCity: String,
    searchState: String,
    onSearchCityChanged: (String) -> Unit,
    onSearchStateChanged: (String) -> Unit,
    onSearch: () -> Unit,
    warehouseStatusMessage: String,
    warehouses: List<Warehouse>,
    onWarehouseSelected: (Warehouse) -> Unit
)
  • The search button then calls the OnSearch method on the OnClick listener.
Button(modifier = Modifier
 .padding(top = 4.dp),
  colors = ButtonDefaults.buttonColors(backgroundColor = Red500),
  onClick = {
   onSearch()
  })
val onSearch: () -> Unit = {
 viewModelScope.launch { // <1>
  if (searchCity.value.length >= 2) { // <2>
   isLoading.value = true
   val warehouses = warehouseRepository // <3>
    .getByCityState(searchCity.value, searchState.value) // <3>
   if (warehouses.isNotEmpty()) { // <4>
    withContext(Dispatchers.Main) {
    warehousesState.clear()
    warehousesState.addAll(warehouses)
    isLoading.value = false
   } 
  } else { // <5>
   withContext(Dispatchers.Main) {
    warehousesState.clear()
    locationStatusMessage.value = "No Locations Found"
    isLoading.value = false
    }
   }
  }
 }
}
  1. Since we will be talking to the database using suspend functions, we want to make sure we use viewModel Scope
  2. Business logic to check if the searchCity value is greater than or equal to 2 characters. If so, then we can search for warehouses.
  3. Call the warehouseRepository getByCityState function which takes in the searchCity and searchState values.
  4. If the warehouses list is not empty, then we can set the warehousesState to the warehouses list.
  5. If not, then we set the locationStatusMessage to “No Locations Found”.
override suspend fun getByCityState(searchCity: String, searchState: String?): List<Warehouse> {
  • We wil use withContext(Dispatchers.IO) to make sure we are working on the IO thread since we will be talking to the database.
try {
 val db = databaseResources.warehouseDatabase
 db?.let { database ->
  //search by city
  var whereQueryExpression = Function
   .lower(Expression.property(cityAttributeName))
   .like(Expression.string("%" + searchCity.lowercase() + "%")) // <1>

  //search by optional state
  searchState?.let { state ->
   if (state.isNotEmpty()) { // <2>
    val stateQueryExpression = Function
     .lower(Expression.property(stateAttributeName))
     .like(Expression.string("%" + state.lowercase() + "%"))  // <2>

     whereQueryExpression =
      whereQueryExpression.and(stateQueryExpression)  // <2>
   }
  }

  //add type filter
  val typeQueryExpression = Function
   .lower(Expression.property(typeAttributeName))
   .equalTo(Expression.string(documentType))  // <3>

  whereQueryExpression = whereQueryExpression.and(typeQueryExpression) // <3>

  //create query to execute using QueryBuilder API
  val query = QueryBuilder
   .select(SelectResult.all())
   .from(DataSource.database(database).`as`(itemAliasName))
   .where(whereQueryExpression) // <4>

  //loop through results and add to list
  query.execute().allResults().forEach { item -> // <5>
   val json = item.toJSON()
   val warehouse = Json.decodeFromString<WarehouseDao>(json).item
   warehouses.add(warehouse)
  }
 }
} catch (e: Exception){
 Log.e(e.message, e.stackTraceToString())
}
  1. Create a query expression off the city attribute and use the like function to pass in the searchCity value.
  2. If the searchState value is not null, then we will create a query expression off the state attribute and use the like function to pass in the searchState value.
  3. Create a query expression off the type attribute and use the equalTo function to pass in the documentType value. This makes sure we only match warehouse documents.
  4. We will build a query using the QueryBuilder API to select all the results from the database and pass in our whereQueryExpression with our like statement(s).
  5. We loop through all the results and add them to the warehouses list that is then returned to the ViewModel.

Mehr erfahren

Herzlichen Glückwunsch zu diesem Schritt auf unserem Lernweg!

This step of the learning path walked you through the Query Builder API in Couchbase Lite and used it to return documents from the database and we looked at calling the Query API built-in count function. Check out the following links for further documenation and continue on to the next step to learn more about how to use Query Builder with SQL++ syntax.

Hinweise

Vergessen Sie die Databasenflut

Jede Stapelaufteilung bedeutet eine weitere Rechnung und mehr zu verwalten. Schätzen Sie aus, wie viel Sie in 2 Minuten sparen könnten.