11import { websitesApi } from '@databuddy/auth' ;
22import { and , chQuery , eq , isNull , websites } from '@databuddy/db' ;
3+ import { createDrizzleCache , redis } from '@databuddy/redis' ;
34import { logger } from '@databuddy/shared' ;
45import {
56 createWebsiteSchema ,
@@ -17,29 +18,47 @@ import {
1718 trackWebsiteUsage ,
1819} from '../utils/billing' ;
1920
21+ const drizzleCache = createDrizzleCache ( { redis, namespace : 'websites' } ) ;
22+
23+ const CACHE_TTL = 60 ;
24+
2025const buildDomain = ( domain : string , subdomain ?: string ) =>
2126 subdomain ? `${ subdomain } .${ domain } ` : domain ;
2227
2328export const websitesRouter = createTRPCRouter ( {
2429 list : protectedProcedure
2530 . input ( z . object ( { organizationId : z . string ( ) . optional ( ) } ) . default ( { } ) )
2631 . query ( ( { ctx, input } ) => {
27- const where = input . organizationId
28- ? eq ( websites . organizationId , input . organizationId )
29- : and (
30- eq ( websites . userId , ctx . user . id ) ,
31- isNull ( websites . organizationId )
32- ) ;
33- return ctx . db . query . websites . findMany ( {
34- where,
35- orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
32+ const cacheKey = `list:${ ctx . user . id } :${ input . organizationId || '' } ` ;
33+ return drizzleCache . withCache ( {
34+ key : cacheKey ,
35+ ttl : CACHE_TTL ,
36+ tables : [ 'websites' ] ,
37+ queryFn : ( ) => {
38+ const where = input . organizationId
39+ ? eq ( websites . organizationId , input . organizationId )
40+ : and (
41+ eq ( websites . userId , ctx . user . id ) ,
42+ isNull ( websites . organizationId )
43+ ) ;
44+ return ctx . db . query . websites . findMany ( {
45+ where,
46+ orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
47+ } ) ;
48+ } ,
3649 } ) ;
3750 } ) ,
3851
3952 getById : publicProcedure
4053 . input ( z . object ( { id : z . string ( ) } ) )
4154 . query ( ( { ctx, input } ) => {
42- return authorizeWebsiteAccess ( ctx , input . id , 'read' ) ;
55+ const cacheKey = `getById:${ input . id } ` ;
56+ return drizzleCache . withCache ( {
57+ key : cacheKey ,
58+ ttl : CACHE_TTL ,
59+ tables : [ 'websites' ] ,
60+ queryFn : ( ) => authorizeWebsiteAccess ( ctx , input . id , 'read' ) ,
61+ } ) ;
4362 } ) ,
4463
4564 create : protectedProcedure
@@ -114,6 +133,8 @@ export const websitesRouter = createTRPCRouter({
114133 }
115134 ) ;
116135
136+ await drizzleCache . invalidateByTables ( [ 'websites' ] ) ;
137+
117138 return website ;
118139 } ) ,
119140
@@ -143,6 +164,11 @@ export const websitesRouter = createTRPCRouter({
143164 }
144165 ) ;
145166
167+ await Promise . all ( [
168+ drizzleCache . invalidateByTables ( [ 'websites' ] ) ,
169+ drizzleCache . invalidateByKey ( `getById:${ input . id } ` ) ,
170+ ] ) ;
171+
146172 return updatedWebsite ;
147173 } ) ,
148174
@@ -171,6 +197,11 @@ export const websitesRouter = createTRPCRouter({
171197 }
172198 ) ;
173199
200+ await Promise . all ( [
201+ drizzleCache . invalidateByTables ( [ 'websites' ] ) ,
202+ drizzleCache . invalidateByKey ( `getById:${ input . id } ` ) ,
203+ ] ) ;
204+
174205 return { success : true } ;
175206 } ) ,
176207
@@ -215,6 +246,11 @@ export const websitesRouter = createTRPCRouter({
215246 }
216247 ) ;
217248
249+ await Promise . all ( [
250+ drizzleCache . invalidateByTables ( [ 'websites' ] ) ,
251+ drizzleCache . invalidateByKey ( `getById:${ input . websiteId } ` ) ,
252+ ] ) ;
253+
218254 return updatedWebsite ;
219255 } ) ,
220256
0 commit comments