How to use the dexie.currentTransaction function in dexie

To help you get started, we’ve selected a few dexie examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dfahlander / Dexie.js / test / tests-asyncawait.js View on Github external
Dexie.ignoreTransaction(()=>{
            ok(Dexie.currentTransaction == null, "No Transaction in this zone");
            function promiseFlow () {
                return NativePromise.resolve().then(()=>{
                    if(Dexie.currentTransaction !== null) ok(false, "PSD zone leaked");
                    return new NativePromise(resolve => NativePromise.resolve().then(resolve));
                });
            };
            otherZonePromise = promiseFlow();
            for (let i=0;i<100;++i) {
                otherZonePromise = otherZonePromise.then(promiseFlow);
            }
        });
        // In parallell with the above 2*100 async tasks are being executed and verified,
github dfahlander / Dexie.js / test / tests-asyncawait.js View on Github external
db.transaction('rw', db.items, ()=>{
        let trans = Dexie.currentTransaction;
        ok(trans !== null, "Should have a current transaction");
        let otherZonePromise;
        Dexie.ignoreTransaction(()=>{
            ok(Dexie.currentTransaction == null, "No Transaction in this zone");
            function promiseFlow () {
                return NativePromise.resolve().then(()=>{
                    if(Dexie.currentTransaction !== null) ok(false, "PSD zone leaked");
                    return new NativePromise(resolve => NativePromise.resolve().then(resolve));
                });
            };
            otherZonePromise = promiseFlow();
            for (let i=0;i<100;++i) {
                otherZonePromise = otherZonePromise.then(promiseFlow);
            }
        });
        // In parallell with the above 2*100 async tasks are being executed and verified,
github dfahlander / Dexie.js / addons / Dexie.Syncable / src / finally-commit-all-changes.js View on Github external
return db.transaction('rw!', tablesToIncludeInTrans, () => {
      var trans = Dexie.currentTransaction;
      var localRevisionBeforeChanges = 0;
      return db._changes.orderBy('rev').last(function (lastChange) {
        // Store what revision we were at before committing the changes
        localRevisionBeforeChanges = (lastChange && lastChange.rev) || 0;
      }).then(() => {
        // Specify the source. Important for the change consumer to ignore changes originated from self!
        trans.source = node.id;
        // 2. Apply uncommitted changes and delete each uncommitted change
        return db._uncommittedChanges.where('node').equals(node.id).toArray();
      }).then(function (uncommittedChanges) {
        return applyChanges(uncommittedChanges);
      }).then(function () {
        return db._uncommittedChanges.where('node').equals(node.id).delete();
      }).then(function () {
        // 3. Apply last chunk of changes
        return applyChanges(changes);
github learningequality / studio / contentcuration / contentcuration / frontend / shared / data / changes.js View on Github external
revert() {
    if (this._isReverted) {
      return Promise.resolve();
    }

    if (!this._isBlocking) {
      throw new Error('Unable to revert changes without locks');
    }

    this._isReverted = true;

    if (Dexie.currentTransaction) {
      if (Dexie.currentTransaction.source === REVERT_SOURCE) {
        return Promise.resolve();
      }

      // We're in the middle of a transaction, so just abort that
      Dexie.currentTransaction.abort();
      return Promise.resolve();
    }

    return getLocks({ tracker_id: this.id })
      .then(locks => {
        if (!locks.length) {
          throw new Error('Nothing to revert');
        }

        return promiseChunk(locks, 1, ([lock]) => {
github dfahlander / Dexie.js / test / tests-transaction.js View on Github external
return db.transaction('rw', db.pets, function() {
                    var innermostTransaction2 = Dexie.currentTransaction;
                    ok(innermostTransaction2.parent == level2Transaction, "Another 3rd level transaction has parent set to our level2 transaction");
                    return db.pets.add({
                        id: 321,
                        value: 'val'
                    }).then(function(resultId2) {
                        return Dexie.Promise.resolve(resultId2);
                    }).then(function(resultId2) {
                        ok(Dexie.currentTransaction === innermostTransaction2, "We're still in the innermostTransaction (second one)");
                        return Dexie.Promise.resolve(resultId2).then(function(x) {
                            ok(Dexie.currentTransaction === innermostTransaction2, "We're still in the innermostTransaction (second one)");
                            return x;
                        });
                    });
                }).then(function(resultId2) {
                    equal(resultId2, 321, "Result2 was 321 as expected");
github dfahlander / Dexie.js / test / tests-asyncawait.js View on Github external
}).then(()=>{
            ok(Dexie.currentTransaction === trans, "Transaction persisted after window.Promise.resolve().then()");
            return (async ()=>{})();
        });
        ok(Dexie.currentTransaction === trans, "Transaction persisted between await calls of mixed promises");
github dfahlander / Dexie.js / test / tests-transaction.js View on Github external
db.transaction('rw', db.users, db.pets, db.petsPerUser, function () {
        var trans = Dexie.currentTransaction;
        parentTrans = Dexie.currentTransaction;
        ok(trans._reculock === 0, "Main transaction not locked yet");
        addUser({ username: "user1" }, [{ kind: "dog" }, { kind: "cat" }]).then(function () {
            db.users.get("someoneelse", function (someone) {
                equal(someone.username, "someoneelse", "Someonelse was recently added");
            });
        });
        ok(trans._reculock > 0, "Main transaction is now locked");
        db.users.get("someoneelse", function (someone) {
            ok(!someone, "Someoneelse not yet added");
        });
        db.users.add({ username: "someoneelse" });
        return addUser({ username: "user2" }, [{ kind: "giraff" }]).then(function (val) {
            ok(trans._reculock == 0, "Main transaction not locked anymore");
            return val;
        });
github learningequality / studio / contentcuration / contentcuration / frontend / shared / data / changes.js View on Github external
.transaction('rw', lock.table_name, () => {
        Dexie.currentTransaction.source = REVERT_SOURCE;
        return changeTable
          .where('rev')
          .above(lock.rev_start)
          .toArray()
          .then(changes => {
            return changeTable
              .where('rev')
              .above(lock.rev_start)
              .delete()
              .then(() => changes);
          });
      })
      .then(changes => {
github learningequality / studio / contentcuration / contentcuration / frontend / shared / data / applyRemoteChanges.js View on Github external
return db.transaction('rw', tables, () => {
    Dexie.currentTransaction.source = IGNORED_SOURCE;
    const promises = [];
    table_names.forEach(table_name => {
      const table = db.table(table_name);
      const specifyKeys = !table.schema.primKey.keyPath;
      const createChangesToApply = collectedChanges[table_name][CREATED];
      const deleteChangesToApply = collectedChanges[table_name][DELETED];
      const updateChangesToApply = collectedChanges[table_name][UPDATED];
      const moveChangesToApply = collectedChanges[table_name][MOVED];
      if (createChangesToApply.length) {
        promises.push(
          table.bulkPut(
            createChangesToApply.map(c => c.obj),
            specifyKeys ? createChangesToApply.map(c => c.key) : undefined
          )
        );
      }