UNPKG

5.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const instance_1 = require("./instance");
4class ManagedDatabaseConnection {
5 constructor(requiresWrite = false) {
6 this._managed = false;
7 this._requiresWrite = requiresWrite;
8 this._instantionStack = new Error().stack;
9 }
10 setConnection(connection) {
11 if (this._connection) {
12 const oldConnection = this._connection;
13 if (oldConnection.isTransaction()) {
14 instance_1.getInstance().getLogger().warn('Rolling back a transaction because setConnection was called on a ManagedDatabaseConnection in a transaction in progress.');
15 instance_1.getInstance().getLogger().trace(new Error('Stacktrace'));
16 oldConnection.rollback().then(() => {
17 oldConnection.close();
18 }).catch((error) => {
19 instance_1.getInstance().getLogger().error(error);
20 oldConnection.close(true);
21 });
22 }
23 else {
24 oldConnection.close();
25 }
26 }
27 this._connection = connection;
28 this._managed = true;
29 }
30 isWriteRequired() {
31 return this._requiresWrite;
32 }
33 isManaged() {
34 return this._managed;
35 }
36 hasConnection() {
37 return !!this._connection;
38 }
39 setInstantiationStack(stack) {
40 if (this.hasConnection()) {
41 this._connection.setInstantiationStack(stack);
42 }
43 else {
44 this._instantionStack = stack;
45 }
46 }
47 getInstantiationStack() {
48 if (this.hasConnection()) {
49 return this._connection.getInstantiationStack();
50 }
51 else {
52 return this._instantionStack;
53 }
54 }
55 isReadOnly() {
56 if (this.hasConnection()) {
57 return this._connection.isReadOnly();
58 }
59 else {
60 return true;
61 }
62 }
63 setTimeout(timeout) {
64 this._getConnection().then((connection) => {
65 connection.setTimeout(timeout);
66 });
67 }
68 getTimeout() {
69 if (this.hasConnection()) {
70 return this._connection.getTimeout();
71 }
72 else {
73 return null;
74 }
75 }
76 query(query, params) {
77 return new Promise((resolve, reject) => {
78 this._getConnection().then((connection) => {
79 connection.query(query, params).then(resolve).catch(reject);
80 }).catch(reject);
81 });
82 }
83 stream(query, params, streamOptions) {
84 throw new Error('stream is not supported on Managed Connections');
85 }
86 close(forceClose) {
87 return new Promise((resolve, reject) => {
88 if (this.hasConnection() && !this.isManaged()) {
89 this._connection.close(forceClose).then(() => {
90 this._connection = null;
91 this._managed = false;
92 resolve();
93 }).catch(reject);
94 }
95 else {
96 resolve();
97 }
98 });
99 }
100 startTransaction() {
101 return new Promise((resolve, reject) => {
102 this._getConnection().then((connection) => {
103 if (!this.isManaged()) {
104 connection.startTransaction().then(resolve).catch(reject);
105 }
106 else {
107 resolve();
108 }
109 }).catch(reject);
110 });
111 }
112 isTransaction() {
113 if (this.hasConnection()) {
114 return this._connection.isTransaction();
115 }
116 else {
117 return false;
118 }
119 }
120 commit() {
121 return new Promise((resolve, reject) => {
122 this._getConnection().then((connection) => {
123 if (!this.isManaged()) {
124 connection.commit().then(resolve).catch(reject);
125 }
126 else {
127 resolve();
128 }
129 }).catch(reject);
130 });
131 }
132 rollback() {
133 return new Promise((resolve, reject) => {
134 this._getConnection().then((connection) => {
135 if (!this.isManaged()) {
136 connection.rollback().then(resolve).catch(reject);
137 }
138 else {
139 resolve();
140 }
141 }).catch(reject);
142 });
143 }
144 _getConnection() {
145 return new Promise((resolve, reject) => {
146 let promise = null;
147 let setInstantationStack = false;
148 if (!this._connection) {
149 promise = instance_1.getInstance().getDB().getConnection(this._requiresWrite);
150 setInstantationStack = true;
151 }
152 else {
153 promise = Promise.resolve(this._connection);
154 }
155 promise.then((connection) => {
156 if (setInstantationStack) {
157 connection.setInstantiationStack(this._instantionStack);
158 }
159 this._connection = connection;
160 resolve(this._connection);
161 }).catch(reject);
162 });
163 }
164 getAPI() {
165 if (this.hasConnection()) {
166 return this._connection.getAPI();
167 }
168 else {
169 return null;
170 }
171 }
172}
173exports.ManagedDatabaseConnection = ManagedDatabaseConnection;
174//# sourceMappingURL=ManagedDatabaseConnection.js.map
\No newline at end of file