Create conditional cte insert query
Hello
Is it possible to create a condition insert query with conditional cte like this :
let query = db.with('new_group', (db) =>
db
.insertInto('group')
.values({
organization__id: orgId,
name: name,
avatar: avatarUrl,
visibility: visibility,
is_default: false,
})
.returningAll()
)
if (userId) {
query.with('new_user', (db) =>
db.insertInto('group_organization_user').values({
user__id: userId,
organization__id: orgId,
group__id: query.selectFrom('new_group').select('id'),
})
)
}
if (!!data.sports) {
query.with('sports', (query) =>
query.insertInto('tag_organization_group').values(
data.sports!.map((s) => ({
organization__id: orgId,
group__id: query.selectFrom('new_group').select('id'),
tag__id: s,
}))
)
)
}
const newGroup = await query.selectFrom('new_group').selectAll().executeTakeFirstOrThrow()
return newGroup
0 Replies