Jetrak
Jetrak
Explore posts from servers
DTDrizzle Team
Created by Jetrak on 12/15/2024 in #help
getTableColumns() for `with`?
Hi, is there any alternative to getTableColumns() for with CTEs? If I use the example from the docs:
const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
const result = await db.with(sq).select().from(sq);
const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
const result = await db.with(sq).select().from(sq);
How would I get the columns of sq? Calling getTableColumns(sq) does not work. It looks like sq._.selectedFields works as an alternative, but is that the right way?
4 replies
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