From 1759832650a059b4016dbe4fe341ffa357ba2f37 Mon Sep 17 00:00:00 2001 From: Henry Ball Date: Fri, 22 Nov 2024 10:38:25 -0800 Subject: [PATCH] stub out create database --- package-lock.json | 49 +++++++++++++++++++++++++ src/cli.mjs | 7 ++-- src/commands/database.mjs | 57 ------------------------------ src/commands/database/create.mjs | 28 +++++++++++++++ src/commands/database/database.mjs | 32 +++++++++++++++++ src/commands/database/list.mjs | 24 +++++++++++++ src/lib/fauna-account-client.mjs | 4 +-- 7 files changed, 139 insertions(+), 62 deletions(-) delete mode 100644 src/commands/database.mjs create mode 100644 src/commands/database/create.mjs create mode 100644 src/commands/database/database.mjs create mode 100644 src/commands/database/list.mjs diff --git a/package-lock.json b/package-lock.json index 7d03dff7..412dcf0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "awilix": "^12.0.2", "chalk": "^5.3.0", "eslint": "^9.12.0", + "fauna": "^2.3.0", "faunadb": "^4.5.4", "inquirer": "^12.0.0", "open": "10.1.0", @@ -710,6 +711,7 @@ }, "node_modules/ansi-colors": { "version": "4.1.3", + "dev": true, "license": "MIT", "engines": { "node": ">=6" @@ -760,6 +762,7 @@ }, "node_modules/anymatch": { "version": "3.1.3", + "dev": true, "license": "ISC", "dependencies": { "normalize-path": "^3.0.0", @@ -823,6 +826,7 @@ }, "node_modules/binary-extensions": { "version": "2.3.0", + "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -895,6 +899,7 @@ }, "node_modules/browser-stdout": { "version": "1.3.1", + "dev": true, "license": "ISC" }, "node_modules/btoa-lite": { @@ -986,6 +991,7 @@ }, "node_modules/chokidar": { "version": "3.6.0", + "dev": true, "license": "MIT", "dependencies": { "anymatch": "~3.1.2", @@ -1140,6 +1146,7 @@ }, "node_modules/decamelize": { "version": "4.0.0", + "dev": true, "license": "MIT", "engines": { "node": ">=10" @@ -1203,6 +1210,7 @@ }, "node_modules/diff": { "version": "5.2.0", + "dev": true, "license": "BSD-3-Clause", "engines": { "node": ">=0.3.1" @@ -1524,6 +1532,15 @@ "reusify": "^1.0.4" } }, + "node_modules/fauna": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/fauna/-/fauna-2.3.0.tgz", + "integrity": "sha512-7mavTqMGvXGtmCOo5Ng+q2OdLWQrSfGX8iJIz8ABcJVC6IvI/sWwvOLRl59fZ4w1coyWX6zin0TC8DfroTiS3g==", + "license": "MPL-2.0", + "dependencies": { + "base64-js": "^1.5.1" + } + }, "node_modules/faunadb": { "version": "4.8.0", "hasInstallScript": true, @@ -1591,6 +1608,7 @@ }, "node_modules/flat": { "version": "5.0.2", + "dev": true, "license": "BSD-3-Clause", "bin": { "flat": "cli.js" @@ -1620,10 +1638,12 @@ }, "node_modules/fs.realpath": { "version": "1.0.0", + "dev": true, "license": "ISC" }, "node_modules/fsevents": { "version": "2.3.3", + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -1652,6 +1672,7 @@ }, "node_modules/glob": { "version": "8.1.0", + "dev": true, "license": "ISC", "dependencies": { "fs.realpath": "^1.0.0", @@ -1679,6 +1700,7 @@ }, "node_modules/glob/node_modules/brace-expansion": { "version": "2.0.1", + "dev": true, "license": "MIT", "dependencies": { "balanced-match": "^1.0.0" @@ -1686,6 +1708,7 @@ }, "node_modules/glob/node_modules/minimatch": { "version": "5.1.6", + "dev": true, "license": "ISC", "dependencies": { "brace-expansion": "^2.0.1" @@ -1738,6 +1761,7 @@ }, "node_modules/he": { "version": "1.2.0", + "dev": true, "license": "MIT", "bin": { "he": "bin/he" @@ -1797,6 +1821,7 @@ }, "node_modules/inflight": { "version": "1.0.6", + "dev": true, "license": "ISC", "dependencies": { "once": "^1.3.0", @@ -1805,6 +1830,7 @@ }, "node_modules/inherits": { "version": "2.0.4", + "dev": true, "license": "ISC" }, "node_modules/ini": { @@ -1829,6 +1855,7 @@ }, "node_modules/is-binary-path": { "version": "2.1.0", + "dev": true, "license": "MIT", "dependencies": { "binary-extensions": "^2.0.0" @@ -1951,6 +1978,7 @@ }, "node_modules/is-plain-obj": { "version": "2.1.0", + "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -1958,6 +1986,7 @@ }, "node_modules/is-unicode-supported": { "version": "0.1.0", + "dev": true, "license": "MIT", "engines": { "node": ">=10" @@ -2102,6 +2131,7 @@ "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true, "license": "MIT" }, "node_modules/lodash.get": { @@ -2115,6 +2145,7 @@ }, "node_modules/log-symbols": { "version": "4.1.0", + "dev": true, "license": "MIT", "dependencies": { "chalk": "^4.1.0", @@ -2129,6 +2160,7 @@ }, "node_modules/log-symbols/node_modules/chalk": { "version": "4.1.2", + "dev": true, "license": "MIT", "dependencies": { "ansi-styles": "^4.1.0", @@ -2227,6 +2259,7 @@ }, "node_modules/mocha": { "version": "10.7.3", + "dev": true, "license": "MIT", "dependencies": { "ansi-colors": "^4.1.3", @@ -2277,6 +2310,7 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/mocha-multi-reporters/-/mocha-multi-reporters-1.5.1.tgz", "integrity": "sha512-Yb4QJOaGLIcmB0VY7Wif5AjvLMUFAdV57D2TWEva1Y0kU/3LjKpeRVmlMIfuO1SVbauve459kgtIizADqxMWPg==", + "dev": true, "license": "MIT", "dependencies": { "debug": "^4.1.1", @@ -2291,6 +2325,7 @@ }, "node_modules/mocha/node_modules/brace-expansion": { "version": "2.0.1", + "dev": true, "license": "MIT", "dependencies": { "balanced-match": "^1.0.0" @@ -2298,6 +2333,7 @@ }, "node_modules/mocha/node_modules/cliui": { "version": "7.0.4", + "dev": true, "license": "ISC", "dependencies": { "string-width": "^4.2.0", @@ -2307,6 +2343,7 @@ }, "node_modules/mocha/node_modules/minimatch": { "version": "5.1.6", + "dev": true, "license": "ISC", "dependencies": { "brace-expansion": "^2.0.1" @@ -2317,6 +2354,7 @@ }, "node_modules/mocha/node_modules/supports-color": { "version": "8.1.1", + "dev": true, "license": "MIT", "dependencies": { "has-flag": "^4.0.0" @@ -2330,6 +2368,7 @@ }, "node_modules/mocha/node_modules/yargs": { "version": "16.2.0", + "dev": true, "license": "MIT", "dependencies": { "cliui": "^7.0.2", @@ -2403,6 +2442,7 @@ }, "node_modules/normalize-path": { "version": "3.0.0", + "dev": true, "license": "MIT", "engines": { "node": ">=0.10.0" @@ -2417,6 +2457,7 @@ }, "node_modules/once": { "version": "1.4.0", + "dev": true, "license": "ISC", "dependencies": { "wrappy": "1" @@ -2665,6 +2706,7 @@ }, "node_modules/randombytes": { "version": "2.1.0", + "dev": true, "license": "MIT", "dependencies": { "safe-buffer": "^5.1.0" @@ -2697,6 +2739,7 @@ }, "node_modules/readdirp": { "version": "3.6.0", + "dev": true, "license": "MIT", "dependencies": { "picomatch": "^2.2.1" @@ -2797,6 +2840,7 @@ }, "node_modules/safe-buffer": { "version": "5.2.1", + "dev": true, "funding": [ { "type": "github", @@ -2829,6 +2873,7 @@ }, "node_modules/serialize-javascript": { "version": "6.0.2", + "dev": true, "license": "BSD-3-Clause", "dependencies": { "randombytes": "^2.1.0" @@ -3235,6 +3280,7 @@ }, "node_modules/workerpool": { "version": "6.5.1", + "dev": true, "license": "Apache-2.0" }, "node_modules/wrap-ansi": { @@ -3254,6 +3300,7 @@ }, "node_modules/wrappy": { "version": "1.0.2", + "dev": true, "license": "ISC" }, "node_modules/xdg-basedir": { @@ -3296,6 +3343,7 @@ }, "node_modules/yargs-parser": { "version": "20.2.9", + "dev": true, "license": "ISC", "engines": { "node": ">=10" @@ -3303,6 +3351,7 @@ }, "node_modules/yargs-unparser": { "version": "2.0.0", + "dev": true, "license": "MIT", "dependencies": { "camelcase": "^6.0.0", diff --git a/src/cli.mjs b/src/cli.mjs index b15faa7c..c8ac30e8 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -3,12 +3,13 @@ import chalk from "chalk"; import yargs from "yargs"; -import databaseCommand from "./commands/database.mjs"; -import evalCommand from "./commands/eval.mjs"; import keyCommand from "./commands/key.mjs"; +import evalCommand from "./commands/eval.mjs"; +import shellCommand from "./commands/shell.mjs"; import loginCommand from "./commands/login.mjs"; import schemaCommand from "./commands/schema/schema.mjs"; -import shellCommand from "./commands/shell.mjs"; +import databaseCommand from "./commands/database/database.mjs"; + import { authNZMiddleware } from "./lib/auth/authNZ.mjs"; import { checkForUpdates, fixPaths, logArgv } from "./lib/middleware.mjs"; diff --git a/src/commands/database.mjs b/src/commands/database.mjs deleted file mode 100644 index cba5837f..00000000 --- a/src/commands/database.mjs +++ /dev/null @@ -1,57 +0,0 @@ -//@ts-check - -import { container } from "../cli.mjs"; - -async function listDatabases(profile) { - const logger = container.resolve("logger"); - const accountClient = container.resolve("accountClient"); - const accountCreds = container.resolve("accountCreds"); - const accountKey = accountCreds.get({ key: profile }).accountKey; - logger.stdout("Listing Databases..."); - const databases = await accountClient.listDatabases(accountKey); - logger.stdout(databases); -} - -function buildDatabaseCommand(yargs) { - return yargs - .positional("method", { - type: "string", - choices: ["create", "list", "delete"], - describe: "choose a method to interact with your databases", - }) - .options({ - profile: { - type: "string", - description: "a user profile", - default: "default", - }, - }) - .help("help", "show help") - .example([["$0 db list"]]); -} - -function databaseHandler(argv) { - const logger = container.resolve("logger"); - const method = argv.method; - switch (method) { - case "create": - logger.stdout("Creating database..."); - break; - case "delete": - logger.stdout("Deleting database..."); - break; - case "list": - listDatabases(argv.profile); - break; - default: - break; - } -} - -export default { - command: "database ", - aliases: ["db"], - description: "Interact with your databases:", - builder: buildDatabaseCommand, - handler: databaseHandler, -}; diff --git a/src/commands/database/create.mjs b/src/commands/database/create.mjs new file mode 100644 index 00000000..d0aad869 --- /dev/null +++ b/src/commands/database/create.mjs @@ -0,0 +1,28 @@ +//@ts-check + +import { container } from "../../cli.mjs"; + +async function createDatabase(argv) { + const logger = container.resolve("logger"); + logger.stdout(`TBD dog`); +} + +function buildCreateCommand(yargs) { + return yargs + .options({ + name: { + type: "string", + description: "the name of the database to create", + }, + }) + .demandOption("name") + .version(false) + .help("help", "show help"); +} + +export default { + command: "create", + description: "Creates a database", + builder: buildCreateCommand, + handler: createDatabase, +}; diff --git a/src/commands/database/database.mjs b/src/commands/database/database.mjs new file mode 100644 index 00000000..8c300be8 --- /dev/null +++ b/src/commands/database/database.mjs @@ -0,0 +1,32 @@ +//@ts-check + +import createCommand from "./create.mjs"; +import listCommand from "./list.mjs"; + +function buildDatabase(yargs) { + return yargs + .options({ + profile: { + type: "string", + description: "a user profile", + default: "default", + }, + secret: { + type: "string", + description: "a secret key", + }, + }) + .command(createCommand) + .command(listCommand) + .demandCommand() + .version(false) + .help("help", "show help"); +} + +export default { + command: "database", + describe: "Interact with your databases", + builder: buildDatabase, + // eslint-disable-next-line no-empty-function + handler: () => {}, +}; diff --git a/src/commands/database/list.mjs b/src/commands/database/list.mjs new file mode 100644 index 00000000..93c550b8 --- /dev/null +++ b/src/commands/database/list.mjs @@ -0,0 +1,24 @@ +//@ts-check + +import { container } from "../../cli.mjs"; + +async function listDatabases(argv) { + const profile = argv.profile; + const logger = container.resolve("logger"); + const accountClient = container.resolve("accountClient"); + const accountCreds = container.resolve("accountCreds"); + const accountKey = accountCreds.get({ key: profile }).accountKey; + const databases = await accountClient.listDatabases(accountKey); + logger.stdout(databases); +} + +function buildListCommand(yargs) { + return yargs.version(false).help("help", "show help"); +} + +export default { + command: "list", + description: "Lists your databases", + builder: buildListCommand, + handler: listDatabases, +}; diff --git a/src/lib/fauna-account-client.mjs b/src/lib/fauna-account-client.mjs index ba2b1ab5..8ffbd713 100644 --- a/src/lib/fauna-account-client.mjs +++ b/src/lib/fauna-account-client.mjs @@ -9,6 +9,7 @@ export class FaunaAccountClient { constructor() { this.makeAccountRequest = container.resolve("makeAccountRequest"); } + /** * Starts an OAuth request to the Fauna account API. * @@ -119,12 +120,11 @@ export class FaunaAccountClient { */ async listDatabases(accountKey) { try { - const response = await this.makeAccountRequest({ + return this.makeAccountRequest({ method: "GET", path: "/databases", secret: accountKey, }); - return await response.json(); } catch (err) { err.message = `Failure to list databases: ${err.message}`; throw err;