@nasriya/cachify
Version:
A lightweight, extensible in-memory caching library for storing anything, with built-in TTL and customizable cache types.
86 lines (85 loc) • 3.33 kB
JavaScript
import BackupStream from "./helpers/BackupStream.js";
import RestoreStream from "./helpers/RestoreStream.js";
// Drivers
import LocalStorageDriver from "../../api/persistence/local/local.driver.js";
import S3StorageDriver from "../../api/persistence/s3/s3.driver.js";
class PersistenceManager {
#_client;
#_drivers = new Map();
constructor(client) { this.#_client = client; }
/**
* Adds a new adapter to the storage drivers manager.
*
* Initializes a persistence service instance for the specified service and configuration,
* and registers it under the given name. If the service type does not already exist in the
* drivers map, a new entry is created.
*
* @template D - The type of storage service.
* @param {D} service - The type of storage service to be added.
* @param {PersistanceStorageServices[D]['configs']} configs - The configuration settings for the service.
* @since v1.0.0
*/
defineAdapter(service, configs) {
const serviceInstance = (() => {
switch (service) {
case 'local':
return new LocalStorageDriver(this, configs);
case 's3':
return new S3StorageDriver(this, configs);
default:
throw new Error(`Unknown or unsupported persistence service: ${service}`);
}
})();
this.#_drivers.set(service, serviceInstance);
}
/**
* Retrieves the map of all registered persistence services, keyed by service type.
*
* @returns A map of maps, where each key is a service type (e.g. "local" or "s3"),
* and each value is a map of adapter names to their corresponding persistence service
* instances. The map is ordered by service type, and the inner maps are ordered by
* adapter name.
* @since v1.0.0
*/
get drivers() { return this.#_drivers; }
/**
* Retrieves a persistence service instance by its service type.
*
* @template S - The type of storage service.
* @param {S} service - The type of storage service to retrieve.
* @returns The persistence service instance associated with the given
* service type, or `undefined` if no such service is registered.
* @since v1.0.0
*/
getDriver(service) {
const driver = this.#_drivers.get(service);
return driver;
}
/**
* Creates and initializes a new persistence stream.
*
* This method initializes a new instance of `PersistenceStream`, writes
* the initial persistence metadata to the stream, and returns the instance.
* The metadata includes a version identifier and a timestamp indicating
* the creation time of the persistence.
*
* @returns An initialized `PersistenceStream` instance.
* @since v1.0.0
*/
createBackupStream() {
return new BackupStream();
}
/**
*
* This method initializes a new instance of `RestoreStream`, using the
* associated `CachifyClient` instance to set records, and returns the
* instance.
*
* @returns An initialized `RestoreStream` instance.
* @since v1.0.0
*/
createRestoreStream() {
return new RestoreStream(this.#_client);
}
}
export default PersistenceManager;