oke
oke
DTDrizzle Team
Created by oke on 2/18/2024 in #help
Update with join with Drizzle
I am using Postgres. Table Author has a 1 to 1 relations with table User. Author table has field userId that links to the primary key in the User table. Is there a way to write ONE update query to set the penName field in the Author table, but return all fields from both the Author and the User tables. Right now I need to write two queries
// Version 1
await db.update(author).set(request.body).where(eq(judge.id, request.params.id))

const updatedAuthor = await db.query.author.findFirst({
where: eq(author.id, request.params.id),
with: { user: true },
})
return updatedAuthor

// Version 2: Tried using .returning() but it also needs 2 queries
const [updated] = await db.update(author).set(request.body).where(eq(judge.id, request.params.id)).returning()

const toReturn = await db.query.author.findFirst({
where: eq(author.id, updated.id),
with: { user: true },
})
return toReturn
// Version 1
await db.update(author).set(request.body).where(eq(judge.id, request.params.id))

const updatedAuthor = await db.query.author.findFirst({
where: eq(author.id, request.params.id),
with: { user: true },
})
return updatedAuthor

// Version 2: Tried using .returning() but it also needs 2 queries
const [updated] = await db.update(author).set(request.body).where(eq(judge.id, request.params.id)).returning()

const toReturn = await db.query.author.findFirst({
where: eq(author.id, updated.id),
with: { user: true },
})
return toReturn
Or is two queries the standard way?
1 replies
DTDrizzle Team
Created by oke on 12/20/2023 in #help
How do detect if a Transaction failure occurs?
What is the return value of a transaction when it fails mid way, or does it throw an error? Example:
const authorOrNull = await db.transaction(async (tx) => {
const [u] = tx.insert(user).values(val).onConflictDoNothing().returning()

if u:
const [a] = tx.insert(author).values({ uId: u.id }).returning()
return a ?? null
})
const authorOrNull = await db.transaction(async (tx) => {
const [u] = tx.insert(user).values(val).onConflictDoNothing().returning()

if u:
const [a] = tx.insert(author).values({ uId: u.id }).returning()
return a ?? null
})
How will I know when a transaction failure or a rollback has occurred, for example in case of random I/O failure, or resource exhaustion, or when the database connection is down half-way through the transaction
1 replies
DTDrizzle Team
Created by oke on 12/18/2023 in #help
Drizzle JOIN vs relations
Seems like both Join and relations are equally capable when query data that have relationships. I'd assume that if one is already using Drizzle relations (with with field), then one wouldn't need to use .join(), and vice versa. Is there a reason one should use JOIN vs. relations, or is it personal preference?
3 replies
DTDrizzle Team
Created by oke on 12/17/2023 in #help
How do I infer type of pgEnum
How can I infer the type of a pgEnum?
export const roleEnum = pgEnum('role', ['A', 'B', 'C', 'D', 'E'])

// Expected to somehow have type Role = 'A' | 'B' | 'C' | 'D' | 'E'
type Role = typeof roleEnum.$inferMagic
export const roleEnum = pgEnum('role', ['A', 'B', 'C', 'D', 'E'])

// Expected to somehow have type Role = 'A' | 'B' | 'C' | 'D' | 'E'
type Role = typeof roleEnum.$inferMagic
2 replies