-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework eval and shell to fully implement eval #443
Conversation
src/commands/query.mjs
Outdated
const output = formatQueryResponse(results, argv); | ||
|
||
if (argv.output) { | ||
container.resolve("fs").writeFileSync(argv.output, output); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to output the result even when we write to file? sort of like tee
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see -i
and -o
as helpers for pipe/redirection so my instinct would to redirect output to the file. But if it makes sense to enough people to do both, that's fine too.
src/commands/query.mjs
Outdated
type: "string", | ||
description: "the query to run; use - to read from stdin", | ||
}) | ||
.nargs('query', 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this'll probably cause issues with queries that have spaces in them. is the behavior better if we don't specify a number and let yargs infer? or do we want to enforce string quoting?
see queries like:
fauna query let x = 5 --no-color
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah. Good call out. Let me putz around with it and see.
This usage came from the yargs parser tests, but those were also operating on single tokens/values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, trying to write FQL in zsh without quotes is super dangerous...so I might not do anything about this until we get more feedback from folks.
ca195e1
to
f16b66c
Compare
f16b66c
to
2024620
Compare
['$0 query "Collection.all()" --database us-std/example --role admin', "run the query and write to stdout "], | ||
["$0 query -i /path/to/queries.fql --database us-std/example --role admin", "run the query from a file"], | ||
['echo "1 + 1" | $0 query - --database us-std/example --role admin', "run the query from stdin"], | ||
['$0 query -i /path/to/queries.fql -o /tmp/result.json --database us-std/example --role admin', "run the query and write to a file"], | ||
['$0 query -i /path/to/queries.fql -o /tmp/result.json --extra --database us-std/example --role admin', "run the query and write full API response to a file"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--role will default to admin when minting a key for the specified database path. Maybe examples should show you don't have to provide role arg.
Also reminds me to go test out custom user roles...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure?
./src/user-entrypoint.mjs query -d us-std/A --url https://db.fauna-dev.com "Database.all()"
fauna query [fql]
<...snip>
Failed to make request to Fauna account API [404]: undefined - Role 'undefined' doesn't exist.
const credentials = container.resolve("credentials"); | ||
|
||
await credentials.databaseKeys.onInvalidCreds(); | ||
const refreshedSecret = await credentials.databaseKeys.getOrRefreshKey(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we already awaited the refresh via onInvalidCreds, safe to just access ".key" here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll clean this up in a follow-up.
const validateQueryParams = ({ query, client, url, secret }) => { | ||
if (!query) { | ||
throw new Error("A query is required."); | ||
} else if (!client && (!url || !secret)) { | ||
throw new Error("A client or url and secret are required."); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Saw some validation of args above somewhere as well. I have some arg validation in Credentials class too.
Thoughts on validating all args and their combinations in one place in middleware before we get to handlers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, this check doesn't need to be here at all. The fauna client will barf if we're missing things. This error isn't adding a lot of value as-is.
Problem
The current implementation of eval was not fully ported over, and recent CLI changes have started to add some drift in how we interact with Fauna.
Solution
This is a "big" PR that's really pretty simple:
query
but enables aneval
alias for the old-timersfauna.mjs
andfaunadb.mjs
. Each module implements the following:stringExpressionToQuery
: Transform a string into a querygetClient
: to return a clientrunQuery
: execute a queryrunQueryFromString
: helper to run a query directly from a string inputformatError
: support error formatting in a single placeformatQueryResponse
: support driver response formatting in a single placerunQueryFromString
: wraps the version specific implementationsformatError
: wraps the version specific implementationsformatQueryResponse
: wraps the version specific implementationsextra
: returns the full API response instead of just the data. This flag is surfaced in the shell via.toggleExtra
quiet
: mutes stderrapiVersion
: version has been renamed to not conflict with cli versionformat
: When using the driver, format isn't specifically helpful. The undocumenteddecorated
format doesn't add enough value to manage separately. If we want colorized JSON output (or something else altogether), plenty of options exist.Result
Testing
See query tests.
Next/Out of scope