eveningkid / denodb

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
https://eveningkid.com/denodb-docs
MIT License
1.93k stars 129 forks source link

Postgres transaction are broken #342

Open ninjinskii opened 2 years ago

ninjinskii commented 2 years ago

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

ninjinskii commented 2 years ago

I've submit a PR that should fix the problem https://github.com/eveningkid/denodb/pull/343

ninjinskii commented 2 years ago

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>;
  }