UNPKG

@nasriya/cachify

Version:

A lightweight, extensible in-memory caching library for storing anything, with built-in TTL and customizable cache types.

91 lines (90 loc) 3.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const BackupStream_1 = __importDefault(require("./helpers/BackupStream")); const RestoreStream_1 = __importDefault(require("./helpers/RestoreStream")); // Drivers const local_driver_1 = __importDefault(require("../../api/persistence/local/local.driver")); const s3_driver_1 = __importDefault(require("../../api/persistence/s3/s3.driver")); 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 local_driver_1.default(this, configs); case 's3': return new s3_driver_1.default(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_1.default(); } /** * * 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_1.default(this.#_client); } } exports.default = PersistenceManager;