Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs #14

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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