Skip to content

Commit 745fe16

Browse files
committed
Fix aql.literal
1 parent f891774 commit 745fe16

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/aql-query.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
export type AqlQuery = {
1+
import { ArangoCollection, isArangoCollection } from "./collection";
2+
3+
export interface AqlQuery {
24
query: string;
35
bindVars: { [key: string]: any };
4-
};
6+
}
57

6-
export type AqlLiteral = {
8+
export interface AqlLiteral {
79
toAQL: () => string;
8-
};
10+
}
11+
12+
export type AqlValue =
13+
| string
14+
| number
15+
| boolean
16+
| ArangoCollection
17+
| AqlLiteral;
918

1019
export function isAqlQuery(query: any): query is AqlQuery {
1120
return Boolean(query && query.query && query.bindVars);
@@ -15,21 +24,24 @@ export function isAqlLiteral(literal: any): literal is AqlLiteral {
1524
return Boolean(literal && typeof literal.toAQL === "function");
1625
}
1726

18-
export function aql(strings: TemplateStringsArray, ...args: any[]): AqlQuery {
27+
export function aql(
28+
strings: TemplateStringsArray,
29+
...args: AqlValue[]
30+
): AqlQuery {
1931
const bindVars: AqlQuery["bindVars"] = {};
2032
const bindVals = [];
2133
let query = strings[0];
2234
for (let i = 0; i < args.length; i++) {
2335
const rawValue = args[i];
2436
let value = rawValue;
25-
if (rawValue && typeof rawValue.toAQL === "function") {
37+
if (isAqlLiteral(rawValue)) {
2638
query += `${rawValue.toAQL()}${strings[i + 1]}`;
2739
continue;
2840
}
2941
const index = bindVals.indexOf(rawValue);
3042
const isKnown = index !== -1;
3143
let name = `value${isKnown ? index : bindVals.length}`;
32-
if (rawValue && rawValue.isArangoCollection) {
44+
if (isArangoCollection(rawValue)) {
3345
name = `@${name}`;
3446
value = rawValue.name;
3547
}
@@ -42,4 +54,10 @@ export function aql(strings: TemplateStringsArray, ...args: any[]): AqlQuery {
4254
return { query, bindVars };
4355
}
4456

45-
aql.literal = (value: any) => ({toAQL () {return value;}});
57+
export namespace aql {
58+
export const literal = (value: any): AqlLiteral => ({
59+
toAQL() {
60+
return String(value);
61+
}
62+
});
63+
}

src/test/05-aql-queries.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Database, aql } from "../arangojs";
2-
3-
import { ArangoError } from "../error";
4-
import { ArrayCursor } from "../cursor";
51
import { expect } from "chai";
2+
import { aql, Database } from "../arangojs";
3+
import { ArrayCursor } from "../cursor";
4+
import { ArangoError } from "../error";
65

76
describe("AQL queries", function() {
87
// create database takes 11s in a standard cluster
@@ -134,7 +133,7 @@ describe("AQL queries", function() {
134133
});
135134
describe("aql", () => {
136135
it("correctly handles simple parameters", () => {
137-
let values = [
136+
let values: any[] = [
138137
0,
139138
42,
140139
-1,
@@ -190,7 +189,7 @@ describe("AQL queries", function() {
190189
name = "tomato";
191190
}
192191
let collection = new ArangoCollection();
193-
let query = aql`${collection}`;
192+
let query = aql`${collection as any}`;
194193
expect(query.query).to.equal("@@value0");
195194
expect(Object.keys(query.bindVars)).to.eql(["@value0"]);
196195
expect(query.bindVars["@value0"]).to.equal("tomato");

0 commit comments

Comments
 (0)