Skip to content

Commit

Permalink
setup create database
Browse files Browse the repository at this point in the history
  • Loading branch information
henryfauna authored and echo-bravo-yahoo committed Nov 23, 2024
1 parent 40d10bd commit e18006f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
//@ts-check

import { fql } from "fauna";
import { container } from "../../cli.mjs";
import { runV10Query } from "../../lib/fauna.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function createDatabase() {
async function createDatabase(argv) {
const logger = container.resolve("logger");
logger.stdout(`TBD`);

await runV10Query({
url: argv.url,
secret: argv.secret,
query: fql`Database.create({
name: ${argv.name},
protected: ${argv.protected ?? false},
typechecked: ${argv.typechecked ?? false}
})`,
});

logger.stdout(`Database ${argv.name} created`);
}

function buildCreateCommand(yargs) {
return yargs
.options({
...commonQueryOptions,
name: {
type: "string",
description: "the name of the database to create",
},
typechecked: {
type: "boolean",
description: "enable typechecking for the database",
},
protected: {
type: "boolean",
description: "allow destructive schema changes",
},
priority: {
type: "number",
description: "user-defined priority assigned to the child database",
},
})
.demandOption("name")
.version(false)
Expand Down
31 changes: 31 additions & 0 deletions src/lib/fauna.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const getV10Client = async ({ url, secret }) => {
const { Client } = (await import("fauna")).default;
return new Client({
secret,
endpoint: url,
});
};

export const runV10Query = async ({
query,
url = undefined,
secret = undefined,
client = undefined,
options = {
format: "simple",
typecheck: false,
},
}) => {
let _client = client;

if (!_client && url && secret) {
_client = await getV10Client({ url, secret });
} else if (!_client) {
throw new Error("No client provided and no url and secret provided");
}

return _client.query(query, options).finally(() => {
// Clean up the client if one was created internally.
if (!client) _client.close();
});
};

0 comments on commit e18006f

Please sign in to comment.