@zeit/cosmosdb-query
Advanced tools
Comparing version 0.0.4 to 0.0.5
// | ||
exports.ABS = (v ) => Math.abs(v) | ||
exports.ABS = (v ) => Math.abs(v); | ||
exports.ARRAY_CONCAT = (...a ) => a.reduce((b, c) => [...b, ...c], []) | ||
exports.ARRAY_CONCAT = (...a ) => | ||
a.reduce((b, c) => [...b, ...c], []); | ||
exports.ARRAY_CONTAINS = (a , c ) => a.some(i => exports.IS_OBJECT(c) ? Object.keys(c).every(k => i[k] === c[k]) : i === c) | ||
exports.ARRAY_CONTAINS = (a , c ) => | ||
a.some( | ||
i => | ||
exports.IS_OBJECT(c) ? Object.keys(c).every(k => i[k] === c[k]) : i === c | ||
); | ||
exports.ARRAY_LENGTH = (a ) => a.length | ||
exports.ARRAY_LENGTH = (a ) => a.length; | ||
// $FlowFixMe | ||
exports.ARRAY_SLICE = (a , b , c ) => a.slice(b, c != null ? b + c : undefined) | ||
exports.ARRAY_SLICE = (a , b , c ) => | ||
// $FlowFixMe | ||
a.slice(b, c != null ? b + c : undefined); | ||
exports.CEILING = (v ) => Math.ceil(v) | ||
exports.CEILING = (v ) => Math.ceil(v); | ||
exports.CONCAT = (...a ) => a.join('') | ||
exports.CONCAT = (...a ) => a.join(""); | ||
exports.CONTAINS = (a , b ) => a.includes(b) | ||
exports.CONTAINS = (a , b ) => a.includes(b); | ||
exports.FLOOR = (v ) => Math.floor(v) | ||
exports.FLOOR = (v ) => Math.floor(v); | ||
exports.INDEX_OF = (a , b ) => a.indexOf(b) | ||
exports.INDEX_OF = (a , b ) => a.indexOf(b); | ||
exports.IS_ARRAY = (v ) => Array.isArray(v) | ||
exports.IS_ARRAY = (v ) => Array.isArray(v); | ||
exports.IS_BOOL = (v ) => typeof v === 'boolean' | ||
exports.IS_BOOL = (v ) => typeof v === "boolean"; | ||
exports.IS_DEFINED = (v ) => typeof v !== 'undefined' | ||
exports.IS_DEFINED = (v ) => typeof v !== "undefined"; | ||
exports.IS_NULL = (v ) => v === null | ||
exports.IS_NULL = (v ) => v === null; | ||
exports.IS_NUMBER = (v ) => typeof v === 'number' | ||
exports.IS_NUMBER = (v ) => typeof v === "number"; | ||
exports.IS_OBJECT = (v ) => Boolean(v) && typeof v === 'object' && !Array.isArray(v) | ||
exports.IS_OBJECT = (v ) => | ||
Boolean(v) && typeof v === "object" && !Array.isArray(v); | ||
exports.IS_PRIMITIVE = (v ) => exports.IS_NULL(v) || exports.IS_NUMBER(v) || exports.IS_STRING(v) || exports.IS_BOOL(v) | ||
exports.IS_PRIMITIVE = (v ) => | ||
exports.IS_NULL(v) || | ||
exports.IS_NUMBER(v) || | ||
exports.IS_STRING(v) || | ||
exports.IS_BOOL(v); | ||
exports.IS_STRING = (v ) => typeof v === 'string' | ||
exports.IS_STRING = (v ) => typeof v === "string"; | ||
exports.LENGTH = (v ) => v.length | ||
exports.LENGTH = (v ) => v.length; | ||
exports.LOWER = (v ) => v.toLowerCase() | ||
exports.LOWER = (v ) => v.toLowerCase(); | ||
exports.REVERSE = (v ) => v.split('').reverse().join('') | ||
exports.REVERSE = (v ) => | ||
v | ||
.split("") | ||
.reverse() | ||
.join(""); | ||
exports.ROUND = (v ) => Math.round(v) | ||
exports.ROUND = (v ) => Math.round(v); | ||
exports.STARTSWITH = (a , b ) => a.startsWith(b) | ||
exports.STARTSWITH = (a , b ) => a.startsWith(b); | ||
exports.SUBSTRING = (a , b , c ) => a.substring(b, c != null ? b + c : undefined) | ||
exports.SUBSTRING = (a , b , c ) => | ||
a.substring(b, c != null ? b + c : undefined); | ||
exports.ToString = (v ) => typeof v === 'undefined' ? undefined : String(v) | ||
exports.ToString = (v ) => | ||
typeof v === "undefined" ? undefined : String(v); | ||
exports.TRIM = (v ) => v.trim() | ||
exports.TRIM = (v ) => v.trim(); | ||
exports.UPPER = (v ) => v.toUpperCase() | ||
exports.UPPER = (v ) => v.toUpperCase(); |
// | ||
const aggregateFunctions = require("./aggregate-functions"); | ||
const builtinFunctions = require("./builtin-functions"); | ||
@@ -20,3 +21,3 @@ | ||
// $FlowFixMe | ||
return execute(builtinFunctions, collection, params); | ||
return execute(aggregateFunctions, builtinFunctions, collection, params); | ||
}; |
@@ -13,19 +13,12 @@ // | ||
constructor(query ) { | ||
this._query = query; | ||
this._ast = null; | ||
this._code = null; | ||
this.ast = parse(this._query.trim()); | ||
} | ||
get ast() { | ||
if (!this._ast) { | ||
this._ast = parse(this._query.trim()); | ||
} | ||
return this._ast; | ||
} | ||
get code() { | ||
@@ -32,0 +25,0 @@ if (!this._code) { |
// | ||
const { default: traverse } = require("@babel/traverse"); | ||
const aggregateFunctions = require("./aggregate-functions"); | ||
@@ -18,2 +19,14 @@ function transform(ctx , node ) { | ||
function isAggregateFunction({ type, name, udf }) { | ||
return ( | ||
type === "scalar_function_expression" && | ||
// $FlowFixMe | ||
Object.protorype.hasOwnProperty.call( | ||
aggregateFunctions, | ||
name.name.toUpperCase() | ||
) && | ||
!udf | ||
); | ||
} | ||
const definitions = { | ||
@@ -173,15 +186,16 @@ array_constant(ctx, { elements }) { | ||
return { | ||
type: 'ObjectExpression', | ||
type: "ObjectExpression", | ||
properties: properties.map(({ key, value }) => ({ | ||
type: 'ObjectProperty', | ||
type: "ObjectProperty", | ||
key: transform(ctx, key), | ||
value: transform(ctx, value) | ||
})) | ||
} | ||
}; | ||
}, | ||
object_property_list(ctx, { properties }) { | ||
let n = 0; | ||
return { | ||
type: "ObjectExpression", | ||
properties: properties.map(({ property, alias }, i) => { | ||
properties: properties.map(({ property, alias }) => { | ||
const key = alias || property.property; | ||
@@ -192,3 +206,4 @@ return { | ||
? transform(ctx, key) | ||
: { type: "Identifier", name: `$${i + 1}` }, | ||
: // eslint-disable-next-line no-plusplus | ||
{ type: "Identifier", name: `$${++n}` }, | ||
value: transform(ctx, property) | ||
@@ -232,4 +247,26 @@ }; | ||
const l = transform(ctx, left); | ||
const r = transform(ctx, right); | ||
if (op === "??") { | ||
throw new Error("The operator ?? is not supported yet"); | ||
// `typeof left !== "undefined" ? left : right` | ||
return { | ||
type: "ConditionalExpression", | ||
test: { | ||
type: "BinaryExpression", | ||
left: { | ||
type: "UnaryExpression", | ||
operator: "typeof", | ||
prefix: true, | ||
argument: l | ||
}, | ||
operator: "!==", | ||
right: { | ||
type: "StringLiteral", | ||
value: "undefined" | ||
} | ||
}, | ||
consequent: l, | ||
alternate: r | ||
}; | ||
} | ||
@@ -239,5 +276,5 @@ | ||
type: "BinaryExpression", | ||
left: transform(ctx, left), | ||
left: l, | ||
operator: op, | ||
right: transform(ctx, right) | ||
right: r | ||
}; | ||
@@ -255,5 +292,8 @@ }, | ||
scalar_function_expression(ctx, { name, arguments: args, udf }) { | ||
scalar_function_expression(ctx, { type, name, arguments: args, udf }) { | ||
const aggregation = | ||
ctx.aggregation && isAggregateFunction({ type, name, udf }); | ||
return { | ||
type: 'CallExpression', | ||
type: "CallExpression", | ||
callee: { | ||
@@ -263,8 +303,31 @@ type: "MemberExpression", | ||
type: "Identifier", | ||
name: udf ? "udf" : "$b" | ||
// eslint-disable-next-line no-nested-ternary | ||
name: udf ? "udf" : aggregation ? "$a" : "$b" | ||
}, | ||
property: transform(ctx, name) | ||
}, | ||
arguments: args.map((a) => transform(ctx, a)) | ||
} | ||
arguments: aggregation | ||
? args.map(a => ({ | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: { | ||
type: "Identifier", | ||
name: "$" | ||
}, | ||
property: { | ||
type: "Identifier", | ||
name: "map" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: ctx.document ? [ctx.document] : [], | ||
body: transform(ctx, a) | ||
} | ||
] | ||
})) | ||
: args.map(a => transform(ctx, a)) | ||
}; | ||
}, | ||
@@ -301,3 +364,3 @@ | ||
select_query(ctx, { select, from, where, orderBy }) { | ||
select_query(ctx, { top, select, from, where, orderBy }) { | ||
const name = "$c"; | ||
@@ -334,2 +397,6 @@ | ||
if (top) { | ||
ctx.ast = transform(ctx, top); | ||
} | ||
ctx.ast = transform(ctx, select); | ||
@@ -340,5 +407,10 @@ | ||
params: [ | ||
// built-int functions | ||
// aggregate functions | ||
{ | ||
type: "Identifier", | ||
name: "$a" | ||
}, | ||
// built-in functions | ||
{ | ||
type: "Identifier", | ||
name: "$b" | ||
@@ -355,2 +427,7 @@ }, | ||
name: "$p" | ||
}, | ||
// intermediate cache | ||
{ | ||
type: "Identifier", | ||
name: "$" | ||
} | ||
@@ -367,18 +444,25 @@ ], | ||
if (properties || value) { | ||
ctx.aggregation = properties | ||
? properties.properties.some(({ property }) => | ||
isAggregateFunction(property) | ||
) | ||
: isAggregateFunction(value); | ||
if (ctx.aggregation) { | ||
// `($ = $c.filter(), [{ $1: COUNT($.map(c => c.id)), $2: ... }])` | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: ctx.ast, | ||
property: { | ||
type: "Identifier", | ||
name: "map" | ||
} | ||
}, | ||
arguments: [ | ||
type: "SequenceExpression", | ||
expressions: [ | ||
// cache filtered result to a variable | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: ctx.document ? [ctx.document] : [], | ||
body: transform(ctx, properties || value) | ||
type: "AssignmentExpression", | ||
left: { | ||
type: "Identifier", | ||
name: "$" | ||
}, | ||
operator: "=", | ||
right: ctx.ast | ||
}, | ||
{ | ||
type: "ArrayExpression", | ||
elements: [transform(ctx, properties || value)] | ||
} | ||
@@ -389,3 +473,20 @@ ] | ||
return ctx.ast; | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: ctx.ast, | ||
property: { | ||
type: "Identifier", | ||
name: "map" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: ctx.document ? [ctx.document] : [], | ||
body: transform(ctx, properties || value) | ||
} | ||
] | ||
}; | ||
}, | ||
@@ -398,3 +499,14 @@ | ||
type: "MemberExpression", | ||
object: ctx.ast, | ||
object: { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: ctx.ast, | ||
property: { | ||
type: "Identifier", | ||
name: "slice" | ||
} | ||
}, | ||
arguments: [] | ||
}, | ||
property: { | ||
@@ -496,2 +608,26 @@ type: "Identifier", | ||
top_specification(ctx, { value }) { | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: ctx.ast, | ||
property: { | ||
type: "Identifier", | ||
name: "slice" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "NumericLiteral", | ||
value: 0 | ||
}, | ||
{ | ||
type: "NumericLiteral", | ||
value | ||
} | ||
] | ||
}; | ||
}, | ||
undefined_constant() { | ||
@@ -498,0 +634,0 @@ return { |
{ | ||
"name": "@zeit/cosmosdb-query", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"license": "UNLICENSED", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,4 +5,2 @@ # cosmosdb-query | ||
This module is experimental, vulnerable and slow. Not intended to be used on production. | ||
```js | ||
@@ -28,9 +26,6 @@ const query = require('@zeit/cosmosdb-query') | ||
- Aggregate functions | ||
- BETWEEN keyword | ||
- TOP operator | ||
- JOIN keyword | ||
- IN keyword | ||
- Some Built-in functions (See [src/builtin-functions.js](https://github.com/zeit/cosmosdb-query/blob/master/src/builtin-functions.js) for supported functions) | ||
- Coalesce operator | ||
- User-defined functions |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
181152
9
6322
30