fix(api): use synchronous transaction for wishlist reorder
better-sqlite3 doesn't support async transactions. Removed async/await and used synchronous .run() and .all() methods instead. Fixes #38
This commit is contained in:
@@ -101,24 +101,24 @@ export async function POST(
|
|||||||
allWishlists.splice(newSortOrder, 0, movingWishlist);
|
allWishlists.splice(newSortOrder, 0, movingWishlist);
|
||||||
|
|
||||||
// Update all sortOrders in a transaction for atomicity
|
// Update all sortOrders in a transaction for atomicity
|
||||||
const updatedWishlist = await db.transaction(async (tx) => {
|
// Note: better-sqlite3 is synchronous, so no async/await in transaction
|
||||||
// Update all sortOrders
|
const updatedWishlist = db.transaction((tx) => {
|
||||||
for (let i = 0; i < allWishlists.length; i++) {
|
for (let i = 0; i < allWishlists.length; i++) {
|
||||||
await tx
|
tx.update(wishlists)
|
||||||
.update(wishlists)
|
|
||||||
.set({
|
.set({
|
||||||
sortOrder: i,
|
sortOrder: i,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
.where(eq(wishlists.id, allWishlists[i].id));
|
.where(eq(wishlists.id, allWishlists[i].id))
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the updated wishlist
|
const result = tx
|
||||||
const result = await tx
|
|
||||||
.select()
|
.select()
|
||||||
.from(wishlists)
|
.from(wishlists)
|
||||||
.where(eq(wishlists.id, id))
|
.where(eq(wishlists.id, id))
|
||||||
.limit(1);
|
.limit(1)
|
||||||
|
.all();
|
||||||
|
|
||||||
return result[0];
|
return result[0];
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user