How to use the loopback-connector.Transaction.prototype function in loopback-connector

To help you get started, we’ve selected a few loopback-connector examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
}, options.timeout);
      }
      cb(err, transaction);
    });
  } else {
    process.nextTick(function() {
      const err = new Error(g.f('{{Transaction}} is not supported'));
      cb(err);
    });
  }
  return cb.promise;
};

// Promisify the transaction apis
if (Transaction) {
  jutil.mixin(Transaction.prototype, ObserverMixin);
  /**
   * Commit a transaction and release it back to the pool.
   *
   * Example:
   *
   * ```js
   * MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
   *   // some crud operation of your choice
   *   tx.commit(function(err) {
   *     // release the connection pool upon committing
   *     tx.close(err);
   *   });
   * });
   * ```
   *
   * @callback {Function} cb Callback function.
github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
* Example:
   *
   * ```js
   * MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
   *   // some crud operation of your choice
   *   tx.commit(function(err) {
   *     // release the connection pool upon committing
   *     tx.close(err);
   *   });
   * });
   * ```
   *
   * @callback {Function} cb Callback function.
   * @returns {Promise|undefined} Returns a callback promise.
   */
  Transaction.prototype.commit = function(cb) {
    cb = cb || utils.createPromiseCallback();
    if (this.ensureActive(cb)) {
      const context = {
        transaction: this,
        operation: 'commit',
      };
      this.notifyObserversAround('commit', context,
        done => {
          this.connector.commit(this.connection, done);
        },
        err => {
          // Deference the connection to mark the transaction is not active
          // The connection should have been released back the pool
          this.connection = null;
          cb(err);
        });
github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
});
    }
    return cb.promise;
  };

  Transaction.prototype.ensureActive = function(cb) {
    // Report an error if the transaction is not active
    if (!this.connection) {
      process.nextTick(() => {
        cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
      });
    }
    return !!this.connection;
  };

  Transaction.prototype.toJSON = function() {
    return this.id;
  };

  Transaction.prototype.toString = function() {
    return this.id;
  };
}

TransactionMixin.Transaction = Transaction;
github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
Transaction.prototype.ensureActive = function(cb) {
    // Report an error if the transaction is not active
    if (!this.connection) {
      process.nextTick(() => {
        cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
      });
    }
    return !!this.connection;
  };

  Transaction.prototype.toJSON = function() {
    return this.id;
  };

  Transaction.prototype.toString = function() {
    return this.id;
  };
}

TransactionMixin.Transaction = Transaction;
github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
*  Example:
   *
   * ```js
   * MyModel.beginTransaction('READ COMMITTED', function(err, tx) {
   *   // some crud operation of your choice
   *   tx.rollback(function(err) {
   *     // release the connection pool upon committing
   *     tx.close(err);
   *   });
   * });
   * ```
   *
   * @callback {Function} cb Callback function.
   * @returns {Promise|undefined} Returns a callback promise.
   */
  Transaction.prototype.rollback = function(cb) {
    cb = cb || utils.createPromiseCallback();
    if (this.ensureActive(cb)) {
      const context = {
        transaction: this,
        operation: 'rollback',
      };
      this.notifyObserversAround('rollback', context,
        done => {
          this.connector.rollback(this.connection, done);
        },
        err => {
          // Deference the connection to mark the transaction is not active
          // The connection should have been released back the pool
          this.connection = null;
          cb(err);
        });
github strongloop / loopback-datasource-juggler / lib / transaction.js View on Github external
};
      this.notifyObserversAround('rollback', context,
        done => {
          this.connector.rollback(this.connection, done);
        },
        err => {
          // Deference the connection to mark the transaction is not active
          // The connection should have been released back the pool
          this.connection = null;
          cb(err);
        });
    }
    return cb.promise;
  };

  Transaction.prototype.ensureActive = function(cb) {
    // Report an error if the transaction is not active
    if (!this.connection) {
      process.nextTick(() => {
        cb(new Error(g.f('The {{transaction}} is not active: %s', this.id)));
      });
    }
    return !!this.connection;
  };

  Transaction.prototype.toJSON = function() {
    return this.id;
  };

  Transaction.prototype.toString = function() {
    return this.id;
  };