Skip to content

Commit 0d662fb

Browse files
committed
Allow setting cursor item types on aql strings
1 parent ade2b12 commit 0d662fb

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ This driver uses semantic versioning:
2323
Previously these methods would make requests without a database prefix,
2424
implicitly using the `_system` database.
2525

26+
- `aql` template strings now take a generic type argument
27+
28+
This allows explictly setting the item type of the `ArrayCursor` returned by
29+
`db.query` when using `aql` template strings. Note that like when setting
30+
the type on `db.query` directly, arangojs can make no guarantees that the
31+
type matches the actual data returned by the query.
32+
33+
```ts
34+
const numbers = await db.query(aql<{ index: number; squared: number }>`
35+
FOR i IN 1..1000
36+
RETURN {
37+
index: i,
38+
squared: i * i
39+
}
40+
`);
41+
const first = await numbers.next(); // { index: number; squared: number; }
42+
console.log(first.index, first.squared); // 1 1
43+
```
44+
2645
### Fixed
2746

2847
- Fixed `listUsers` behavior ([#782](https://github.com/arangodb/arangojs/issues/782))
@@ -244,7 +263,7 @@ for upgrading your code to arangojs v8.
244263
squared: i * i
245264
}
246265
`);
247-
const first = await numbers.next();
266+
const first = await numbers.next(); // { index: number; squared: number; }
248267
console.log(first.index, first.squared); // 1 1
249268
```
250269

src/aql.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import { ArangoCollection, isArangoCollection } from "./collection";
1515
import { Graph, isArangoGraph } from "./graph";
1616
import { isArangoView, View } from "./view";
1717

18+
declare const type: unique symbol;
19+
1820
/**
1921
* Generic AQL query object consisting of an AQL query string and its bind
2022
* parameters.
2123
*/
22-
export interface AqlQuery {
24+
export interface AqlQuery<T = any> {
25+
[type]?: T;
2326
/**
2427
* An AQL query string.
2528
*/
@@ -40,7 +43,7 @@ export interface AqlQuery {
4043
*
4144
* @internal
4245
*/
43-
export interface GeneratedAqlQuery extends AqlQuery {
46+
export interface GeneratedAqlQuery<T = any> extends AqlQuery<T> {
4447
/**
4548
* @internal
4649
*/
@@ -200,10 +203,10 @@ export function isAqlLiteral(literal: any): literal is AqlLiteral {
200203
* `);
201204
* ```
202205
*/
203-
export function aql(
206+
export function aql<T = any>(
204207
templateStrings: TemplateStringsArray,
205208
...args: AqlValue[]
206-
): GeneratedAqlQuery {
209+
): GeneratedAqlQuery<T> {
207210
const strings = [...templateStrings];
208211
const bindVars: Record<string, any> = {};
209212
const bindValues = [];

src/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3808,7 +3808,7 @@ export class Database {
38083808
* ```
38093809
*/
38103810
query<T = any>(
3811-
query: AqlQuery,
3811+
query: AqlQuery<T>,
38123812
options?: QueryOptions
38133813
): Promise<ArrayCursor<T>>;
38143814
/**

0 commit comments

Comments
 (0)