dexie / Dexie.js

A Minimalistic Wrapper for IndexedDB
https://dexie.org
Apache License 2.0
11.49k stars 642 forks source link

Hello, world ES6/ES7 example failing under Node.js with IndexedDBShim #712

Open aral opened 6 years ago

aral commented 6 years ago

A slightly modified (using IndexedDBShim and console.log instead of alert()) version of the Hello, world ES6/ES7 example that uses transactions is failing under Node.js.

To reproduce

After linking in the latest Dexie and IndexedDBShim, run:

const Dexie = require('dexie')

// Node shim
const setGlobalVars = require("indexeddbshim");
const shim = {};
setGlobalVars(shim, { checkOrigin: false });
const { indexedDB, IDBKeyRange } = shim;

Dexie.dependencies.indexedDB = indexedDB;
Dexie.dependencies.IDBKeyRange = IDBKeyRange;

//
// Declare Database
//
var db = new Dexie("FriendDatabase");
db.version(1).stores({ friends: "++id,name,age" });

db.transaction('rw', db.friends, async() => {

    // Make sure we have something in DB:
    if ((await db.friends.where('name').equals('Josephine').count()) === 0) {
        let id = await db.friends.add({name: "Josephine", age: 21});
        alert (`Addded friend with id ${id}`);
    }

    // Query:
    let youngFriends = await db.friends.where("age").below(25).toArray();

    // Show result:
    alert ("My young friends: " + JSON.stringify(youngFriends));

}).catch(e => {
    console.log(e.stack || e);
});

Error

TransactionInactiveError: A request was placed against a transaction which is currently not active, or which is finished

/Users/aral/indie/other/forks/IndexedDBShim/dist/indexeddbshim-node.js:5467
            throw err;

This appears to be the same error we’re seeing in tests-asyncawait, assertion 12 (#709).

dfahlander commented 6 years ago

Think this is the native async/await issue. This hello-world example was written when no browser or node version had support for it, so it was expected to be transpiled. See https://github.com/dfahlander/Dexie.js/issues/317. Today the sample would work without transpilation in chrome, edge and safari only. Not Firefox (yet, what I know), and not IndexedDBShim.

dfahlander commented 6 years ago

Note: This issue should only apply when using transactions. Transactionless code can use native async/await. Could be tested on the shim by skipping the transaction block around the code and replace it with a simple async function.

dfahlander commented 6 years ago

Clarification: Dexie itself handles native async/await but certain browsers, including Firefox v<=60 and Indexeddbshim trigger transaction commit too early when using it.