Blame view

node_modules/loopback-connector/lib/transaction.js 2.7 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  // Copyright IBM Corp. 2015,2016. All Rights Reserved.
  // Node module: loopback-connector
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  
  var assert = require('assert');
  var util = require('util');
  var EventEmitter = require('events').EventEmitter;
  var debug = require('debug')('loopback:connector:transaction');
  
  module.exports = Transaction;
  
  /**
   * Create a new Transaction object
   * @param {Connector} connector The connector instance
   * @param {*} connection A connection to the DB
   * @constructor
   */
  function Transaction(connector, connection) {
    this.connector = connector;
    this.connection = connection;
    EventEmitter.call(this);
  }
  
  util.inherits(Transaction, EventEmitter);
  
  // Isolation levels
  Transaction.SERIALIZABLE = 'SERIALIZABLE';
  Transaction.REPEATABLE_READ = 'REPEATABLE READ';
  Transaction.READ_COMMITTED = 'READ COMMITTED';
  Transaction.READ_UNCOMMITTED = 'READ UNCOMMITTED';
  
  Transaction.hookTypes = {
    BEFORE_COMMIT: 'before commit',
    AFTER_COMMIT: 'after commit',
    BEFORE_ROLLBACK: 'before rollback',
    AFTER_ROLLBACK: 'after rollback',
    TIMEOUT: 'timeout',
  };
  
  /**
   * Commit a transaction and release it back to the pool
   * @param cb
   * @returns {*}
   */
  Transaction.prototype.commit = function(cb) {
    return this.connector.commit(this.connection, cb);
  };
  
  /**
   * Rollback a transaction and release it back to the pool
   * @param cb
   * @returns {*|boolean}
   */
  Transaction.prototype.rollback = function(cb) {
    return this.connector.rollback(this.connection, cb);
  };
  
  /**
   * Begin a new transaction
   * @param {Connector} connector The connector instance
   * @param {Object} [options] Options {isolationLevel: '...', timeout: 1000}
   * @param cb
   */
  Transaction.begin = function(connector, options, cb) {
    if (typeof options === 'function' && cb === undefined) {
      cb = options;
      options = {};
    }
    if (typeof options === 'string') {
      options = { isolationLevel: options };
    }
    var isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
    assert(isolationLevel === Transaction.SERIALIZABLE ||
      isolationLevel === Transaction.REPEATABLE_READ ||
      isolationLevel === Transaction.READ_COMMITTED ||
      isolationLevel === Transaction.READ_UNCOMMITTED, 'Invalid isolationLevel');
  
    debug('Starting a transaction with options: %j', options);
    assert(typeof connector.beginTransaction === 'function',
      'beginTransaction must be function implemented by the connector');
    connector.beginTransaction(isolationLevel, function(err, connection) {
      if (err) {
        return cb(err);
      }
      var tx = connection;
      if (!(connection instanceof Transaction)) {
        tx = new Transaction(connector, connection);
      }
      cb(err, tx);
    });
  };