UNPKG

4.64 kBJavaScriptView Raw
1// Copyright IBM Corp. 2015,2020. All Rights Reserved.
2// Node module: loopback-connector
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7const assert = require('assert');
8const util = require('util');
9const EventEmitter = require('events').EventEmitter;
10const debug = require('debug')('loopback:connector:transaction');
11const uuid = require('uuid');
12const {createPromiseCallback} = require('./utils');
13
14module.exports = Transaction;
15
16/**
17 * Create a new Transaction object
18 * @param {Connector} connector The connector instance
19 * @param {*} connection A connection to the DB
20 * @constructor
21 */
22function Transaction(connector, connection) {
23 this.connector = connector;
24 this.connection = connection;
25 EventEmitter.call(this);
26}
27
28util.inherits(Transaction, EventEmitter);
29
30// Isolation levels
31Transaction.SERIALIZABLE = 'SERIALIZABLE';
32Transaction.REPEATABLE_READ = 'REPEATABLE READ';
33Transaction.READ_COMMITTED = 'READ COMMITTED';
34Transaction.READ_UNCOMMITTED = 'READ UNCOMMITTED';
35
36Transaction.hookTypes = {
37 BEFORE_COMMIT: 'before commit',
38 AFTER_COMMIT: 'after commit',
39 BEFORE_ROLLBACK: 'before rollback',
40 AFTER_ROLLBACK: 'after rollback',
41 TIMEOUT: 'timeout',
42};
43
44/**
45 * Commit a transaction and release it back to the pool
46 * @param cb
47 * @returns {*}
48 */
49Transaction.prototype.commit = function(cb) {
50 cb = cb || createPromiseCallback();
51 if (cb.promise) {
52 this.connector.commit(this.connection, cb);
53 return cb.promise;
54 } else {
55 return this.connector.commit(this.connection, cb);
56 }
57};
58
59/**
60 * Rollback a transaction and release it back to the pool
61 * @param cb
62 * @returns {*|boolean}
63 */
64Transaction.prototype.rollback = function(cb) {
65 cb = cb || createPromiseCallback();
66 if (cb.promise) {
67 this.connector.rollback(this.connection, cb);
68 return cb.promise;
69 } else {
70 return this.connector.rollback(this.connection, cb);
71 }
72};
73
74/**
75 * Begin a new transaction
76 * @param {Connector} connector The connector instance
77 * @param {Object} [options] Options {isolationLevel: '...', timeout: 1000}
78 * @param cb
79 */
80Transaction.begin = function(connector, options, cb) {
81 if (typeof options === 'function' && cb === undefined) {
82 cb = options;
83 options = {};
84 }
85 cb = cb || createPromiseCallback();
86 if (typeof options === 'string') {
87 options = {isolationLevel: options};
88 }
89 const isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
90 assert(isolationLevel === Transaction.SERIALIZABLE ||
91 isolationLevel === Transaction.REPEATABLE_READ ||
92 isolationLevel === Transaction.READ_COMMITTED ||
93 isolationLevel === Transaction.READ_UNCOMMITTED, 'Invalid isolationLevel');
94
95 debug('Starting a transaction with options: %j', options);
96 assert(typeof connector.beginTransaction === 'function',
97 'beginTransaction must be function implemented by the connector');
98 connector.beginTransaction(isolationLevel, function(err, connection) {
99 if (err) {
100 return cb(err);
101 }
102 let tx = connection;
103
104 // When the connector and juggler node module have different version of this module as a dependency,
105 // the transaction is not an instanceof Transaction.
106 // i.e. (connection instanceof Transaction) == false
107 // Check for existence of required functions and properties, instead of prototype inheritance.
108 if (connection.connector == undefined || connection.connection == undefined ||
109 connection.commit == undefined || connection.rollback == undefined) {
110 tx = new Transaction(connector, connection);
111 }
112 // Set an informational transaction id
113 tx.id = uuid.v1();
114 // NOTE(lehni) Handling of transaction timeouts here only works with recent
115 // versions of `loopback-datasource-juggler` which make its own handling of
116 // timeouts conditional based on the absence of an already set `tx.timeout`,
117 // see: https://github.com/strongloop/loopback-datasource-juggler/pull/1484
118 if (options.timeout) {
119 tx.timeout = setTimeout(function() {
120 const context = {
121 transaction: tx,
122 operation: 'timeout',
123 };
124 tx.notifyObserversOf('timeout', context, function(err) {
125 if (!err) {
126 tx.rollback(function() {
127 debug('Transaction %s is rolled back due to timeout', tx.id);
128 });
129 }
130 });
131 }, options.timeout);
132 }
133 cb(err, tx);
134 });
135 if (cb.promise) return cb.promise;
136};
137
138/**
139 * Check whether a transaction has an active connection.
140 */
141Transaction.prototype.isActive = function() {
142 return !!this.connection;
143};