afroitz
afroitz
DTDrizzle Team
Created by afroitz on 8/13/2024 in #help
Are (async) transactions supposed to work in expo-sqlite?
I'm using drizzle with expo-sqlite in an expo/react-native project. The database works as expected except for transactions. As shown below, when an error happens inside a transaction the transaction is not rolled back and the error is not caught in any way. With manual tx.rollback(), I get [DrizzleError: Rollback] as expected, but it also is not caught and no rollback happens. Am I doing something wrong or are transactions even supposed to work in expo-sqlite? I couldn't find anything explicit in the docs about this. There's also this open issue (https://github.com/drizzle-team/drizzle-orm/issues/1723) where expo-sqlite was also mentioned at some point.
import { drizzle } from "drizzle-orm/expo-sqlite";
import { openDatabaseSync } from "expo-sqlite/next";
import { dbOrders } from "./schema";
import { eq } from "drizzle-orm";

const expo = openDatabaseSync("db.db");
const db = drizzle(expo);

try {
// Create transaction
await db.transaction(async (tx) => {
// Insert test order
await tx
.insert(dbOrders)
.values([{ id: "test", value: { test: "test" } }]);

throw new Error("Should cause rollback");
});
} catch (e) {
console.log("Should have been caught, causing rollback.");
console.log(e);
}

// Should not exist
const testOrder = await db
.select()
.from(dbOrders)
.where(eq(dbOrders.id, "test"));
import { drizzle } from "drizzle-orm/expo-sqlite";
import { openDatabaseSync } from "expo-sqlite/next";
import { dbOrders } from "./schema";
import { eq } from "drizzle-orm";

const expo = openDatabaseSync("db.db");
const db = drizzle(expo);

try {
// Create transaction
await db.transaction(async (tx) => {
// Insert test order
await tx
.insert(dbOrders)
.values([{ id: "test", value: { test: "test" } }]);

throw new Error("Should cause rollback");
});
} catch (e) {
console.log("Should have been caught, causing rollback.");
console.log(e);
}

// Should not exist
const testOrder = await db
.select()
.from(dbOrders)
.where(eq(dbOrders.id, "test"));
1 replies