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

Add docs for query operators #549

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
105 changes: 105 additions & 0 deletions client/www/pages/docs/instaql.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,116 @@ console.log(data)
{
"id": reviewPRsId,
"title": "Review PRs"
}
]
}
```

### Comparison operators

The `where` clause supports comparison operators on fields that are indexed and have checked types.

{% callout %}
Add indexes and checked types to your attributes from the [Explorer on the the Instant dashboard](/dash?t=explorer) or from the [cli with Schema-as-code](/docs/schema).
{% /callout %}

| Operator | Description | JS equivalent |
| :------: | :----------------------: | :-----------: |
| `$gt` | greater than | `>` |
| `$lt` | less than | `<` |
| `$gte` | greater than or equal to | `>=` |
| `$lte` | less than or equal to | `<=` |

```javascript
const query = {
todos: {
$: {
where: {
timeEstimateHours: { $gt: 24 },
},
},
},
};
const { isLoading, error, data } = db.useQuery(query);
```

```javascript
console.log(data);
{
"todos": [
{
"id": buildShipId,
"title": "Build a starship prototype",
"timeEstimateHours": 5000
}
]
}
```

Dates can be stored as timestamps (milliseconds since the epoch, e.g. `Date.now()`) or as ISO 8601 strings (e.g. `JSON.stringify(new Date())`) and can be queried in the same formats:

```javascript
const now = '2024-11-26T15:25:00.054Z';
const query = {
todos: {
$: { where: { dueDate: { $lte: now } } },
},
};
const { isLoading, error, data } = db.useQuery(query);
```

```javascript
console.log(data);
{
"todos": [
{
"id": slsFlightId,
"title": "Space Launch System maiden flight",
"dueDate": "2017-01-01T00:00:00Z"
}
]
}
```

If you try to use comparison operators on data that isn't indexed and type-checked, you'll get an error:

```javascript
const query = {
todos: {
$: { where: { priority: { $gt: 2 } } },
},
};
const { isLoading, error, data } = db.useQuery(query);
```

```javascript
console.log(error);
{
"message": "Validation failed for query",
"hint": {
"data-type": "query",
"errors": [
{
"expected?": "indexed?",
"in": ["priority", "$", "where", "priority"],
"message": "The `todos.priority` attribute must be indexed to use comparison operators."
}
],
"input": {
"todos": {
"$": {
"where": {
"priority": {
"$gt": 2
}
}
}
}
}
}
}
```

### $not

The `where` clause supports `$not` queries that will return entities that don't
Expand Down