arangojs 7.8.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +264 -3
- package/CONTRIBUTING.md +1 -1
- package/MIGRATING.md +126 -2
- package/README.md +7 -13
- package/analyzer.d.ts +531 -253
- package/analyzer.d.ts.map +1 -1
- package/analyzer.js +9 -8
- package/analyzer.js.map +1 -1
- package/aql.d.ts +122 -139
- package/aql.d.ts.map +1 -1
- package/aql.js +146 -148
- package/aql.js.map +1 -1
- package/collection.d.ts +311 -310
- package/collection.d.ts.map +1 -1
- package/collection.js +163 -96
- package/collection.js.map +1 -1
- package/connection.d.ts +65 -52
- package/connection.d.ts.map +1 -1
- package/connection.js +142 -140
- package/connection.js.map +1 -1
- package/cursor.d.ts +108 -26
- package/cursor.d.ts.map +1 -1
- package/cursor.js +24 -23
- package/cursor.js.map +1 -1
- package/database.d.ts +298 -330
- package/database.d.ts.map +1 -1
- package/database.js +178 -252
- package/database.js.map +1 -1
- package/documents.d.ts +3 -4
- package/documents.d.ts.map +1 -1
- package/documents.js +0 -1
- package/documents.js.map +1 -1
- package/error.d.ts +3 -6
- package/error.d.ts.map +1 -1
- package/error.js +5 -7
- package/error.js.map +1 -1
- package/foxx-manifest.d.ts +7 -8
- package/foxx-manifest.d.ts.map +1 -1
- package/foxx-manifest.js +1 -1
- package/foxx-manifest.js.map +1 -1
- package/graph.d.ts +38 -56
- package/graph.d.ts.map +1 -1
- package/graph.js +59 -63
- package/graph.js.map +1 -1
- package/index.d.ts +6 -8
- package/index.d.ts.map +1 -1
- package/index.js +1 -6
- package/index.js.map +1 -1
- package/indexes.d.ts +348 -127
- package/indexes.d.ts.map +1 -1
- package/indexes.js +1 -3
- package/indexes.js.map +1 -1
- package/lib/blob.d.ts +0 -2
- package/lib/blob.d.ts.map +1 -1
- package/lib/blob.js +0 -1
- package/lib/blob.js.map +1 -1
- package/lib/btoa.d.ts +1 -3
- package/lib/btoa.d.ts.map +1 -1
- package/lib/btoa.js +3 -5
- package/lib/btoa.js.map +1 -1
- package/lib/btoa.web.d.ts +1 -3
- package/lib/btoa.web.d.ts.map +1 -1
- package/lib/btoa.web.js +4 -6
- package/lib/btoa.web.js.map +1 -1
- package/lib/codes.d.ts +2 -2
- package/lib/codes.d.ts.map +1 -1
- package/lib/codes.js +3 -3
- package/lib/codes.js.map +1 -1
- package/lib/errback.d.ts +0 -2
- package/lib/errback.d.ts.map +1 -1
- package/lib/errback.js.map +1 -1
- package/lib/joinPath.d.ts +0 -2
- package/lib/joinPath.d.ts.map +1 -1
- package/lib/joinPath.js +0 -2
- package/lib/joinPath.js.map +1 -1
- package/lib/joinPath.web.d.ts +1 -3
- package/lib/joinPath.web.d.ts.map +1 -1
- package/lib/joinPath.web.js +7 -4
- package/lib/joinPath.web.js.map +1 -1
- package/lib/multipart.d.ts +0 -4
- package/lib/multipart.d.ts.map +1 -1
- package/lib/multipart.js +16 -37
- package/lib/multipart.js.map +1 -1
- package/lib/multipart.web.d.ts +0 -2
- package/lib/multipart.web.d.ts.map +1 -1
- package/lib/multipart.web.js +0 -2
- package/lib/multipart.web.js.map +1 -1
- package/lib/normalizeUrl.d.ts +0 -2
- package/lib/normalizeUrl.d.ts.map +1 -1
- package/lib/normalizeUrl.js +0 -2
- package/lib/normalizeUrl.js.map +1 -1
- package/lib/omit.d.ts +1 -3
- package/lib/omit.d.ts.map +1 -1
- package/lib/omit.js +1 -2
- package/lib/omit.js.map +1 -1
- package/lib/querystringify.d.ts +4 -0
- package/lib/querystringify.d.ts.map +1 -0
- package/lib/querystringify.js +20 -0
- package/lib/querystringify.js.map +1 -0
- package/lib/querystringify.web.d.ts +2 -0
- package/lib/querystringify.web.d.ts.map +1 -0
- package/lib/querystringify.web.js +30 -0
- package/lib/querystringify.web.js.map +1 -0
- package/lib/request.d.ts +0 -1
- package/lib/request.d.ts.map +1 -1
- package/lib/request.js +6 -3
- package/lib/request.js.map +1 -1
- package/lib/request.node.d.ts +2 -8
- package/lib/request.node.d.ts.map +1 -1
- package/lib/request.node.js +16 -7
- package/lib/request.node.js.map +1 -1
- package/lib/request.web.d.ts +2 -3
- package/lib/request.web.d.ts.map +1 -1
- package/lib/request.web.js +26 -29
- package/lib/request.web.js.map +1 -1
- package/lib/xhr.d.ts +1 -3
- package/lib/xhr.d.ts.map +1 -1
- package/lib/xhr.js +0 -2
- package/lib/xhr.js.map +1 -1
- package/package.json +5 -6
- package/route.d.ts +1 -2
- package/route.d.ts.map +1 -1
- package/route.js +1 -2
- package/route.js.map +1 -1
- package/transaction.d.ts +31 -6
- package/transaction.d.ts.map +1 -1
- package/transaction.js +13 -6
- package/transaction.js.map +1 -1
- package/view.d.ts +237 -181
- package/view.d.ts.map +1 -1
- package/view.js +17 -25
- package/view.js.map +1 -1
- package/web.js +1 -1
- package/web.js.map +1 -1
- package/lib/error.d.ts +0 -18
- package/lib/error.d.ts.map +0 -1
- package/lib/error.js +0 -18
- package/lib/error.js.map +0 -1
package/aql.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.aql = exports.isAqlLiteral = exports.isGeneratedAqlQuery = exports.isAqlQuery = void 0;
|
|
3
|
+
exports.join = exports.literal = exports.aql = exports.isAqlLiteral = exports.isGeneratedAqlQuery = exports.isAqlQuery = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* ```js
|
|
6
6
|
* import { aql } from "arangojs/aql";
|
|
@@ -13,6 +13,7 @@ exports.aql = exports.isAqlLiteral = exports.isGeneratedAqlQuery = exports.isAql
|
|
|
13
13
|
*
|
|
14
14
|
* @packageDocumentation
|
|
15
15
|
*/
|
|
16
|
+
const analyzer_1 = require("./analyzer");
|
|
16
17
|
const collection_1 = require("./collection");
|
|
17
18
|
const graph_1 = require("./graph");
|
|
18
19
|
const view_1 = require("./view");
|
|
@@ -31,7 +32,6 @@ exports.isAqlQuery = isAqlQuery;
|
|
|
31
32
|
* @param query - A value that might be a `GeneratedAqlQuery`.
|
|
32
33
|
*
|
|
33
34
|
* @internal
|
|
34
|
-
* @hidden
|
|
35
35
|
*/
|
|
36
36
|
function isGeneratedAqlQuery(query) {
|
|
37
37
|
return isAqlQuery(query) && typeof query._source === "function";
|
|
@@ -56,8 +56,8 @@ exports.isAqlLiteral = isAqlLiteral;
|
|
|
56
56
|
* Tagged template strings will return an {@link AqlQuery} object with
|
|
57
57
|
* `query` and `bindVars` attributes reflecting any interpolated values.
|
|
58
58
|
*
|
|
59
|
-
* Any {@link ArangoCollection} instance used in a query string will
|
|
60
|
-
* recognized as a collection reference and generate an AQL collection bind
|
|
59
|
+
* Any {@link collection.ArangoCollection} instance used in a query string will
|
|
60
|
+
* be recognized as a collection reference and generate an AQL collection bind
|
|
61
61
|
* parameter instead of a regular AQL value bind parameter.
|
|
62
62
|
*
|
|
63
63
|
* **Note**: you should always use the `aql` template tag when writing
|
|
@@ -169,9 +169,10 @@ function aql(templateStrings, ...args) {
|
|
|
169
169
|
const index = bindValues.indexOf(rawValue);
|
|
170
170
|
const isKnown = index !== -1;
|
|
171
171
|
let name = `value${isKnown ? index : bindValues.length}`;
|
|
172
|
-
if (collection_1.isArangoCollection(rawValue) ||
|
|
173
|
-
graph_1.isArangoGraph(rawValue) ||
|
|
174
|
-
view_1.isArangoView(rawValue)
|
|
172
|
+
if ((0, collection_1.isArangoCollection)(rawValue) ||
|
|
173
|
+
(0, graph_1.isArangoGraph)(rawValue) ||
|
|
174
|
+
(0, view_1.isArangoView)(rawValue) ||
|
|
175
|
+
(0, analyzer_1.isArangoAnalyzer)(rawValue)) {
|
|
175
176
|
name = `@${name}`;
|
|
176
177
|
value = rawValue.name;
|
|
177
178
|
}
|
|
@@ -188,147 +189,144 @@ function aql(templateStrings, ...args) {
|
|
|
188
189
|
};
|
|
189
190
|
}
|
|
190
191
|
exports.aql = aql;
|
|
191
|
-
|
|
192
|
-
(
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
if (isAqlLiteral(value)) {
|
|
246
|
-
return value;
|
|
247
|
-
}
|
|
248
|
-
return {
|
|
249
|
-
toAQL() {
|
|
250
|
-
if (value === undefined) {
|
|
251
|
-
return "";
|
|
252
|
-
}
|
|
253
|
-
return String(value);
|
|
254
|
-
},
|
|
255
|
-
};
|
|
192
|
+
/**
|
|
193
|
+
* Marks an arbitrary scalar value (i.e. a string, number or boolean) as
|
|
194
|
+
* safe for being inlined directly into AQL queries when used in an `aql`
|
|
195
|
+
* template string, rather than being converted into a bind parameter.
|
|
196
|
+
*
|
|
197
|
+
* **Note**: Nesting `aql` template strings is a much safer alternative for
|
|
198
|
+
* most use cases. This low-level helper function only exists to help with
|
|
199
|
+
* rare edge cases where a trusted AQL query fragment must be read from a
|
|
200
|
+
* string (e.g. when reading query fragments from JSON) and should only be
|
|
201
|
+
* used as a last resort.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```js
|
|
205
|
+
* // BAD! DO NOT DO THIS!
|
|
206
|
+
* const sortDirection = literal('ASC');
|
|
207
|
+
*
|
|
208
|
+
* // GOOD! DO THIS INSTEAD!
|
|
209
|
+
* const sortDirection = aql`ASC`;
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```js
|
|
214
|
+
* // BAD! DO NOT DO THIS!
|
|
215
|
+
* const filterColor = literal('FILTER d.color == "green"');
|
|
216
|
+
* const result = await db.query(aql`
|
|
217
|
+
* FOR d IN some-collection
|
|
218
|
+
* ${filterColor}
|
|
219
|
+
* RETURN d
|
|
220
|
+
* `);
|
|
221
|
+
*
|
|
222
|
+
* // GOOD! DO THIS INSTEAD!
|
|
223
|
+
* const color = "green";
|
|
224
|
+
* const filterColor = aql`FILTER d.color === ${color}`;
|
|
225
|
+
* const result = await db.query(aql`
|
|
226
|
+
* FOR d IN some-collection
|
|
227
|
+
* ${filterColor}
|
|
228
|
+
* RETURN d
|
|
229
|
+
* `);
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```js
|
|
234
|
+
* // WARNING: We explicitly trust the environment variable to be safe!
|
|
235
|
+
* const filter = literal(process.env.FILTER_STATEMENT);
|
|
236
|
+
* const users = await db.query(aql`
|
|
237
|
+
* FOR user IN users
|
|
238
|
+
* ${filter}
|
|
239
|
+
* RETURN user
|
|
240
|
+
* `);
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
function literal(value) {
|
|
244
|
+
if (isAqlLiteral(value)) {
|
|
245
|
+
return value;
|
|
256
246
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
247
|
+
return {
|
|
248
|
+
toAQL() {
|
|
249
|
+
if (value === undefined) {
|
|
250
|
+
return "";
|
|
251
|
+
}
|
|
252
|
+
return String(value);
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
exports.literal = literal;
|
|
257
|
+
/**
|
|
258
|
+
* Constructs {@link AqlQuery} objects from an array of arbitrary values.
|
|
259
|
+
*
|
|
260
|
+
* **Note**: Nesting `aql` template strings is a much safer alternative
|
|
261
|
+
* for most use cases. This low-level helper function only exists to
|
|
262
|
+
* complement the `aql` tag when constructing complex queries from dynamic
|
|
263
|
+
* arrays of query fragments.
|
|
264
|
+
*
|
|
265
|
+
* @param values - Array of values to join. These values will behave exactly
|
|
266
|
+
* like values interpolated in an `aql` template string.
|
|
267
|
+
* @param sep - Seperator to insert between values. This value will behave
|
|
268
|
+
* exactly like a value passed to {@link literal}, i.e. it will be
|
|
269
|
+
* inlined as-is, rather than being converted into a bind parameter.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```js
|
|
273
|
+
* const users = db.collection("users");
|
|
274
|
+
* const filters = [];
|
|
275
|
+
* if (adminsOnly) filters.push(aql`FILTER user.admin`);
|
|
276
|
+
* if (activeOnly) filters.push(aql`FILTER user.active`);
|
|
277
|
+
* const result = await db.query(aql`
|
|
278
|
+
* FOR user IN ${users}
|
|
279
|
+
* ${join(filters)}
|
|
280
|
+
* RETURN user
|
|
281
|
+
* `);
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```js
|
|
286
|
+
* const users = db.collection("users");
|
|
287
|
+
* const keys = ["jreyes", "ghermann"];
|
|
288
|
+
*
|
|
289
|
+
* // BAD! NEEDLESSLY COMPLEX!
|
|
290
|
+
* const docs = keys.map(key => aql`DOCUMENT(${users}, ${key}`));
|
|
291
|
+
* const result = await db.query(aql`
|
|
292
|
+
* FOR user IN [
|
|
293
|
+
* ${join(docs, ", ")}
|
|
294
|
+
* ]
|
|
295
|
+
* RETURN user
|
|
296
|
+
* `);
|
|
297
|
+
* // Query:
|
|
298
|
+
* // FOR user IN [
|
|
299
|
+
* // DOCUMENT(@@value0, @value1), DOCUMENT(@@value0, @value2)
|
|
300
|
+
* // ]
|
|
301
|
+
* // RETURN user
|
|
302
|
+
* // Bind parameters:
|
|
303
|
+
* // @value0 -> "users"
|
|
304
|
+
* // value1 -> "jreyes"
|
|
305
|
+
* // value2 -> "ghermann"
|
|
306
|
+
*
|
|
307
|
+
* // GOOD! MUCH SIMPLER!
|
|
308
|
+
* const result = await db.query(aql`
|
|
309
|
+
* FOR key IN ${keys}
|
|
310
|
+
* LET user = DOCUMENT(${users}, key)
|
|
311
|
+
* RETURN user
|
|
312
|
+
* `);
|
|
313
|
+
* // Query:
|
|
314
|
+
* // FOR user IN @value0
|
|
315
|
+
* // LET user = DOCUMENT(@@value1, key)
|
|
316
|
+
* // RETURN user
|
|
317
|
+
* // Bind parameters:
|
|
318
|
+
* // value0 -> ["jreyes", "ghermann"]
|
|
319
|
+
* // @value1 -> "users"
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
function join(values, sep = " ") {
|
|
323
|
+
if (!values.length) {
|
|
324
|
+
return aql ``;
|
|
331
325
|
}
|
|
332
|
-
|
|
333
|
-
|
|
326
|
+
if (values.length === 1) {
|
|
327
|
+
return aql `${values[0]}`;
|
|
328
|
+
}
|
|
329
|
+
return aql(["", ...Array(values.length - 1).fill(sep), ""], ...values);
|
|
330
|
+
}
|
|
331
|
+
exports.join = join;
|
|
334
332
|
//# sourceMappingURL=aql.js.map
|
package/aql.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aql.js","sourceRoot":"","sources":["../src/aql.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;GAWG;AACH,6CAAoE;AAEpE,mCAA+C;AAC/C,iCAA4C;AAsE5C;;;;GAIG;AACH,SAAgB,UAAU,CAAC,KAAU;IACnC,OAAO,OAAO,CAAC,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC;AAC7E,CAAC;AAFD,gCAEC;AAED;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CAAC,KAAU;IAC5C,OAAO,UAAU,CAAC,KAAK,CAAC,IAAI,OAAQ,KAAa,CAAC,OAAO,KAAK,UAAU,CAAC;AAC3E,CAAC;AAFD,kDAEC;AAED;;;;GAIG;AACH,SAAgB,YAAY,CAAC,OAAY;IACvC,OAAO,OAAO,CAAC,OAAO,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,UAAU,CAAC,CAAC;AACjE,CAAC;AAFD,oCAEC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwFG;AACH,SAAgB,GAAG,CACjB,eAAqC,E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,CAAC;AA7DD,kBA6DC;AAED,2DAA2D;AAC3D,WAAiB,GAAG;IAClB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkDG;IACH,SAAgB,OAAO,CACrB,KAAgE;QAEhE,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE;YACvB,OAAO,KAAK,CAAC;SACd;QACD,OAAO;YACL,KAAK;gBACH,IAAI,KAAK,KAAK,SAAS,EAAE;oBACvB,OAAO,EAAE,CAAC;iBACX;gBACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;SACF,CAAC;IACJ,CAAC;IAde,WAAO,UActB,CAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgEG;IACH,SAAgB,IAAI,CAClB,MAAkB,EAClB,MAAc,GAAG;QAEjB,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YAClB,OAAO,GAAG,CAAA,EAAE,CAAC;SACd;QACD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;YACvB,OAAO,GAAG,CAAA,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;SAC1B;QACD,OAAO,GAAG,CACR,CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAQ,EACtD,GAAG,MAAM,CACV,CAAC;IACJ,CAAC;IAde,QAAI,OAcnB,CAAA;AACH,CAAC,EApJgB,GAAG,GAAH,WAAG,KAAH,WAAG,QAoJnB","sourcesContent":["/**\n * ```js\n * import { aql } from \"arangojs/aql\";\n * ```\n *\n * The \"aql\" module provides the {@link aql} template string handler and\n * helper functions, as well as associated types and interfaces for TypeScript.\n *\n * The aql function and namespace is also re-exported by the \"index\" module.\n *\n * @packageDocumentation\n */\nimport { ArangoCollection, isArangoCollection } from \"./collection\";\nimport { Dict } from \"./connection\";\nimport { Graph, isArangoGraph } from \"./graph\";\nimport { isArangoView, View } from \"./view\";\n\n/**\n * Generic AQL query object consisting of an AQL query string and its bind\n * parameters.\n */\nexport interface AqlQuery {\n /**\n * An AQL query string.\n */\n query: string;\n /**\n * An object mapping AQL bind parameter names to their respective values.\n *\n * Names of parameters representing collections are prefixed with an\n * at-symbol.\n */\n bindVars: Dict<any>;\n}\n\n/**\n * Derived type representing AQL query objects generated by the AQL helper\n * functions and the AQL template string handler. These objects can be fed\n * back into these helper functions to be inlined or merged in complex queries.\n *\n * @internal\n */\nexport interface GeneratedAqlQuery extends AqlQuery {\n /**\n * @internal\n * @hidden\n */\n _source: () => { strings: string[]; args: AqlValue[] };\n}\n\n/**\n * An object representing a trusted AQL literal that will be inlined directly\n * when used in an AQL template or passed to AQL helper functions.\n *\n * Arbitrary values can be converted to trusted AQL literals by passing them\n * to the {@link aql.literal} helper function.\n */\nexport interface AqlLiteral {\n /**\n * @internal\n *\n * Returns a string representation of this AQL literal that can be inlined\n * in an AQL template.\n */\n toAQL: () => string;\n}\n\n/**\n * A value that can be used in an AQL template string or passed to an AQL\n * helper function.\n */\nexport type AqlValue =\n | ArangoCollection\n | View\n | Graph\n | GeneratedAqlQuery\n | AqlLiteral\n | string\n | number\n | boolean\n | null\n | undefined\n | Record<string, any>\n | any[];\n\n/**\n * Indicates whether the given value is an {@link AqlQuery}.\n *\n * @param query - A value that might be an `AqlQuery`.\n */\nexport function isAqlQuery(query: any): query is AqlQuery {\n return Boolean(query && typeof query.query === \"string\" && query.bindVars);\n}\n\n/**\n * Indicates whether the given value is a {@link GeneratedAqlQuery}.\n *\n * @param query - A value that might be a `GeneratedAqlQuery`.\n *\n * @internal\n * @hidden\n */\nexport function isGeneratedAqlQuery(query: any): query is GeneratedAqlQuery {\n return isAqlQuery(query) && typeof (query as any)._source === \"function\";\n}\n\n/**\n * Indicates whether the given value is an {@link AqlLiteral}.\n *\n * @param literal - A value that might be an `AqlLiteral`.\n */\nexport function isAqlLiteral(literal: any): literal is AqlLiteral {\n return Boolean(literal && typeof literal.toAQL === \"function\");\n}\n\n/**\n * Template string handler (template tag) for AQL queries.\n *\n * The `aql` tag can be used to write complex AQL queries as multi-line strings\n * without having to worry about `bindVars` and the distinction between\n * collections and regular parameters.\n *\n * Tagged template strings will return an {@link AqlQuery} object with\n * `query` and `bindVars` attributes reflecting any interpolated values.\n *\n * Any {@link ArangoCollection} instance used in a query string will be\n * recognized as a collection reference and generate an AQL collection bind\n * parameter instead of a regular AQL value bind parameter.\n *\n * **Note**: you should always use the `aql` template tag when writing\n * dynamic AQL queries instead of using untagged (normal) template strings.\n * Untagged template strings will inline any interpolated values and return\n * a plain string as result. The `aql` template tag will only inline references\n * to the interpolated values and produce an AQL query object containing both\n * the query and the values. This prevents most injection attacks when using\n * untrusted values in dynamic queries.\n *\n * @example\n * ```js\n * // Some user-supplied string that may be malicious\n * const untrustedValue = req.body.email;\n *\n * // Without aql tag: BAD! DO NOT DO THIS!\n * const badQuery = `\n * FOR user IN users\n * FILTER user.email == \"${untrustedValue}\"\n * RETURN user\n * `;\n * // e.g. if untrustedValue is '\" || user.admin == true || \"':\n * // Query:\n * // FOR user IN users\n * // FILTER user.email == \"\" || user.admin == true || \"\"\n * // RETURN user\n *\n * // With the aql tag: GOOD! MUCH SAFER!\n * const betterQuery = aql`\n * FOR user IN users\n * FILTER user.email == ${untrustedValue}\n * RETURN user\n * `;\n * // Query:\n * // FOR user IN users\n * // FILTER user.email == @value0\n * // RETURN user\n * // Bind parameters:\n * // value0 -> untrustedValue\n * ```\n *\n * @example\n * ```js\n * const collection = db.collection(\"some-collection\");\n * const minValue = 23;\n * const result = await db.query(aql`\n * FOR d IN ${collection}\n * FILTER d.num > ${minValue}\n * RETURN d\n * `);\n *\n * // Equivalent raw query object\n * const result2 = await db.query({\n * query: `\n * FOR d IN @@collection\n * FILTER d.num > @minValue\n * RETURN d\n * `,\n * bindVars: {\n * \"@collection\": collection.name,\n * minValue: minValue\n * }\n * });\n * ```\n *\n * @example\n * ```js\n * const collection = db.collection(\"some-collection\");\n * const color = \"green\";\n * const filter = aql`FILTER d.color == ${color}'`;\n * const result = await db.query(aql`\n * FOR d IN ${collection}\n * ${filter}\n * RETURN d\n * `);\n * ```\n */\nexport function aql(\n templateStrings: TemplateStringsArray,\n ...args: AqlValue[]\n): GeneratedAqlQuery {\n const strings = [...templateStrings];\n const bindVars: Dict<any> = {};\n const bindValues = [];\n let query = strings[0];\n for (let i = 0; i < args.length; i++) {\n const rawValue = args[i];\n let value = rawValue;\n if (isGeneratedAqlQuery(rawValue)) {\n const src = rawValue._source();\n if (src.args.length) {\n query += src.strings[0];\n args.splice(i, 1, ...src.args);\n strings.splice(\n i,\n 2,\n strings[i] + src.strings[0],\n ...src.strings.slice(1, src.args.length),\n src.strings[src.args.length] + strings[i + 1]\n );\n } else {\n query += rawValue.query + strings[i + 1];\n args.splice(i, 1);\n strings.splice(i, 2, strings[i] + rawValue.query + strings[i + 1]);\n }\n i -= 1;\n continue;\n }\n if (rawValue === undefined) {\n query += strings[i + 1];\n continue;\n }\n if (isAqlLiteral(rawValue)) {\n query += `${rawValue.toAQL()}${strings[i + 1]}`;\n continue;\n }\n const index = bindValues.indexOf(rawValue);\n const isKnown = index !== -1;\n let name = `value${isKnown ? index : bindValues.length}`;\n if (\n isArangoCollection(rawValue) ||\n isArangoGraph(rawValue) ||\n isArangoView(rawValue)\n ) {\n name = `@${name}`;\n value = rawValue.name;\n }\n if (!isKnown) {\n bindValues.push(rawValue);\n bindVars[name] = value;\n }\n query += `@${name}${strings[i + 1]}`;\n }\n return {\n query,\n bindVars,\n _source: () => ({ strings, args }),\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-namespace\nexport namespace aql {\n /**\n * Marks an arbitrary scalar value (i.e. a string, number or boolean) as\n * safe for being inlined directly into AQL queries when used in an `aql`\n * template string, rather than being converted into a bind parameter.\n *\n * **Note**: Nesting `aql` template strings is a much safer alternative for\n * most use cases. This low-level helper function only exists to help with\n * rare edge cases where a trusted AQL query fragment must be read from a\n * string (e.g. when reading query fragments from JSON) and should only be\n * used as a last resort.\n *\n * @example\n * ```js\n * // BAD! DO NOT DO THIS!\n * const sortDirection = aql.literal('ASC');\n *\n * // GOOD! DO THIS INSTEAD!\n * const sortDirection = aql`ASC`;\n * ```\n *\n * @example\n * ```js\n * // BAD! DO NOT DO THIS!\n * const filterColor = aql.literal('FILTER d.color == \"green\"');\n * const result = await db.query(aql`\n * FOR d IN some-collection\n * ${filterColor}\n * RETURN d\n * `);\n *\n * // GOOD! DO THIS INSTEAD!\n * const color = \"green\";\n * const filterColor = aql`FILTER d.color === ${color}`;\n * const result = await db.query(aql`\n * FOR d IN some-collection\n * ${filterColor}\n * RETURN d\n * `);\n * ```\n *\n * @example\n * ```js\n * // WARNING: We explicitly trust the environment variable to be safe!\n * const filter = aql.literal(process.env.FILTER_STATEMENT);\n * const users = await db.query(aql`\n * FOR user IN users\n * ${filter}\n * RETURN user\n * `);\n * ```\n */\n export function literal(\n value: string | number | boolean | AqlLiteral | null | undefined\n ): AqlLiteral {\n if (isAqlLiteral(value)) {\n return value;\n }\n return {\n toAQL() {\n if (value === undefined) {\n return \"\";\n }\n return String(value);\n },\n };\n }\n\n /**\n * Constructs {@link AqlQuery} objects from an array of arbitrary values.\n *\n * **Note**: Nesting `aql` template strings is a much safer alternative\n * for most use cases. This low-level helper function only exists to\n * complement the `aql` tag when constructing complex queries from dynamic\n * arrays of query fragments.\n *\n * @param values - Array of values to join. These values will behave exactly\n * like values interpolated in an `aql` template string.\n * @param sep - Seperator to insert between values. This value will behave\n * exactly like a value passed to {@link aql.literal}, i.e. it will be\n * inlined as-is, rather than being converted into a bind parameter.\n *\n * @example\n * ```js\n * const users = db.collection(\"users\");\n * const filters = [];\n * if (adminsOnly) filters.push(aql`FILTER user.admin`);\n * if (activeOnly) filters.push(aql`FILTER user.active`);\n * const result = await db.query(aql`\n * FOR user IN ${users}\n * ${aql.join(filters)}\n * RETURN user\n * `);\n * ```\n *\n * @example\n * ```js\n * const users = db.collection(\"users\");\n * const keys = [\"jreyes\", \"ghermann\"];\n *\n * // BAD! NEEDLESSLY COMPLEX!\n * const docs = keys.map(key => aql`DOCUMENT(${users}, ${key}`));\n * const result = await db.query(aql`\n * FOR user IN [\n * ${aql.join(docs, \", \")}\n * ]\n * RETURN user\n * `);\n * // Query:\n * // FOR user IN [\n * // DOCUMENT(@@value0, @value1), DOCUMENT(@@value0, @value2)\n * // ]\n * // RETURN user\n * // Bind parameters:\n * // @value0 -> \"users\"\n * // value1 -> \"jreyes\"\n * // value2 -> \"ghermann\"\n *\n * // GOOD! MUCH SIMPLER!\n * const result = await db.query(aql`\n * FOR key IN ${keys}\n * LET user = DOCUMENT(${users}, key)\n * RETURN user\n * `);\n * // Query:\n * // FOR user IN @value0\n * // LET user = DOCUMENT(@@value1, key)\n * // RETURN user\n * // Bind parameters:\n * // value0 -> [\"jreyes\", \"ghermann\"]\n * // @value1 -> \"users\"\n * ```\n */\n export function join(\n values: AqlValue[],\n sep: string = \" \"\n ): GeneratedAqlQuery {\n if (!values.length) {\n return aql``;\n }\n if (values.length === 1) {\n return aql`${values[0]}`;\n }\n return aql(\n [\"\", ...Array(values.length - 1).fill(sep), \"\"] as any,\n ...values\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"aql.js","sourceRoot":"","sources":["../src/aql.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;GAWG;AACH,yCAA8C;AAC9C,6CAAoE;AACpE,mCAA+C;AAC/C,iCAA4C;AAqE5C;;;;GAIG;AACH,SAAgB,UAAU,CAAC,KAAU;IACnC,OAAO,OAAO,CAAC,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC;AAC7E,CAAC;AAFD,gCAEC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAC,KAAU;IAC5C,OAAO,UAAU,CAAC,KA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MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAQ,EAAE,GAAG,MAAM,CAAC,CAAC;AAChF,CAAC;AARD,oBAQC","sourcesContent":["/**\n * ```js\n * import { aql } from \"arangojs/aql\";\n * ```\n *\n * The \"aql\" module provides the {@link aql} template string handler and\n * helper functions, as well as associated types and interfaces for TypeScript.\n *\n * The aql function and namespace is also re-exported by the \"index\" module.\n *\n * @packageDocumentation\n */\nimport { isArangoAnalyzer } from \"./analyzer\";\nimport { ArangoCollection, isArangoCollection } from \"./collection\";\nimport { Graph, isArangoGraph } from \"./graph\";\nimport { isArangoView, View } from \"./view\";\n\n/**\n * Generic AQL query object consisting of an AQL query string and its bind\n * parameters.\n */\nexport interface AqlQuery {\n /**\n * An AQL query string.\n */\n query: string;\n /**\n * An object mapping AQL bind parameter names to their respective values.\n *\n * Names of parameters representing collections are prefixed with an\n * at-symbol.\n */\n bindVars: Record<string, any>;\n}\n\n/**\n * Derived type representing AQL query objects generated by the AQL helper\n * functions and the AQL template string handler. These objects can be fed\n * back into these helper functions to be inlined or merged in complex queries.\n *\n * @internal\n */\nexport interface GeneratedAqlQuery extends AqlQuery {\n /**\n * @internal\n */\n _source: () => { strings: string[]; args: AqlValue[] };\n}\n\n/**\n * An object representing a trusted AQL literal that will be inlined directly\n * when used in an AQL template or passed to AQL helper functions.\n *\n * Arbitrary values can be converted to trusted AQL literals by passing them\n * to the {@link literal} helper function.\n */\nexport interface AqlLiteral {\n /**\n * @internal\n *\n * Returns a string representation of this AQL literal that can be inlined\n * in an AQL template.\n */\n toAQL: () => string;\n}\n\n/**\n * A value that can be used in an AQL template string or passed to an AQL\n * helper function.\n */\nexport type AqlValue =\n | ArangoCollection\n | View\n | Graph\n | GeneratedAqlQuery\n | AqlLiteral\n | string\n | number\n | boolean\n | null\n | undefined\n | Record<string, any>\n | any[];\n\n/**\n * Indicates whether the given value is an {@link AqlQuery}.\n *\n * @param query - A value that might be an `AqlQuery`.\n */\nexport function isAqlQuery(query: any): query is AqlQuery {\n return Boolean(query && typeof query.query === \"string\" && query.bindVars);\n}\n\n/**\n * Indicates whether the given value is a {@link GeneratedAqlQuery}.\n *\n * @param query - A value that might be a `GeneratedAqlQuery`.\n *\n * @internal\n */\nexport function isGeneratedAqlQuery(query: any): query is GeneratedAqlQuery {\n return isAqlQuery(query) && typeof (query as any)._source === \"function\";\n}\n\n/**\n * Indicates whether the given value is an {@link AqlLiteral}.\n *\n * @param literal - A value that might be an `AqlLiteral`.\n */\nexport function isAqlLiteral(literal: any): literal is AqlLiteral {\n return Boolean(literal && typeof literal.toAQL === \"function\");\n}\n\n/**\n * Template string handler (template tag) for AQL queries.\n *\n * The `aql` tag can be used to write complex AQL queries as multi-line strings\n * without having to worry about `bindVars` and the distinction between\n * collections and regular parameters.\n *\n * Tagged template strings will return an {@link AqlQuery} object with\n * `query` and `bindVars` attributes reflecting any interpolated values.\n *\n * Any {@link collection.ArangoCollection} instance used in a query string will\n * be recognized as a collection reference and generate an AQL collection bind\n * parameter instead of a regular AQL value bind parameter.\n *\n * **Note**: you should always use the `aql` template tag when writing\n * dynamic AQL queries instead of using untagged (normal) template strings.\n * Untagged template strings will inline any interpolated values and return\n * a plain string as result. The `aql` template tag will only inline references\n * to the interpolated values and produce an AQL query object containing both\n * the query and the values. This prevents most injection attacks when using\n * untrusted values in dynamic queries.\n *\n * @example\n * ```js\n * // Some user-supplied string that may be malicious\n * const untrustedValue = req.body.email;\n *\n * // Without aql tag: BAD! DO NOT DO THIS!\n * const badQuery = `\n * FOR user IN users\n * FILTER user.email == \"${untrustedValue}\"\n * RETURN user\n * `;\n * // e.g. if untrustedValue is '\" || user.admin == true || \"':\n * // Query:\n * // FOR user IN users\n * // FILTER user.email == \"\" || user.admin == true || \"\"\n * // RETURN user\n *\n * // With the aql tag: GOOD! MUCH SAFER!\n * const betterQuery = aql`\n * FOR user IN users\n * FILTER user.email == ${untrustedValue}\n * RETURN user\n * `;\n * // Query:\n * // FOR user IN users\n * // FILTER user.email == @value0\n * // RETURN user\n * // Bind parameters:\n * // value0 -> untrustedValue\n * ```\n *\n * @example\n * ```js\n * const collection = db.collection(\"some-collection\");\n * const minValue = 23;\n * const result = await db.query(aql`\n * FOR d IN ${collection}\n * FILTER d.num > ${minValue}\n * RETURN d\n * `);\n *\n * // Equivalent raw query object\n * const result2 = await db.query({\n * query: `\n * FOR d IN @@collection\n * FILTER d.num > @minValue\n * RETURN d\n * `,\n * bindVars: {\n * \"@collection\": collection.name,\n * minValue: minValue\n * }\n * });\n * ```\n *\n * @example\n * ```js\n * const collection = db.collection(\"some-collection\");\n * const color = \"green\";\n * const filter = aql`FILTER d.color == ${color}'`;\n * const result = await db.query(aql`\n * FOR d IN ${collection}\n * ${filter}\n * RETURN d\n * `);\n * ```\n */\nexport function aql(\n templateStrings: TemplateStringsArray,\n ...args: AqlValue[]\n): GeneratedAqlQuery {\n const strings = [...templateStrings];\n const bindVars: Record<string, any> = {};\n const bindValues = [];\n let query = strings[0];\n for (let i = 0; i < args.length; i++) {\n const rawValue = args[i];\n let value = rawValue;\n if (isGeneratedAqlQuery(rawValue)) {\n const src = rawValue._source();\n if (src.args.length) {\n query += src.strings[0];\n args.splice(i, 1, ...src.args);\n strings.splice(\n i,\n 2,\n strings[i] + src.strings[0],\n ...src.strings.slice(1, src.args.length),\n src.strings[src.args.length] + strings[i + 1]\n );\n } else {\n query += rawValue.query + strings[i + 1];\n args.splice(i, 1);\n strings.splice(i, 2, strings[i] + rawValue.query + strings[i + 1]);\n }\n i -= 1;\n continue;\n }\n if (rawValue === undefined) {\n query += strings[i + 1];\n continue;\n }\n if (isAqlLiteral(rawValue)) {\n query += `${rawValue.toAQL()}${strings[i + 1]}`;\n continue;\n }\n const index = bindValues.indexOf(rawValue);\n const isKnown = index !== -1;\n let name = `value${isKnown ? index : bindValues.length}`;\n if (\n isArangoCollection(rawValue) ||\n isArangoGraph(rawValue) ||\n isArangoView(rawValue) ||\n isArangoAnalyzer(rawValue)\n ) {\n name = `@${name}`;\n value = rawValue.name;\n }\n if (!isKnown) {\n bindValues.push(rawValue);\n bindVars[name] = value;\n }\n query += `@${name}${strings[i + 1]}`;\n }\n return {\n query,\n bindVars,\n _source: () => ({ strings, args }),\n };\n}\n\n/**\n * Marks an arbitrary scalar value (i.e. a string, number or boolean) as\n * safe for being inlined directly into AQL queries when used in an `aql`\n * template string, rather than being converted into a bind parameter.\n *\n * **Note**: Nesting `aql` template strings is a much safer alternative for\n * most use cases. This low-level helper function only exists to help with\n * rare edge cases where a trusted AQL query fragment must be read from a\n * string (e.g. when reading query fragments from JSON) and should only be\n * used as a last resort.\n *\n * @example\n * ```js\n * // BAD! DO NOT DO THIS!\n * const sortDirection = literal('ASC');\n *\n * // GOOD! DO THIS INSTEAD!\n * const sortDirection = aql`ASC`;\n * ```\n *\n * @example\n * ```js\n * // BAD! DO NOT DO THIS!\n * const filterColor = literal('FILTER d.color == \"green\"');\n * const result = await db.query(aql`\n * FOR d IN some-collection\n * ${filterColor}\n * RETURN d\n * `);\n *\n * // GOOD! DO THIS INSTEAD!\n * const color = \"green\";\n * const filterColor = aql`FILTER d.color === ${color}`;\n * const result = await db.query(aql`\n * FOR d IN some-collection\n * ${filterColor}\n * RETURN d\n * `);\n * ```\n *\n * @example\n * ```js\n * // WARNING: We explicitly trust the environment variable to be safe!\n * const filter = literal(process.env.FILTER_STATEMENT);\n * const users = await db.query(aql`\n * FOR user IN users\n * ${filter}\n * RETURN user\n * `);\n * ```\n */\nexport function literal(\n value: string | number | boolean | AqlLiteral | null | undefined\n): AqlLiteral {\n if (isAqlLiteral(value)) {\n return value;\n }\n return {\n toAQL() {\n if (value === undefined) {\n return \"\";\n }\n return String(value);\n },\n };\n}\n\n/**\n * Constructs {@link AqlQuery} objects from an array of arbitrary values.\n *\n * **Note**: Nesting `aql` template strings is a much safer alternative\n * for most use cases. This low-level helper function only exists to\n * complement the `aql` tag when constructing complex queries from dynamic\n * arrays of query fragments.\n *\n * @param values - Array of values to join. These values will behave exactly\n * like values interpolated in an `aql` template string.\n * @param sep - Seperator to insert between values. This value will behave\n * exactly like a value passed to {@link literal}, i.e. it will be\n * inlined as-is, rather than being converted into a bind parameter.\n *\n * @example\n * ```js\n * const users = db.collection(\"users\");\n * const filters = [];\n * if (adminsOnly) filters.push(aql`FILTER user.admin`);\n * if (activeOnly) filters.push(aql`FILTER user.active`);\n * const result = await db.query(aql`\n * FOR user IN ${users}\n * ${join(filters)}\n * RETURN user\n * `);\n * ```\n *\n * @example\n * ```js\n * const users = db.collection(\"users\");\n * const keys = [\"jreyes\", \"ghermann\"];\n *\n * // BAD! NEEDLESSLY COMPLEX!\n * const docs = keys.map(key => aql`DOCUMENT(${users}, ${key}`));\n * const result = await db.query(aql`\n * FOR user IN [\n * ${join(docs, \", \")}\n * ]\n * RETURN user\n * `);\n * // Query:\n * // FOR user IN [\n * // DOCUMENT(@@value0, @value1), DOCUMENT(@@value0, @value2)\n * // ]\n * // RETURN user\n * // Bind parameters:\n * // @value0 -> \"users\"\n * // value1 -> \"jreyes\"\n * // value2 -> \"ghermann\"\n *\n * // GOOD! MUCH SIMPLER!\n * const result = await db.query(aql`\n * FOR key IN ${keys}\n * LET user = DOCUMENT(${users}, key)\n * RETURN user\n * `);\n * // Query:\n * // FOR user IN @value0\n * // LET user = DOCUMENT(@@value1, key)\n * // RETURN user\n * // Bind parameters:\n * // value0 -> [\"jreyes\", \"ghermann\"]\n * // @value1 -> \"users\"\n * ```\n */\nexport function join(values: AqlValue[], sep: string = \" \"): GeneratedAqlQuery {\n if (!values.length) {\n return aql``;\n }\n if (values.length === 1) {\n return aql`${values[0]}`;\n }\n return aql([\"\", ...Array(values.length - 1).fill(sep), \"\"] as any, ...values);\n}\n"]}
|