santi
santi
Explore posts from servers
PPrisma
Created by santi on 7/18/2024 in #help-and-questions
unexpected message from server
Bump help pls
3 replies
TtRPC
Created by santi on 4/12/2024 in #❓-help
Is it possible to get the procedure name / id in middleware?
https://github.com/trpc/trpc/issues/4066 <-- seems related to this feature request
8 replies
TtRPC
Created by santi on 4/12/2024 in #❓-help
Is it possible to get the procedure name / id in middleware?
import isNil from 'lodash.isnil'
import { RedisService } from '@centrito/api/nest/data/redis.service'
import { t } from '@centrito/api/trpc/server'

const DEFAULT_CACHE_TTL = 60 * 60 * 1 // 1 hour

export const withCaching = t.middleware(async ({ ctx, next, meta, input }) => {
const routeCacheKey = meta?.routeCacheKey
if (routeCacheKey) {
const redis = ctx.app.get(RedisService)
const cacheKey = JSON.stringify({ env: 'dev2', route: routeCacheKey, input })
const cachedData = await redis.get(cacheKey)
if (!isNil(cachedData)) {
return {
data: cachedData,
}
} else {
const result = await next({ ctx })
if (result.ok) {
// No await here, it slows down the request / the response is not needed
redis.setex(cacheKey, DEFAULT_CACHE_TTL, result.data)
}
return result
}
} else {
console.log('Caching skipped as routeCacheKey is not defined')
return next({ ctx })
}
})
import isNil from 'lodash.isnil'
import { RedisService } from '@centrito/api/nest/data/redis.service'
import { t } from '@centrito/api/trpc/server'

const DEFAULT_CACHE_TTL = 60 * 60 * 1 // 1 hour

export const withCaching = t.middleware(async ({ ctx, next, meta, input }) => {
const routeCacheKey = meta?.routeCacheKey
if (routeCacheKey) {
const redis = ctx.app.get(RedisService)
const cacheKey = JSON.stringify({ env: 'dev2', route: routeCacheKey, input })
const cachedData = await redis.get(cacheKey)
if (!isNil(cachedData)) {
return {
data: cachedData,
}
} else {
const result = await next({ ctx })
if (result.ok) {
// No await here, it slows down the request / the response is not needed
redis.setex(cacheKey, DEFAULT_CACHE_TTL, result.data)
}
return result
}
} else {
console.log('Caching skipped as routeCacheKey is not defined')
return next({ ctx })
}
})
@Alex / KATT 🐱, thanks for taking the time. I'm struggling to set up this cache middleware What should the return be when the results ARE cached? Right now I wrote the following:
return {
data: cachedData,
}
return {
data: cachedData,
}
But this is surely wrong
8 replies
TtRPC
Created by santi on 4/12/2024 in #❓-help
Is it possible to get the procedure name / id in middleware?
export default publicProcedure
.use(cacheMiddleware)
.input(GetProductsQuerySchema)
.query(async ({ input, ctx }): Promise<GetProductsQueryResultsLegacy> => {
console.log({ path: __filename })
const results = await ctx.app.get(ProductService).getProducts(input)
return {
...results,
products: results.products.map((p) => p.toLegacy()),
}
})
export default publicProcedure
.use(cacheMiddleware)
.input(GetProductsQuerySchema)
.query(async ({ input, ctx }): Promise<GetProductsQueryResultsLegacy> => {
console.log({ path: __filename })
const results = await ctx.app.get(ProductService).getProducts(input)
return {
...results,
products: results.products.map((p) => p.toLegacy()),
}
})
Trying this to get a filename/routename. I wonder if there's a better way
8 replies
TtRPC
Created by santi on 4/12/2024 in #❓-help
Is it possible to get the procedure name / id in middleware?
No description
8 replies
TtRPC
Created by swammer on 4/4/2024 in #❓-help
Lambda container reuse
Any hint in terms of your workaround?
4 replies
TtRPC
Created by swammer on 4/4/2024 in #❓-help
Lambda container reuse
Also facing the same issue. Did you figure this out @swammer ?
4 replies
TtRPC
Created by santi on 4/8/2024 in #❓-help
Create client that is used in every request, without re-creating client
It'd be ideal here
export const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
},
})
export const t = initTRPC.context<Context>().create({
transformer: superjson,
errorFormatter({ shape }) {
return shape
},
})
6 replies
TtRPC
Created by santi on 4/8/2024 in #❓-help
Create client that is used in every request, without re-creating client
To where though?
6 replies