export type TableValue = IDBValidKey | IDBKeyRange; export type TinyTableDbRecord = { /** * - The record key. */ key: Key; /** * - The record value. */ value: Value; /** * - Unix timestamp (in milliseconds). */ timestamp: number; }; /** * A single persisted record. * @template {TableValue} Key * @template {*} Value * @typedef {Object} TinyTableDbRecord * @property {Key} key - The record key. * @property {Value} value - The record value. * @property {number} timestamp - Unix timestamp (in milliseconds). */ /** * Shared IndexedDB plumbing for {@link TinySetDb} and {@link TinyMapDb}. * * This class owns the connection, the write queue, the action queue and the * hydration lifecycle. Subclasses only implement the in-memory cache and the * public API, which keeps both collections perfectly consistent. */ declare class TinyTableDb { #private; /** * @param {{ storeName: string, openDatabase: () => Promise }} options - Table options. * @throws {TypeError} If `storeName` is not a non-empty string. * @throws {TypeError} If `openDatabase` is not a function. */ constructor({ storeName, openDatabase }: { storeName: string; openDatabase: () => Promise; }); /** * Name of the object store that backs this table. * @returns {string} */ get storeName(): string; /** * Whether the in-memory cache was already hydrated from IndexedDB. * @returns {boolean} */ get isReady(): boolean; /** * Tag used by `Object.prototype.toString`. * @returns {string} */ get [Symbol.toStringTag](): string; /** * Loads every persisted value into the in-memory cache. * Safe to call multiple times, the work happens only once. * @returns {Promise} */ hydrate(): Promise; /** * Runs an operation with exclusive access to the in-memory cache. * @param {() => Promise} operation - The operation to run. * @returns {Promise} */ _action(operation: () => Promise): Promise; /** * Runs a write operation with exclusive access to the object store. * @param {() => Promise} operation - The operation to run. * @returns {Promise} */ _write(operation: () => Promise): Promise; /** * Persists a single record. * @param {TinyTableDbRecord|TinySetDbRecord} record - The record to persist. * @returns {Promise} */ _persist(record: TinyTableDbRecord | TinySetDbRecord): Promise; /** * Deletes a single record by its key. * @param {TableValue} key - The key to delete. * @returns {Promise} */ _remove(key: TableValue): Promise; /** * Deletes every record from the object store. * @returns {Promise} */ _clear(): Promise; /** * Populates the in-memory cache from the persisted records. * @abstract * @param {(TinyTableDbRecord|TinySetDbRecord)[]} records - The persisted records. * @returns {void} */ _load(records: (TinyTableDbRecord | TinySetDbRecord)[]): void; } export type TinySetDbRecord = { /** * - The unique value that was stored. */ value: Value; /** * - Unix timestamp (in milliseconds) of the first detection. */ timestamp: number; }; export type TinySetDbValidator = (value: Value) => boolean; export type TinySetDbOptions = { /** * - Name of the object store (table) that backs this set. */ storeName: string; /** * - Resolves with the shared connection. */ openDatabase: () => Promise; /** * - Optional predicate applied to every value. */ validate?: TinySetDbValidator | null; }; export type TinySetDbSource = TinySetDb | Set | Value[]; export type TinySetDbForEachCallback = (value: Value, sameValue: Value, set: TinySetDb) => void; /** * A single persisted value entry. * @template {TableValue} Value * @typedef {Object} TinySetDbRecord * @property {Value} value - The unique value that was stored. * @property {number} timestamp - Unix timestamp (in milliseconds) of the first detection. */ /** * Predicate used to reject values that must never reach a table. * @template {TableValue} Value * @callback TinySetDbValidator * @param {Value} value - The candidate value. * @returns {boolean} A truthy value when the value is accepted. */ /** * @template {TableValue} Value * @typedef {Object} TinySetDbOptions * @property {string} storeName - Name of the object store (table) that backs this set. * @property {() => Promise} openDatabase - Resolves with the shared connection. * @property {TinySetDbValidator|null} [validate] - Optional predicate applied to every value. */ /** * A source of values accepted by the set operations. * @template {TableValue} Value * @typedef {TinySetDb|Set|Value[]} TinySetDbSource */ /** * Callback invoked by {@link TinySetDb#forEach}. * @template {TableValue} Value * @callback TinySetDbForEachCallback * @param {Value} value - The current value. * @param {Value} sameValue - The same value, mirroring `Set.prototype.forEach`. * @param {TinySetDb} set - The set being iterated. * @returns {void} */ /** * @template {TableValue} Value * @extends {TinyTableDb} * Persistent set of values backed by a single IndexedDB object store. */ declare class TinySetDb extends TinyTableDb { #private; /** * @param {TinySetDbOptions} options - Table options. * @throws {TypeError} If `validate` is neither a function nor `null`. */ constructor({ storeName, openDatabase, validate }: TinySetDbOptions); /** * Number of values currently held in the in-memory cache. * @returns {number} */ get size(): number; /** * Checks whether the given value is already stored. * @param {Value} value - The value to look up. * @returns {Promise} `true` when the value was already logged. * @throws {TypeError} If `value` is not a non-empty string. * @throws {TypeError} If the table validator rejects the value. */ has(value: Value): Promise; /** * Adds a value to the cache and persists it. * @param {Value} value - The value to store. * @returns {Promise} `true` when the value was added, `false` when it already existed. * @throws {TypeError} If `value` is not a non-empty string. * @throws {TypeError} If the table validator rejects the value. */ add(value: Value): Promise; /** * Removes a value from the cache and from the object store. * @param {Value} value - The value to remove. * @returns {Promise} `true` when the value existed and was removed. * @throws {TypeError} If `value` is not a non-empty string. * @throws {TypeError} If the table validator rejects the value. */ delete(value: Value): Promise; /** * Removes every value from both the cache and the object store. * @returns {Promise} */ clear(): Promise; /** * Returns a snapshot of every stored value. * @returns {Promise} A new array with the current values. */ toArray(): Promise; /** * Serialization hook used by `JSON.stringify`. * @returns {Promise} A new array with the current values. */ toJSON(): Promise; /** * Iterates over the stored values. * @returns {AsyncGenerator} An async generator of values. */ values(): AsyncGenerator; /** * Alias of {@link TinySetDb#values}, mirroring `Set.prototype.keys`. * @returns {AsyncGenerator} An async generator of values. */ keys(): AsyncGenerator; /** * Iterates over `[value, value]` pairs, mirroring `Set.prototype.entries`. * @returns {AsyncGenerator<[Value, Value], void, void>} An async generator of pairs. */ entries(): AsyncGenerator<[Value, Value], void, void>; /** * Iterates over the stored values. * @returns {AsyncGenerator} An async generator of values. */ [Symbol.asyncIterator](): AsyncGenerator; /** * Runs a callback for every stored value. * @param {TinySetDbForEachCallback} callbackFn - Callback invoked per value. * @param {unknown} [thisArg] - Value used as `this` inside the callback. * @returns {Promise} * @throws {TypeError} If `callbackFn` is not a function. */ forEach(callbackFn: TinySetDbForEachCallback, thisArg?: unknown): Promise; /** * Returns a new native `Set` with the values of this set and every source. * @param {...TinySetDbSource} sources - Extra value sources. * @returns {Promise>} A new native `Set`. */ union(...sources: TinySetDbSource[]): Promise>; /** * Returns a new native `Set` with the values present in both sets. * @param {TinySetDbSource} other - The other value source. * @returns {Promise>} A new native `Set`. */ intersection(other: TinySetDbSource): Promise>; /** * Returns a new native `Set` with the values of this set that are not in `other`. * @param {TinySetDbSource} other - The other value source. * @returns {Promise>} A new native `Set`. */ difference(other: TinySetDbSource): Promise>; /** * Returns a new native `Set` with the values present in only one of the sets. * @param {TinySetDbSource} other - The other value source. * @returns {Promise>} A new native `Set`. */ symmetricDifference(other: TinySetDbSource): Promise>; /** * Checks whether every value of this set exists in `other`. * @param {TinySetDbSource} other - The other value source. * @returns {Promise} `true` when this set is a subset of `other`. */ isSubsetOf(other: TinySetDbSource): Promise; /** * Checks whether every value of `other` exists in this set. * @param {TinySetDbSource} other - The other value source. * @returns {Promise} `true` when this set is a superset of `other`. */ isSupersetOf(other: TinySetDbSource): Promise; /** * Checks whether this set and `other` share no values. * @param {TinySetDbSource} other - The other value source. * @returns {Promise} `true` when the sets are disjoint. */ isDisjointFrom(other: TinySetDbSource): Promise; /** * Populates the in-memory cache from the persisted records. * @param {TinySetDbRecord[]} records - The persisted records. * @returns {void} */ _load(records: TinySetDbRecord[]): void; } export type TinyMapDbRecord = { /** * - The unique key that was stored. */ key: Key; /** * - The value bound to the key. */ value: Value; /** * - Unix timestamp (in milliseconds) of the last write. */ timestamp: number; }; export type TinyMapDbValidator = (key: Key, value: Value) => boolean; export type TinyMapDbOptions = { /** * - Name of the object store (table) that backs this map. */ storeName: string; /** * - Resolves with the shared connection. */ openDatabase: () => Promise; /** * - Optional predicate applied to every entry. */ validate?: TinyMapDbValidator | null; }; export type TinyMapDbForEachCallback = (value: Value, key: Key, map: TinyMapDb) => void; /** * A single persisted entry. * @template {TableValue} Key * @template {*} Value * @typedef {Object} TinyMapDbRecord * @property {Key} key - The unique key that was stored. * @property {Value} value - The value bound to the key. * @property {number} timestamp - Unix timestamp (in milliseconds) of the last write. */ /** * Predicate used to reject entries that must never reach a table. * @template {TableValue} Key * @template {*} Value * @callback TinyMapDbValidator * @param {Key} key - The candidate key. * @param {Value} value - The candidate value. * @returns {boolean} A truthy value when the entry is accepted. */ /** * @template {TableValue} Key * @template {*} Value * @typedef {Object} TinyMapDbOptions * @property {string} storeName - Name of the object store (table) that backs this map. * @property {() => Promise} openDatabase - Resolves with the shared connection. * @property {TinyMapDbValidator|null} [validate] - Optional predicate applied to every entry. */ /** * Callback invoked by {@link TinyMapDb#forEach}. * @template {TableValue} Key * @template {*} Value * @callback TinyMapDbForEachCallback * @param {Value} value - The current value. * @param {Key} key - The current key. * @param {TinyMapDb} map - The map being iterated. * @returns {void} */ /** * @template {TableValue} Key * @template {*} Value * @extends {TinyTableDb} * Persistent key/value map backed by a single IndexedDB object store. * * The class keeps a synchronous in-memory cache so it can be queried from * synchronous code while every write is mirrored to IndexedDB, so the data * survives a service worker restart. */ declare class TinyMapDb extends TinyTableDb { #private; /** * @param {TinyMapDbOptions} options - Table options. * @throws {TypeError} If `validate` is neither a function nor `null`. */ constructor({ storeName, openDatabase, validate }: TinyMapDbOptions); /** * Number of entries currently held in the in-memory cache. * @returns {number} */ get size(): number; /** * Checks whether the given key is already stored. * @param {Key} key - The key to look up. * @returns {Promise} `true` when the key exists. * @throws {TypeError} If `key` is not a non-empty string. */ has(key: Key): Promise; /** * Reads the value bound to the given key. * @param {Key} key - The key to look up. * @returns {Promise} The stored value, or `undefined` when absent. * @throws {TypeError} If `key` is not a non-empty string. */ get(key: Key): Promise; /** * Binds a value to a key, in the cache and in the object store. * @param {Key} key - The key to write. * @param {Value} value - The value to bind. * @returns {Promise} The same instance, allowing chained calls. * @throws {TypeError} If `key` is not a non-empty string. * @throws {TypeError} If the table validator rejects the entry. */ set(key: Key, value: Value): Promise; /** * Removes an entry from the cache and from the object store. * @param {Key} key - The key to remove. * @returns {Promise} `true` when the key existed and was removed. * @throws {TypeError} If `key` is not a non-empty string. */ delete(key: Key): Promise; /** * Removes every entry from both the cache and the object store. * @returns {Promise} */ clear(): Promise; /** * Returns a snapshot of every stored entry. * @returns {Promise>} A new array with the current entries. */ toArray(): Promise>; /** * Serialization hook used by `JSON.stringify`. * @returns {Promise>} A new array with the current entries. */ toJSON(): Promise>; /** * Iterates over the stored keys. * @returns {AsyncGenerator} An async generator of keys. */ keys(): AsyncGenerator; /** * Iterates over the stored values. * @returns {AsyncGenerator} An async generator of values. */ values(): AsyncGenerator; /** * Iterates over the stored entries. * @returns {AsyncGenerator<[Key, Value], void, void>} An async generator of entries. */ entries(): AsyncGenerator<[Key, Value], void, void>; /** * Iterates over the stored entries. * @returns {AsyncGenerator<[Key, Value], void, void>} An async generator of entries. */ [Symbol.asyncIterator](): AsyncGenerator<[Key, Value], void, void>; /** * Runs a callback for every stored entry. * @param {TinyMapDbForEachCallback} callbackFn - Callback invoked per entry. * @param {unknown} [thisArg] - Value used as `this` inside the callback. * @returns {Promise} * @throws {TypeError} If `callbackFn` is not a function. */ forEach(callbackFn: TinyMapDbForEachCallback, thisArg?: unknown): Promise; /** * Populates the in-memory cache from the persisted records. * @param {TinyMapDbRecord[]} records - The persisted records. * @returns {void} */ _load(records: TinyMapDbRecord[]): void; } export type TinyDatabaseTableConfig = { /** * - Name of the object store. */ name: string; /** * - Kind of table. */ type: 'set' | 'map'; /** * - Optional predicate, read from the latest migration that creates the table. */ validate?: ((...args: any[]) => boolean) | null; }; export type TinyDatabaseTableDefinition = string | TinyDatabaseTableConfig; export type TinyDatabaseMigration = { /** * - Tables created by this version. */ create?: TinyDatabaseTableDefinition[]; /** * - Names of the tables dropped by this version. */ delete?: string[]; }; export type TinyDatabaseNormalizedMigration = { /** * - Tables created by this version. */ create: Required[]; /** * - Names of the tables dropped by this version. */ delete: string[]; }; /** * A single table definition. * @typedef {Object} TinyDatabaseTableConfig * @property {string} name - Name of the object store. * @property {'set'|'map'} type - Kind of table. * @property {((...args: any[]) => boolean)|null} [validate] - Optional predicate, read from the latest migration that creates the table. */ /** * A table can be declared by name or with a full configuration object. * @typedef {string|TinyDatabaseTableConfig} TinyDatabaseTableDefinition */ /** * A single schema migration. * * The version number of a migration is its 1-based position in the array, so * the first entry is version 1, the second is version 2, and so on. Dropping a * table and creating it again inside the same entry is the supported way of * changing a key path. * * @typedef {Object} TinyDatabaseMigration * @property {TinyDatabaseTableDefinition[]} [create] - Tables created by this version. * @property {string[]} [delete] - Names of the tables dropped by this version. */ /** * A migration whose table definitions were already normalized. * * Unlike {@link TinyDatabaseMigration}, every property is present and every * table is a full configuration object, so the upgrade handler can read * `name` and `type` without narrowing the union first. * * @typedef {Object} TinyDatabaseNormalizedMigration * @property {Required[]} create - Tables created by this version. * @property {string[]} delete - Names of the tables dropped by this version. */ /** * A single IndexedDB database that owns several {@link TinySetDb} and * {@link TinyMapDb} tables. * * The migration array is the single source of truth: the schema is the result * of replaying every entry, and the database version is the array length. Every * table is an object store inside the same database, so a service worker only * ever opens one connection. */ declare class TinySetMapDatabase { #private; /** * @param {string} name - Name of the IndexedDB database. * @param {TinyDatabaseMigration[]} migrations - Ordered schema history, one entry per version. * @throws {TypeError} If `name` is not a non-empty string. * @throws {TypeError} If `migrations` is not a non-empty array. * @throws {TypeError} If a migration is invalid. * @throws {Error} If a table is created twice without being dropped in between. */ constructor(name: string, migrations: TinyDatabaseMigration[]); /** * Name of the IndexedDB database in use. * @returns {string} */ get name(): string; /** * Schema version of the IndexedDB database in use. * @returns {number} */ get version(): number; /** * Names of every table exposed by this database. * @returns {string[]} */ get tableNames(): string[]; /** * Resolves once the connection is open and every table is hydrated. * @returns {Promise} */ get ready(): Promise; /** * Returns a Set instance of table by name. * @param {string} tableName - Name of the table. * @returns {TinySetDb} The requested table. * @throws {TypeError} If `tableName` is not a non-empty string. * @throws {RangeError} If the table was not declared in the constructor. */ tableSet(tableName: string): TinySetDb; /** * Returns a Map instance of table by name. * @param {string} tableName - Name of the table. * @returns {TinyMapDb} The requested table. * @throws {TypeError} If `tableName` is not a non-empty string. * @throws {RangeError} If the table was not declared in the constructor. */ tableMap(tableName: string): TinyMapDb; /** * Checks whether a table was declared in the constructor. * @param {string} tableName - Name of the table. * @returns {boolean} `true` when the table exists. * @throws {TypeError} If `tableName` is not a non-empty string. */ has(tableName: string): boolean; /** * Closes the underlying connection. * @returns {Promise} */ close(): Promise; } export { TinySetMapDatabase, TinySetDb, TinyMapDb, TinyTableDb }; //# sourceMappingURL=TinySetMapDatabase.d.mts.map