1 | const { Command, flags } = require('@oclif/command')
|
2 | const { buildConnectionOptions, errorOut } = require('../lib/misc.js')
|
3 | const faunadb = require('faunadb')
|
4 | const q = faunadb.query
|
5 |
|
6 | /**
|
7 | * This is the base class for all fauna-shell commands.
|
8 | */
|
9 | class FaunaCommand extends Command {
|
10 | /**
|
11 | * During init we parse the flags and arguments and assign them
|
12 | * to the `flags` and `args` member variables.
|
13 | *
|
14 | * We call `this.parse(this.constructor)` because we need to load
|
15 | * flags and args for the command being run in the CLI.
|
16 | * In this way we parse the flags and args defined in that command,
|
17 | * plus the ones defined here. A command then needs to define its flags
|
18 | * as follows, if it wants to inherit the flags defined in FaunaCommand:
|
19 | *
|
20 | * CreateKeyCommand.flags = {
|
21 | * ...FaunaCommand.flags
|
22 | * }
|
23 | *
|
24 | */
|
25 | async init() {
|
26 | const { flags: f, args: a } = this.parse(this.constructor)
|
27 | this.flags = f
|
28 | this.args = a
|
29 | }
|
30 |
|
31 | /**
|
32 | * Runs the function in the context of a database connection.
|
33 | *
|
34 | * @param {function} f - The function to run
|
35 | * @param {string} dbScope - The database in which the function will be executed.
|
36 | * @param {string} role - The user role with which the function will be executed.
|
37 | */
|
38 | withClient(f, dbScope, role) {
|
39 | const cmdFlags = this.flags
|
40 | return buildConnectionOptions(cmdFlags, dbScope, role)
|
41 | .then(function (connectionOptions) {
|
42 | var client = new faunadb.Client({
|
43 | ...connectionOptions,
|
44 | headers: {
|
45 | 'X-Fauna-Source': 'Fauna Shell',
|
46 | },
|
47 | })
|
48 | //TODO this should return a Promise
|
49 | return f(client, connectionOptions)
|
50 | })
|
51 | .catch(function (err) {
|
52 | return errorOut(err, 1)
|
53 | })
|
54 | }
|
55 |
|
56 | /**
|
57 | * Runs the provided query, while logging a message before running it.
|
58 | * Calls the success callback on success, or the failure one otherwise.
|
59 | *
|
60 | * @param {query} queryExpr - The Query to execute.
|
61 | * @param {string} logMsg - The message to display before executing the query.
|
62 | * @param {function} success - On success callback.
|
63 | * @param {function} failure - On error callback.
|
64 | */
|
65 | query(queryExpr, logMsg, success, failure) {
|
66 | const log = this.log
|
67 | return this.withClient(function (client, _) {
|
68 | log(logMsg)
|
69 | return client.query(queryExpr)
|
70 | .then(success)
|
71 | .catch(failure)
|
72 | })
|
73 | }
|
74 |
|
75 | dbExists(dbName, callback) {
|
76 | return this.withClient(function (testDbClient, _) {
|
77 | return testDbClient.query(q.Exists(q.Database(dbName)))
|
78 | .then(callback)
|
79 | })
|
80 | }
|
81 | }
|
82 |
|
83 | /**
|
84 | * These flags allow the user to override endpoint configuration.
|
85 | * They are inherited by all shell commands that extend FaunaCommand.
|
86 | * See each command's flags to see how this mechanism works.
|
87 | */
|
88 | FaunaCommand.flags = {
|
89 | ...Command.flags,
|
90 | domain: flags.string({
|
91 | description: 'FaunaDB server domain',
|
92 | }),
|
93 | scheme: flags.string({
|
94 | description: 'Connection scheme',
|
95 | options: ['https', 'http'],
|
96 | }),
|
97 | port: flags.string({
|
98 | description: 'Connection port',
|
99 | }),
|
100 | timeout: flags.string({
|
101 | description: 'Connection timeout in milliseconds',
|
102 | }),
|
103 | secret: flags.string({
|
104 | description: 'FaunaDB secret key',
|
105 | }),
|
106 | endpoint: flags.string({
|
107 | description: 'FaunaDB server endpoint',
|
108 | }),
|
109 | }
|
110 |
|
111 | module.exports = FaunaCommand
|