feat(db): migrate from lowdb to SQLite with repos pattern

- Add modular DB layer (adapters, migrations, repos, helpers)
- Replace localDb/usageDb/requestDetailsDb monoliths with repos
- Add Tailscale tunnel integration & status check API
- Add /api/cli-tools/all-statuses aggregated endpoint
- Add settingsStore (Zustand) and mitm/dbReader
- Add DB unit tests (benchmark, concurrent, migration, vs-lowdb)
This commit is contained in:
decolua
2026-05-09 17:48:20 +07:00
parent 145f588cc0
commit bee8dad946
63 changed files with 4223 additions and 2330 deletions
+10 -3
View File
@@ -1,13 +1,15 @@
"use client";
import { create } from "zustand";
import { CLIENT_STORE_TTL_MS } from "@/shared/constants/config";
const useProviderStore = create((set, get) => ({
providers: [],
loading: false,
error: null,
lastFetched: 0,
setProviders: (providers) => set({ providers }),
setProviders: (providers) => set({ providers, lastFetched: Date.now() }),
addProvider: (provider) =>
set((state) => ({ providers: [provider, ...state.providers] })),
@@ -24,17 +26,22 @@ const useProviderStore = create((set, get) => ({
providers: state.providers.filter((p) => p._id !== id),
})),
invalidate: () => set({ lastFetched: 0 }),
setLoading: (loading) => set({ loading }),
setError: (error) => set({ error }),
fetchProviders: async () => {
// Skips network when cache is fresh (< CLIENT_STORE_TTL_MS). Pass {force:true} to override.
fetchProviders: async ({ force = false } = {}) => {
const { lastFetched, providers } = get();
if (!force && providers.length > 0 && Date.now() - lastFetched < CLIENT_STORE_TTL_MS) return;
set({ loading: true, error: null });
try {
const response = await fetch("/api/providers");
const data = await response.json();
if (response.ok) {
set({ providers: data.providers, loading: false });
set({ providers: data.connections || data.providers || [], loading: false, lastFetched: Date.now() });
} else {
set({ error: data.error, loading: false });
}
+51
View File
@@ -0,0 +1,51 @@
"use client";
import { create } from "zustand";
import { CLIENT_STORE_TTL_MS } from "@/shared/constants/config";
const useSettingsStore = create((set, get) => ({
settings: null,
loading: false,
error: null,
lastFetched: 0,
invalidate: () => set({ lastFetched: 0 }),
// Skips network when cache is fresh; pass {force:true} to override
fetchSettings: async ({ force = false } = {}) => {
const { lastFetched, settings } = get();
if (!force && settings && Date.now() - lastFetched < CLIENT_STORE_TTL_MS) return settings;
set({ loading: true, error: null });
try {
const res = await fetch("/api/settings");
const data = await res.json();
if (res.ok) {
set({ settings: data, loading: false, lastFetched: Date.now() });
return data;
}
set({ error: data.error, loading: false });
} catch (e) {
set({ error: "Failed to fetch settings", loading: false });
}
return null;
},
// PATCH server + merge into local cache (no extra fetch needed)
patchSettings: async (patch) => {
try {
const res = await fetch("/api/settings", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patch),
});
if (!res.ok) return null;
const updated = await res.json();
set({ settings: updated, lastFetched: Date.now() });
return updated;
} catch {
return null;
}
},
}));
export default useSettingsStore;