@proofkit/better-auth
Version:
FileMaker adapter for Better Auth
123 lines (122 loc) • 4.65 kB
JavaScript
import { Command } from "@commander-js/extra-typings";
import { FMServerConnection } from "@proofkit/fmodata";
import { logger } from "better-auth";
import { getSchema } from "better-auth/db";
import chalk from "chalk";
import fs from "fs-extra";
import prompts from "prompts";
import { getConfig } from "../better-auth-cli/utils/get-config.js";
import { planMigration, prettyPrintMigrationPlan, executeMigration } from "../migrate.js";
import "dotenv/config";
async function main() {
const program = new Command();
program.command("migrate", { isDefault: true }).option("--cwd <path>", "Path to the current working directory", process.cwd()).option("--config <path>", "Path to the config file").option("-u, --username <username>", "Full Access Username").option("-p, --password <password>", "Full Access Password").option("-y, --yes", "Skip confirmation", false).action(async (options) => {
var _a, _b, _c, _d;
const cwd = options.cwd;
if (!fs.existsSync(cwd)) {
logger.error(`The directory "${cwd}" does not exist.`);
process.exit(1);
}
const config = await getConfig({
cwd,
configPath: options.config
});
if (!config) {
logger.error(
"No configuration file found. Add a `auth.ts` file to your project or pass the path to the configuration file using the `--config` flag."
);
return;
}
const databaseFactory = config.database;
if (!databaseFactory || typeof databaseFactory !== "function") {
logger.error("No database adapter found in auth config.");
process.exit(1);
}
let adapter;
try {
adapter = databaseFactory(config);
} catch (e) {
logger.error(e instanceof Error ? e.message : String(e));
process.exit(1);
}
if ((adapter == null ? void 0 : adapter.id) !== "filemaker") {
logger.error("This generator is only compatible with the FileMaker adapter.");
return;
}
const betterAuthSchema = getSchema(config);
const configDb = adapter.database ?? ((_a = config.database) == null ? void 0 : _a.database);
if (!configDb || typeof configDb !== "object" || !("schema" in configDb)) {
logger.error(
"Could not extract Database instance from adapter. Ensure your auth.ts uses FileMakerAdapter with an fmodata Database."
);
process.exit(1);
}
let db = configDb;
const dbObj = configDb;
const dbName = dbObj._getDatabaseName ?? dbObj.databaseName ?? "";
const baseUrl = (_c = (_b = dbObj.context) == null ? void 0 : _b._getBaseUrl) == null ? void 0 : _c.call(_b);
const serverUrl = baseUrl ? new URL(baseUrl).origin : void 0;
if (options.username && options.password) {
if (!dbName) {
logger.error("Could not determine database filename from adapter config.");
process.exit(1);
}
if (!baseUrl) {
logger.error(
"Could not determine server URL from adapter config. Ensure your auth.ts uses FMServerConnection."
);
process.exit(1);
}
const fetchClientOptions = (_d = dbObj.context) == null ? void 0 : _d._fetchClientOptions;
const connection = new FMServerConnection({
serverUrl,
auth: {
username: options.username,
password: options.password
},
fetchClientOptions
});
db = connection.database(dbName);
}
let migrationPlan;
try {
migrationPlan = await planMigration(db, betterAuthSchema);
} catch (err) {
logger.error(`Failed to read database schema: ${err instanceof Error ? err.message : err}`);
process.exit(1);
}
if (migrationPlan.length === 0) {
logger.info("No changes to apply. Database is up to date.");
return;
}
if (!options.yes) {
prettyPrintMigrationPlan(migrationPlan, { serverUrl, fileName: dbName });
if (migrationPlan.length > 0) {
console.log(chalk.gray("💡 Tip: You can use the --yes flag to skip this confirmation."));
}
const { confirm } = await prompts({
type: "confirm",
name: "confirm",
message: "Apply the above changes to your database?"
});
if (!confirm) {
logger.error("Schema changes not applied.");
return;
}
}
try {
await executeMigration(db, migrationPlan);
logger.info("Migration applied successfully.");
} catch {
process.exit(1);
}
});
await program.parseAsync(process.argv);
process.exit(0);
}
main().catch((err) => {
logger.error(err.message ?? err);
process.exit(1);
});
//# sourceMappingURL=index.js.map