11import { pathExists , outputJSON , readJSON , readJSONSync } from "fs-extra" ;
22import { join } from "path" ;
3- import { cloneDbConfig , DbConfig , SelectedDbItem } from "./db-config" ;
3+ import {
4+ cloneDbConfig ,
5+ DbConfig ,
6+ renameLocalDb ,
7+ renameLocalList ,
8+ renameRemoteList ,
9+ SelectedDbItem ,
10+ } from "./db-config" ;
411import * as chokidar from "chokidar" ;
512import { DisposableObject , DisposeHandler } from "../../pure/disposable-object" ;
613import { DbConfigValidator } from "./db-config-validator" ;
@@ -11,6 +18,11 @@ import {
1118 DbConfigValidationErrorKind ,
1219} from "../db-validation-errors" ;
1320import { ValueResult } from "../../common/value-result" ;
21+ import {
22+ LocalDatabaseDbItem ,
23+ LocalListDbItem ,
24+ RemoteUserDefinedListDbItem ,
25+ } from "../db-item" ;
1426
1527export class DbConfigStore extends DisposableObject {
1628 public readonly onDidChangeConfig : AppEvent < void > ;
@@ -161,6 +173,65 @@ export class DbConfigStore extends DisposableObject {
161173 await this . writeConfig ( config ) ;
162174 }
163175
176+ public async renameLocalList (
177+ currentDbItem : LocalListDbItem ,
178+ newName : string ,
179+ ) {
180+ if ( ! this . config ) {
181+ throw Error ( "Cannot rename local list if config is not loaded" ) ;
182+ }
183+
184+ this . validateLocalListName ( newName ) ;
185+
186+ const updatedConfig = renameLocalList (
187+ this . config ,
188+ currentDbItem . listName ,
189+ newName ,
190+ ) ;
191+
192+ await this . writeConfig ( updatedConfig ) ;
193+ }
194+
195+ public async renameRemoteList (
196+ currentDbItem : RemoteUserDefinedListDbItem ,
197+ newName : string ,
198+ ) {
199+ if ( ! this . config ) {
200+ throw Error ( "Cannot rename remote list if config is not loaded" ) ;
201+ }
202+
203+ this . validateRemoteListName ( newName ) ;
204+
205+ const updatedConfig = renameRemoteList (
206+ this . config ,
207+ currentDbItem . listName ,
208+ newName ,
209+ ) ;
210+
211+ await this . writeConfig ( updatedConfig ) ;
212+ }
213+
214+ public async renameLocalDb (
215+ currentDbItem : LocalDatabaseDbItem ,
216+ newName : string ,
217+ parentListName ?: string ,
218+ ) : Promise < void > {
219+ if ( ! this . config ) {
220+ throw Error ( "Cannot rename local db if config is not loaded" ) ;
221+ }
222+
223+ this . validateLocalDbName ( newName ) ;
224+
225+ const updatedConfig = renameLocalDb (
226+ this . config ,
227+ currentDbItem . databaseName ,
228+ newName ,
229+ parentListName ,
230+ ) ;
231+
232+ await this . writeConfig ( updatedConfig ) ;
233+ }
234+
164235 public doesRemoteListExist ( listName : string ) : boolean {
165236 if ( ! this . config ) {
166237 throw Error ( "Cannot check remote list existence if config is not loaded" ) ;
@@ -179,6 +250,23 @@ export class DbConfigStore extends DisposableObject {
179250 return this . config . databases . local . lists . some ( ( l ) => l . name === listName ) ;
180251 }
181252
253+ public doesLocalDbExist ( dbName : string , listName ?: string ) : boolean {
254+ if ( ! this . config ) {
255+ throw Error (
256+ "Cannot check remote database existence if config is not loaded" ,
257+ ) ;
258+ }
259+
260+ if ( listName ) {
261+ return this . config . databases . local . lists . some (
262+ ( l ) =>
263+ l . name === listName && l . databases . some ( ( d ) => d . name === dbName ) ,
264+ ) ;
265+ }
266+
267+ return this . config . databases . local . databases . some ( ( d ) => d . name === dbName ) ;
268+ }
269+
182270 public doesRemoteDbExist ( dbName : string , listName ?: string ) : boolean {
183271 if ( ! this . config ) {
184272 throw Error (
@@ -344,4 +432,14 @@ export class DbConfigStore extends DisposableObject {
344432 throw Error ( `A remote list with the name '${ listName } ' already exists` ) ;
345433 }
346434 }
435+
436+ private validateLocalDbName ( dbName : string ) : void {
437+ if ( dbName === "" ) {
438+ throw Error ( "Database name cannot be empty" ) ;
439+ }
440+
441+ if ( this . doesLocalDbExist ( dbName ) ) {
442+ throw Error ( `A local database with the name '${ dbName } ' already exists` ) ;
443+ }
444+ }
347445}
0 commit comments