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

Feature: Transaction Support for SDK #386

Open
2 tasks done
reinhard-sanz opened this issue Dec 3, 2024 · 3 comments
Open
2 tasks done

Feature: Transaction Support for SDK #386

reinhard-sanz opened this issue Dec 3, 2024 · 3 comments

Comments

@reinhard-sanz
Copy link

reinhard-sanz commented Dec 3, 2024

Is your feature request related to a problem?

It would be awesome to have transaction support for the js sdk. It would make error handling in js way more easier and would require less custom handling on errors.
A good usecase would be for creating 2 table entries with db.create and relating them using db.relate, if any of these operation fail, i want all of the changes to not be applied and the db to be in the same state as before.

Describe the solution

Maybe something similar to how sequelize does it:

// option to create a transaction
const transaction = await db.transaction();
try {
   // possibility to add the transaction to db methods
   await db.create(user, {name: 'testUser'}, { transaction });
   
   // supported for every available db method (create, query, insert, relate, etc)
   await db.query('UPDATE ...', { transaction })

   // try to apply the transaction
   await transaction.commit();
} catch {
   // on error just rollback
   await transaction.rollback();
}

Alternative methods

IIRC the only possible way to do transactions from js is using it directly inside db.query like this

db.query(`
  BEGIN TRANSACTION;

  CREATE account:one SET balance = 135605.16;
  CREATE account:two SET balance = 91031.31;
  UPDATE account:one SET balance += 300.00;
  UPDATE account:two SET balance -= 300.00;

  COMMIT TRANSACTION;
`);

While this works in many cases it would still be awesome do be able to it with the actualy sdk methods.

SurrealDB version

surreal 2.1.1 for linux on x86_64

JavaScript SDK version

1.1.0

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@LucyEgan
Copy link

LucyEgan commented Dec 3, 2024

+1 for proper support for this.

So far the ive only recreated firestores client side transactions via read read read write write write, to build a single RPC transaction that aborts of the doc versions have changed since read.

But it would be so nice to have read write read write read write

@kearfy
Copy link
Member

kearfy commented Dec 3, 2024

Duplicate of #139

@kearfy kearfy marked this as a duplicate of #139 Dec 3, 2024
@reinhard-sanz
Copy link
Author

Duplicate of #139

@kearfy Sorry about the duplicate, apparently i had a typo while searching thourgh existing issues and the one above didn't show up 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants