mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
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:
@@ -0,0 +1,73 @@
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { getAdapter } from "../driver.js";
|
||||
import { parseJson, stringifyJson } from "../helpers/jsonCol.js";
|
||||
|
||||
function rowToCombo(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
kind: row.kind,
|
||||
models: parseJson(row.models, []),
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getCombos() {
|
||||
const db = await getAdapter();
|
||||
const rows = db.all(`SELECT * FROM combos ORDER BY createdAt ASC`);
|
||||
return rows.map(rowToCombo);
|
||||
}
|
||||
|
||||
export async function getComboById(id) {
|
||||
const db = await getAdapter();
|
||||
const row = db.get(`SELECT * FROM combos WHERE id = ?`, [id]);
|
||||
return rowToCombo(row);
|
||||
}
|
||||
|
||||
export async function getComboByName(name) {
|
||||
const db = await getAdapter();
|
||||
const row = db.get(`SELECT * FROM combos WHERE name = ?`, [name]);
|
||||
return rowToCombo(row);
|
||||
}
|
||||
|
||||
export async function createCombo(data) {
|
||||
const db = await getAdapter();
|
||||
const now = new Date().toISOString();
|
||||
const combo = {
|
||||
id: uuidv4(),
|
||||
name: data.name,
|
||||
kind: data.kind || null,
|
||||
models: data.models || [],
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
db.run(
|
||||
`INSERT INTO combos(id, name, kind, models, createdAt, updatedAt) VALUES(?, ?, ?, ?, ?, ?)`,
|
||||
[combo.id, combo.name, combo.kind, stringifyJson(combo.models), combo.createdAt, combo.updatedAt]
|
||||
);
|
||||
return combo;
|
||||
}
|
||||
|
||||
export async function updateCombo(id, data) {
|
||||
const db = await getAdapter();
|
||||
let result = null;
|
||||
db.transaction(() => {
|
||||
const row = db.get(`SELECT * FROM combos WHERE id = ?`, [id]);
|
||||
if (!row) return;
|
||||
const merged = { ...rowToCombo(row), ...data, updatedAt: new Date().toISOString() };
|
||||
db.run(
|
||||
`UPDATE combos SET name = ?, kind = ?, models = ?, updatedAt = ? WHERE id = ?`,
|
||||
[merged.name, merged.kind, stringifyJson(merged.models || []), merged.updatedAt, id]
|
||||
);
|
||||
result = merged;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function deleteCombo(id) {
|
||||
const db = await getAdapter();
|
||||
const res = db.run(`DELETE FROM combos WHERE id = ?`, [id]);
|
||||
return (res?.changes ?? 0) > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user