Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blob): add support unique transaction for get blob #275

Merged
merged 3 commits into from
Nov 2, 2022

Conversation

jesusvilla
Copy link
Contributor

@jesusvilla jesusvilla commented Nov 2, 2022

For unique transaction:

const Firebird = require('node-firebird');

async function query (sql, bindings = []) {
  return new Promise((resolve, reject) => {
    Firebird.attach(config, (error, conn) => {
      if (error) {
        return reject(error);
      }

      conn.transaction(Firebird.ISOLATION_READ_COMMITED, (err, transaction) => {
        if (error) {
          return reject(error);
        }
        transaction.query(sql, bindings, (error, result) => {
          if (error) {
            transaction.rollback();
            return reject(error);
          }

          const arrBlob = [];
          for (const item of result) {
            const fields = Object.keys(item);
            for (const key of fields) {
              if (typeof item[key] === 'function') {
                item[key] = new Promise((resolve, reject) => {
                  // the same transaction is used (better performance)
                  // this is optional
                  item[key](transaction, (error, name, event, row) => {
                    if (error) {
                      return reject(error);
                    }

                    let value = '';
                    event.on('data', (chunk) => {
                      value += chunk.toString('binary');
                    });
                    event.on('end', () => {
                      resolve({ value, column: name, row });
                    });
                  });
                });
                arrBlob.push(item[key]);
              }
            }
          }

          Promise.all(arrBlob).then((blobs) => {
            for (const blob of blobs) {
              result[blob.row][blob.column] = blob.value;
            }

            transaction.commit((error) => {
              if (error) {
                transaction.rollback();
                return reject(error);
              }
  
              conn.detach();
              resolve(result);
            }).catch((error) => {
              transaction.rollback();
              reject(error);
            });
          })
        });
      });
    });
  });
}

@mariuz mariuz merged commit 9665154 into hgourvest:master Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants