kriszyp / lmdb-js

Simple, efficient, ultra-fast, scalable data store wrapper for LMDB
Other
520 stars 44 forks source link

transaction without callback #280

Closed mikeTWC1984 closed 8 months ago

mikeTWC1984 commented 8 months ago

Is there a way to use transaction without callback. E.g. just begin transaction, put values as needed and commit. Same way as node-lmdb:

var txn = env.beginTxn();
var value = txn.getString(dbi, 1);
txn.putString(dbi, 2, "Yes, it's this simple!");
txn.commit();
kriszyp commented 8 months ago

If you are simply wanting to avoid asynchronicity in the callback, you can do:

db.transactionSync(() => {
  var value = db.get(1);
  db.put(2, "Yes, it's this simple!");
});

If you are wanting to ensure the transaction can span call scopes, you could do:

let commit;
db.transactionSync(() => new Promise(resolve => commit = resolve));
var value = db.get(1);
db.put(2, "Yes, it's this simple!");
commit();
mikeTWC1984 commented 8 months ago

OK, thanks. What I needed is second option. I work with a storage engine that uses lmdb as backend, so I cannot define that callback to perform multiple operation, needed a way to open transaction "globally". The option you provided worked for me.