@zeit/cosmosdb-query
Advanced tools
Comparing version 0.0.2 to 0.0.3
// | ||
/* eslint-disable no-underscore-dangle */ | ||
const { default: generate } = require("@babel/generator"); | ||
const containsPartitionKeys = require("./contains-partition-keys"); | ||
const execute = require("./executor"); | ||
@@ -8,13 +10,42 @@ // $FlowFixMe | ||
module.exports = ( | ||
coll , | ||
{ | ||
query, | ||
parameters | ||
} | ||
) => { | ||
const sqlAst = parse(query.trim()); | ||
const jsAst = transform(sqlAst); | ||
const { code } = generate(jsAst); | ||
return execute(coll, { code, parameters }); | ||
}; | ||
class Query { | ||
constructor(query ) { | ||
this._query = query; | ||
this._ast = null; | ||
this._code = null; | ||
} | ||
get ast() { | ||
if (!this._ast) { | ||
this._ast = parse(this._query.trim()); | ||
} | ||
return this._ast; | ||
} | ||
get code() { | ||
if (!this._code) { | ||
const jsAst = transform(this.ast); | ||
const { code } = generate(jsAst); | ||
this._code = code; | ||
} | ||
return this._code; | ||
} | ||
exec(coll , parameters ) { | ||
const { code } = this; | ||
return execute(coll, { code, parameters }); | ||
} | ||
containsPartitionKeys(paths ) { | ||
if (!this.ast.where) return false; | ||
return containsPartitionKeys(this.ast.where.condition, paths); | ||
} | ||
} | ||
module.exports = (query ) => new Query(query); |
@@ -184,3 +184,3 @@ // | ||
type: "Identifier", | ||
name: "__p" | ||
name: "$p" | ||
}, | ||
@@ -194,2 +194,9 @@ property: { | ||
scalar_array_expression(ctx, { values }) { | ||
return { | ||
type: "ArrayExpression", | ||
elements: values.map(v => transform(ctx, v)) | ||
}; | ||
}, | ||
scalar_binary_expression(ctx, { left, operator, right }) { | ||
@@ -201,2 +208,3 @@ const op = | ||
"<>": "!==", | ||
"||": "+", | ||
AND: "&&", | ||
@@ -218,2 +226,11 @@ OR: "||" | ||
scalar_conditional_expression(ctx, { test, consequent, alternate }) { | ||
return { | ||
type: "ConditionalExpression", | ||
test: transform(ctx, test), | ||
consequent: transform(ctx, consequent), | ||
alternate: transform(ctx, alternate) | ||
}; | ||
}, | ||
scalar_member_expression(ctx, { object, property, computed }) { | ||
@@ -249,10 +266,9 @@ return { | ||
select_query(ctx, { select, from, where, orderBy }) { | ||
const name = "__c"; | ||
const name = "$c"; | ||
ctx.ast = { | ||
type: "Identifier", | ||
name | ||
}; | ||
if (from) { | ||
ctx.ast = { | ||
type: "Identifier", | ||
name | ||
}; | ||
ctx.document = transform( | ||
@@ -263,2 +279,11 @@ ctx, | ||
ctx.ast = transform(ctx, from); | ||
} else { | ||
ctx.ast = { | ||
type: "ArrayExpression", | ||
elements: [ | ||
{ | ||
type: "NullLiteral" | ||
} | ||
] | ||
}; | ||
} | ||
@@ -285,3 +310,3 @@ | ||
type: "Identifier", | ||
name: "__p" | ||
name: "$p" | ||
} | ||
@@ -312,3 +337,3 @@ ], | ||
type: "ArrowFunctionExpression", | ||
params: [ctx.document], | ||
params: ctx.document ? [ctx.document] : [], | ||
body: transform(ctx, properties) | ||
@@ -315,0 +340,0 @@ } |
{ | ||
"name": "@zeit/cosmosdb-query", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"license": "UNLICENSED", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -9,2 +9,3 @@ # cosmosdb-query | ||
const query = require('@zeit/cosmosdb-query') | ||
const collection = [ | ||
@@ -14,7 +15,11 @@ { id: 'foo' }, | ||
] | ||
const docs = query(collection, { | ||
query: 'SELECT * FROM c WHERE c.id = @id', | ||
parameters: [{ name: '@id', value: 'foo' }] | ||
}) | ||
const docs = query('SELECT * FROM c WHERE c.id = @id') | ||
.exec(collection, [{ name: '@id', value: 'foo' }]) | ||
console.log(docs) // [ { id: 'foo' } ] | ||
q = query('SELECT * FROM c WHERE c.id = 1') | ||
if (!q.containsPartitionKeys(['/key'])) { | ||
throw new Error('query doesn\'t contain partition keys') | ||
} | ||
``` | ||
@@ -21,0 +26,0 @@ |
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
162634
7
5715
38