Where do you guys put the Back-End logic?

More specifically, how do you organise it? It is just a bunch of functions inside functions.
37 Replies
Neto
Neto•17mo ago
with trpc you can just call the function in the backend but in general, you can split the routing logic to the business log
Neto
Neto•17mo ago
Domain Driven Design | SST
So we are ready to start working on our app. We'll be adding a simple comments feature to our Reddit clone. There are many ways of implementing this feature but with SST we want you to adopt a setup that'll scale as your app grows.
Sawaid
SawaidOP•17mo ago
And how will I do that in t3 for example?
Neto
Neto•17mo ago
just a example create a folder called "functions" or "core" or whatever from trpc, you call those functions if they require drizzle or whatever, just pass from the trpc context
Sawaid
SawaidOP•17mo ago
Okay And where do I store Models?
Neto
Neto•17mo ago
depends on what you are using be prisma, drizzle or whatever but you can just put everything on the trpc router will work just fine
Sawaid
SawaidOP•17mo ago
@zion_lg Here.
Zion
Zion•17mo ago
anyways just include it like, you have a "where" you can add an "include" and include the todos
Sawaid
SawaidOP•17mo ago
Like this?
No description
Sawaid
SawaidOP•17mo ago
Then?
Zion
Zion•17mo ago
should work now anything that is another model need to be included, because prisma needs to perform seperate read requests to get these
Sawaid
SawaidOP•17mo ago
These are all the options I get.
No description
Zion
Zion•17mo ago
I think you need to await it first
Sawaid
SawaidOP•17mo ago
do I have to await prisma calls as well?
Sawaid
SawaidOP•17mo ago
Got it.
No description
Sawaid
SawaidOP•17mo ago
I was confused why are we not awaiting db calls. xD
Zion
Zion•17mo ago
yeah, tho if you dont need to user (or just need something like the user id) its better to use session context and query the todo's
Sawaid
SawaidOP•17mo ago
btw, can I calls to the db now using user.todos? It is my first day trying to use t3 xD
Zion
Zion•17mo ago
which I just noticed you use input for it you can use ctx.session.user.id
Sawaid
SawaidOP•17mo ago
What if the user is not logged in?
Zion
Zion•17mo ago
use protectedProcedure you dont have todo's if there is no user also using input like this is a privacy issue, since you allow for every user to get the todos of every user they want I can be user x, and i can put in the input user y, and get his tasks
Sawaid
SawaidOP•17mo ago
I know, I was just checking it out. Still learning trpc.
Zion
Zion•17mo ago
ah okay
Sawaid
SawaidOP•17mo ago
Is this possible?
Zion
Zion•17mo ago
Not sure I understand it
Sawaid
SawaidOP•17mo ago
Suppose a Todo has children todos, like a tree structure. How will I get access to its children? I want to get the whole tree. Is that possible?
Zion
Zion•17mo ago
to my knowledge you'd have to do an include for each layer
include: {
todos: {
todos: {
todos: true
}
}
}
include: {
todos: {
todos: {
todos: true
}
}
}
the last layer will have the true which is not a very good way you can just use a query to get all of his todos, and have a reference to the child todo, than format it if you want to have multiple children you can maybe have a refrence to the parent instead
Sawaid
SawaidOP•17mo ago
That kind of ugly tbh. Can I write my own SQL and have it set all the relations correctly?
Zion
Zion•17mo ago
you probably have to replace prisma for this
Sawaid
SawaidOP•17mo ago
Btw, how expensive is the call from db?
Zion
Zion•17mo ago
GitHub
Tree structures support · Issue #4562 · prisma/prisma
Problem Hello! There is no real functionality for working with tree structures recursively. It would be awesome if there was a functionality added to define a model & query the most popular hie...
Zion
Zion•17mo ago
prisma is pretty expensive regarding reads tbf, but if you use something like planetscale its probably not an issue maybe theres a workaround in the comments since this is still an open issue
Sawaid
SawaidOP•17mo ago
It is still open 🥲
Zion
Zion•17mo ago
yep, there are some very annoying missing features that their issues were opened for years and still are
Sawaid
SawaidOP•17mo ago
guess I will go back to ruby on rails. xD How fast can I go from t3-app to fully PWA, desktop and mobile app? Is there a way to put the app logic in the same location and use it both on the server and on client as well?
Zion
Zion•17mo ago
you can import whatever you want to both, you can make a utils folder or something and import the functions from there to both server and client
Sawaid
SawaidOP•17mo ago
Nice. I will just call raw sql with prisma and create my object there on the fly.
Want results from more Discord servers?
Add your server