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:
1 parent
145f588cc0
commit
bee8dad946
63 files changed
+4223
-2330
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { getAdapter } from "../driver.js";
|
||||
import { parseJson, stringifyJson } from "../helpers/jsonCol.js";
|
||||
import { makeKv } from "../helpers/kvStore.js";
|
||||
|
||||
const aliasKv = makeKv("modelAliases");
|
||||
const customKv = makeKv("customModels");
|
||||
const mitmKv = makeKv("mitmAlias");
|
||||
|
||||
// modelAliases: key=alias, value=modelString
|
||||
export async function getModelAliases() {
|
||||
return await aliasKv.getAll();
|
||||
}
|
||||
|
||||
export async function setModelAlias(alias, model) {
|
||||
await aliasKv.set(alias, model);
|
||||
}
|
||||
|
||||
export async function deleteModelAlias(alias) {
|
||||
await aliasKv.remove(alias);
|
||||
}
|
||||
|
||||
// customModels: key=`${providerAlias}|${id}|${type}`, value=full model object
|
||||
function customKey(providerAlias, id, type) {
|
||||
return `${providerAlias}|${id}|${type}`;
|
||||
}
|
||||
|
||||
export async function getCustomModels() {
|
||||
const all = await customKv.getAll();
|
||||
return Object.values(all);
|
||||
}
|
||||
|
||||
// Atomic check-then-insert inside transaction to prevent duplicate races
|
||||
export async function addCustomModel({ providerAlias, id, type = "llm", name }) {
|
||||
const k = customKey(providerAlias, id, type);
|
||||
const db = await getAdapter();
|
||||
let added = false;
|
||||
db.transaction(() => {
|
||||
const row = db.get(`SELECT 1 FROM kv WHERE scope = 'customModels' AND key = ?`, [k]);
|
||||
if (row) return;
|
||||
const value = stringifyJson({ providerAlias, id, type, name: name || id });
|
||||
db.run(`INSERT INTO kv(scope, key, value) VALUES('customModels', ?, ?)`, [k, value]);
|
||||
added = true;
|
||||
});
|
||||
return added;
|
||||
}
|
||||
|
||||
export async function deleteCustomModel({ providerAlias, id, type = "llm" }) {
|
||||
await customKv.remove(customKey(providerAlias, id, type));
|
||||
}
|
||||
|
||||
// mitmAlias: key=toolName, value=mappings object
|
||||
export async function getMitmAlias(toolName) {
|
||||
if (toolName) {
|
||||
const v = await mitmKv.get(toolName);
|
||||
return v || {};
|
||||
}
|
||||
return await mitmKv.getAll();
|
||||
}
|
||||
|
||||
export async function setMitmAliasAll(toolName, mappings) {
|
||||
await mitmKv.set(toolName, mappings || {});
|
||||
}
|
||||
Reference in new issue
Block a user