eveningkid / denodb

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno

Home Page:https://eveningkid.com/denodb-docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Postgres transaction are broken

ninjinskii opened this issue · comments

Hello !

Postgres transaction are bugged, i cant use them, i think i know why.

In the postgres docs, we see that after opening a transaction, we have to use the object returned by transaction() to make queries, not the base client
Deno postgres docs

But DenoDB doesn't give us a chance to work with this transaction object, neither use it to make the queries, thus systematically showing the error "This connection is currently locked by the "transaction" transaction"

This is where the code is broken:
https://github.com/eveningkid/denodb/blob/master/lib/connectors/postgres-connector.ts#L89

I've submit a PR that should fix the problem
#343

As a temporary quick and dirty solution, for anyone else stuck on this problematic, you can use this code to fix the transactions without waiting for the fix to be done:

async doInTransaction(block: () => Promise<void>): Promise<void> {
    // deno-lint-ignore no-explicit-any
    const client = this.db["_connector"]["_client"] as PostgresClient;
    const transaction = client.createTransaction("transaction");
    
    // deno-lint-ignore no-explicit-any
    this.db["_connector"]["_client"] = transaction;
    
    await transaction.begin();
    await block()
    await transaction.commit();

    // deno-lint-ignore no-explicit-any
    this.db["_connector"]["_client"] = client;

    // Waiting for a fix of DenoDB
    //return this.client.transaction(block) as Promise<void>;
  }