UNPKG

bentocache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

64 lines (63 loc) 2.04 kB
import { DatabaseDriver } from "../../../../chunk-CVIVKJ25.js"; import "../../../../chunk-YAGCWAYQ.js"; import "../../../../chunk-BO75WXSS.js"; // src/drivers/database/adapters/knex.ts function knexDriver(options) { return { options, factory: (config) => { const adapter = new KnexAdapter(config); return new DatabaseDriver(adapter, config); } }; } var KnexAdapter = class { #connection; #tableName; constructor(config) { this.#connection = config.connection; } setTableName(tableName) { this.#tableName = tableName; } async get(key) { const result = await this.#connection.from(this.#tableName).select(["value", "expires_at"]).where("key", key).first(); if (!result) return; return { value: result.value, expiresAt: result.expires_at }; } async delete(key) { const result = await this.#connection.from(this.#tableName).where("key", key).delete(); return result > 0; } async deleteMany(keys) { return await this.#connection.from(this.#tableName).whereIn("key", keys).delete(); } async disconnect() { await this.#connection.destroy(); } async createTableIfNotExists() { const hasTable = await this.#connection.schema.hasTable(this.#tableName); if (hasTable) return; await this.#connection.schema.createTable(this.#tableName, (table) => { table.string("key", 255).notNullable().primary(); table.text("value", "longtext"); table.timestamp("expires_at").nullable(); }); } async pruneExpiredEntries() { await this.#connection.from(this.#tableName).where("expires_at", "<", /* @__PURE__ */ new Date()).delete(); } async clear(prefix) { await this.#connection.from(this.#tableName).where("key", "like", `${prefix}%`).delete(); } async set(row) { await this.#connection.from(this.#tableName).insert({ key: row.key, value: row.value, expires_at: row.expiresAt }).onConflict("key").merge(["value", "expires_at"]); } }; export { KnexAdapter, knexDriver }; //# sourceMappingURL=knex.js.map