UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

217 lines (214 loc) 7.23 kB
"use strict"; /* Copyright 2017-2021 Norman Breau Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ManagedDatabaseConnection = void 0; const instance_1 = require("./instance"); const TAG = 'ManagedDatabaseConnection'; class ManagedDatabaseConnection { constructor(requiresWrite = false) { this.$managed = false; this.$requiresWrite = requiresWrite; this.$instantionStack = new Error().stack; } formatQuery(query) { throw new Error('Unsupported API call'); } // TODO(Breaking): Turn this API into async setConnection(connection) { if (this.$connection) { // Store original connection because of async, // but we don't really need to wait for the async operations // to complete to set the new connection object. let oldConnection = this.$connection; /** * If the old connection has a transaction, only care about it * if this particular instance of managed connections has write access. */ if (this.$requiresWrite && oldConnection.isTransaction()) { (0, instance_1.getInstance)().getLogger().warn(TAG, 'Rolling back a transaction because setConnection was called on a ManagedDatabaseConnection in a transaction in progress.'); (0, instance_1.getInstance)().getLogger().trace(TAG, new Error('Stacktrace')); oldConnection.rollback().then(() => { oldConnection.close(); }).catch((error) => { (0, instance_1.getInstance)().getLogger().error(TAG, error); oldConnection.close(true); }); } else { oldConnection.close(); } } this.$connection = connection; this.$managed = true; } isClosed() { if (this.$connection) { return this.$connection.isClosed(); } else { return true; } } async getCurrentDatabasePosition() { let conn = await this.$getConnection(); return await conn.getCurrentDatabasePosition(); } isWriteRequired() { return this.$requiresWrite; } isManaged() { return this.$managed; } hasConnection() { return !!this.$connection; } setInstantiationStack(stack) { if (this.hasConnection()) { this.$connection.setInstantiationStack(stack); } else { this.$instantionStack = stack; } } getInstantiationStack() { if (this.hasConnection()) { return this.$connection.getInstantiationStack(); } else { return this.$instantionStack; } } isReadOnly() { if (this.hasConnection()) { return this.$connection.isReadOnly(); } else { return true; } } // TODO: Turn this into a promise -- but the public interface also needs to change setTimeout(timeout) { this.$getConnection().then((connection) => { connection.setTimeout(timeout); }).catch((error) => { console.error(error); }); } getTimeout() { if (this.hasConnection()) { return this.$connection.getTimeout(); } else { return null; } } query(query, params) { return new Promise((resolve, reject) => { this.$getConnection().then((connection) => { connection.query(query, params).then(resolve).catch(reject); }).catch(reject); }); } stream(query, params, streamOptions) { throw new Error('stream is not supported on Managed Connections'); } close(forceClose) { return new Promise((resolve, reject) => { if (this.hasConnection() && !this.isManaged()) { this.$connection.close(forceClose).then(() => { this.$connection = null; this.$managed = false; resolve(); }).catch(reject); } else { resolve(); } }); } startTransaction(isolationLevel) { return new Promise((resolve, reject) => { this.$getConnection().then((connection) => { if (!this.isManaged()) { connection.startTransaction(isolationLevel).then(resolve).catch(reject); } else { resolve(); } }).catch(reject); }); } isTransaction() { if (this.hasConnection()) { return this.$connection.isTransaction(); } else { return false; } } commit() { return new Promise((resolve, reject) => { this.$getConnection().then((connection) => { if (!this.isManaged()) { connection.commit().then(resolve).catch(reject); } else { resolve(); } }).catch(reject); }); } rollback() { return new Promise((resolve, reject) => { this.$getConnection().then((connection) => { if (!this.isManaged()) { connection.rollback().then(resolve).catch(reject); } else { resolve(); } }).catch(reject); }); } $getConnection() { return new Promise((resolve, reject) => { let promise = null; let shouldSetInstantationStack = false; if (!this.$connection) { promise = (0, instance_1.getInstance)().getDB().getConnection(this.$requiresWrite); shouldSetInstantationStack = true; } else { promise = Promise.resolve(this.$connection); } promise.then((connection) => { if (shouldSetInstantationStack) { connection.setInstantiationStack(this.$instantionStack); } this.$connection = connection; resolve(this.$connection); }).catch(reject); }); } getAPI() { if (this.hasConnection()) { return this.$connection.getAPI(); } else { return null; } } } exports.ManagedDatabaseConnection = ManagedDatabaseConnection; //# sourceMappingURL=ManagedDatabaseConnection.js.map