import { KyselyCacheAdapter } from "@daiso-tech/core/cache/kysely-cache-adapter";
import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
import SQLite from 'better-sqlite3'
import { Kysely, SqliteDialect } from 'kysely'
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheAdapter = new KyselyCacheAdapter({
kysely: new Kysely({
dialect: new SqliteDialect({
database: new Sqlite("local.db"),
}),
}),
serde,
});
// You need initialize the adapter once before using it.
await cacheAdapter.init();
Retrieves a cached value by key. Returns the complete cache entry with value and expiration, or null if not found.
The cache key identifier
Cache entry with value and expiration, or null if not found.
Removes all entries from the cache. Complete cache flush operation.
Void (always succeeds)
Removes all cache entries whose keys start with the given prefix. Useful for clearing related cache entries (e.g., all user data: "user:*").
Key prefix to match (e.g., "user:123:" for all data for user 123)
Void (always succeeds)
Removes multiple specific cache entries by key. Silently ignores missing keys.
Readable execution context for the operation
Array of cache key identifiers to delete
Array of expiration info for all removed entries
Executes a function within a database transaction. Ensures all cache operations in the function are atomic and isolated.
Async function receiving transaction object, returns value of any type
The value returned by the transaction function
Updates the value for an existing cache key. Returns null if the key doesn't exist (use upsert for auto-create).
Readable execution context for the operation
The cache key to update
The new value to cache
Expiration info of the updated entry, or null if key not found
To utilize the
KyselyCacheAdapter, you must install the"kysely"package and configure aKyselyclass instance. The adapter have been tested withsqlite,postgresandmysqldatabases.IMPORT_PATH:
"@daiso-tech/core/cache/kysely-cache-adapter"