Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to use DatabaseContext in node-postgres template #67

Open
lasseklovstad opened this issue Jan 22, 2025 · 0 comments
Open

Comments

@lasseklovstad
Copy link

lasseklovstad commented Jan 22, 2025

Hi! I am trying to use the node-postgres template and I am not familiar with AsyncLocalStorage api from nodejs. How should I use const db = database() in my app? I se two approches:

  1. Call the database context everywhere I want to use it?
  2. Call the database context at the start of a loader and pass it down to functions that need access to the database?
// Approach 1
function getUser(userId: string){
   return database().query.users.findFirst(...)
}

// Approach 2
function getUser(db: Database, userId: string){
   return db.query.users.findFirst(...)
}

What is the recommended approach? Is it bad to call DatabaseContext.getStore() to many times?

And one follow-up question:
I see in the epic-stack from Kent C. Dodds they don't use a DatabaseContext. The epic-stack uses https://github.com/epicweb-dev/remember library to not initilize the db every request. What is the difference between the two approches?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant