mock数据分组管理
This commit is contained in:
@@ -7,6 +7,7 @@ import type {
|
||||
ApiListItem,
|
||||
AppConfig,
|
||||
MockFileRow,
|
||||
MockGroupRow,
|
||||
RouteApiNameMap,
|
||||
RouteConfig,
|
||||
RouteEnabledMap,
|
||||
@@ -100,6 +101,12 @@ export async function initDatabase(): Promise<void> {
|
||||
name TEXT NOT NULL
|
||||
)
|
||||
`);
|
||||
await dbRun(`
|
||||
CREATE TABLE IF NOT EXISTS mock_groups (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
)
|
||||
`);
|
||||
await dbRun(`
|
||||
CREATE TABLE IF NOT EXISTS mock_files (
|
||||
file_path TEXT PRIMARY KEY,
|
||||
@@ -111,6 +118,11 @@ export async function initDatabase(): Promise<void> {
|
||||
).catch(() => {
|
||||
// ignore when column already exists
|
||||
});
|
||||
await dbRun(
|
||||
"ALTER TABLE mock_files ADD COLUMN group_id INTEGER",
|
||||
).catch(() => {
|
||||
// ignore when column already exists
|
||||
});
|
||||
await dbRun(`
|
||||
CREATE TABLE IF NOT EXISTS server_config (
|
||||
key TEXT PRIMARY KEY,
|
||||
@@ -211,8 +223,16 @@ export async function deleteApiItemFromDb(route: string): Promise<void> {
|
||||
export async function upsertMockFilePathToDb(
|
||||
filePath: string,
|
||||
alias?: string,
|
||||
groupId?: number | null,
|
||||
): Promise<void> {
|
||||
if (typeof alias === "string") {
|
||||
if (typeof groupId !== "undefined") {
|
||||
await dbRun(
|
||||
"INSERT INTO mock_files(file_path, alias, group_id) VALUES(?, ?, ?) ON CONFLICT(file_path) DO UPDATE SET alias = excluded.alias, group_id = excluded.group_id",
|
||||
[filePath, alias, groupId ?? null],
|
||||
);
|
||||
return;
|
||||
}
|
||||
await dbRun(
|
||||
"INSERT INTO mock_files(file_path, alias) VALUES(?, ?) ON CONFLICT(file_path) DO UPDATE SET alias = excluded.alias",
|
||||
[filePath, alias],
|
||||
@@ -230,14 +250,75 @@ export async function removeMockFilePathFromDb(filePath: string): Promise<void>
|
||||
|
||||
export async function loadMockFilePathsFromDb(): Promise<MockFileRow[]> {
|
||||
const rows = await dbAll<MockFileRow>(
|
||||
"SELECT file_path, alias FROM mock_files ORDER BY file_path ASC",
|
||||
"SELECT file_path, alias, group_id FROM mock_files ORDER BY file_path ASC",
|
||||
);
|
||||
return rows.map((row) => ({
|
||||
file_path: row.file_path,
|
||||
alias: String(row.alias || ""),
|
||||
group_id: row.group_id ?? null,
|
||||
}));
|
||||
}
|
||||
|
||||
// ── mock_groups CRUD ──
|
||||
|
||||
export async function loadMockGroups(): Promise<MockGroupRow[]> {
|
||||
const rows = await dbAll<MockGroupRow>(
|
||||
"SELECT id, name FROM mock_groups ORDER BY id ASC",
|
||||
);
|
||||
return rows.map((row) => ({ id: row.id, name: row.name }));
|
||||
}
|
||||
|
||||
export async function insertMockGroup(name: string): Promise<number> {
|
||||
await dbRun("INSERT INTO mock_groups(name) VALUES(?)", [name]);
|
||||
const row = await dbAll<{ id: number }>(
|
||||
"SELECT id FROM mock_groups WHERE name = ?",
|
||||
[name],
|
||||
);
|
||||
return row[0]?.id ?? 0;
|
||||
}
|
||||
|
||||
export async function updateMockGroup(
|
||||
id: number,
|
||||
name: string,
|
||||
): Promise<void> {
|
||||
await dbRun("UPDATE mock_groups SET name = ? WHERE id = ?", [name, id]);
|
||||
}
|
||||
|
||||
export async function deleteMockGroup(id: number): Promise<void> {
|
||||
await dbRun("DELETE FROM mock_groups WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
export async function countMockFilesByGroup(
|
||||
groupId: number,
|
||||
): Promise<number> {
|
||||
const rows = await dbAll<{ cnt: number }>(
|
||||
"SELECT COUNT(*) AS cnt FROM mock_files WHERE group_id = ?",
|
||||
[groupId],
|
||||
);
|
||||
return rows[0]?.cnt ?? 0;
|
||||
}
|
||||
|
||||
export async function updateMockFilesGroupId(
|
||||
oldGroupId: number,
|
||||
newGroupId: number | null,
|
||||
): Promise<void> {
|
||||
await dbRun("UPDATE mock_files SET group_id = ? WHERE group_id = ?", [
|
||||
newGroupId,
|
||||
oldGroupId,
|
||||
]);
|
||||
}
|
||||
|
||||
export async function deleteMockFilesByGroup(
|
||||
groupId: number,
|
||||
): Promise<MockFileRow[]> {
|
||||
const rows = await dbAll<MockFileRow>(
|
||||
"SELECT file_path FROM mock_files WHERE group_id = ?",
|
||||
[groupId],
|
||||
);
|
||||
await dbRun("DELETE FROM mock_files WHERE group_id = ?", [groupId]);
|
||||
return rows.map((row) => ({ file_path: row.file_path, alias: "", group_id: null }));
|
||||
}
|
||||
|
||||
const DEFAULT_SERVER_CONFIG: AppConfig = {
|
||||
cacheConfig: true,
|
||||
reloadOnChange: true,
|
||||
|
||||
Reference in New Issue
Block a user