Git Product home page Git Product logo

Comments (8)

emmanuelchucks avatar emmanuelchucks commented on August 17, 2024 13

Use the Batch API to achieve 'transactions' in Cloudflare D1

const batchResponse: BatchResponse = await db.batch([
  db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ id: usersTable.id }),
  db.update(usersTable).set({ name: 'Dan' }).where(eq(usersTable.id, 1)),
  db.query.usersTable.findMany({}),
  db.select().from(usersTable).where(eq(usersTable.id, 1)),
  db.select({ id: usersTable.id, invitedBy: usersTable.invitedBy }).from(usersTable),
]);

from drizzle-orm.

duducpp avatar duducpp commented on August 17, 2024 9

@emmanuelchucks unfortunately, this workaround will not work in some scenarios like the following:

const [createdUser] = await global.db
.insert(users)
.values({
    name: account.name
})
.returning({
    id: users.id
});

// It may fail if the account already exists, 
// in this case I want to rollback the transaction, removing the previously inserted user
const [createdAccount] = await global.db
.insert(accounts)
.values({
    userId: createdUser.id, // I need to know the id returned from the previous query
    provider: account.provider,
    providerAccountId: account.providerAccountId,
})
.returning();

from drizzle-orm.

dcalsky avatar dcalsky commented on August 17, 2024 1

@emmanuelchucks unfortunately, this workaround will not work in some scenarios like the following:

const [createdUser] = await global.db
.insert(users)
.values({
    name: account.name
})
.returning({
    id: users.id
});

// It may fail if the account already exists, 
// in this case I want to rollback the transaction, removing the previously inserted user
const [createdAccount] = await global.db
.insert(accounts)
.values({
    userId: createdUser.id, // I need to know the id returned from the previous query
    provider: account.provider,
    providerAccountId: account.providerAccountId,
})
.returning();

@duducpp It's the drawback of Cloudflare D1 rather than drizzle. Even if you switch to prisma, this transaction feature is still unsupported. They (cloudflare team) have a workaround may help you: https://blog.cloudflare.com/whats-new-with-d1

from drizzle-orm.

Deniffer avatar Deniffer commented on August 17, 2024

facing same error how could i fix these?

from drizzle-orm.

sameerdewan avatar sameerdewan commented on August 17, 2024

@emmanuelchucks

This is a curious situation where batch is not a true workaround, as one action against the database in a batch "tx" may have return data needed for the following action in the batch call.

from drizzle-orm.

duducpp avatar duducpp commented on August 17, 2024

@emmanuelchucks unfortunately, this workaround will not work in some scenarios like the following:

const [createdUser] = await global.db
.insert(users)
.values({
    name: account.name
})
.returning({
    id: users.id
});

// It may fail if the account already exists, 
// in this case I want to rollback the transaction, removing the previously inserted user
const [createdAccount] = await global.db
.insert(accounts)
.values({
    userId: createdUser.id, // I need to know the id returned from the previous query
    provider: account.provider,
    providerAccountId: account.providerAccountId,
})
.returning();

@duducpp It's the drawback of Cloudflare D1 rather than drizzle. Even if you switch to prisma, this transaction feature is still unsupported. They (cloudflare team) have a workaround may help you: https://blog.cloudflare.com/whats-new-with-d1

This is the only workaround, thanks for the tip.

from drizzle-orm.

sameerdewan avatar sameerdewan commented on August 17, 2024

@duducpp I did see this last night as well: https://blog.cloudflare.com/whats-new-with-d1

Are you aware if we can instead of workers-qb, use drizzle in this pattern? I reached out to the authors as well.

from drizzle-orm.

emmanuelchucks avatar emmanuelchucks commented on August 17, 2024

Cloudflare never shipped the stored procedure feature mentioned in that blog post. It was just something they were looking into at the time. See here: https://community.cloudflare.com/t/d1-stored-procedures-can-not-use/649207

from drizzle-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.