cachified-adapter-sqlite is an adapter for @epic-web/cachified that allows you to use SQLite as a cache backend.
It supports better-sqlite3, sqlite, sqlite3, bun:sqlite, and node:sqlite.
Install the package and @epic-web/cachified (if you haven't already — it's a peer dependency).
npm i cachified-adapter-sqlite @epic-web/cachifiedOther package managers
pnpm add cachified-adapter-sqlite @epic-web/cachifiedyarn add cachified-adapter-sqlite @epic-web/cachifiedbun add cachified-adapter-sqlite @epic-web/cachifiedimport{cachified}from"@epic-web/cachified"import{betterSqlite3CacheAdapter,createBetterSqlite3CacheTable}from"cachified-adapter-sqlite/better-sqlite3"// better-sqlite3importDatabasefrom"better-sqlite3"// ---- OR ----import{sqliteCacheAdapter,createSqliteCacheTable}from"cachified-adapter-sqlite/sqlite"// sqliteimport{sqlite3CacheAdapter,createSqlite3CacheTable}from"cachified-adapter-sqlite/sqlite3"// sqlite3import{bunSqliteCacheAdapter,createBunSqliteCacheTable}from"cachified-adapter-sqlite/bun"// bun:sqliteimport{nodeSqliteCacheAdapter,createNodeSqliteCacheTable}from"cachified-adapter-sqlite/node-sqlite"// node:sqliteconstTABLE_NAME="cache"constdatabase=newDatabase(":memory:")// create a database using your library of choicecreateBetterSqlite3CacheTable(database,TABLE_NAME)constcache=betterSqlite3CacheAdapter({
database,tableName: TABLE_NAME,keyPrefix: "my-app",// optionally specify a key prefixname: "my-app-cache",// optionally specify a cache name})exportasyncfunctiongetUserById(id: number){returncachified({key: `user-${id}`,
cache,asyncgetFreshValue(){constresponse=awaitfetch(`https://jsonplaceholder.typicode.com/users/${id}`)returnresponse.json()asPromise<Record<string,unknown>>},ttl: 60_000,// 1 minutestaleWhileRevalidate: 300_000,// 5 minutes})}Kent C. Dodds and contributors for the @epic-web/cachified library, and to Adishwar Rishi for his inspirational cachified-adapter-cloudflare-kv