Skip to content

Commit 78a7c49

Browse files
authored
Update filtering example (#90)
1 parent a49273f commit 78a7c49

File tree

3 files changed

+114
-48
lines changed

3 files changed

+114
-48
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
```js
2+
const db = new Polybase({ defaultNamespace: "your-namespace" });
3+
const collectionReference = db.collection("cities");
4+
const records = await collectionReference
5+
.where("name", "==", "abc")
6+
.where("country", "==", "UK")
7+
.get();
8+
```
9+
10+
You would need the following schema:
11+
12+
```js
13+
@public
14+
collection cities {
15+
name: string;
16+
country: string;
17+
18+
@index(name, country);
19+
20+
...
21+
}
22+
```
23+
24+
If you want to order your results, you also need to include that in the index:
25+
26+
```js
27+
const db = new Polybase({ defaultNamespace: "your-namespace" });
28+
const collectionReference = db.collection("cities");
29+
const records = await collectionReference
30+
.where("name", "==", "abc")
31+
.sort("population", "desc")
32+
.get();
33+
```
34+
35+
You would need the following schema:
36+
37+
```js
38+
@public
39+
collection cities {
40+
name: string;
41+
country: string;
42+
population: number;
43+
44+
@index(name, [population, desc]);
45+
}
46+
```

collections.mdx

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -512,53 +512,8 @@ you only need to specify multi-field indexes.
512512
Indexing array/map fields is currently not possible.
513513
</Note>
514514

515+
<Snippet file="indexes/multi-field-index.mdx" />
515516

516-
```js
517-
const db = new Polybase({ defaultNamespace: "your-namespace" });
518-
const collectionReference = db.collection("cities");
519-
const records = await collectionReference
520-
.where("name", "==", "abc")
521-
.where("country", "==", "UK")
522-
.get();
523-
```
524-
525-
You would need the following schema:
526-
527-
```js
528-
@public
529-
collection cities {
530-
name: string;
531-
country: string;
532-
533-
@index(name, country);
534-
535-
...
536-
}
537-
```
538-
539-
If you want to order your results, you also need to include that in the index:
540-
541-
```js
542-
const db = new Polybase({ defaultNamespace: "your-namespace" });
543-
const collectionReference = db.collection("cities");
544-
const records = await collectionReference
545-
.where("name", "==", "abc")
546-
.sort("population", "desc")
547-
.get();
548-
```
549-
550-
You would need the following schema:
551-
552-
```js
553-
@public
554-
collection cities {
555-
name: string;
556-
country: string;
557-
population: number;
558-
559-
@index(name, [population, desc]);
560-
}
561-
```
562517

563518
## Permissions
564519

read.mdx

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ export async function listRecords () {
6969

7070
## Filter records
7171

72-
To use the `where()` filter, you must have a corresponding index specified on
73-
the collection.
72+
Filter records using `where(name, op, value)` filters. For single field where/sort clauses, you do not need to explicitly
73+
define your index.
74+
75+
Valid operators are:
76+
77+
- `==` - Equals
78+
- `>` - Greater than
79+
- `>=` - Greater than or equal to
80+
- `<` - Less than
81+
- `<=` - Less than or equal to
82+
7483

7584
```js
7685
import { Polybase } from "@polybase/client"
@@ -87,9 +96,65 @@ export async function listRecordsWithFilter () {
8796
// Records is QueryResponse, so we can use it to get the next page of results
8897
await records.next();
8998
}
99+
```
100+
101+
### Collection filters
102+
103+
You can filter on a field of type `Collection`, by passing a Collection Record reference to the `where` clause. Only
104+
`==` is supported for collection filters.
105+
106+
<Note>
107+
Fields of type Collection must be indexed explicitly `@index(collectionField)`.
108+
</Note>
109+
110+
```js
111+
import { Polybase } from "@polybase/client"
112+
113+
const db = new Polybase({ defaultNamespace: "your-namespace" });
114+
const collectionReference = db.collection("NFT");
115+
116+
export async function listRecordsWithFilter () {
117+
const collectionRecord = collectionReference.record("user_id");
118+
const records = await collectionReference.where("user", "==", collectionRecord).get();
119+
120+
// Array of records is available under the data property
121+
const { data, cursor } = records;
122+
123+
// Records is QueryResponse, so we can use it to get the next page of results
124+
await records.next();
125+
}
126+
```
127+
128+
You would need the following schema:
129+
130+
```js
131+
@public
132+
collection User {
133+
id: string;
134+
135+
...
136+
}
90137

138+
@public
139+
collection NFT {
140+
id: string;
141+
user: User;
142+
143+
@index(user);
144+
145+
...
146+
}
91147
```
92148

149+
150+
### Multiple filters
151+
152+
In order to use multiple where or sorts, you must specify the corresponding index.
153+
154+
<Snippet file="indexes/multi-field-index.mdx" />
155+
156+
157+
93158
## Listen for updates on a collection
94159

95160
```js

0 commit comments

Comments
 (0)