kgni
kgni
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
Well, I spoke with a guy that works at neon, and he made me aware that "The pool is initially created empty and will create new clients lazily as they are needed.", so all of this is not necessary 🙃 The only thing I need in my case is to have a middleware that closes/ends the pool after the response.
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
Is this implementation stupid?
const WebSocketsDb = t.middleware(async ({ next, ctx }) => {
const { dbWebSockets, pool } = createDBWebSocket(ctx.c.env.DATABASE_URL);

const res = await next({
ctx: {
...ctx,
db: dbWebSockets,
},
});

// still not sure which of these waitUntil to use since it is running in a worker
ctx.c.executionCtx.waitUntil(pool.end());
ctx.c.event.waitUntil(pool.end());

return res;
});

export const router = t.router;
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(isAuthenticated);
export const publicProcedureWebSockets = t.procedure.use(WebSocketsDb);
export const protectedProcedureWebSockets =
protectedProcedure.use(WebSocketsDb);
const WebSocketsDb = t.middleware(async ({ next, ctx }) => {
const { dbWebSockets, pool } = createDBWebSocket(ctx.c.env.DATABASE_URL);

const res = await next({
ctx: {
...ctx,
db: dbWebSockets,
},
});

// still not sure which of these waitUntil to use since it is running in a worker
ctx.c.executionCtx.waitUntil(pool.end());
ctx.c.event.waitUntil(pool.end());

return res;
});

export const router = t.router;
export const publicProcedure = t.procedure;
export const protectedProcedure = t.procedure.use(isAuthenticated);
export const publicProcedureWebSockets = t.procedure.use(WebSocketsDb);
export const protectedProcedureWebSockets =
protectedProcedure.use(WebSocketsDb);
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
Thanks for your inputs, much appreciated!
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
Yeah I think I'm going to do it in a middleware so I can automatically close the connection
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
With this implementation, wouldn't it always be open, which I don't want.. sorry for not being clear enough per neondb documentation it is advised to close the db connection after every response. So I want to open it when I need it (only in specific procedures) and close it after it is done
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
Ah I see, so you could technically split it up as follows: 1. Create the Websocket database instance in a middleware and then pass it to the context - instead of creating it directly in the context (you could also close the pool in the middleware then). 2. Recreate all of your procedures, so that you have 2 different types - 1 that utilizes HTTP and 1 for websockets, for example: - publicProcedure, protectedProcedure (both just have the HTTP database instance) - publicProcedureWebSocketets, protectedProcedureWebSockets. Would that be a way of doing it to avoid opening the websocket connection every single time the context is created? Also, would there be a smarter way of doing this, than to recreate all of the procedures with websockets (seems a bit too repetitive and redundant)
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
It might be a question more towards neondb and drizzle (or for pools in general), sorry if that is the case i'm still a newbie 😄
15 replies
TtRPC
Created by kgni on 5/6/2024 in #❓-help
tRPC context, NeonDB & WebSockets
I'm using the neon serverless driver to create a drizzle client which connects over websockets, I just don't know if this connection will open everytime the context is created. I don't want that to happen when/if I'm only querying the database over HTTP. I hope I'm making sense. Essentially I have: 2 different db clients created in context. As far as I know context is created every time a procedure runs. So if I create a client with a pool like this:
export const createDBWebSocket = (DATABASE_URL: string) => {
const pool = new Pool({ connectionString: DATABASE_URL });
return drizzleServerless(pool, {
schema,
logger: true,
});
};
export const createDBWebSocket = (DATABASE_URL: string) => {
const pool = new Pool({ connectionString: DATABASE_URL });
return drizzleServerless(pool, {
schema,
logger: true,
});
};
Then the connection would technically open every time the context is created right? Or is it only when I'm actually using the client? I want the connection to only open when it is used and not when context is created.
15 replies