Add functionality to set the selected db item (#1839)

This commit is contained in:
Charis Kyriakou
2022-12-06 15:20:07 +00:00
committed by GitHub
parent c436688eb2
commit a5fcfe7f40
3 changed files with 85 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import { pathExists, writeJSON, readJSON, readJSONSync } from "fs-extra";
import { join } from "path";
import { cloneDbConfig, DbConfig } from "./db-config";
import { cloneDbConfig, DbConfig, SelectedDbItem } from "./db-config";
import * as chokidar from "chokidar";
import { DisposableObject, DisposeHandler } from "../../pure/disposable-object";
import { DbConfigValidator } from "./db-config-validator";
@@ -56,11 +56,31 @@ export class DbConfigStore extends DisposableObject {
return this.configPath;
}
public async setSelectedDbItem(dbItem: SelectedDbItem): Promise<void> {
if (!this.config) {
// If the app is trying to set the selected item without a config
// being set it means that there is a bug in our code, so we throw
// an error instead of just returning an error result.
throw Error("Cannot select database item if config is not loaded");
}
const config: DbConfig = {
...this.config,
selected: dbItem,
};
await this.writeConfig(config);
}
private async writeConfig(config: DbConfig): Promise<void> {
await writeJSON(this.configPath, config, {
spaces: 2,
});
}
private async loadConfig(): Promise<void> {
if (!(await pathExists(this.configPath))) {
await writeJSON(this.configPath, this.createEmptyConfig(), {
spaces: 2,
});
await this.writeConfig(this.createEmptyConfig());
}
await this.readConfig();

View File

@@ -1,4 +1,5 @@
import { DbItem, DbItemKind, LocalDbItem, RemoteDbItem } from "./db-item";
import { SelectedDbItem, SelectedDbItemKind } from "./config/db-config";
export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
for (const dbItem of dbItems) {
@@ -42,3 +43,52 @@ function extractSelected(
}
return undefined;
}
export function mapDbItemToSelectedDbItem(
dbItem: DbItem,
): SelectedDbItem | undefined {
switch (dbItem.kind) {
case DbItemKind.RootLocal:
case DbItemKind.RootRemote:
// Root items are not selectable.
return undefined;
case DbItemKind.LocalList:
return {
kind: SelectedDbItemKind.LocalUserDefinedList,
listName: dbItem.listName,
};
case DbItemKind.RemoteUserDefinedList:
return {
kind: SelectedDbItemKind.RemoteUserDefinedList,
listName: dbItem.listName,
};
case DbItemKind.RemoteSystemDefinedList:
return {
kind: SelectedDbItemKind.RemoteSystemDefinedList,
listName: dbItem.listName,
};
case DbItemKind.RemoteOwner:
return {
kind: SelectedDbItemKind.RemoteOwner,
ownerName: dbItem.ownerName,
};
case DbItemKind.LocalDatabase:
return {
kind: SelectedDbItemKind.LocalDatabase,
listName: dbItem?.parentListName,
databaseName: dbItem.databaseName,
};
case DbItemKind.RemoteRepo:
return {
kind: SelectedDbItemKind.RemoteRepository,
listName: dbItem?.parentListName,
repositoryName: dbItem.repoFullName,
};
}
}

View File

@@ -3,7 +3,10 @@ import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem } from "./db-item";
import { getSelectedDbItem } from "./db-item-selection";
import {
getSelectedDbItem,
mapDbItemToSelectedDbItem,
} from "./db-item-selection";
import { createLocalTree, createRemoteTree } from "./db-tree-creator";
export class DbManager {
@@ -44,4 +47,11 @@ export class DbManager {
public getConfigPath(): string {
return this.dbConfigStore.getConfigPath();
}
public async setSelectedDbItem(dbItem: DbItem): Promise<void> {
const selectedDbItem = mapDbItemToSelectedDbItem(dbItem);
if (selectedDbItem) {
await this.dbConfigStore.setSelectedDbItem(selectedDbItem);
}
}
}