DT
Drizzle Team•13mo ago
Amur

Type of `tx` when using `db.transactions`

I want to use helper functions within db.transactions but I can't get the type of tx working so basically i don't know how to type tx in the updateUserNotifications function
const updateUserNotifications = async (
userId: UpdateUserAttributes["id"],
notifications: Required<UpdateUserAttributes>["notifications"],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tx: any, // TODO: what's the type of this
) => {
// check if user has a notification settings record
const userNotificationSettings = await tx.select().from(userNotifications).where(eq(users.id, userId));

// create user notification settings record if it doesn't exist
if (!userNotificationSettings.length) {
await tx.insert(userNotifications).values({
userId,
favorites: notifications.favorites,
});
}

await tx.update(userNotifications).set({ favorites: notifications.favorites }).where(eq(users.id, userId));
};

export const updateUser = async (userAttributes: UpdateUserAttributes) => {
await db.transaction(async (tx) => {
// update user notification settings
if (userAttributes.notifications) {
await updateUserNotifications(userAttributes.id, userAttributes.notifications, tx);
}
});
};
const updateUserNotifications = async (
userId: UpdateUserAttributes["id"],
notifications: Required<UpdateUserAttributes>["notifications"],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tx: any, // TODO: what's the type of this
) => {
// check if user has a notification settings record
const userNotificationSettings = await tx.select().from(userNotifications).where(eq(users.id, userId));

// create user notification settings record if it doesn't exist
if (!userNotificationSettings.length) {
await tx.insert(userNotifications).values({
userId,
favorites: notifications.favorites,
});
}

await tx.update(userNotifications).set({ favorites: notifications.favorites }).where(eq(users.id, userId));
};

export const updateUser = async (userAttributes: UpdateUserAttributes) => {
await db.transaction(async (tx) => {
// update user notification settings
if (userAttributes.notifications) {
await updateUserNotifications(userAttributes.id, userAttributes.notifications, tx);
}
});
};
7 Replies
Angelelz
Angelelz•13mo ago
Did you try
type Tx = typeof db;
type Tx = typeof db;
The transaction class extends from the Database class What driver are you using? You might just lose tx.rollback maybe? Only at the type level, you'll have at runtime I'll a couple tests later
Amur
Amur•13mo ago
postgres-js yeah this seems to work, i was overcomplicated it 😅 indeed there is no rollback but it's fine for now, i dont do manual rollbacks
Angelelz
Angelelz•13mo ago
Try this:
type Tx = typeof db & { rollback: () => void }
type Tx = typeof db & { rollback: () => void }
You can also just throw inside and it will roll back
Amur
Amur•13mo ago
yeah that works i only want to rollback now if my query throws so that's fine thanks for the help!
DiamondDragon
DiamondDragon•13mo ago
You think I should refer to @amur0501 ‘s example for my question here ? https://discord.com/channels/1043890932593987624/1148843024881827890
Angelelz
Angelelz•13mo ago
I don't know what example you're referring to But if it helps, you def refer to it
DiamondDragon
DiamondDragon•13mo ago
ah does neon not support transactions? No transactions support in neon-http driver
Want results from more Discord servers?
Add your server