mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
cli: try to fallback to env if present
This commit is contained in:
@@ -74,6 +74,7 @@
|
|||||||
"@vitejs/plugin-react": "^4.3.3",
|
"@vitejs/plugin-react": "^4.3.3",
|
||||||
"autoprefixer": "^10.4.20",
|
"autoprefixer": "^10.4.20",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
|
"dotenv": "^16.4.7",
|
||||||
"esbuild-postcss": "^0.0.4",
|
"esbuild-postcss": "^0.0.4",
|
||||||
"jotai": "^2.10.1",
|
"jotai": "^2.10.1",
|
||||||
"kysely-d1": "^0.3.0",
|
"kysely-d1": "^0.3.0",
|
||||||
|
|||||||
@@ -76,3 +76,9 @@ export async function getConfigPath(filePath?: string) {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getConnectionCredentialsFromEnv() {
|
||||||
|
const dbUrl = process.env.DB_URL;
|
||||||
|
const dbToken = process.env.DB_TOKEN;
|
||||||
|
return dbUrl ? { url: dbUrl, authToken: dbToken } : undefined;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,15 +4,18 @@ import { StorageLocalAdapter } from "adapter/node";
|
|||||||
import type { CliBkndConfig, CliCommand } from "cli/types";
|
import type { CliBkndConfig, CliCommand } from "cli/types";
|
||||||
import { Option } from "commander";
|
import { Option } from "commander";
|
||||||
import { config } from "core";
|
import { config } from "core";
|
||||||
|
import dotenv from "dotenv";
|
||||||
import { registries } from "modules/registries";
|
import { registries } from "modules/registries";
|
||||||
import {
|
import {
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
type Platform,
|
type Platform,
|
||||||
attachServeStatic,
|
attachServeStatic,
|
||||||
getConfigPath,
|
getConfigPath,
|
||||||
|
getConnectionCredentialsFromEnv,
|
||||||
startServer
|
startServer
|
||||||
} from "./platform";
|
} from "./platform";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
const isBun = typeof Bun !== "undefined";
|
const isBun = typeof Bun !== "undefined";
|
||||||
|
|
||||||
export const run: CliCommand = (program) => {
|
export const run: CliCommand = (program) => {
|
||||||
@@ -101,16 +104,26 @@ async function action(options: {
|
|||||||
}) {
|
}) {
|
||||||
const configFilePath = await getConfigPath(options.config);
|
const configFilePath = await getConfigPath(options.config);
|
||||||
|
|
||||||
let app: App;
|
let app: App | undefined = undefined;
|
||||||
if (options.dbUrl || !configFilePath) {
|
if (options.dbUrl) {
|
||||||
const connection = options.dbUrl
|
const connection = options.dbUrl
|
||||||
? { url: options.dbUrl, authToken: options.dbToken }
|
? { url: options.dbUrl, authToken: options.dbToken }
|
||||||
: undefined;
|
: undefined;
|
||||||
app = await makeApp({ connection, server: { platform: options.server } });
|
app = await makeApp({ connection, server: { platform: options.server } });
|
||||||
} else {
|
} else if (configFilePath) {
|
||||||
console.log("Using config from:", configFilePath);
|
console.log("[INFO] Using config from:", configFilePath);
|
||||||
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
|
const config = (await import(configFilePath).then((m) => m.default)) as CliBkndConfig;
|
||||||
app = await makeConfigApp(config, options.server);
|
app = await makeConfigApp(config, options.server);
|
||||||
|
} else {
|
||||||
|
const credentials = getConnectionCredentialsFromEnv();
|
||||||
|
if (credentials) {
|
||||||
|
console.log("[INFO] Using connection from environment");
|
||||||
|
app = await makeConfigApp({ app: { connection: credentials } }, options.server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!app) {
|
||||||
|
app = await makeApp({ server: { platform: options.server } });
|
||||||
}
|
}
|
||||||
|
|
||||||
await startServer(options.server, app, { port: options.port });
|
await startServer(options.server, app, { port: options.port });
|
||||||
|
|||||||
@@ -107,7 +107,6 @@ export class SqliteIntrospector implements DatabaseIntrospector, ConnectionIntro
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tables = await query.execute();
|
const tables = await query.execute();
|
||||||
console.log("tables", tables);
|
|
||||||
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
|
return Promise.all(tables.map(({ name }) => this.#getTableMetadata(name)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +118,6 @@ export class SqliteIntrospector implements DatabaseIntrospector, ConnectionIntro
|
|||||||
|
|
||||||
async #getTableMetadata(table: string): Promise<TableMetadata> {
|
async #getTableMetadata(table: string): Promise<TableMetadata> {
|
||||||
const db = this.#db;
|
const db = this.#db;
|
||||||
console.log("get table metadata", table);
|
|
||||||
|
|
||||||
// Get the SQL that was used to create the table.
|
// Get the SQL that was used to create the table.
|
||||||
const tableDefinition = await db
|
const tableDefinition = await db
|
||||||
|
|||||||
Reference in New Issue
Block a user