ForerunnerDB icon indicating copy to clipboard operation
ForerunnerDB copied to clipboard

Implement Transactions to Support Begin / Commit / Rollback

Open rognoni opened this issue 10 years ago • 4 comments

I created the nextval function that the first time creates a sequence collection and after increments its value:

nextval: 1
nextval: 2

========== sequence
{"schema":"info","entity":"history","property":"id","val":2,"_id":"3fd5cd0d081b280"}

but this function is not secure without the database transaction:

  nextval: function(schema, entity, property) {
    var sequenceCollection = this.fdb.db(schema).collection("sequence");
    var sequenceResult = sequenceCollection.find({
      schema: {
        "$eq": schema
      },
      entity: {
        "$eq": entity
      },
      property: {
        "$eq": property
      }
    });

    if (sequenceResult.length == 0) {
      sequenceCollection.insert({schema: schema, entity: entity, property: property, val: 1});
      return 1;
    } else {
      var val = sequenceResult[0].val + 1;
      sequenceCollection.updateById(sequenceResult[0]._id, {val: val});
      return val;
    }
  }

https://github.com/rognoni/adaptable/blob/master/Front-end/HTML5-Bootstrap4/database.js

rognoni avatar Mar 18 '16 20:03 rognoni

Hey ya,

Could you clarify what you are asking? I don't know how to answer you at the moment.

Thanks!

Rob

Irrelon avatar Mar 21 '16 08:03 Irrelon

Yes, probably is not simple to implement this feature into ForerunnerDB (using pure JavaScript)

https://en.wikipedia.org/wiki/Database_transaction

but for example IndexedDB uses the transaction concepts:

var objectStore = db.transaction(["customers"], "readwrite").objectStore("customers");
var request = objectStore.get("444-44-4444");
request.onerror = function(event) {
  // Handle errors!
};
request.onsuccess = function(event) {
  // Get the old value that we want to update
  var data = request.result;

  // update the value(s) in the object that you want to change
  data.age = 42;

  // Put this updated object back into the database.
  var requestUpdate = objectStore.put(data);
   requestUpdate.onerror = function(event) {
     // Do something with the error
   };
   requestUpdate.onsuccess = function(event) {
     // Success - the data is updated!
   };
};

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB#Updating_an_entry_in_the_database

rognoni avatar Mar 21 '16 13:03 rognoni

Ahh I SEE... you are asking if we can implement transactions... sorry that was definitely not clear from your first post!

It is possible. It is on the roadmap (I think). You are correct that it could be quite complex though :)

Irrelon avatar Mar 21 '16 14:03 Irrelon

I found now this project if can help:

https://github.com/yathit/ydn-db#transaction

rognoni avatar Mar 21 '16 14:03 rognoni