UNPKG

@n8n/typeorm

Version:

Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.

133 lines (131 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SqliteWriteConnection = void 0; const tslib_1 = require("tslib"); const node_1 = require("@sentry/node"); const async_mutex_1 = require("async-mutex"); const assert_1 = tslib_1.__importDefault(require("assert")); const DriverAlreadyReleasedError_1 = require("../../error/DriverAlreadyReleasedError"); const LeasedDbConnection_1 = require("./LeasedDbConnection"); const Timer_1 = require("./Timer"); /** * A single write connection to the database. */ class SqliteWriteConnection { constructor(sqliteLibray, options) { this.sqliteLibray = sqliteLibray; this.options = options; this.writeConnectionPromise = null; /** * Should the connection be re-created after it has been released */ this.isConnectionValid = true; this.isReleased = false; const acquireTimeout = options.acquireTimeout; this.writeConnectionMutex = (0, async_mutex_1.withTimeout)(new async_mutex_1.Mutex(), acquireTimeout); } async connect() { this.assertNotReleased(); await this.writeConnectionMutex.runExclusive(async () => await this.createConnection()); } async close() { if (this.isReleased) return; this.isReleased = true; // Cancel any pending acquires this.writeConnectionMutex.cancel(); // If there is an existing lease, request it to be released if (this.dbLease) { this.dbLease.requestRelease(); } const timeoutTimer = Timer_1.TimeoutTimer.start(this.options.destroyTimeout); await Promise.race([ this.writeConnectionMutex.acquire(), timeoutTimer.promise, ]).finally(() => { timeoutTimer.clear(); }); if (this.writeConnectionPromise) { const dbConnection = await this.writeConnectionPromise; this.sqliteLibray.destroyDatabaseConnection(dbConnection); } } async runExclusive(dbLeaseHolder, callback) { this.assertNotReleased(); try { return await this.writeConnectionMutex.runExclusive(async () => { this.dbLease = await this.createAndGetConnection(dbLeaseHolder); const result = await callback(this.dbLease); return result; }); } catch (error) { if (error === async_mutex_1.E_TIMEOUT) { (0, node_1.captureException)(error); } throw error; } finally { this.dbLease = undefined; } } async leaseConnection(dbLeaseHolder) { this.assertNotReleased(); try { await this.writeConnectionMutex.acquire(); } catch (error) { (0, node_1.captureException)(error); throw error; } this.dbLease = await this.createAndGetConnection(dbLeaseHolder); return this.dbLease; } invalidateConnection(leasedDbConnection) { (0, assert_1.default)(this.dbLease === leasedDbConnection); (0, assert_1.default)(this.writeConnectionMutex.isLocked()); (0, assert_1.default)(this.writeConnectionPromise); this.isConnectionValid = false; } async releaseConnection(leasedDbConnection) { (0, assert_1.default)(this.dbLease === leasedDbConnection); (0, assert_1.default)(this.writeConnectionMutex.isLocked()); (0, assert_1.default)(this.writeConnectionPromise); try { const connection = await this.writeConnectionPromise; if (!this.isConnectionValid) { this.sqliteLibray.destroyDatabaseConnection(connection); this.writeConnectionPromise = null; } } finally { this.dbLease = undefined; this.writeConnectionMutex.release(); } } async createAndGetConnection(dbLeaseHolder) { if (!this.writeConnectionPromise) { this.writeConnectionPromise = this.sqliteLibray.createDatabaseConnection(); } const dbConnection = await this.writeConnectionPromise; (0, assert_1.default)(!this.dbLease); return new LeasedDbConnection_1.LeasedDbConnection(dbConnection, this, dbLeaseHolder); } async createConnection() { this.assertNotReleased(); if (this.writeConnectionPromise) { throw new Error("Connection already created"); } this.writeConnectionPromise = this.sqliteLibray.createDatabaseConnection(); return this.writeConnectionPromise; } assertNotReleased() { if (this.isReleased) { throw new DriverAlreadyReleasedError_1.DriverAlreadyReleasedError(); } } } exports.SqliteWriteConnection = SqliteWriteConnection; //# sourceMappingURL=SqliteWriteConnection.js.map