@zeit/cosmosdb-query
Advanced tools
Comparing version 0.0.7 to 0.0.8
// | ||
exports.ABS = (v ) => Math.abs(v); | ||
const ifDefined = f => (...a ) => | ||
a.every(v => typeof v !== "undefined") ? f(...a) : undefined; | ||
exports.ARRAY_CONCAT = (...a ) => | ||
a.reduce((b, c) => [...b, ...c], []); | ||
exports.ABS = ifDefined((v ) => Math.abs(v)); | ||
exports.ARRAY_CONTAINS = (a , c ) => | ||
exports.ACOS = ifDefined((v ) => Math.acos(v)); | ||
exports.ARRAY_CONCAT = ifDefined((...a ) => | ||
a.reduce((b, c) => [...b, ...c], []) | ||
); | ||
exports.ARRAY_CONTAINS = ifDefined((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 = ifDefined((a ) => a.length); | ||
exports.ARRAY_SLICE = (a , b , c ) => | ||
exports.ARRAY_SLICE = ifDefined((a , b , c ) => | ||
// $FlowFixMe | ||
a.slice(b, c != null ? b + c : undefined); | ||
a.slice(b, c != null ? b + c : undefined) | ||
); | ||
exports.CEILING = (v ) => Math.ceil(v); | ||
exports.ASIN = ifDefined((v ) => Math.asin(v)); | ||
exports.CONCAT = (...a ) => a.join(""); | ||
exports.ATAN = ifDefined((v ) => Math.atan(v)); | ||
exports.CONTAINS = (a , b ) => a.includes(b); | ||
exports.ATN2 = ifDefined((a , b ) => Math.atan2(b, a)); | ||
exports.FLOOR = (v ) => Math.floor(v); | ||
exports.CEILING = ifDefined((v ) => Math.ceil(v)); | ||
exports.INDEX_OF = (a , b ) => a.indexOf(b); | ||
exports.CONCAT = ifDefined((...a ) => a.join("")); | ||
exports.CONTAINS = ifDefined((a , b ) => a.includes(b)); | ||
exports.COS = ifDefined((v ) => Math.cos(v)); | ||
exports.COT = ifDefined((v ) => 1 / Math.tan(v)); | ||
exports.DEGREES = ifDefined((v ) => (v * 180) / Math.PI); | ||
exports.ENDSWITH = ifDefined((a , b ) => a.endsWith(b)); | ||
exports.EXP = ifDefined((v ) => Math.exp(v)); | ||
exports.FLOOR = ifDefined((v ) => Math.floor(v)); | ||
exports.INDEX_OF = ifDefined((a , b ) => a.indexOf(b)); | ||
exports.IS_ARRAY = (v ) => Array.isArray(v); | ||
@@ -51,24 +75,61 @@ | ||
exports.LENGTH = (v ) => v.length; | ||
exports.LEFT = ifDefined((a , b ) => a.slice(0, b)); | ||
exports.LOWER = (v ) => v.toLowerCase(); | ||
exports.LENGTH = ifDefined((v ) => v.length); | ||
exports.REVERSE = (v ) => | ||
exports.LOG = ifDefined((v ) => Math.log(v)); | ||
exports.LOG10 = ifDefined((v ) => Math.log10(v)); | ||
exports.LOWER = ifDefined((v ) => v.toLowerCase()); | ||
exports.LTRIM = ifDefined((v ) => v.trimLeft()); | ||
exports.PI = () => Math.PI; | ||
exports.POWER = ifDefined((a , b ) => a ** b); | ||
exports.RADIANS = ifDefined((v ) => (v * Math.PI) / 180); | ||
exports.REPLACE = ifDefined((a , b , c ) => | ||
a.replace(b, c) | ||
); | ||
exports.REPLICATE = ifDefined((a , b ) => a.repeat(b)); | ||
exports.REVERSE = ifDefined((v ) => | ||
v | ||
.split("") | ||
.reverse() | ||
.join(""); | ||
.join("") | ||
); | ||
exports.ROUND = (v ) => Math.round(v); | ||
exports.RIGHT = ifDefined((a , b ) => a.slice(-b)); | ||
exports.STARTSWITH = (a , b ) => a.startsWith(b); | ||
exports.ROUND = ifDefined((v ) => Math.round(v)); | ||
exports.SUBSTRING = (a , b , c ) => | ||
a.substring(b, c != null ? b + c : undefined); | ||
exports.RTRIM = ifDefined((v ) => v.trimRight()); | ||
exports.ToString = (v ) => | ||
typeof v === "undefined" ? undefined : String(v); | ||
exports.SIGN = ifDefined((v ) => Math.sign(v)); | ||
exports.TRIM = (v ) => v.trim(); | ||
exports.SIN = ifDefined((v ) => Math.sin(v)); | ||
exports.UPPER = (v ) => v.toUpperCase(); | ||
exports.SQRT = ifDefined((v ) => Math.sqrt(v)); | ||
exports.SQUARE = ifDefined((v ) => v ** 2); | ||
exports.STARTSWITH = ifDefined((a , b ) => a.startsWith(b)); | ||
exports.SUBSTRING = ifDefined((a , b , c ) => | ||
a.substring(b, c != null ? b + c : undefined) | ||
); | ||
exports.TAN = ifDefined((v ) => Math.tan(v)); | ||
exports.ToString = ifDefined((v ) => String(v)); | ||
exports.TRIM = ifDefined((v ) => v.trim()); | ||
exports.TRUNC = ifDefined((v ) => Math.trunc(v)); | ||
exports.UPPER = ifDefined((v ) => v.toUpperCase()); |
@@ -30,6 +30,7 @@ // | ||
module.exports = function containsPartitionKeys( | ||
condition , | ||
paths | ||
) { | ||
module.exports = function containsPartitionKeys(ast , paths ) { | ||
if (!paths.length) return true; | ||
if (!ast.where) return false; | ||
const { condition } = ast.where; | ||
const nodes = conditionKeyNodes(condition); | ||
@@ -36,0 +37,0 @@ const keys = nodes.map(n => new Set(n.map(toPartitionKey))); |
@@ -5,2 +5,24 @@ // | ||
function removeUndefined(obj) { | ||
if (Array.isArray(obj)) { | ||
// remove `undefined` from array unlike JSON | ||
return obj.reduce( | ||
(o, v) => (typeof v !== "undefined" ? [...o, removeUndefined(v)] : o), | ||
[] | ||
); | ||
} | ||
if (obj && typeof obj === "object") { | ||
return Object.entries(obj).reduce((o, [k, v]) => { | ||
if (typeof v !== "undefined") { | ||
// eslint-disable-next-line no-param-reassign | ||
o[k] = removeUndefined(v); | ||
} | ||
return o; | ||
}, {}); | ||
} | ||
return obj; | ||
} | ||
module.exports = ( | ||
@@ -22,3 +44,9 @@ collection , | ||
// $FlowFixMe | ||
return execute(aggregateFunctions, builtinFunctions, collection, params); | ||
const result = execute( | ||
aggregateFunctions, | ||
builtinFunctions, | ||
collection, | ||
params | ||
); | ||
return result.map(removeUndefined); | ||
}; |
@@ -38,4 +38,3 @@ // | ||
containsPartitionKeys(paths ) { | ||
if (!this.ast.where) return false; | ||
return containsPartitionKeys(this.ast.where.condition, paths); | ||
return containsPartitionKeys(this.ast, paths); | ||
} | ||
@@ -42,0 +41,0 @@ } |
@@ -5,2 +5,6 @@ // | ||
const jsEqualityOperators = new Set(["===", "!==", "==", "!="]); | ||
const jsRelationalOperators = new Set([">", "<", ">=", "<="]); | ||
const jsAndOrOperators = new Set(["&&", "||"]); | ||
function transform(ctx , node ) { | ||
@@ -32,2 +36,24 @@ // eslint-disable-next-line no-use-before-define | ||
function strictTrue(node) { | ||
if ( | ||
node.type === "BinaryExpression" && | ||
(jsAndOrOperators.has(node.operator) || | ||
jsEqualityOperators.has(node.operator) || | ||
jsRelationalOperators.has(node.operator)) | ||
) | ||
return node; | ||
if (node.type === "UnaryExpression" && node.operator === "!") return node; | ||
if (node.type === "BooleanLiteral" && node.value === true) return node; | ||
return { | ||
type: "BinaryExpression", | ||
left: node, | ||
operator: "===", | ||
right: { | ||
type: "BooleanLiteral", | ||
value: true | ||
} | ||
}; | ||
} | ||
const definitions = { | ||
@@ -76,3 +102,3 @@ array_constant(ctx, { elements }) { | ||
params: [ctx.document], | ||
body: transform(ctx, condition) | ||
body: strictTrue(transform(ctx, condition)) | ||
} | ||
@@ -87,8 +113,9 @@ ] | ||
from_specification(ctx, { source }) { | ||
const node = transform(ctx, source.expression); | ||
from_specification(ctx, { source, joins }) { | ||
ctx.document = transform(ctx, source); | ||
const exp = transform(ctx, source.expression); | ||
let arg; | ||
traverse( | ||
{ type: "Program", body: [node] }, | ||
{ type: "Program", body: [exp] }, | ||
{ | ||
@@ -105,4 +132,7 @@ MemberExpression(path) { | ||
let object; | ||
if (source.iteration) { | ||
return { | ||
// e.g, "FROM c IN Families.children" | ||
// collection.reduce(($, Families) => [...$, ...Families.children], []) | ||
object = { | ||
type: "CallExpression", | ||
@@ -120,3 +150,3 @@ callee: { | ||
type: "ArrowFunctionExpression", | ||
params: [{ type: "Identifier", name: "__" }, arg], | ||
params: [{ type: "Identifier", name: "$" }, arg], | ||
body: { | ||
@@ -129,3 +159,3 @@ type: "ArrayExpression", | ||
type: "Identifier", | ||
name: "__" | ||
name: "$" | ||
} | ||
@@ -135,3 +165,3 @@ }, | ||
type: "SpreadElement", | ||
argument: node | ||
argument: exp | ||
} | ||
@@ -147,6 +177,4 @@ ] | ||
}; | ||
} | ||
if (node.type === "MemberExpression") { | ||
return { | ||
} else if (exp.type === "MemberExpression") { | ||
object = { | ||
type: "CallExpression", | ||
@@ -165,9 +193,217 @@ callee: { | ||
params: [arg], | ||
body: node | ||
body: exp | ||
} | ||
] | ||
}; | ||
} else { | ||
object = ctx.ast; | ||
} | ||
return ctx.ast; | ||
return (joins || []).reduce((obj, { expression, alias, iteration }) => { | ||
const node = transform(ctx, alias || expression); | ||
const nameNode = alias ? node : node.property; | ||
const { document } = ctx; | ||
ctx.document = { | ||
type: "ObjectPattern", | ||
properties: [ | ||
...(document.type === "ObjectPattern" | ||
? document.properties | ||
: [ | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: document, | ||
value: document | ||
} | ||
]), | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: nameNode, | ||
value: nameNode | ||
} | ||
] | ||
}; | ||
if (iteration) { | ||
// e.g, | ||
// FROM Families f | ||
// JOIN c IN f.children | ||
// JOIN p IN c.pets | ||
// | ||
// collection | ||
// .reduce(($, f) => [...$, ...(f.children || []).map(c => ({ c, f }))], []) | ||
// .reduce(($, { f, c }) => [...$, ...(c.pets || []).map(p => ({ c, f, p }))], []) | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: obj, | ||
property: { | ||
type: "Identifier", | ||
name: "reduce" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: [{ type: "Identifier", name: "$" }, document], | ||
body: { | ||
type: "ArrayExpression", | ||
elements: [ | ||
{ | ||
type: "SpreadElement", | ||
argument: { | ||
type: "Identifier", | ||
name: "$" | ||
} | ||
}, | ||
{ | ||
type: "SpreadElement", | ||
argument: { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: { | ||
type: "LogicalExpression", | ||
left: transform(ctx, expression), | ||
operator: "||", | ||
right: { | ||
type: "ArrayExpression", | ||
elements: [] | ||
} | ||
}, | ||
property: { | ||
type: "Identifier", | ||
name: "map" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: [nameNode], | ||
body: { | ||
type: "ObjectExpression", | ||
properties: [ | ||
...(document.type === "ObjectPattern" | ||
? document.properties | ||
: [ | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: document, | ||
value: document | ||
} | ||
]), | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: nameNode, | ||
value: nameNode | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
type: "ArrayExpression", | ||
elements: [] | ||
} | ||
] | ||
}; | ||
} | ||
// e.g, | ||
// FROM Families f | ||
// JOIN f.children | ||
// | ||
// collection | ||
// .reduce(($, f) => (typeof f.children !== 'undefined' ? [...$, { f, children: f.children }] : $), []) | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: obj, | ||
property: { | ||
type: "Identifier", | ||
name: "reduce" | ||
} | ||
}, | ||
arguments: [ | ||
{ | ||
type: "ArrowFunctionExpression", | ||
params: [{ type: "Identifier", name: "$" }, document], | ||
body: { | ||
type: "ConditionalExpression", | ||
test: { | ||
type: "BinaryExpression", | ||
left: { | ||
type: "UnaryExpression", | ||
operator: "typeof", | ||
prefix: true, | ||
argument: node | ||
}, | ||
operator: "!==", | ||
right: { | ||
type: "StringLiteral", | ||
value: "undefined" | ||
} | ||
}, | ||
consequent: { | ||
type: "ArrayExpression", | ||
elements: [ | ||
{ | ||
type: "SpreadElement", | ||
argument: { | ||
type: "Identifier", | ||
name: "$" | ||
} | ||
}, | ||
{ | ||
type: "ObjectExpression", | ||
properties: [ | ||
...(document.type === "ObjectPattern" | ||
? document.properties | ||
: [ | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: document, | ||
value: document | ||
} | ||
]), | ||
{ | ||
type: "ObjectProperty", | ||
computed: false, | ||
shorthand: true, | ||
key: nameNode, | ||
value: node | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
alternate: { | ||
type: "Identifier", | ||
name: "$" | ||
} | ||
} | ||
}, | ||
{ | ||
type: "ArrayExpression", | ||
elements: [] | ||
} | ||
] | ||
}; | ||
}, object); | ||
}, | ||
@@ -243,17 +479,27 @@ | ||
scalar_between_expression(ctx, { value, begin, end }) { | ||
const left = transform(ctx, value); | ||
return { | ||
type: "BinaryExpression", | ||
left: { | ||
type: "BinaryExpression", | ||
left, | ||
operator: ">=", | ||
right: transform(ctx, begin) | ||
}, | ||
operator: "&&", | ||
right: { | ||
type: "BinaryExpression", | ||
left, | ||
operator: "<=", | ||
right: transform(ctx, end) | ||
} | ||
}; | ||
}, | ||
scalar_binary_expression(ctx, { left, operator, right }) { | ||
const op = | ||
{ | ||
"=": "===", | ||
"!=": "!==", | ||
"<>": "!==", | ||
"||": "+", | ||
AND: "&&", | ||
OR: "||" | ||
}[operator] || operator; | ||
const l = transform(ctx, left); | ||
const r = transform(ctx, right); | ||
if (op === "??") { | ||
if (operator === "??") { | ||
// `typeof left !== "undefined" ? left : right` | ||
@@ -281,7 +527,18 @@ return { | ||
const op = | ||
{ | ||
"=": "===", | ||
"!=": "!==", | ||
"<>": "!==", | ||
"||": "+", | ||
AND: "&&", | ||
OR: "||" | ||
}[operator] || operator; | ||
const mustStrictTrue = operator === "AND" || operator === "OR"; | ||
return { | ||
type: "BinaryExpression", | ||
left: l, | ||
left: mustStrictTrue ? strictTrue(l) : l, | ||
operator: op, | ||
right: r | ||
right: mustStrictTrue ? strictTrue(r) : r | ||
}; | ||
@@ -321,3 +578,3 @@ }, | ||
type: "Identifier", | ||
name: "$" | ||
name: "$_" | ||
}, | ||
@@ -341,2 +598,20 @@ property: { | ||
scalar_in_expression(ctx, { value, list }) { | ||
return { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: { | ||
type: "ArrayExpression", | ||
elements: list.map(l => transform(ctx, l)) | ||
}, | ||
property: { | ||
type: "Identifier", | ||
name: "includes" | ||
} | ||
}, | ||
arguments: [transform(ctx, value)] | ||
}; | ||
}, | ||
scalar_member_expression(ctx, { object, property, computed }) { | ||
@@ -379,6 +654,2 @@ return { | ||
}; | ||
ctx.document = transform( | ||
ctx, | ||
from.source.alias || from.source.expression | ||
); | ||
ctx.ast = transform(ctx, from); | ||
@@ -436,3 +707,3 @@ } else { | ||
type: "Identifier", | ||
name: "$" | ||
name: "$_" | ||
} | ||
@@ -464,3 +735,3 @@ ], | ||
type: "Identifier", | ||
name: "$" | ||
name: "$_" | ||
}, | ||
@@ -524,4 +795,4 @@ operator: "=", | ||
params: [ | ||
{ type: "Identifier", name: "a" }, | ||
{ type: "Identifier", name: "b" } | ||
{ type: "Identifier", name: "$1" }, | ||
{ type: "Identifier", name: "$2" } | ||
], | ||
@@ -549,3 +820,3 @@ body: expressions.reduce((left, right) => { | ||
[["a", left], ["b", right]].forEach(([name, node]) => { | ||
[["$1", left], ["$2", right]].forEach(([name, node]) => { | ||
traverse( | ||
@@ -556,7 +827,19 @@ { type: "Program", body: [node] }, | ||
const { object } = path.node; | ||
if ( | ||
object.type === "Identifier" && | ||
object.name === ctx.document.name | ||
) { | ||
object.name = name; | ||
if (object.type === "Identifier") { | ||
// eslint-disable-next-line no-param-reassign | ||
path.node.object = | ||
ctx.document.type === "ObjectPattern" | ||
? { | ||
type: "MemberExpression", | ||
object: { | ||
type: "Identifier", | ||
name | ||
}, | ||
property: object | ||
} | ||
: { | ||
type: "Identifier", | ||
name | ||
}; | ||
path.stop(); | ||
} | ||
@@ -563,0 +846,0 @@ } |
{ | ||
"name": "@zeit/cosmosdb-query", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"license": "UNLICENSED", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -25,6 +25,3 @@ # cosmosdb-query | ||
- BETWEEN keyword | ||
- 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) | ||
- Spatial functions | ||
- 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
192353
6719
27