shandy
shandy
Explore posts from servers
DTDrizzle Team
Created by shandy on 11/5/2024 in #help
Using drizzle postgres with trpc in Next.js app
I'm getting the following error with the below code: Module not found: Can't resolve 'dns'. For some reason, my Next.js app is trying to load drizzle on the client where it doesn't have access to Node modules. Has anyone ever run into this before?
import { publicProcedure, router } from "../trpc";
import { z } from "zod";

import { drizzle } from "drizzle-orm/node-postgres";

// If I comment out this function, the error goes away.
export const createDbClient = () => {
return drizzle(process.env.DB_URL!);
};

const formSchema = z.object({
name: z.string().min(1, "Must provide a name."),
email: z.string().email(),
});

export const formRouter = router({
test: publicProcedure.query(() => {
return `test`;
}),
submit: publicProcedure.input(formSchema).mutation(({ input }) => {
// const client = createDbClient();

return `received form values`;
}),
});

export type FormRouter = typeof formRouter;
import { publicProcedure, router } from "../trpc";
import { z } from "zod";

import { drizzle } from "drizzle-orm/node-postgres";

// If I comment out this function, the error goes away.
export const createDbClient = () => {
return drizzle(process.env.DB_URL!);
};

const formSchema = z.object({
name: z.string().min(1, "Must provide a name."),
email: z.string().email(),
});

export const formRouter = router({
test: publicProcedure.query(() => {
return `test`;
}),
submit: publicProcedure.input(formSchema).mutation(({ input }) => {
// const client = createDbClient();

return `received form values`;
}),
});

export type FormRouter = typeof formRouter;
4 replies
TtRPC
Created by shandy on 10/6/2023 in #❓-help
Does `fetchRequestHandler()` automatically opt out of /app Route Handler caching?
I noticed that Next.js always skips cache for my TRPC API Route Handlers even though the request URLs are identical and the cache/revalidate config calls for persistent caching. I feel confident it has nothing to do with the fetch() calls within the Route Handlers as the same fetch() calls are happening separately within SSR via createCaller() and are being cached within Next's Data Cache as expected. However, I just read this from Route Handler docs:
You can opt out of caching by: Using the Request object with the GET method.
And I see that fetchRequestHandler() takes in the Request as a param:
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";

import { appRouter } from "@/server/routers/_app";
import { createContext } from "@/server/trpc";

const handler = (req: Request) =>
fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext,
});

export { handler as GET, handler as POST };
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";

import { appRouter } from "@/server/routers/_app";
import { createContext } from "@/server/trpc";

const handler = (req: Request) =>
fetchRequestHandler({
endpoint: "/api/trpc",
req,
router: appRouter,
createContext,
});

export { handler as GET, handler as POST };
Does fetchRequestHandler() automatically opt out of /app Route Handler caching? And if so, is there a way to enable caching? Thanks in advance <:Sadge_pray:860473532700819506>
2 replies