/** * @module DataStoreEngine * This module contains the `DataStoreEngine` class and some of its subclasses like `FileStorageEngine` and `BrowserStorageEngine`. * [See the documentation for more info.](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-datastoreengine) */ import type { DataStoreData, DataStoreOptions } from "./DataStore.ts"; import type { Prettify, SerializableVal } from "./types.ts"; /** Contains the only properties of {@linkcode DataStoreOptions} that are relevant to the {@linkcode DataStoreEngine} class. */ export type DataStoreEngineDSOptions = Prettify, "decodeData" | "encodeData" | "id">>; export interface DataStoreEngine { /** Deletes all data in persistent storage, including the data container itself (e.g. a file or a database) */ deleteStorage?(): Promise; } /** * Base class for creating {@linkcode DataStore} storage engines. * This acts as an interchangeable API for writing and reading persistent JSON-serializable data in various environments. */ export declare abstract class DataStoreEngine { protected dataStoreOptions: DataStoreEngineDSOptions; constructor(options?: DataStoreEngineDSOptions); /** Called by DataStore on creation, to pass its options. Only call this if you are using this instance standalone! */ setDataStoreOptions(dataStoreOptions: DataStoreEngineDSOptions): void; /** Fetches a value from persistent storage. Defaults to `defaultValue` if the value does not exist. `null` is considered a valid value. */ abstract getValue(name: string, defaultValue: TValue): Promise; /** Sets a value in persistent storage */ abstract setValue(name: string, value: SerializableVal): Promise; /** Deletes a value from persistent storage */ abstract deleteValue(name: string): Promise; /** Serializes the given object to a string, optionally encoded with `options.encodeData` if {@linkcode useEncoding} is not set to false and the `encodeData` and `decodeData` options are set */ serializeData(data: TData, useEncoding?: boolean): Promise; /** Deserializes the given string to a JSON object, optionally decoded with `options.decodeData` if {@linkcode useEncoding} is set to true */ deserializeData(data: string, useEncoding?: boolean): Promise; /** Throws an error if the DataStoreOptions are not set or invalid */ protected ensureDataStoreOptions(): void; /** * Copies a JSON-compatible object and loses all its internal references in the process. * Uses [`structuredClone()`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) if available, otherwise falls back to `JSON.parse(JSON.stringify(obj))`. */ deepCopy(obj: T): T; } /** Options for the {@linkcode BrowserStorageEngine} class */ export type BrowserStorageEngineOptions = { /** Whether to store the data in LocalStorage (default) or SessionStorage */ type?: "localStorage" | "sessionStorage"; /** * Specifies the necessary options for storing data. * - ⚠️ Only specify this if you are using this instance standalone! The parent DataStore will set this automatically. */ dataStoreOptions?: DataStoreEngineDSOptions; }; /** * Storage engine for the {@linkcode DataStore} class that uses the browser's LocalStorage or SessionStorage to store JSON-serializable data. * * - ⚠️ Requires a DOM environment * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance */ export declare class BrowserStorageEngine extends DataStoreEngine { protected options: BrowserStorageEngineOptions & Required>; /** * Creates an instance of `BrowserStorageEngine`. * * - ⚠️ Requires a DOM environment * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance */ constructor(options?: BrowserStorageEngineOptions); /** Fetches a value from persistent storage */ getValue(name: string, defaultValue: TValue): Promise; /** Sets a value in persistent storage */ setValue(name: string, value: SerializableVal): Promise; /** Deletes a value from persistent storage */ deleteValue(name: string): Promise; } /** Options for the {@linkcode FileStorageEngine} class */ export type FileStorageEngineOptions = { /** Function that returns a string or a plain string that is the data file path, including name and extension. Defaults to `.ds-${dataStoreID}` */ filePath?: ((dataStoreID: string, dataStoreOptions: DataStoreEngineDSOptions) => string) | string; /** * Specifies the necessary options for storing data. * - ⚠️ Only specify this if you are using this instance standalone! The parent DataStore will set this automatically. */ dataStoreOptions?: DataStoreEngineDSOptions; }; /** * Storage engine for the {@linkcode DataStore} class that uses a JSON file to store JSON-serializable data. * * - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+) * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance */ export declare class FileStorageEngine extends DataStoreEngine { protected options: FileStorageEngineOptions & Required>; private fileAccessQueue; /** * Creates an instance of `FileStorageEngine`. * * - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+) * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance */ constructor(options?: FileStorageEngineOptions); /** Reads the file contents */ protected readFile(): Promise; /** Overwrites the file contents */ protected writeFile(data: TData): Promise; /** Fetches a value from persistent storage */ getValue(name: string, defaultValue: TValue): Promise; /** Sets a value in persistent storage */ setValue(name: string, value: TValue): Promise; /** Deletes a value from persistent storage */ deleteValue(name: string): Promise; /** Deletes the file that contains the data of this DataStore. */ deleteStorage(): Promise; }