UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

95 lines 3.32 kB
/** * @module Cache */ import { KyselyCacheAdapter } from "../../../../cache/implementations/adapters/kysely-cache-adapter/_module.js"; // eslint-disable-next-line @typescript-eslint/no-unused-vars import { SuperJsonSerdeAdapter } from "../../../../serde/implementations/adapters/_module-exports.js"; import { Kysely } from "kysely"; import { LibsqlDialect } from "@libsql/kysely-libsql"; import { KyselyTableNameTransformerPlugin } from "../../../../utilities/_module-exports.js"; /** * To utilize the `LibsqlCacheAdapter`, you must install the `"@libsql/client"` package and supply a {@link ISerde | `ISerde<string>`}, with an adapter like {@link SuperJsonSerdeAdapter | `SuperJsonSerdeAdapter `}. * * IMPORT_PATH: `"@daiso-tech/core/cache/adapters"` * @group Adapters */ export class LibsqlCacheAdapter { adapter; /*** * @example * ```ts * import { LibsqlCacheAdapter } from "@daiso-tech/core/cache/adapters"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"; * import { createClient } from "@libsql/client"; * * const database = createClient({ url: "file:local.db" }); * const serde = new Serde(new SuperJsonSerdeAdapter()); * const cacheAdapter = new LibsqlCacheAdapter({ * database, * serde, * }); * // You need initialize the adapter once before using it. * await cacheAdapter.init(); * ``` */ constructor(settings) { const { database, tableName = "cache", serde, disableTransaction, expiredKeysRemovalInterval, shouldRemoveExpiredKeys, } = settings; this.adapter = new KyselyCacheAdapter({ database: new Kysely({ dialect: new LibsqlDialect({ client: database, }), plugins: [ new KyselyTableNameTransformerPlugin({ cache: tableName, }), ], }), serde, disableTransaction, expiredKeysRemovalInterval, shouldRemoveExpiredKeys, }); } async removeAllExpired() { await this.adapter.removeAllExpired(); } async deInit() { await this.adapter.deInit(); } async init() { await this.adapter.init(); } async insert(data) { await this.adapter.insert(data); } async upsert(data) { return await this.adapter.upsert(data); } async find(key) { return await this.adapter.find(key); } async updateExpired(data) { return await this.adapter.updateExpired(data); } async updateUnexpired(data) { return await this.adapter.updateUnexpired(data); } async incrementUnexpired(data) { return await this.adapter.incrementUnexpired(data); } async removeUnexpiredMany(keys) { return await this.adapter.removeUnexpiredMany(keys); } async removeExpiredMany(keys) { return await this.adapter.removeExpiredMany(keys); } async removeAll() { await this.adapter.removeAll(); } async removeByKeyPrefix(prefix) { await this.adapter.removeByKeyPrefix(prefix); } } //# sourceMappingURL=libsql-cache-adapter.js.map