Skip to content

TransactionContext Error: Failed to commit: File is not opened in write mode #2182

@Darker

Description

@Darker

What happens?

I had no success using this library normally, with persistent storage. Contrary to what is advertised on the website, there seems to be no way to initialize it so that I can execute queries.

Here is how I initialize my database:

async function initDuckDB(dbName) {
    const MANUAL_BUNDLES = {
        mvp: {
            mainModule: '/js/lib/ext/@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm',
            mainWorker: '/js/lib/ext/@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js',
        },
        eh: {
            mainModule: '/js/lib/ext/@duckdb/duckdb-wasm/dist/duckdb-eh.wasm',
            mainWorker: '/js/lib/ext/@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js',
        },
    };
    const bundle = await duckdb.selectBundle(MANUAL_BUNDLES);

    const worker = new Worker(bundle.mainWorker, { type: 'module' });
    const logger = new duckdb.ConsoleLogger();

    const fbFileName = dbName+".duckdb";

    const db = new duckdb.AsyncDuckDB(logger, worker);

    await db.instantiate(bundle.mainModule, bundle.pthreadWorker);

    await db.open({
        path: "opfs://"+fbFileName,
        accessMode: duckdb.DuckDBAccessMode.READ_WRITE,
    });

    // 6. Create a connection
    return db;
}```


Then I create a connection and wrap it in a wrapper. Wrapper is needed, because there is zero consistency between node and wasm API:

/**

  • @extends {DuckDBInstance}
    /
    class DuckDbWasmWrapper {
    /
    *
    *

    • @param {import("@duckdb/duckdb-wasm").AsyncDuckDB} db
      */
      constructor(db) {
      this.db = db;
      this.closed = false;
      }

    async connect() {
    const conn = await this.db.connect();
    return new WasmConnWrapper(conn);
    }

    closeSync() {
    if(!this.closed) {
    this.closed = true;
    this.db.flushFiles().then(()=>this.db.terminate());
    }
    }

};


`run` is remapped in these wrappers to query. I also tried to introduce this hack, based [on some comments](https://github.com/pondpilot/pondpilot/pull/131#issuecomment-2918707825):

    const refreshname = "__refresh_table"
    for(let i=3; i>=0; --i) {
        try {
            await conn.query(`CREATE OR REPLACE TABLE "${refreshname}" as select 1;`);
            await conn.query(`DROP TABLE "${refreshname}";`);
        }
        catch(e) {
            console.log(e);
        }
    }

That didn't work, I get more of the same error.

### To Reproduce

Described above.

### Browser/Environment:

Firefox

### Device:

PC

### DuckDB-Wasm Version:

1.33.1-dev18.0

### DuckDB-Wasm Deployment:

NPM

### Full Name:

Jakub Mareda

### Affiliation:

Working on a personal project, at home

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions