Skip to content

Commit

Permalink
Optionally, create a db when starting a local container
Browse files Browse the repository at this point in the history
  • Loading branch information
cleve-fauna committed Dec 13, 2024
1 parent 2f32803 commit 4d25345
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function runCreateQuery(secret, argv) {
typechecked: ${argv.typechecked ?? null},
priority: ${argv.priority ?? null},
})`,
options: { format: "decorated" },
});
}

Expand Down
107 changes: 106 additions & 1 deletion src/commands/local.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import chalk from "chalk";
import { AbortError } from "fauna";

import { container } from "../cli.mjs";
import { ensureContainerRunning } from "../lib/docker-containers.mjs";
import { CommandError } from "../lib/errors.mjs";
import { colorize, Format } from "../lib/formatting/colorize.mjs";

/**
* Starts the local Fauna container
Expand All @@ -8,6 +13,7 @@ import { CommandError } from "../lib/errors.mjs";
* It will reject if the container is not ready after the maximum number of attempts.
*/
async function startLocal(argv) {
const color = argv.color;
await ensureContainerRunning({
imageName: argv.image,
containerName: argv.name,
Expand All @@ -17,8 +23,69 @@ async function startLocal(argv) {
pull: argv.pull,
interval: argv.interval,
maxAttempts: argv.maxAttempts,
color: argv.color,
color,
});
if (argv.database) {
await createDatabase(argv);
}
}

async function createDatabase(argv) {
const { fql } = container.resolve("fauna");
const { runQuery } = container.resolve("faunaClientV10");
const logger = container.resolve("logger");
const color = argv.color;
logger.stderr(
colorize(`[CreateDatabase] Creating database '${argv.database}'...`, {
format: Format.LOG,
color,
}),
);
try {
const db = await runQuery({
secret: "secret",
url: `http://${argv.hostIp}:${argv.hostPort}`,
query: fql`
let name = ${argv.name}
let database = Database.byName(name)
let protected = ${argv.protected ?? null}
let typechecked = ${argv.typechecked ?? null}
let priority = ${argv.priority ?? null}
let params = {
name: name,
protected: ${argv.protected ?? null},
typechecked: ${argv.typechecked ?? null},
priority: ${argv.priority ?? null},
}
if (database == null) {
Database.create(params)
} else if (protected == database.protected && typechecked == database.typechecked && priority == database.priority) {
database
} else {
abort(database)
}`,
options: { format: "decorated" },
});
logger.stderr(
colorize(`[CreateDatabase] Database '${argv.database}' created.`, {
format: Format.LOG,
color,
}),
);
logger.stderr(colorize(db.data, { format: Format.FQL, color }));
} catch (e) {
if (e instanceof AbortError) {
throw new CommandError(
`${chalk.red(`[CreateDatabase] Database '${argv.database}' already exists but with differrent properties than requested:\n`)}
-----------------
${colorize(e.abort, { format: Format.FQL, color })}
-----------------
${chalk.red("Please use choose a different name using --name or align the --typechecked, --priority, and --protected with what is currently present.")}`,
{ hideHelp: false },
);
}
throw e;
}
}

/**
Expand Down Expand Up @@ -67,6 +134,26 @@ function buildLocalCommand(yargs) {
type: "boolean",
default: true,
},
database: {
describe:
"The name of a database to create in the container. Omit to create no database.",
type: "string",
},
typechecked: {
describe:
"Enable typechecking for the database. Valid only if --database is set.",
type: "boolean",
},
protected: {
describe:
"Enable protected mode for the database. Protected mode disallows destructive schema changes. Valid only if --database is set.",
type: "boolean",
},
priority: {
type: "number",
description:
"User-defined priority for the database. Valid only if --database is set.",
},
})
.check((argv) => {
if (argv.maxAttempts < 1) {
Expand All @@ -80,6 +167,24 @@ function buildLocalCommand(yargs) {
{ hideHelp: false },
);
}
if (argv.typechecked && !argv.database) {
throw new CommandError(
"--typechecked can only be set if --database is set.",
{ hideHelp: false },
);
}
if (argv.protected && !argv.database) {
throw new CommandError(
"--protected can only be set if --database is set.",
{ hideHelp: false },
);
}
if (argv.priority && !argv.database) {
throw new CommandError(
"--priority can only be set if --database is set.",
{ hideHelp: false },
);
}
return true;
});
}
Expand Down

0 comments on commit 4d25345

Please sign in to comment.