File tree Expand file tree Collapse file tree 6 files changed +77
-0
lines changed Expand file tree Collapse file tree 6 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Composes a cache key by optionally prefixing it with a namespace
3+ *
4+ * @param key - The base cache key
5+ * @param keyPrefix - Optional prefix to namespace the cache key
6+ * @returns The final cache key, either prefixed (`prefix:key`) or unchanged
7+ */
18export function buildCacheKey ( key : string , keyPrefix ?: string ) {
29 return keyPrefix ? `${ keyPrefix } :${ key } ` : key
310}
Original file line number Diff line number Diff line change @@ -3,6 +3,16 @@ import type { Database } from "better-sqlite3"
33import { buildCacheKey } from "../cache-key"
44import type { CachifiedAdapterSqliteOptions } from "../index"
55
6+ /**
7+ * Creates a better-sqlite3 cache adapter for use with cachified
8+ *
9+ * @param options - {@linkcode CachifiedAdapterSqliteOptions} plus a database instance
10+ * @param options.database - The better-sqlite3 database instance
11+ * @param options.tableName - Name of the table to store cache entries
12+ * @param options.keyPrefix - Optional prefix to namespace cache keys
13+ * @param options.name - Optional name for the cache adapter
14+ * @returns A Cache instance that stores data in SQLite using better-sqlite3
15+ */
616export function betterSqlite3CacheAdapter < Value = unknown > (
717 options : CachifiedAdapterSqliteOptions & {
818 database : Database
@@ -64,6 +74,12 @@ export function betterSqlite3CacheAdapter<Value = unknown>(
6474 }
6575}
6676
77+ /**
78+ * Creates a cache table in the SQLite database if it doesn't already exist
79+ *
80+ * @param database - The better-sqlite3 Database instance
81+ * @param tableName - Name of the cache table to create
82+ */
6783export function createBetterSqlite3CacheTable (
6884 database : Database ,
6985 tableName : string ,
Original file line number Diff line number Diff line change @@ -3,6 +3,16 @@ import { type Cache, totalTtl } from "@epic-web/cachified"
33import { buildCacheKey } from "../cache-key"
44import type { CachifiedAdapterSqliteOptions } from "../index"
55
6+ /**
7+ * Creates a Bun SQLite cache adapter for use with cachified
8+ *
9+ * @param options - {@linkcode CachifiedAdapterSqliteOptions} plus a database instance
10+ * @param options.database - The Bun SQLite database instance
11+ * @param options.tableName - Name of the table to store cache entries
12+ * @param options.keyPrefix - Optional prefix to namespace cache keys
13+ * @param options.name - Optional name for the cache adapter
14+ * @returns A Cache instance that stores data in SQLite using Bun's SQLite driver
15+ */
616export function bunSqliteCacheAdapter < Value = unknown > (
717 options : CachifiedAdapterSqliteOptions & {
818 database : Database
@@ -62,6 +72,12 @@ export function bunSqliteCacheAdapter<Value = unknown>(
6272 }
6373}
6474
75+ /**
76+ * Creates a cache table in the SQLite database if it doesn't already exist
77+ *
78+ * @param database - The Bun SQLite Database instance
79+ * @param tableName - Name of the cache table to create
80+ */
6581export function createBunSqliteCacheTable (
6682 database : Database ,
6783 tableName : string ,
Original file line number Diff line number Diff line change @@ -3,6 +3,16 @@ import type { Database } from "sqlite"
33import { buildCacheKey } from "../cache-key"
44import type { CachifiedAdapterSqliteOptions } from "../index"
55
6+ /**
7+ * Creates a sqlite cache adapter for use with cachified
8+ *
9+ * @param options - {@linkcode CachifiedAdapterSqliteOptions} plus a database instance
10+ * @param options.database - The sqlite database instance
11+ * @param options.tableName - Name of the table to store cache entries
12+ * @param options.keyPrefix - Optional prefix to namespace cache keys
13+ * @param options.name - Optional name for the cache adapter
14+ * @returns A Cache instance that stores data in SQLite using sqlite
15+ */
616export function sqliteCacheAdapter < Value = unknown > (
717 options : CachifiedAdapterSqliteOptions & {
818 database : Database
@@ -64,6 +74,12 @@ export function sqliteCacheAdapter<Value = unknown>(
6474 }
6575}
6676
77+ /**
78+ * Creates a cache table in the SQLite database if it doesn't already exist
79+ *
80+ * @param database - The sqlite Database instance
81+ * @param tableName - Name of the cache table to create
82+ */
6783export function createSqliteCacheTable ( database : Database , tableName : string ) {
6884 return database . exec ( `CREATE TABLE IF NOT EXISTS ${ tableName } (
6985 key TEXT PRIMARY KEY,
Original file line number Diff line number Diff line change @@ -3,6 +3,16 @@ import type { Database } from "sqlite3"
33import { buildCacheKey } from "../cache-key"
44import type { CachifiedAdapterSqliteOptions } from "../index"
55
6+ /**
7+ * Creates a sqlite3 cache adapter for use with cachified
8+ *
9+ * @param options - {@linkcode CachifiedAdapterSqliteOptions} plus a database instance
10+ * @param options.database - The sqlite3 database instance
11+ * @param options.tableName - Name of the table to store cache entries
12+ * @param options.keyPrefix - Optional prefix to namespace cache keys
13+ * @param options.name - Optional name for the cache adapter
14+ * @returns A Cache instance that stores data in SQLite using sqlite3
15+ */
616export function sqlite3CacheAdapter < Value = unknown > (
717 options : CachifiedAdapterSqliteOptions & {
818 database : Database
@@ -95,6 +105,12 @@ export function sqlite3CacheAdapter<Value = unknown>(
95105 }
96106}
97107
108+ /**
109+ * Creates a cache table in the SQLite database if it doesn't already exist
110+ *
111+ * @param database - The sqlite3 Database instance
112+ * @param tableName - Name of the cache table to create
113+ */
98114export async function createSqlite3CacheTable (
99115 database : Database ,
100116 tableName : string ,
Original file line number Diff line number Diff line change @@ -3,8 +3,14 @@ export * from "./drivers/sqlite"
33export * from "./drivers/better-sqlite3"
44export * from "./drivers/bun"
55
6+ /**
7+ * Common options for all SQLite-based cache adapters
8+ */
69export type CachifiedAdapterSqliteOptions = {
10+ /** The name of the table to store cache entries */
711 tableName : string
12+ /** Optional prefix to namespace cache keys */
813 keyPrefix ?: string
14+ /** Cache adapter name */
915 name ?: string
1016}
You can’t perform that action at this time.
0 commit comments