Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for batching queries #9

Closed
RobertCraigie opened this issue Feb 4, 2021 · 2 comments
Closed

Add support for batching queries #9

RobertCraigie opened this issue Feb 4, 2021 · 2 comments
Labels
kind/feature A request for a new feature.

Comments

@RobertCraigie
Copy link
Owner

RobertCraigie commented Feb 4, 2021

Problem

In some situations it is desirable that a write operation being committed is dependant on other write operations succeeding, for example, if you update 200 users within a transaction, each update must succeed - if not, all changes are rolled back and the transaction fails as a whole.

Suggested solution

We cannot support the same syntax that prisma does as we need to support a synchronous API as well

This is a good solution as it can easily be modified to support dependencies between write operations if it is added to the prisma query engine, #1844.

The only potential problem with this solution is that we are creating a new client reference which would potential be difficult for some users to implement but I cannot think of a way to do this while maintaining type safety.

async

async with client.batch_() as tx:
    tx.user.create({'name': 'Robert'})
    tx.user.create({'name': 'Bob'})

tx = client.batch_()
tx.user.create({'name': 'Robert'})
tx.user.create({'name': 'Bob'})
await tx.commit()

sync

with client.batch_() as tx:
    tx.user.create({'name': 'Robert'})
    tx.user.create({'name': 'Bob'})

tx = client.batch_()
tx.user.create({'name': 'Robert'})
tx.user.create({'name': 'Bob'})
tx.commit()

Additional context

Implementation notes

As we have to type every action to return None there will be some refactoring to support it, but it should be possible with generics and if not we can always duplicate the whole client/actions classes with Jinja

@RobertCraigie RobertCraigie added the kind/feature A request for a new feature. label Feb 4, 2021
@RobertCraigie RobertCraigie added this to the Public Release milestone Jul 6, 2021
@RobertCraigie RobertCraigie changed the title Add support for transactions Add support for batching queries Aug 26, 2021
@danielweil
Copy link

Can this be used to copy the behaviour seen in this part of the doc? https://www.prisma.io/docs/guides/performance-and-optimization/prisma-client-transactions-guide#interactive-transactions-in-preview

I need to create an object in the database, and if it gives me an error in the function executed next, the changes should be rollbacked...

@RobertCraigie
Copy link
Owner Author

RobertCraigie commented Dec 20, 2021

@danielweil ah that is a different feature, see #53.

Query batching is only used when you want to send a lot of queries at once without having access to the returned objects between calls, e.g. if you're moving data from a JSON file to a Prisma model.

I've updated the original comment examples to illustrate the actual implemented API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature.
Projects
None yet
Development

No branches or pull requests

2 participants