UNPKG

bentocache

Version:

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

82 lines (81 loc) 2.21 kB
import { DatabaseDriver } from "../../../../chunk-N7JOFG2I.js"; import "../../../../chunk-YAGCWAYQ.js"; import "../../../../chunk-BO75WXSS.js"; // src/drivers/database/adapters/orchid.ts function orchidDriver(options) { return { options, factory: (config) => { const adapter = new OrchidAdapter(config); return new DatabaseDriver(adapter, config); } }; } var OrchidAdapter = class { #connection; #tableName; constructor(config) { this.#connection = config.connection; } getTable() { return this.#connection(this.#tableName, (t) => ({ key: t.varchar().primaryKey(), value: t.varchar(), expires_at: t.timestampNoTZ().encode((value) => value).parse((v) => v ? new Date(v).valueOf() : v).nullable() })); } setTableName(tableName) { this.#tableName = tableName; } async get(key) { const result = await this.getTable().findByOptional({ key }).select("value", "expires_at"); if (!result) return; return { value: result.value, expiresAt: result.expires_at }; } async delete(key) { const count = await this.getTable().where({ key }).delete(); return count > 0; } async deleteMany(keys) { return await this.getTable().whereIn("key", keys).delete(); } async disconnect() { await this.#connection.close(); } async createTableIfNotExists() { await this.#connection.adapter.pool.query(` CREATE TABLE IF NOT EXISTS "public"."${this.#tableName}" ( "key" varchar NOT NULL, "value" text NOT NULL, "expires_at" timestamp, PRIMARY KEY ("key") ); `); } async pruneExpiredEntries() { await this.getTable().where({ expires_at: { lt: /* @__PURE__ */ new Date() } }).delete(); } async clear(prefix) { await this.getTable().where({ key: { startsWith: prefix } }).delete(); } async set(row) { await this.getTable().findBy({ key: row.key }).upsert({ create: { key: row.key, value: row.value, expires_at: row.expiresAt }, update: { value: row.value, expires_at: row.expiresAt } }); } }; export { OrchidAdapter, orchidDriver }; //# sourceMappingURL=orchid.js.map