Can someone help me understand these 2 examples using pool and client?

I'm reading through the docs and see that in the 2 Neon examples, they use a pool and client. From what I understand, pools support multiple requests and a client doesn't. The 2 questions I have are: - When would I want to close the pool or client? I would assume I would want to keep it open in production so that I wouldn't need to open the connection again. - Why would I use a client? Here are the examples I'm looking at in the docs: The docs show this example showing to close a pool (https://orm.drizzle.team/docs/installation-and-db-connection/postgresql/neon):
const pool = new Pool({ connectionString: env.DATABASE_URL });
const db = drizzle(pool)
const result = await db.select().from(...);
ctx.waitUntil(pool.end());
const pool = new Pool({ connectionString: env.DATABASE_URL });
const db = drizzle(pool)
const result = await db.select().from(...);
ctx.waitUntil(pool.end());
And the example provided uses a client https://github.com/drizzle-team/drizzle-orm/blob/main/examples/neon-cloudflare/src/index.ts
async function injectDB(request: Request, env: Env) {
request.client = new Client(env.DATABASE_URL);
request.db = drizzle(request.client);
}

router.get('/users', injectDB, async (req: Request, env: Env, ctx: ExecutionContext) => {
req.client.connect();
const result = await req.db.select().from(users);
ctx.waitUntil(req.client.end());
return json({ status: 'ok', result });
});
async function injectDB(request: Request, env: Env) {
request.client = new Client(env.DATABASE_URL);
request.db = drizzle(request.client);
}

router.get('/users', injectDB, async (req: Request, env: Env, ctx: ExecutionContext) => {
req.client.connect();
const result = await req.db.select().from(users);
ctx.waitUntil(req.client.end());
return json({ status: 'ok', result });
});
GitHub
drizzle-orm/index.ts at main · drizzle-team/drizzle-orm
TypeScript ORM that feels like writing SQL. Contribute to drizzle-team/drizzle-orm development by creating an account on GitHub.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server