@@ -2,6 +2,7 @@ import { createServerFn } from "@tanstack/react-start";
22import { nanoid } from "nanoid" ;
33import { getDb , getTursoClient } from "../../db/client" ;
44import { embedText } from "../../db/embed" ;
5+ import { cacheKey , kvCached } from "../../db/kv-cache" ;
56import {
67 getAppAlternatives ,
78 getAppBySlug ,
@@ -34,37 +35,53 @@ export const fetchApps = createServerFn({ method: "GET" })
3435 )
3536 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Drizzle's AppSourceMetadata (Record<string, unknown>) doesn't satisfy TanStack's serialization check
3637 . handler ( async ( { data } ) : Promise < any > => {
37- const db = getDb ( ) ;
38- return listApps ( db , data ) ;
38+ return kvCached ( cacheKey ( "listApps" , data ) , ( ) => {
39+ const db = getDb ( ) ;
40+ return listApps ( db , data ) ;
41+ } ) ;
3942 } ) ;
4043
4144export const fetchAppBySlug = createServerFn ( { method : "GET" } )
4245 . inputValidator ( ( input : { slug : string } ) => input )
4346 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Drizzle's AppSourceMetadata (Record<string, unknown>) doesn't satisfy TanStack's serialization check
4447 . handler ( async ( { data } ) : Promise < any > => {
45- const db = getDb ( ) ;
46- return getAppBySlug ( db , data . slug ) ;
48+ return kvCached ( cacheKey ( "getAppBySlug" , { slug : data . slug } ) , ( ) => {
49+ const db = getDb ( ) ;
50+ return getAppBySlug ( db , data . slug ) ;
51+ } ) ;
4752 } ) ;
4853
4954export const fetchAppAlternatives = createServerFn ( { method : "GET" } )
5055 . inputValidator ( ( input : { appId : string } ) => input )
5156 . handler ( async ( { data } ) => {
52- const db = getDb ( ) ;
53- return getAppAlternatives ( db , data . appId ) ;
57+ return kvCached (
58+ cacheKey ( "getAppAlternatives" , { appId : data . appId } ) ,
59+ ( ) => {
60+ const db = getDb ( ) ;
61+ return getAppAlternatives ( db , data . appId ) ;
62+ } ,
63+ ) ;
5464 } ) ;
5565
5666export const fetchProprietaryApps = createServerFn ( { method : "GET" } )
5767 . inputValidator ( ( input : { page ?: number ; limit ?: number } ) => input )
5868 . handler ( async ( { data } ) => {
59- const db = getDb ( ) ;
60- return listProprietaryApps ( db , data ) ;
69+ return kvCached ( cacheKey ( "listProprietaryApps" , data ) , ( ) => {
70+ const db = getDb ( ) ;
71+ return listProprietaryApps ( db , data ) ;
72+ } ) ;
6173 } ) ;
6274
6375export const fetchProprietaryAppBySlug = createServerFn ( { method : "GET" } )
6476 . inputValidator ( ( input : { slug : string } ) => input )
6577 . handler ( async ( { data } ) => {
66- const db = getDb ( ) ;
67- return getProprietaryAppBySlug ( db , data . slug ) ;
78+ return kvCached (
79+ cacheKey ( "getProprietaryAppBySlug" , { slug : data . slug } ) ,
80+ ( ) => {
81+ const db = getDb ( ) ;
82+ return getProprietaryAppBySlug ( db , data . slug ) ;
83+ } ,
84+ ) ;
6885 } ) ;
6986
7087export const fetchProprietaryAppAlternatives = createServerFn ( {
@@ -73,27 +90,40 @@ export const fetchProprietaryAppAlternatives = createServerFn({
7390 . inputValidator ( ( input : { proprietaryAppId : string } ) => input )
7491 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Drizzle's AppSourceMetadata (Record<string, unknown>) doesn't satisfy TanStack's serialization check
7592 . handler ( async ( { data } ) : Promise < any > => {
76- const db = getDb ( ) ;
77- return getProprietaryAppAlternatives ( db , data . proprietaryAppId ) ;
93+ return kvCached (
94+ cacheKey ( "getProprietaryAppAlternatives" , {
95+ proprietaryAppId : data . proprietaryAppId ,
96+ } ) ,
97+ ( ) => {
98+ const db = getDb ( ) ;
99+ return getProprietaryAppAlternatives ( db , data . proprietaryAppId ) ;
100+ } ,
101+ ) ;
78102 } ) ;
79103
80104export const fetchTags = createServerFn ( { method : "GET" } ) . handler ( async ( ) => {
81- const db = getDb ( ) ;
82- return listTags ( db ) ;
105+ return kvCached ( cacheKey ( "listTags" ) , ( ) => {
106+ const db = getDb ( ) ;
107+ return listTags ( db ) ;
108+ } ) ;
83109} ) ;
84110
85111export const fetchTagsByType = createServerFn ( { method : "GET" } )
86112 . inputValidator ( ( input : { type : TagType } ) => input )
87113 . handler ( async ( { data } ) => {
88- const db = getDb ( ) ;
89- return listTagsByType ( db , data . type ) ;
114+ return kvCached ( cacheKey ( "listTagsByType" , { type : data . type } ) , ( ) => {
115+ const db = getDb ( ) ;
116+ return listTagsByType ( db , data . type ) ;
117+ } ) ;
90118 } ) ;
91119
92120export const fetchCategoriesWithApps = createServerFn ( {
93121 method : "GET" ,
94122} ) . handler ( async ( ) => {
95- const db = getDb ( ) ;
96- return listCategoriesWithApps ( db ) ;
123+ return kvCached ( cacheKey ( "listCategoriesWithApps" ) , ( ) => {
124+ const db = getDb ( ) ;
125+ return listCategoriesWithApps ( db ) ;
126+ } ) ;
97127} ) ;
98128
99129export const fetchSearchResults = createServerFn ( { method : "GET" } )
@@ -135,8 +165,10 @@ export const fetchSearchResults = createServerFn({ method: "GET" })
135165export const fetchTagBySlug = createServerFn ( { method : "GET" } )
136166 . inputValidator ( ( input : { slug : string ; type ?: TagType } ) => input )
137167 . handler ( async ( { data } ) => {
138- const db = getDb ( ) ;
139- return getTagBySlug ( db , data . slug , data . type ) ;
168+ return kvCached ( cacheKey ( "getTagBySlug" , data ) , ( ) => {
169+ const db = getDb ( ) ;
170+ return getTagBySlug ( db , data . slug , data . type ) ;
171+ } ) ;
140172 } ) ;
141173
142174export const fetchAppsByTag = createServerFn ( { method : "GET" } )
@@ -145,21 +177,27 @@ export const fetchAppsByTag = createServerFn({ method: "GET" })
145177 )
146178 // eslint-disable-next-line @typescript-eslint/no-explicit-any
147179 . handler ( async ( { data } ) : Promise < any > => {
148- const db = getDb ( ) ;
149- return listAppsByTag ( db , data . tagSlug , data ) ;
180+ return kvCached ( cacheKey ( "listAppsByTag" , data ) , ( ) => {
181+ const db = getDb ( ) ;
182+ return listAppsByTag ( db , data . tagSlug , data ) ;
183+ } ) ;
150184 } ) ;
151185
152186export const fetchTagsWithCounts = createServerFn ( { method : "GET" } )
153187 . inputValidator ( ( input : { type ?: TagType } ) => input )
154188 . handler ( async ( { data } ) => {
155- const db = getDb ( ) ;
156- return listTagsWithCounts ( db , data . type ) ;
189+ return kvCached ( cacheKey ( "listTagsWithCounts" , data ) , ( ) => {
190+ const db = getDb ( ) ;
191+ return listTagsWithCounts ( db , data . type ) ;
192+ } ) ;
157193 } ) ;
158194
159195export const fetchLicenses = createServerFn ( { method : "GET" } ) . handler (
160196 async ( ) => {
161- const db = getDb ( ) ;
162- return listLicenses ( db ) ;
197+ return kvCached ( cacheKey ( "listLicenses" ) , ( ) => {
198+ const db = getDb ( ) ;
199+ return listLicenses ( db ) ;
200+ } ) ;
163201 } ,
164202) ;
165203
@@ -169,38 +207,51 @@ export const fetchAppsByLicense = createServerFn({ method: "GET" })
169207 )
170208 // eslint-disable-next-line @typescript-eslint/no-explicit-any
171209 . handler ( async ( { data } ) : Promise < any > => {
172- const db = getDb ( ) ;
173- return listAppsByLicense ( db , data . license , data ) ;
210+ return kvCached ( cacheKey ( "listAppsByLicense" , data ) , ( ) => {
211+ const db = getDb ( ) ;
212+ return listAppsByLicense ( db , data . license , data ) ;
213+ } ) ;
174214 } ) ;
175215
176216export const fetchDesktopApps = createServerFn ( { method : "GET" } )
177217 . inputValidator ( ( input : { page ?: number ; limit ?: number } ) => input )
178218 // eslint-disable-next-line @typescript-eslint/no-explicit-any
179219 . handler ( async ( { data } ) : Promise < any > => {
180- const db = getDb ( ) ;
181- return listDesktopApps ( db , data ) ;
220+ return kvCached ( cacheKey ( "listDesktopApps" , data ) , ( ) => {
221+ const db = getDb ( ) ;
222+ return listDesktopApps ( db , data ) ;
223+ } ) ;
182224 } ) ;
183225
184226export const fetchRecentApps = createServerFn ( { method : "GET" } )
185227 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Drizzle's AppSourceMetadata (Record<string, unknown>) doesn't satisfy TanStack's serialization check
186228 . handler ( async ( ) : Promise < any > => {
187- const db = getDb ( ) ;
188- return getRecentApps ( db ) ;
229+ return kvCached ( cacheKey ( "getRecentApps" ) , ( ) => {
230+ const db = getDb ( ) ;
231+ return getRecentApps ( db ) ;
232+ } ) ;
189233 } ) ;
190234
191235export const fetchComparisonBySlug = createServerFn ( { method : "GET" } )
192236 . inputValidator ( ( input : { slug : string } ) => input )
193237 // eslint-disable-next-line @typescript-eslint/no-explicit-any
194238 . handler ( async ( { data } ) : Promise < any > => {
195- const db = getDb ( ) ;
196- return getComparisonBySlug ( db , data . slug ) ;
239+ return kvCached (
240+ cacheKey ( "getComparisonBySlug" , { slug : data . slug } ) ,
241+ ( ) => {
242+ const db = getDb ( ) ;
243+ return getComparisonBySlug ( db , data . slug ) ;
244+ } ,
245+ ) ;
197246 } ) ;
198247
199248export const fetchComparisonPairsForApp = createServerFn ( { method : "GET" } )
200249 . inputValidator ( ( input : { appId : string ; limit ?: number } ) => input )
201250 . handler ( async ( { data } ) => {
202- const db = getDb ( ) ;
203- return listComparisonPairsForApp ( db , data . appId , data . limit ) ;
251+ return kvCached ( cacheKey ( "listComparisonPairsForApp" , data ) , ( ) => {
252+ const db = getDb ( ) ;
253+ return listComparisonPairsForApp ( db , data . appId , data . limit ) ;
254+ } ) ;
204255 } ) ;
205256
206257export const trackDownload = createServerFn ( { method : "POST" } )
0 commit comments