mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-17 04:09:39 +02:00
feat: implement UI-driven database configuration
This commit is contained in:
1 parent
e54e679bd4
commit
0e4c276816
4 files changed
+248
-17
No files matched your search
@@ -0,0 +1,52 @@
|
||||
"use server";
|
||||
|
||||
import { Pool } from "pg";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
const CONFIG_FILE = path.join(process.cwd(), ".db-config.json");
|
||||
|
||||
export interface DbConfig {
|
||||
url: string;
|
||||
}
|
||||
|
||||
export async function testDatabaseConnection(url: string) {
|
||||
console.log("Testing connection to:", url.replace(/:([^@]+)@/, ":****@"));
|
||||
const pool = new Pool({
|
||||
connectionString: url,
|
||||
connectionTimeoutMillis: 5000,
|
||||
});
|
||||
|
||||
try {
|
||||
const client = await pool.connect();
|
||||
await client.query("SELECT 1");
|
||||
client.release();
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
console.error("Database connection test failed:", error.message);
|
||||
return { success: false, error: error.message };
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveDatabaseSettings(url: string) {
|
||||
try {
|
||||
const config: DbConfig = { url };
|
||||
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2));
|
||||
console.log("Database configuration saved to:", CONFIG_FILE);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
console.error("Failed to save database configuration:", error.message);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSavedDatabaseConfig(): Promise<DbConfig | null> {
|
||||
try {
|
||||
const data = await fs.readFile(CONFIG_FILE, "utf-8");
|
||||
return JSON.parse(data);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
|
||||
export interface DatabaseSettings {
|
||||
host: string;
|
||||
port: string;
|
||||
database: string;
|
||||
user: string;
|
||||
password?: string;
|
||||
ssl: boolean;
|
||||
}
|
||||
|
||||
interface DatabaseSettingsState extends DatabaseSettings {
|
||||
setHost: (host: string) => void;
|
||||
setPort: (port: string) => void;
|
||||
setDatabase: (database: string) => void;
|
||||
setUser: (user: string) => void;
|
||||
setPassword: (password: string) => void;
|
||||
setSsl: (ssl: boolean) => void;
|
||||
getDatabaseUrl: () => string;
|
||||
}
|
||||
|
||||
export const useDbSettingsStore = create<DatabaseSettingsState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
host: "localhost",
|
||||
port: "5432",
|
||||
database: "collectflow",
|
||||
user: "postgres",
|
||||
password: "",
|
||||
ssl: false,
|
||||
|
||||
setHost: (host) => set({ host }),
|
||||
setPort: (port) => set({ port }),
|
||||
setDatabase: (database) => set({ database }),
|
||||
setUser: (user) => set({ user }),
|
||||
setPassword: (password) => set({ password }),
|
||||
setSsl: (ssl) => set({ ssl }),
|
||||
|
||||
getDatabaseUrl: () => {
|
||||
const { user, password, host, port, database, ssl } = get();
|
||||
const auth = password ? `${user}:${password}` : user;
|
||||
const url = `postgres://${auth}@${host}:${port}/${database}${ssl ? "?sslmode=require" : ""}`;
|
||||
return url;
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "collectflow-db-settings",
|
||||
}
|
||||
)
|
||||
);
|
||||
Reference in new issue
Block a user