Jetrak
Jetrak
Explore posts from servers
DTDrizzle Team
Created by Jetrak on 11/18/2024 in #help
Nested select in an insert
Hi, is there a better way to use nested select inside an insert than to just use the sql function? For example I have this function:
static async upsert(idPerson: number, studyType: StudyType) {
const idStudyTypeSql = sql`(select id_study_type from study_type where study_type = ${studyType})`;
await db
.insert(student)
.values({
idPerson,
idStudyType: idStudyTypeSql,
})
.onConflictDoUpdate({
target: student.idPerson,
set: {
idStudyType: idStudyTypeSql,
},
});
}
static async upsert(idPerson: number, studyType: StudyType) {
const idStudyTypeSql = sql`(select id_study_type from study_type where study_type = ${studyType})`;
await db
.insert(student)
.values({
idPerson,
idStudyType: idStudyTypeSql,
})
.onConflictDoUpdate({
target: student.idPerson,
set: {
idStudyType: idStudyTypeSql,
},
});
}
It works well, but by using the sql I lose the benefit of typescript and I have to execute the query to verify that it works as expected. If I, for example, use non-existing column in the sql, I won't get any warnings. I could create a separate query to get the id_study_type, but I would prefer to have just one database call if possible.
const idStudyType = (
await db
.select({ id: studyType.idStudyType })
.from(studyType)
.where(eq(studyType.studyType, studyTypeEnum))
)[0]?.id;
const idStudyType = (
await db
.select({ id: studyType.idStudyType })
.from(studyType)
.where(eq(studyType.studyType, studyTypeEnum))
)[0]?.id;
1 replies
TTCTheo's Typesafe Cult
Created by Jetrak on 10/24/2024 in #questions
Is tRPC still recommended?
I'm new to next.js/t3 stack and I have a hard time deciding if I need/want to use tRPC for my app (nothing super complex, just several forms and few possibly large tables where paging would be probably needed). Now, as far as I understand it, tRPC used to be a big DX improvement before the introduction of server actions. But is it still needed today or are server actions sufficient replacement? The t3 stack websites still mentions that tRPC is great, but Theo does not use it in his introductory video (creating t3gallery) and doesn't really explain the decision (maybe I have just missed it). Thank you!
8 replies