RUNNb
RUNNb
Explore posts from servers
DTDrizzle Team
Created by RUNNb on 11/10/2024 in #help
Rollback not happening on tx.rollback() [PG DRIVER]
export async function createRestaurantMenuMutation({
label,
type,
availabilityDate,
categories,
}: CreateRestaurantMenuProps) {
let error: string | undefined;

try {
await db.transaction(async (tx) => {
const createMenu = await db
.insert(menus)
.values(restaurantMenuParse.data)
.returning()
.execute();

if (!createMenu[0]) {
error = "Não foi possível criar o menu.";
tx.rollback();
}

categories.forEach(async (category) => {
const categoryParse =
await createMenuCategorySchema.safeParseAsync({
...category,
});

if (!categoryParse.success) {
console.error(
"Error parsing category: ",
categoryParse.error,
);
error = "Existe um erro nos dados inseridos.";
tx.rollback();
}

const createCategory = await tx
.insert(menuCategories)
.values({
name: category.name,
menuId: createMenu[0]!.id,
})
.returning()
.execute();

if (!createCategory[0]) {
error = `Ocorreu um erro a criar a categoria ${category.name}. `;
tx.rollback();
}
});
return {
...createMenu[0],
categories,
};
});
} catch (e) {
console.log("Exception creating menu: ", { e });
throw new Error(error);
}
}
export async function createRestaurantMenuMutation({
label,
type,
availabilityDate,
categories,
}: CreateRestaurantMenuProps) {
let error: string | undefined;

try {
await db.transaction(async (tx) => {
const createMenu = await db
.insert(menus)
.values(restaurantMenuParse.data)
.returning()
.execute();

if (!createMenu[0]) {
error = "Não foi possível criar o menu.";
tx.rollback();
}

categories.forEach(async (category) => {
const categoryParse =
await createMenuCategorySchema.safeParseAsync({
...category,
});

if (!categoryParse.success) {
console.error(
"Error parsing category: ",
categoryParse.error,
);
error = "Existe um erro nos dados inseridos.";
tx.rollback();
}

const createCategory = await tx
.insert(menuCategories)
.values({
name: category.name,
menuId: createMenu[0]!.id,
})
.returning()
.execute();

if (!createCategory[0]) {
error = `Ocorreu um erro a criar a categoria ${category.name}. `;
tx.rollback();
}
});
return {
...createMenu[0],
categories,
};
});
} catch (e) {
console.log("Exception creating menu: ", { e });
throw new Error(error);
}
}
I've tried a couple different approachs (throwing errors etc, keeps the same issue)
7 replies