Local-storage-backed persistence for GraphDB Workbench, built on the Persistence interface and LocalStorageService.
This guide explains the Persistence API and its local storage implementation in the GraphDB Workbench application. It
includes details about the key interfaces, abstract classes, and practical examples for implementing persistent storage
using local storage.
Persistence APIThe Persistence API provides a generic interface for interacting with a storage system. It supports storing,
retrieving, and removing data via a Storage interface-compatible implementation (e.g., localStorage or sessionStorage).
Persistence Interface
The Persistence interface defines the structure for storage-related services.
Methods:
getStorage(): Storage - Returns the underlying storage implementation.
get(key: string): StorageData - Retrieves the value associated with the provided key.
set(key: string, value: string): void - Stores the given value under the provided key.
remove(key: string): void - Deletes the value associated with the provided key.
The LocalStorageService abstract class implements the Persistence interface using the localStorage API. This
implementation serves as a base class for specialized storage services.
1. Namespace Support:
NAMESPACE to scope its keys.StorageKey.GLOBAL_NAMESPACE) and the service-specific namespace.2. Storage Methods:
get(key: string): StorageData - Fetches a value from localStorage.
storeValue(key: string, value: string): void - Saves a value to localStorage.
remove(key: string): void - Removes a value from localStorage.
3. Key Management:
getPrefixedKey(key: string): string method ensures that all keys are prefixed correctly for consistency and
collision avoidance.export class LanguageStorageService extends LocalStorageService {
readonly NAMESPACE = 'i18n';
set(key: string, value: string) {
this.storeValue(key, value);
}
}
In this example, LanguageStorageService manages language-related properties in the localStorage, scoped under the
i18n namespace.
The LocalStorageSubscriptionHandlerService listens to storage change events and updates the application context
accordingly. It works in conjunction with ContextService implementations to resolve and update the context properties.
StorageEvent, the service parses the key to extract the namespace and property name.resolveHandler(namespace, propertyName) method.handleStorageChange(event: StorageEvent): void {
const withoutGlobalPrefix = event.key?.substring(StorageKey.GLOBAL_NAMESPACE.length + 1);
let namespace = '';
let contextPropertyKey = '';
if (withoutGlobalPrefix) {
namespace = withoutGlobalPrefix.substring(0, withoutGlobalPrefix.indexOf('.'));
contextPropertyKey = withoutGlobalPrefix.substring(namespace.length + 1);
}
const handler = this.resolveHandler(namespace, contextPropertyKey);
if (handler) {
handler.updateContextProperty(contextPropertyKey, event.newValue);
}
}
LocalStorageService to define domain-specific storage services, specifying the NAMESPACE and implementing
additional functionality if needed.The Persistence API and its local storage implementation provide a robust framework for managing persistent data in
the GraphDB Workbench application. By adhering to the namespace conventions and leveraging the LocalStorageService as
a base class, developers can efficiently implement and maintain storage-related functionality.
See also: Developers Guide