@databases/shared
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -1,2 +0,2 @@ | ||
import { SQLQuery } from '@databases/sql'; | ||
import type { SQLQuery } from '@databases/sql'; | ||
import { Disposable, TransactionFactory } from './Factory'; | ||
@@ -10,3 +10,2 @@ import Driver from './Driver'; | ||
readonly type = QueryableType.Connection; | ||
readonly sql: import("@databases/sql").SQL; | ||
protected readonly _lock: Lock; | ||
@@ -22,2 +21,3 @@ private _disposed; | ||
query(query: SQLQuery[]): Promise<any[][]>; | ||
addPostCommitStep(fn: () => Promise<void>): Promise<void>; | ||
queryStream(query: SQLQuery, options?: QueryStreamOptions<TDriver>): AsyncGenerator<any, void, unknown>; | ||
@@ -24,0 +24,0 @@ dispose(): Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const split_sql_query_1 = require("@databases/split-sql-query"); | ||
const sql_1 = require("@databases/sql"); | ||
const QueryableType_1 = require("./QueryableType"); | ||
const utils_1 = require("./utils"); | ||
const lock_1 = require("@databases/lock"); | ||
class BaseConnection { | ||
constructor(driver, factories) { | ||
this.type = QueryableType_1.default.Connection; | ||
this.sql = sql_1.default; | ||
this._driver = driver; | ||
this._factories = factories; | ||
this._lock = lock_1.getLock(driver.acquireLockTimeoutMilliseconds); | ||
} | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a Connection after it has been returned to the pool.'); | ||
constructor(driver, factories) { | ||
this.type = QueryableType_1.default.Connection; | ||
this._driver = driver; | ||
this._factories = factories; | ||
this._lock = lock_1.createLock(driver.acquireLockTimeoutMilliseconds); | ||
} | ||
} | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return await fn(this); | ||
} | ||
async tx(fn, options) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.txInternal(this._driver, this._factories, fn, options); | ||
} finally { | ||
this._lock.releaseLock(); | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a Connection after it has been returned to the pool.'); | ||
} | ||
} | ||
} | ||
async query(query) { | ||
this._throwIfDisposed(); | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) return []; | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.queryInternal(this._driver, query, utils_1.executeAndReturnAll); | ||
} finally { | ||
this._lock.releaseLock(); | ||
} | ||
} else { | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.queryInternal(this._driver, split_sql_query_1.default(query), utils_1.executeAndReturnLast); | ||
} finally { | ||
this._lock.releaseLock(); | ||
} | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return await fn(this); | ||
} | ||
} | ||
async *queryStream(query, options) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
for await (const record of this._driver.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} finally { | ||
this._lock.releaseLock(); | ||
async tx(fn, options) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.txInternal(this._driver, this._factories, fn, options); | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
} | ||
async dispose() { | ||
return this._disposed || (this._disposed = this._lock.pool()); | ||
} | ||
async query(query) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) | ||
return []; | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.queryInternal(this._driver, query, utils_1.executeAndReturnAll); | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
else { | ||
await this._lock.acquireLock(); | ||
try { | ||
return await utils_1.queryInternal(this._driver, split_sql_query_1.default(query), utils_1.executeAndReturnLast); | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
} | ||
async addPostCommitStep(fn) { | ||
await fn(); | ||
} | ||
async *queryStream(query, options) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
for await (const record of this._driver.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
async dispose() { | ||
return this._disposed || (this._disposed = this._lock.pool()); | ||
} | ||
} | ||
exports.default = BaseConnection; | ||
exports.default = BaseConnection; | ||
//# sourceMappingURL=BaseConnection.js.map |
import { ConnectionPool, PoolOptions } from '@databases/connection-pool'; | ||
import { SQLQuery } from '@databases/sql'; | ||
import type { SQLQuery } from '@databases/sql'; | ||
import Factory, { Disposable } from './Factory'; | ||
@@ -11,3 +11,2 @@ import Driver from './Driver'; | ||
readonly type = QueryableType.ConnectionPool; | ||
readonly sql: import("@databases/sql").SQL; | ||
protected readonly _pool: ConnectionPool<TDriver>; | ||
@@ -23,4 +22,5 @@ private readonly _factories; | ||
query(query: SQLQuery[]): Promise<any[][]>; | ||
addPostCommitStep(fn: () => Promise<void>): Promise<void>; | ||
queryStream(query: SQLQuery, options?: QueryStreamOptions<TDriver>): AsyncGenerator<any, void, unknown>; | ||
dispose(): Promise<void>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const connection_pool_1 = require("@databases/connection-pool"); | ||
const split_sql_query_1 = require("@databases/split-sql-query"); | ||
const sql_1 = require("@databases/sql"); | ||
const QueryableType_1 = require("./QueryableType"); | ||
const utils_1 = require("./utils"); | ||
const returnFalse = () => false; | ||
class BaseConnectionPool { | ||
constructor(options, factories) { | ||
this.type = QueryableType_1.default.ConnectionPool; | ||
this.sql = sql_1.default; | ||
this._disposed = false; | ||
this._pool = connection_pool_1.default(options); | ||
this._factories = factories; | ||
} | ||
async _withDriverFromPool(fn, ...args) { | ||
let releasing = false; | ||
const driver = await this._pool.getConnection(); | ||
try { | ||
const result = await fn(driver.connection, ...args); | ||
releasing = true; | ||
driver.release(); | ||
return result; | ||
} catch (ex) { | ||
if (releasing) { | ||
throw ex; | ||
} | ||
if (await driver.connection.canRecycleConnectionAfterError(ex).catch(returnFalse)) { | ||
releasing = true; | ||
driver.release(); | ||
} else { | ||
releasing = true; | ||
driver.dispose(); | ||
} | ||
throw ex; | ||
constructor(options, factories) { | ||
this.type = QueryableType_1.default.ConnectionPool; | ||
this._disposed = false; | ||
this._pool = connection_pool_1.default(options); | ||
this._factories = factories; | ||
} | ||
} | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a ConnectionPool after it has been disposed.'); | ||
async _withDriverFromPool(fn, ...args) { | ||
let releasing = false; | ||
const driver = await this._pool.getConnection(); | ||
try { | ||
const result = await fn(driver.connection, ...args); | ||
releasing = true; | ||
driver.release(); | ||
return result; | ||
} | ||
catch (ex) { | ||
if (releasing) { | ||
throw ex; | ||
} | ||
if (await driver.connection | ||
.canRecycleConnectionAfterError(ex) | ||
.catch(returnFalse)) { | ||
releasing = true; | ||
driver.release(); | ||
} | ||
else { | ||
releasing = true; | ||
driver.dispose(); | ||
} | ||
throw ex; | ||
} | ||
} | ||
} | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return this._withDriverFromPool(utils_1.taskInternal, this._factories, fn); | ||
} | ||
async tx(fn, options) { | ||
this._throwIfDisposed(); | ||
return this._withDriverFromPool(utils_1.txInternal, this._factories, fn, options); | ||
} | ||
async query(query) { | ||
this._throwIfDisposed(); | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) return []; | ||
return this._withDriverFromPool(utils_1.queryInternal, query, utils_1.executeAndReturnAll); | ||
} else { | ||
return this._withDriverFromPool(utils_1.queryInternal, split_sql_query_1.default(query), utils_1.executeAndReturnLast); | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a ConnectionPool after it has been disposed.'); | ||
} | ||
} | ||
} | ||
async *queryStream(query, options) { | ||
this._throwIfDisposed(); | ||
const poolRecord = await this._pool.getConnection(); | ||
try { | ||
for await (const record of poolRecord.connection.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} finally { | ||
poolRecord.dispose(); | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return this._withDriverFromPool(utils_1.taskInternal, this._factories, fn); | ||
} | ||
} | ||
async dispose() { | ||
this._disposed = true; | ||
await this._pool.drain(); | ||
} | ||
async tx(fn, options) { | ||
this._throwIfDisposed(); | ||
return this._withDriverFromPool(utils_1.txInternal, this._factories, fn, options); | ||
} | ||
async query(query) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) | ||
return []; | ||
return this._withDriverFromPool(utils_1.queryInternal, query, utils_1.executeAndReturnAll); | ||
} | ||
else { | ||
return this._withDriverFromPool(utils_1.queryInternal, split_sql_query_1.default(query), utils_1.executeAndReturnLast); | ||
} | ||
} | ||
async addPostCommitStep(fn) { | ||
await fn(); | ||
} | ||
async *queryStream(query, options) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
const poolRecord = await this._pool.getConnection(); | ||
try { | ||
for await (const record of poolRecord.connection.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} | ||
finally { | ||
poolRecord.dispose(); | ||
} | ||
} | ||
async dispose() { | ||
this._disposed = true; | ||
await this._pool.drain(); | ||
} | ||
} | ||
exports.default = BaseConnectionPool; | ||
exports.default = BaseConnectionPool; | ||
//# sourceMappingURL=BaseConnectionPool.js.map |
@@ -1,3 +0,3 @@ | ||
import { SQLQuery } from '@databases/sql'; | ||
import { Disposable, TransactionFactory } from './Factory'; | ||
import type { SQLQuery } from '@databases/sql'; | ||
import { Disposable, TransactionFactory, TransactionParentContext } from './Factory'; | ||
import Driver from './Driver'; | ||
@@ -9,3 +9,2 @@ import QueryableType from './QueryableType'; | ||
readonly type = QueryableType.Transaction; | ||
readonly sql: import("@databases/sql").SQL; | ||
protected readonly _lock: Lock; | ||
@@ -16,3 +15,4 @@ private _disposed; | ||
private readonly _factories; | ||
constructor(driver: TDriver, factories: TransactionFactory<TDriver, TTransaction>); | ||
private readonly _parentContext; | ||
constructor(driver: TDriver, factories: TransactionFactory<TDriver, TTransaction>, parentContext: TransactionParentContext); | ||
task<T>(fn: (connection: this) => Promise<T>): Promise<T>; | ||
@@ -22,2 +22,3 @@ tx<T>(fn: (connection: TTransaction) => Promise<T>): Promise<T>; | ||
query(query: SQLQuery[]): Promise<any[][]>; | ||
addPostCommitStep(fn: () => Promise<void>): Promise<void>; | ||
queryStream(query: SQLQuery, options?: QueryStreamOptions<TDriver>): AsyncGenerator<any, void, unknown>; | ||
@@ -24,0 +25,0 @@ dispose(): Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const split_sql_query_1 = require("@databases/split-sql-query"); | ||
const sql_1 = require("@databases/sql"); | ||
const cuid = require("cuid"); | ||
const QueryableType_1 = require("./QueryableType"); | ||
const lock_1 = require("@databases/lock"); | ||
const utils_1 = require("./utils"); | ||
class BaseTransaction { | ||
constructor(driver, factories) { | ||
this.type = QueryableType_1.default.Transaction; | ||
this.sql = sql_1.default; | ||
this._driver = driver; | ||
this._factories = factories; | ||
this._lock = lock_1.getLock(driver.acquireLockTimeoutMilliseconds); | ||
} | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a Transaction after it has been committed or rolled back.'); | ||
constructor(driver, factories, parentContext) { | ||
this.type = QueryableType_1.default.Transaction; | ||
this._driver = driver; | ||
this._factories = factories; | ||
this._lock = lock_1.createLock(driver.acquireLockTimeoutMilliseconds); | ||
this._parentContext = parentContext; | ||
} | ||
} | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return await fn(this); | ||
} | ||
async tx(fn) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
const savepointName = cuid(); | ||
await this._driver.createSavepoint(savepointName); | ||
const subTransaction = this._factories.createTransaction(this._driver); | ||
try { | ||
const result = await fn(subTransaction); | ||
await subTransaction.dispose(); | ||
await this._driver.releaseSavepoint(savepointName); | ||
return result; | ||
} catch (ex) { | ||
await subTransaction.dispose(); | ||
await this._driver.rollbackToSavepoint(savepointName); | ||
throw ex; | ||
} | ||
} finally { | ||
this._lock.releaseLock(); | ||
_throwIfDisposed() { | ||
if (this._disposed) { | ||
throw new Error('You cannot run any operations on a Transaction after it has been committed or rolled back.'); | ||
} | ||
} | ||
} | ||
async query(query) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) return []; | ||
for (const el of query) { | ||
if (!sql_1.isSqlQuery(el)) { | ||
throw new Error('Invalid query, you must use @databases/sql to create your queries.'); | ||
} | ||
async task(fn) { | ||
this._throwIfDisposed(); | ||
return await fn(this); | ||
} | ||
async tx(fn) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
const savepointName = cuid(); | ||
await this._driver.createSavepoint(savepointName); | ||
const subTransaction = this._factories.createTransaction(this._driver, this._parentContext); | ||
try { | ||
const result = await fn(subTransaction); | ||
await subTransaction.dispose(); | ||
await this._driver.releaseSavepoint(savepointName); | ||
return result; | ||
} | ||
catch (ex) { | ||
await subTransaction.dispose(); | ||
await this._driver.rollbackToSavepoint(savepointName); | ||
throw ex; | ||
} | ||
} | ||
return await this._driver.executeAndReturnAll(query); | ||
} else { | ||
if (!sql_1.isSqlQuery(query)) { | ||
throw new Error('Invalid query, you must use @databases/sql to create your queries.'); | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
return await this._driver.executeAndReturnLast(split_sql_query_1.default(query)); | ||
} | ||
} finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
async *queryStream(query, options) { | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
for await (const record of this._driver.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} finally { | ||
this._lock.releaseLock(); | ||
async query(query) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
if (Array.isArray(query)) { | ||
if (query.length === 0) | ||
return []; | ||
return await this._driver.executeAndReturnAll(query); | ||
} | ||
else { | ||
return await this._driver.executeAndReturnLast(split_sql_query_1.default(query)); | ||
} | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
} | ||
async dispose() { | ||
return this._disposed || (this._disposed = this._lock.pool()); | ||
} | ||
async addPostCommitStep(fn) { | ||
this._parentContext.addPostCommitStep(fn); | ||
} | ||
async *queryStream(query, options) { | ||
utils_1.assertSql(query); | ||
this._throwIfDisposed(); | ||
await this._lock.acquireLock(); | ||
try { | ||
for await (const record of this._driver.queryStream(query, options)) { | ||
yield record; | ||
} | ||
} | ||
finally { | ||
this._lock.releaseLock(); | ||
} | ||
} | ||
async dispose() { | ||
return this._disposed || (this._disposed = this._lock.pool()); | ||
} | ||
} | ||
exports.default = BaseTransaction; | ||
exports.default = BaseTransaction; | ||
//# sourceMappingURL=BaseTransaction.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=Driver.js.map |
@@ -1,2 +0,2 @@ | ||
import { SQLQuery } from '@databases/sql'; | ||
import type { SQLQuery } from '@databases/sql'; | ||
export default interface EventHandlers { | ||
@@ -3,0 +3,0 @@ onQueryStart?: (query: SQLQuery, formattedQuery: { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=EventHandlers.js.map |
@@ -0,1 +1,4 @@ | ||
export interface TransactionParentContext { | ||
addPostCommitStep: (fn: () => Promise<void>) => void; | ||
} | ||
export interface Disposable { | ||
@@ -5,3 +8,3 @@ dispose(): Promise<void>; | ||
export interface TransactionFactory<TDriver, TTransaction extends Disposable> { | ||
createTransaction(driver: TDriver): TTransaction; | ||
createTransaction(driver: TDriver, ctx: TransactionParentContext): TTransaction; | ||
} | ||
@@ -8,0 +11,0 @@ export interface ConnectionFactory<TDriver, TConnection extends Disposable> { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=Factory.js.map |
@@ -1,4 +0,4 @@ | ||
import sql, { SQLQuery } from '@databases/sql'; | ||
import type { SQLQuery } from '@databases/sql'; | ||
import { Lock } from '@databases/lock'; | ||
import Factory, { Disposable, ConnectionFactory, TransactionFactory } from './Factory'; | ||
import Factory, { TransactionParentContext, Disposable, ConnectionFactory, TransactionFactory } from './Factory'; | ||
import Driver from './Driver'; | ||
@@ -11,4 +11,3 @@ import QueryableType from './QueryableType'; | ||
export type { SQLQuery, PoolOptions }; | ||
export { sql }; | ||
export type { Driver, Disposable, TransactionFactory, ConnectionFactory, Factory, }; | ||
export type { Driver, Disposable, TransactionParentContext, TransactionFactory, ConnectionFactory, Factory, }; | ||
export { QueryableType, BaseTransaction, BaseConnection, BaseConnectionPool }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.BaseConnectionPool = exports.BaseConnection = exports.BaseTransaction = exports.QueryableType = exports.sql = void 0; | ||
const sql_1 = require("@databases/sql"); | ||
exports.sql = sql_1.default; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseConnectionPool = exports.BaseConnection = exports.BaseTransaction = exports.QueryableType = void 0; | ||
const QueryableType_1 = require("./QueryableType"); | ||
exports.QueryableType = QueryableType_1.default; | ||
const BaseTransaction_1 = require("./BaseTransaction"); | ||
exports.BaseTransaction = BaseTransaction_1.default; | ||
const BaseConnection_1 = require("./BaseConnection"); | ||
exports.BaseConnection = BaseConnection_1.default; | ||
const BaseConnectionPool_1 = require("./BaseConnectionPool"); | ||
exports.BaseConnectionPool = BaseConnectionPool_1.default; | ||
exports.BaseConnectionPool = BaseConnectionPool_1.default; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var QueryableType; | ||
(function (QueryableType) { | ||
QueryableType["Transaction"] = "transaction"; | ||
QueryableType["Connection"] = "connection"; | ||
QueryableType["ConnectionPool"] = "connection_pool"; | ||
QueryableType["Transaction"] = "transaction"; | ||
QueryableType["Connection"] = "connection"; | ||
QueryableType["ConnectionPool"] = "connection_pool"; | ||
})(QueryableType || (QueryableType = {})); | ||
exports.default = QueryableType; | ||
exports.default = QueryableType; | ||
//# sourceMappingURL=QueryableType.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=AbortSignal.js.map |
import type { SQLQuery } from '@databases/sql'; | ||
import type Driver from './Driver'; | ||
import { ConnectionFactory, Disposable, TransactionFactory } from './Factory'; | ||
export declare function assertSql(query: SQLQuery | SQLQuery[]): void; | ||
export declare function executeAndReturnAll<TTransactionOptions>(driver: Driver<TTransactionOptions, any>, queries: SQLQuery[]): Promise<any[][]>; | ||
@@ -5,0 +6,0 @@ export declare function executeAndReturnLast<TTransactionOptions>(driver: Driver<TTransactionOptions, any>, queries: SQLQuery[]): Promise<any[]>; |
136
lib/utils.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.txInternal = exports.taskInternal = exports.queryInternal = exports.executeAndReturnLast = exports.executeAndReturnAll = void 0; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.txInternal = exports.taskInternal = exports.queryInternal = exports.executeAndReturnLast = exports.executeAndReturnAll = exports.assertSql = void 0; | ||
const web_1 = require("@databases/sql/web"); | ||
function assertSql(query) { | ||
if (Array.isArray(query)) { | ||
for (const q of query) { | ||
if (!web_1.isSqlQuery(q)) { | ||
throw new Error('Invalid query, you must use @databases/sql to create your queries.'); | ||
} | ||
} | ||
} | ||
else { | ||
if (!web_1.isSqlQuery(query)) { | ||
throw new Error('Invalid query, you must use @databases/sql to create your queries.'); | ||
} | ||
} | ||
} | ||
exports.assertSql = assertSql; | ||
async function executeAndReturnAll(driver, queries) { | ||
return driver.executeAndReturnAll(queries); | ||
return driver.executeAndReturnAll(queries); | ||
} | ||
exports.executeAndReturnAll = executeAndReturnAll; | ||
async function executeAndReturnLast(driver, queries) { | ||
return driver.executeAndReturnLast(queries); | ||
return driver.executeAndReturnLast(queries); | ||
} | ||
exports.executeAndReturnLast = executeAndReturnLast; | ||
async function queryInternal(driver, queries, fn) { | ||
const hasTransaction = queries.length > 1; | ||
try { | ||
if (hasTransaction) { | ||
await driver.beginTransaction(undefined); | ||
const hasTransaction = queries.length > 1; | ||
try { | ||
if (hasTransaction) { | ||
await driver.beginTransaction(undefined); | ||
} | ||
const results = await fn(driver, queries); | ||
if (hasTransaction) { | ||
await driver.commitTransaction(); | ||
} | ||
return results; | ||
} | ||
const results = await fn(driver, queries); | ||
if (hasTransaction) { | ||
await driver.commitTransaction(); | ||
catch (ex) { | ||
if (hasTransaction) { | ||
await driver.rollbackTransaction(); | ||
} | ||
throw ex; | ||
} | ||
return results; | ||
} catch (ex) { | ||
if (hasTransaction) { | ||
await driver.rollbackTransaction(); | ||
} | ||
throw ex; | ||
} | ||
} | ||
exports.queryInternal = queryInternal; | ||
async function taskInternal(driver, factories, fn) { | ||
const connection = factories.createConnection(driver); | ||
try { | ||
return await fn(connection); | ||
} finally { | ||
await connection.dispose(); | ||
} | ||
const connection = factories.createConnection(driver); | ||
try { | ||
return await fn(connection); | ||
} | ||
finally { | ||
await connection.dispose(); | ||
} | ||
} | ||
exports.taskInternal = taskInternal; | ||
async function txInternal(driver, factories, fn, options) { | ||
let failureCount = 0; | ||
await driver.beginTransaction(options); | ||
while (true) { | ||
const tx = factories.createTransaction(driver); | ||
try { | ||
const result = await fn(tx); | ||
await tx.dispose(); | ||
await driver.commitTransaction(); | ||
return result; | ||
} catch (ex) { | ||
await tx.dispose(); | ||
await driver.rollbackTransaction(); | ||
if (await driver.shouldRetryTransactionFailure(options, ex, ++failureCount)) { | ||
continue; | ||
} | ||
throw ex; | ||
let failureCount = 0; | ||
await driver.beginTransaction(options); | ||
while (true) { | ||
const postCommitSteps = []; | ||
const tx = factories.createTransaction(driver, { | ||
addPostCommitStep: (fn) => { | ||
postCommitSteps.push(fn); | ||
}, | ||
}); | ||
let result; | ||
try { | ||
result = await fn(tx); | ||
await tx.dispose(); | ||
await driver.commitTransaction(); | ||
} | ||
catch (ex) { | ||
await tx.dispose(); | ||
await driver.rollbackTransaction(); | ||
if (await driver.shouldRetryTransactionFailure(options, ex, ++failureCount)) { | ||
continue; | ||
} | ||
throw ex; | ||
} | ||
for (const step of postCommitSteps) { | ||
await step(); | ||
} | ||
return result; | ||
} | ||
} | ||
} | ||
exports.txInternal = txInternal; | ||
exports.txInternal = txInternal; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@databases/shared", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
231011
33
524