Skip to content

Commit 5c4f0b6

Browse files
committed
add documentation for new (and some old) DataSync functions
1 parent f90ebb2 commit 5c4f0b6

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Guide/realtime-spas.markdown

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,81 @@ const todos = await query('todos').fetchAndRefresh(callback);
597597
598598
The `fetchAndRefresh` function is using a websocket to be notified about any changes to the selected data set. [It's using IHP's `DataSubscription` API.](https://github.com/digitallyinduced/ihp/blob/master/lib/IHP/static/vendor/ihp-datasync.js) For more fine grained control you can use the `DataSubscription` API directly instead of relying on `fetchAndRefresh`.
599599
600+
#### Filtering
601+
602+
Filtering records works similar to normal IHP. In JavaScript you have the choice between `filterWhere` and `where` - the two function work identically.
603+
604+
```javascript
605+
const todos = await query('todos')
606+
.filterWhere('id', 'd94173ec-1d91-421e-8fdc-20a3161b7802')
607+
.fetch()
608+
609+
// is equivalent to:
610+
611+
const todos = await query('todos')
612+
.where('id', 'd94173ec-1d91-421e-8fdc-20a3161b7802')
613+
.fetch()
614+
```
615+
616+
##### Chaining conditions
617+
618+
You can chain as many conditions as you want - they will be `AND`ed in the resulting SQL query:
619+
620+
```javascript
621+
const todos = await('todos')
622+
.where('title', 'test')
623+
.where('id', 'd94173ec-1d91-421e-8fdc-20a3161b7802')
624+
.fetch()
625+
626+
// SQL:
627+
// SELECT * FROM todos
628+
// WHERE title = 'test'
629+
// AND id = 'd94173ec-1d91-421e-8fdc-20a3161b7802'
630+
```
631+
632+
##### Adding alternative conditions using `or`
633+
634+
You can also combine conditions using or with the `.or` function, which will take all previous conditions and allow any row to match that fulfills the alternative condition:
635+
636+
```javascript
637+
// add this import
638+
import { where } from 'ihp-datasync/ihp-querybuilder'
639+
640+
const todos = await('todos')
641+
.where('id', 'd94173ec-1d91-421e-8fdc-20a3161b7802')
642+
.or(where('id', '173ecd94-911d-1e42-dc8f-1b780320a316'))
643+
.fetch()
644+
645+
// SQL:
646+
// SELECT * FROM todos
647+
// WHERE id = 'd94173ec-1d91-421e-8fdc-20a3161b7802'
648+
// OR id = '173ecd94-911d-1e42-dc8f-1b780320a316'
649+
```
650+
651+
You can of course chain the conditions inside the `.or` call as well.
652+
653+
##### Adding additional conditions using `and`
654+
655+
In some cases you might want to add a combination of conditions to a query using `and` and doing so by chaining would be unclear. In that case, you can use the `.and` function which functions identically to the `.or` function, except for of course combining the conditions using `AND`, not `OR`.
656+
657+
```javascript
658+
// add this import
659+
import { where } from 'ihp-datasync/ihp-querybuilder'
660+
661+
const todos = await('todos')
662+
.where('userId', '173ecd94-911d-1e42-dc8f-1b780320a316')
663+
.and(where('title', 'Test').or(where('title', 'test')))
664+
665+
// SQL:
666+
// SELECT * FROM todos
667+
// WHERE user_id = '173ecd94-911d-1e42-dc8f-1b780320a316'
668+
// AND (title = 'Test' OR title = 'test')
669+
```
670+
671+
##### Checking for inequality
672+
673+
If you want to check for inequality instead of equality of a column value, you can use the `filterWhereNot` and `whereNot` functions. They work identically to their `filterWhere` and `where` counterparts and can be used in any situation those functions can be used in.
674+
600675
#### Fetching a single record
601676
602677
When you have the id of a record, you can also use `fetchOne` to get it from the database:

0 commit comments

Comments
 (0)