You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use prepared statements in my repository class. Typically you would pass in the database object into the class and make calls there. I'm not sure whether the prepared statement should be outside of the class, or inside the class when using it in the context of aws lambas / server functions. Looking for another brain to share some thoughts...
Option 1 - Prepared statement outside the repository class
The prepared statement calls the _db instatiation every time. Is this less efficient than passing it once into the UserRepository class to use since it needs to reinstantiate every time (or is the db connection done only once for the life of the server function?), or the same as passing it in once into the UserRepository?
import{db}from"@/src/connect";constpreparedCreateUser=db.insert(users).values({}).prepare("createUser");classUserRepository{asynccreateUser(user: NewUser){awaitpreparedCreateUser.execute({ user });}}
Option 2 - Prepared statement inside the repository class
This seems redundant (I think) since the class gets instantiated on every server function call so using prepared statements here seems pointless?
import{dbas_db}from"@/src/connect";classUserRepository{privatedb: typeof_db;constructor(db: typeof_db){this.db=db;}asynccreateUser(user: NewUser){constpreparedCreateUser=this.db.insert(users).values({}).prepare("createUser");awaitpreparedCreateUser.execute({ user });}}
My own answer: Based on the docs for prepared statements, it says if I want to reuse my db connection and prepared statements, it just has to be declared outside the handler, so in this case, I think option 1 is the correct solution right?
Next logical step...
So then if option #1 is right, the next logical step seems to be creating a class of prepared statements and a repository class to call that prepared statement class. Does this still get the benefit of prepared statements, or is this overcomplicating it?
Should prepared statements be standalone declarations instead?
import{dbas_db}from"@/src/connect";classPreparedUserRepositoryStatements{// Should there be a constructor here?privatedb: typeof_db;constructor(db: typeof_db){this.db=db;}createUser=db.insert(users).value({}).prepare("createUser");// deleteUser = ... }classUserRepositoryextendsPreparedUserRepositoryStatements{constructor(db: typeof_db){super(db);}asynccreateUser(user: NewUser){awaitthis.createUser.execute({ user });}// async deleteUser }
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to use prepared statements in my repository class. Typically you would pass in the database object into the class and make calls there. I'm not sure whether the prepared statement should be outside of the class, or inside the class when using it in the context of aws lambas / server functions. Looking for another brain to share some thoughts...
Option 1 - Prepared statement outside the repository class
Option 2 - Prepared statement inside the repository class
My own answer: Based on the docs for prepared statements, it says if I want to reuse my db connection and prepared statements, it just has to be declared outside the handler, so in this case, I think option 1 is the correct solution right?
Next logical step...
So then if option #1 is right, the next logical step seems to be creating a class of prepared statements and a repository class to call that prepared statement class. Does this still get the benefit of prepared statements, or is this overcomplicating it?
Beta Was this translation helpful? Give feedback.
All reactions