Skip to content

Commit

Permalink
Merge pull request #14 from Couchbase-Ecosystem/update-ionic-docs
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
biozal authored May 30, 2024
2 parents 7581287 + 3ecf937 commit 5f8c8c9
Show file tree
Hide file tree
Showing 14 changed files with 2,222 additions and 87 deletions.
43 changes: 42 additions & 1 deletion docs/Queries/live-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@ id: live-queries
sidebar_position: 6
---

# Live Queries
# Live Queries

> Description - _Couchbase Lite Live Query Concepts_
> Related Content - [SQL++ for Mobile](sqlplusplus.md)
## Activating a Live Query

A live query is a query that, once activated, remains active and monitors the database for changes; refreshing the result set whenever a change occurs. As such, it is a great way to build reactive user interfaces — especially table/list views — that keep themselves up to date.

**So, a simple use case may be:** A replicator running and pulling new data from a server, whilst a live-query-driven UI automatically updates to show the data without the user having to manually refresh. This helps your app feel quick and responsive.

With Couchbase Lite for Ionic, live queries can be watched through:

* Listener callbacks: `Query.addChangeListener`

Each time you start watching a live query, the query is executed and an initial change notification is dispatched. The query is then kept active and further change notifications are dispatched whenever a change occurs.

#### Example 1. Starting a Live Query - Change Listener

```typescript
// Register a change listener and await the Promise returned from the registration call.
const token = await query.addChangeListener((change) => {
if (change.error !== null && change.error !== undefined) {
// deal with error...
} else {
const results = change.results;
//loop through ResultSet
for (const doc of results) {
//do something with doc
}
}
});
```

To stop receiving notifications, call `Query.removeChangeListener` with the token that was returned from the registration call. Regardless of the whether the API is synchronous or asynchronous, listeners will stop receiving notifications immediately:

#### Example 2. Stopping a Live Query - Change Listener

```typescript
const token = await query.addChangeListener((change) => { ... });
await query.removeChangeListener(token);
```
6 changes: 0 additions & 6 deletions docs/Queries/query-builder.md

This file was deleted.

19 changes: 18 additions & 1 deletion docs/Queries/query-result-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,21 @@ id: query-result-set
sidebar_position: 5
---

# Query Result Sets
# Query Result Sets

When querying a database, the results are returned as an array of objects (`ResultSet`). Each object (`Result`) has keys based on the collection names used in the `FROM` statement of your query. If an alias is used, the key will be the alias name. This allows you to access the properties of the results easily and ensures that the structure of your results matches the query structure.

#### Example 1. Query Result Sets

```typescript
const query = database.createQuery('SELECT * FROM inventory.hotel AS hotelItems WHERE city="Medway"');
const resultSet: ResultSet = await query.execute();

for (const result of resultSet) {
console.log(result['hotelItems'].propertyName);
}
```

In this example, `hotelItems` is the alias used for the collection in the query, and it serves as the key in the `Result` objects within the `ResultSet`.


Loading

0 comments on commit 5f8c8c9

Please sign in to comment.