cachified-adapter-sqlite
Version:
SQLite adapter for @epic-web/cachified. Compatible with better-sqlite3, sqlite, sqlite3, node:sqlite, and bun:sqlite.
59 lines (56 loc) • 1.71 kB
JavaScript
import { b as buildCacheKey, t as totalTtl } from './cache-key-CFVg9mcK.js';
function bunSqliteCacheAdapter(options) {
const getStatement = options.database.query(
`SELECT value, metadata FROM ${options.tableName} WHERE key = ?`
);
const setStatement = options.database.query(
`INSERT OR REPLACE INTO ${options.tableName} (key, value, metadata) VALUES (?, ?, ?)`
);
const deleteStatement = options.database.query(
`DELETE FROM ${options.tableName} WHERE key = ?`
);
return {
name: options.name ?? "bun-sqlite",
get: (key) => {
const cacheKey = buildCacheKey(key, options.keyPrefix);
const row = getStatement.get(cacheKey);
if (!row) {
return null;
}
const entry = {
value: JSON.parse(row.value),
metadata: JSON.parse(row.metadata)
};
if (!entry.value) {
return null;
}
return entry;
},
set: (key, entry) => {
const cacheKey = buildCacheKey(key, options.keyPrefix);
const ttl = totalTtl(entry.metadata);
setStatement.run(
cacheKey,
JSON.stringify(entry.value),
JSON.stringify({
...entry.metadata,
ttl: ttl === Number.POSITIVE_INFINITY ? null : ttl
})
);
return entry.value;
},
delete: (key) => {
const cacheKey = buildCacheKey(key, options.keyPrefix);
deleteStatement.run(cacheKey);
}
};
}
function createBunSqliteCacheTable(database, tableName) {
database.run(`CREATE TABLE IF NOT EXISTS ${tableName} (
key TEXT PRIMARY KEY,
value TEXT,
metadata TEXT
)`);
}
export { bunSqliteCacheAdapter, createBunSqliteCacheTable };
//# sourceMappingURL=bun.js.map