@contember/database 2.1.0-beta.2 → 2.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +3 -0
- package/dist/development/index.cjs +4 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +4 -0
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/builders/CaseStatement.cjs.map +1 -1
- package/dist/development/src/builders/CaseStatement.js.map +1 -1
- package/dist/development/src/builders/ColumnExpressionFactory.cjs.map +1 -1
- package/dist/development/src/builders/ColumnExpressionFactory.js.map +1 -1
- package/dist/development/src/builders/Compiler.cjs.map +1 -1
- package/dist/development/src/builders/Compiler.js.map +1 -1
- package/dist/development/src/builders/ConditionBuilder.cjs +6 -0
- package/dist/development/src/builders/ConditionBuilder.cjs.map +1 -1
- package/dist/development/src/builders/ConditionBuilder.js +6 -0
- package/dist/development/src/builders/ConditionBuilder.js.map +1 -1
- package/dist/development/src/builders/DeleteBuilder.cjs.map +1 -1
- package/dist/development/src/builders/DeleteBuilder.js.map +1 -1
- package/dist/development/src/builders/InsertBuilder.cjs.map +1 -1
- package/dist/development/src/builders/InsertBuilder.js.map +1 -1
- package/dist/development/src/builders/LimitByGroupWrapper.cjs.map +1 -1
- package/dist/development/src/builders/LimitByGroupWrapper.js.map +1 -1
- package/dist/development/src/builders/SelectBuilder.cjs.map +1 -1
- package/dist/development/src/builders/SelectBuilder.js.map +1 -1
- package/dist/development/src/builders/UpdateBuilder.cjs.map +1 -1
- package/dist/development/src/builders/UpdateBuilder.js.map +1 -1
- package/dist/development/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/development/src/builders/WindowFunction.js.map +1 -1
- package/dist/development/src/builders/internal/Returning.cjs.map +1 -1
- package/dist/development/src/builders/internal/Returning.js.map +1 -1
- package/dist/development/src/builders/internal/Subqueries.cjs.map +1 -1
- package/dist/development/src/builders/internal/Subqueries.js.map +1 -1
- package/dist/development/src/builders/internal/Where.cjs.map +1 -1
- package/dist/development/src/builders/internal/Where.js.map +1 -1
- package/dist/development/src/builders/internal/With.cjs.map +1 -1
- package/dist/development/src/builders/internal/With.js.map +1 -1
- package/dist/development/src/builders/utils.cjs.map +1 -1
- package/dist/development/src/builders/utils.js.map +1 -1
- package/dist/development/src/client/AcquiredConnection.cjs.map +1 -1
- package/dist/development/src/client/AcquiredConnection.js.map +1 -1
- package/dist/development/src/client/Client.cjs.map +1 -1
- package/dist/development/src/client/Client.js.map +1 -1
- package/dist/development/src/client/Connection.cjs +11 -0
- package/dist/development/src/client/Connection.cjs.map +1 -1
- package/dist/development/src/client/Connection.js +11 -0
- package/dist/development/src/client/Connection.js.map +1 -1
- package/dist/development/src/client/EventManager.cjs.map +1 -1
- package/dist/development/src/client/EventManager.js.map +1 -1
- package/dist/development/src/client/Pool.cjs.map +1 -1
- package/dist/development/src/client/Pool.js.map +1 -1
- package/dist/development/src/client/ScopeLimitedConnection.cjs +37 -0
- package/dist/development/src/client/ScopeLimitedConnection.cjs.map +1 -0
- package/dist/development/src/client/ScopeLimitedConnection.js +37 -0
- package/dist/development/src/client/ScopeLimitedConnection.js.map +1 -0
- package/dist/development/src/client/Transaction.cjs.map +1 -1
- package/dist/development/src/client/Transaction.js.map +1 -1
- package/dist/development/src/metadata/DatabaseMetadata.cjs.map +1 -1
- package/dist/development/src/metadata/DatabaseMetadata.js.map +1 -1
- package/dist/development/src/metadata/DatabaseMetadataResolver.cjs.map +1 -1
- package/dist/development/src/metadata/DatabaseMetadataResolver.js.map +1 -1
- package/dist/development/src/metadata/IndexMetadata.cjs.map +1 -1
- package/dist/development/src/metadata/IndexMetadata.js.map +1 -1
- package/dist/development/src/metadata/UniqueConstraintMetadata.cjs.map +1 -1
- package/dist/development/src/metadata/UniqueConstraintMetadata.js.map +1 -1
- package/dist/development/src/queryable/DatabaseQuery.cjs.map +1 -1
- package/dist/development/src/queryable/DatabaseQuery.js.map +1 -1
- package/dist/development/src/queryable/DatabaseQueryable.cjs.map +1 -1
- package/dist/development/src/queryable/DatabaseQueryable.js.map +1 -1
- package/dist/development/src/utils/ConstraintHelper.cjs.map +1 -1
- package/dist/development/src/utils/ConstraintHelper.js.map +1 -1
- package/dist/development/src/utils/Semaphore.cjs +43 -0
- package/dist/development/src/utils/Semaphore.cjs.map +1 -0
- package/dist/development/src/utils/Semaphore.js +43 -0
- package/dist/development/src/utils/Semaphore.js.map +1 -0
- package/dist/development/src/utils/createDatabaseIfNotExists.cjs.map +1 -1
- package/dist/development/src/utils/createDatabaseIfNotExists.js.map +1 -1
- package/dist/development/src/utils/listen.cjs.map +1 -1
- package/dist/development/src/utils/listen.js.map +1 -1
- package/dist/development/src/utils/pgClientFactory.cjs +5 -3
- package/dist/development/src/utils/pgClientFactory.cjs.map +1 -1
- package/dist/development/src/utils/pgClientFactory.js +5 -3
- package/dist/development/src/utils/pgClientFactory.js.map +1 -1
- package/dist/development/src/utils/retryTransaction.cjs.map +1 -1
- package/dist/development/src/utils/retryTransaction.js.map +1 -1
- package/dist/development/src/utils/sql.cjs.map +1 -1
- package/dist/development/src/utils/sql.js.map +1 -1
- package/dist/development/src/utils/withDatabaseAdvisoryLock.cjs.map +1 -1
- package/dist/development/src/utils/withDatabaseAdvisoryLock.js.map +1 -1
- package/dist/production/index.cjs +4 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +4 -0
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/builders/CaseStatement.cjs.map +1 -1
- package/dist/production/src/builders/CaseStatement.js.map +1 -1
- package/dist/production/src/builders/ColumnExpressionFactory.cjs.map +1 -1
- package/dist/production/src/builders/ColumnExpressionFactory.js.map +1 -1
- package/dist/production/src/builders/Compiler.cjs.map +1 -1
- package/dist/production/src/builders/Compiler.js.map +1 -1
- package/dist/production/src/builders/ConditionBuilder.cjs +6 -0
- package/dist/production/src/builders/ConditionBuilder.cjs.map +1 -1
- package/dist/production/src/builders/ConditionBuilder.js +6 -0
- package/dist/production/src/builders/ConditionBuilder.js.map +1 -1
- package/dist/production/src/builders/DeleteBuilder.cjs.map +1 -1
- package/dist/production/src/builders/DeleteBuilder.js.map +1 -1
- package/dist/production/src/builders/InsertBuilder.cjs.map +1 -1
- package/dist/production/src/builders/InsertBuilder.js.map +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.cjs.map +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.js.map +1 -1
- package/dist/production/src/builders/SelectBuilder.cjs.map +1 -1
- package/dist/production/src/builders/SelectBuilder.js.map +1 -1
- package/dist/production/src/builders/UpdateBuilder.cjs.map +1 -1
- package/dist/production/src/builders/UpdateBuilder.js.map +1 -1
- package/dist/production/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/production/src/builders/WindowFunction.js.map +1 -1
- package/dist/production/src/builders/internal/Returning.cjs.map +1 -1
- package/dist/production/src/builders/internal/Returning.js.map +1 -1
- package/dist/production/src/builders/internal/Subqueries.cjs.map +1 -1
- package/dist/production/src/builders/internal/Subqueries.js.map +1 -1
- package/dist/production/src/builders/internal/Where.cjs.map +1 -1
- package/dist/production/src/builders/internal/Where.js.map +1 -1
- package/dist/production/src/builders/internal/With.cjs.map +1 -1
- package/dist/production/src/builders/internal/With.js.map +1 -1
- package/dist/production/src/builders/utils.cjs.map +1 -1
- package/dist/production/src/builders/utils.js.map +1 -1
- package/dist/production/src/client/AcquiredConnection.cjs.map +1 -1
- package/dist/production/src/client/AcquiredConnection.js.map +1 -1
- package/dist/production/src/client/Client.cjs.map +1 -1
- package/dist/production/src/client/Client.js.map +1 -1
- package/dist/production/src/client/Connection.cjs +11 -0
- package/dist/production/src/client/Connection.cjs.map +1 -1
- package/dist/production/src/client/Connection.js +11 -0
- package/dist/production/src/client/Connection.js.map +1 -1
- package/dist/production/src/client/EventManager.cjs.map +1 -1
- package/dist/production/src/client/EventManager.js.map +1 -1
- package/dist/production/src/client/Pool.cjs.map +1 -1
- package/dist/production/src/client/Pool.js.map +1 -1
- package/dist/production/src/client/ScopeLimitedConnection.cjs +37 -0
- package/dist/production/src/client/ScopeLimitedConnection.cjs.map +1 -0
- package/dist/production/src/client/ScopeLimitedConnection.js +37 -0
- package/dist/production/src/client/ScopeLimitedConnection.js.map +1 -0
- package/dist/production/src/client/Transaction.cjs.map +1 -1
- package/dist/production/src/client/Transaction.js.map +1 -1
- package/dist/production/src/metadata/DatabaseMetadata.cjs.map +1 -1
- package/dist/production/src/metadata/DatabaseMetadata.js.map +1 -1
- package/dist/production/src/metadata/DatabaseMetadataResolver.cjs.map +1 -1
- package/dist/production/src/metadata/DatabaseMetadataResolver.js.map +1 -1
- package/dist/production/src/metadata/IndexMetadata.cjs.map +1 -1
- package/dist/production/src/metadata/IndexMetadata.js.map +1 -1
- package/dist/production/src/metadata/UniqueConstraintMetadata.cjs.map +1 -1
- package/dist/production/src/metadata/UniqueConstraintMetadata.js.map +1 -1
- package/dist/production/src/queryable/DatabaseQuery.cjs.map +1 -1
- package/dist/production/src/queryable/DatabaseQuery.js.map +1 -1
- package/dist/production/src/queryable/DatabaseQueryable.cjs.map +1 -1
- package/dist/production/src/queryable/DatabaseQueryable.js.map +1 -1
- package/dist/production/src/utils/ConstraintHelper.cjs.map +1 -1
- package/dist/production/src/utils/ConstraintHelper.js.map +1 -1
- package/dist/production/src/utils/Semaphore.cjs +43 -0
- package/dist/production/src/utils/Semaphore.cjs.map +1 -0
- package/dist/production/src/utils/Semaphore.js +43 -0
- package/dist/production/src/utils/Semaphore.js.map +1 -0
- package/dist/production/src/utils/createDatabaseIfNotExists.cjs.map +1 -1
- package/dist/production/src/utils/createDatabaseIfNotExists.js.map +1 -1
- package/dist/production/src/utils/listen.cjs.map +1 -1
- package/dist/production/src/utils/listen.js.map +1 -1
- package/dist/production/src/utils/pgClientFactory.cjs +5 -3
- package/dist/production/src/utils/pgClientFactory.cjs.map +1 -1
- package/dist/production/src/utils/pgClientFactory.js +5 -3
- package/dist/production/src/utils/pgClientFactory.js.map +1 -1
- package/dist/production/src/utils/retryTransaction.cjs.map +1 -1
- package/dist/production/src/utils/retryTransaction.js.map +1 -1
- package/dist/production/src/utils/sql.cjs.map +1 -1
- package/dist/production/src/utils/sql.js.map +1 -1
- package/dist/production/src/utils/withDatabaseAdvisoryLock.cjs.map +1 -1
- package/dist/production/src/utils/withDatabaseAdvisoryLock.js.map +1 -1
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +1 -1
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -1
- package/dist/tests/cases/unit/scopeLimitedConnection.test.d.ts +2 -0
- package/dist/tests/cases/unit/scopeLimitedConnection.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/semaphore.test.d.ts +2 -0
- package/dist/tests/cases/unit/semaphore.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/builders/CaseStatement.d.ts +2 -2
- package/dist/types/builders/CaseStatement.d.ts.map +1 -1
- package/dist/types/builders/ColumnExpressionFactory.d.ts +6 -6
- package/dist/types/builders/ColumnExpressionFactory.d.ts.map +1 -1
- package/dist/types/builders/Compiler.d.ts +5 -5
- package/dist/types/builders/Compiler.d.ts.map +1 -1
- package/dist/types/builders/ConditionBuilder.d.ts +8 -6
- package/dist/types/builders/ConditionBuilder.d.ts.map +1 -1
- package/dist/types/builders/DeleteBuilder.d.ts +8 -8
- package/dist/types/builders/DeleteBuilder.d.ts.map +1 -1
- package/dist/types/builders/InsertBuilder.d.ts +10 -10
- package/dist/types/builders/InsertBuilder.d.ts.map +1 -1
- package/dist/types/builders/LimitByGroupWrapper.d.ts +3 -3
- package/dist/types/builders/LimitByGroupWrapper.d.ts.map +1 -1
- package/dist/types/builders/QueryBuilder.d.ts +5 -5
- package/dist/types/builders/QueryBuilder.d.ts.map +1 -1
- package/dist/types/builders/SelectBuilder.d.ts +9 -9
- package/dist/types/builders/SelectBuilder.d.ts.map +1 -1
- package/dist/types/builders/UpdateBuilder.d.ts +9 -9
- package/dist/types/builders/UpdateBuilder.d.ts.map +1 -1
- package/dist/types/builders/WindowFunction.d.ts +2 -2
- package/dist/types/builders/WindowFunction.d.ts.map +1 -1
- package/dist/types/builders/index.d.ts +12 -12
- package/dist/types/builders/index.d.ts.map +1 -1
- package/dist/types/builders/internal/Returning.d.ts +3 -3
- package/dist/types/builders/internal/Returning.d.ts.map +1 -1
- package/dist/types/builders/internal/Subqueries.d.ts +4 -4
- package/dist/types/builders/internal/Subqueries.d.ts.map +1 -1
- package/dist/types/builders/internal/Where.d.ts +3 -3
- package/dist/types/builders/internal/Where.d.ts.map +1 -1
- package/dist/types/builders/internal/With.d.ts +3 -3
- package/dist/types/builders/internal/With.d.ts.map +1 -1
- package/dist/types/builders/utils.d.ts +2 -2
- package/dist/types/builders/utils.d.ts.map +1 -1
- package/dist/types/client/AcquiredConnection.d.ts +3 -3
- package/dist/types/client/AcquiredConnection.d.ts.map +1 -1
- package/dist/types/client/Client.d.ts +4 -4
- package/dist/types/client/Client.d.ts.map +1 -1
- package/dist/types/client/Connection.d.ts +12 -4
- package/dist/types/client/Connection.d.ts.map +1 -1
- package/dist/types/client/EventManager.d.ts +1 -1
- package/dist/types/client/EventManager.d.ts.map +1 -1
- package/dist/types/client/Pool.d.ts +3 -3
- package/dist/types/client/Pool.d.ts.map +1 -1
- package/dist/types/client/ScopeLimitedConnection.d.ts +34 -0
- package/dist/types/client/ScopeLimitedConnection.d.ts.map +1 -0
- package/dist/types/client/Transaction.d.ts +2 -2
- package/dist/types/client/Transaction.d.ts.map +1 -1
- package/dist/types/client/index.d.ts +8 -7
- package/dist/types/client/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/metadata/DatabaseMetadata.d.ts +3 -3
- package/dist/types/metadata/DatabaseMetadata.d.ts.map +1 -1
- package/dist/types/metadata/DatabaseMetadataResolver.d.ts +2 -2
- package/dist/types/metadata/DatabaseMetadataResolver.d.ts.map +1 -1
- package/dist/types/metadata/index.d.ts +5 -5
- package/dist/types/metadata/index.d.ts.map +1 -1
- package/dist/types/queryable/DatabaseQuery.d.ts +1 -1
- package/dist/types/queryable/DatabaseQuery.d.ts.map +1 -1
- package/dist/types/queryable/DatabaseQueryable.d.ts +1 -1
- package/dist/types/queryable/DatabaseQueryable.d.ts.map +1 -1
- package/dist/types/queryable/index.d.ts +2 -2
- package/dist/types/queryable/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/ConstraintHelper.d.ts +2 -2
- package/dist/types/utils/ConstraintHelper.d.ts.map +1 -1
- package/dist/types/utils/Semaphore.d.ts +17 -0
- package/dist/types/utils/Semaphore.d.ts.map +1 -0
- package/dist/types/utils/createDatabaseIfNotExists.d.ts +1 -1
- package/dist/types/utils/createDatabaseIfNotExists.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +11 -10
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/listen.d.ts +1 -1
- package/dist/types/utils/listen.d.ts.map +1 -1
- package/dist/types/utils/pgClientFactory.d.ts +3 -3
- package/dist/types/utils/pgClientFactory.d.ts.map +1 -1
- package/dist/types/utils/sql.d.ts +1 -1
- package/dist/types/utils/sql.d.ts.map +1 -1
- package/dist/types/utils/withDatabaseAdvisoryLock.d.ts +1 -1
- package/dist/types/utils/withDatabaseAdvisoryLock.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/builders/CaseStatement.ts +3 -3
- package/src/builders/ColumnExpressionFactory.ts +7 -7
- package/src/builders/Compiler.ts +9 -9
- package/src/builders/ConditionBuilder.ts +13 -7
- package/src/builders/DeleteBuilder.ts +8 -8
- package/src/builders/InsertBuilder.ts +11 -11
- package/src/builders/LimitByGroupWrapper.ts +5 -5
- package/src/builders/QueryBuilder.ts +5 -5
- package/src/builders/SelectBuilder.ts +11 -11
- package/src/builders/UpdateBuilder.ts +10 -10
- package/src/builders/WindowFunction.ts +4 -4
- package/src/builders/index.ts +12 -12
- package/src/builders/internal/Returning.ts +4 -4
- package/src/builders/internal/Subqueries.ts +4 -4
- package/src/builders/internal/Where.ts +4 -4
- package/src/builders/internal/With.ts +4 -4
- package/src/builders/utils.ts +4 -4
- package/src/client/AcquiredConnection.ts +7 -7
- package/src/client/Client.ts +5 -5
- package/src/client/Connection.ts +18 -6
- package/src/client/EventManager.ts +1 -1
- package/src/client/Pool.ts +5 -5
- package/src/client/ScopeLimitedConnection.ts +65 -0
- package/src/client/Transaction.ts +4 -4
- package/src/client/index.ts +8 -7
- package/src/index.ts +11 -10
- package/src/metadata/DatabaseMetadata.ts +3 -3
- package/src/metadata/DatabaseMetadataResolver.ts +5 -5
- package/src/metadata/IndexMetadata.ts +1 -1
- package/src/metadata/UniqueConstraintMetadata.ts +1 -1
- package/src/metadata/index.ts +5 -5
- package/src/queryable/DatabaseQuery.ts +2 -2
- package/src/queryable/DatabaseQueryable.ts +1 -1
- package/src/queryable/index.ts +2 -2
- package/src/types.ts +1 -0
- package/src/utils/ConstraintHelper.ts +3 -3
- package/src/utils/Semaphore.ts +49 -0
- package/src/utils/createDatabaseIfNotExists.ts +3 -3
- package/src/utils/index.ts +11 -10
- package/src/utils/listen.ts +2 -2
- package/src/utils/pgClientFactory.ts +15 -10
- package/src/utils/retryTransaction.ts +1 -1
- package/src/utils/sql.ts +1 -1
- package/src/utils/withDatabaseAdvisoryLock.ts +1 -1
- package/tests/cases/unit/connection.test.ts +2 -2
- package/tests/cases/unit/createConnectionMockAlt.ts +2 -2
- package/tests/cases/unit/eventManager.test.ts +2 -2
- package/tests/cases/unit/pool.test.ts +1 -1
- package/tests/cases/unit/queryBuilder.test.ts +7 -3
- package/tests/cases/unit/scopeLimitedConnection.test.ts +204 -0
- package/tests/cases/unit/semaphore.test.ts +53 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Connection } from './Connection.js'
|
|
2
|
+
import { EventManager } from './EventManager.js'
|
|
3
|
+
import { Client } from './Client.js'
|
|
4
|
+
import { PoolStatus } from './Pool.js'
|
|
5
|
+
import { Semaphore } from '../utils/Semaphore.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a {@link Connection} and limits the number of pool connections that may be
|
|
9
|
+
* acquired concurrently through it.
|
|
10
|
+
*
|
|
11
|
+
* Every top-level {@link Connection.scope} call (and therefore every {@link query} and
|
|
12
|
+
* {@link transaction}) acquires exactly one pool connection. By routing those calls
|
|
13
|
+
* through a {@link Semaphore} we cap how many connections a single owner of this
|
|
14
|
+
* instance (typically a single HTTP request) can hold at once, preventing one request
|
|
15
|
+
* from starving the shared pool under high concurrency.
|
|
16
|
+
*
|
|
17
|
+
* Nested scopes/transactions reuse the already-acquired connection (they go through
|
|
18
|
+
* `AcquiredConnection`, not back to the underlying `Connection`), so they are not
|
|
19
|
+
* counted again and cannot deadlock against the limit.
|
|
20
|
+
*/
|
|
21
|
+
export class ScopeLimitedConnection implements Connection.ConnectionType {
|
|
22
|
+
private readonly semaphore: Semaphore
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly inner: Connection.ConnectionType,
|
|
26
|
+
maxConnections: number,
|
|
27
|
+
public readonly eventManager: EventManager = inner.eventManager,
|
|
28
|
+
) {
|
|
29
|
+
this.semaphore = new Semaphore(maxConnections)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
33
|
+
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async scope<Result>(
|
|
37
|
+
callback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,
|
|
38
|
+
options: { eventManager?: EventManager } = {},
|
|
39
|
+
): Promise<Result> {
|
|
40
|
+
return await this.semaphore.execute(() => this.inner.scope(callback, options))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async transaction<Result>(
|
|
44
|
+
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
45
|
+
options: { eventManager?: EventManager } = {},
|
|
46
|
+
): Promise<Result> {
|
|
47
|
+
return await this.scope(async connection => {
|
|
48
|
+
return await connection.transaction(callback)
|
|
49
|
+
}, options)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async query<Row extends Record<string, any>>(
|
|
53
|
+
sql: string,
|
|
54
|
+
parameters: any[] = [],
|
|
55
|
+
meta: Record<string, any> = {},
|
|
56
|
+
): Promise<Connection.Result<Row>> {
|
|
57
|
+
return await this.scope(async connection => {
|
|
58
|
+
return await connection.query(sql, parameters, meta)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getPoolStatus(): PoolStatus | undefined {
|
|
63
|
+
return this.inner.getPoolStatus()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Connection } from './Connection'
|
|
2
|
-
import { EventManager } from './EventManager'
|
|
3
|
-
import { wrapIdentifier } from '../utils'
|
|
1
|
+
import { Connection } from './Connection.js'
|
|
2
|
+
import { EventManager } from './EventManager.js'
|
|
3
|
+
import { wrapIdentifier } from '../utils/index.js'
|
|
4
4
|
import { Notification } from 'pg'
|
|
5
|
-
import { CannotCommitError, DatabaseError } from './errors'
|
|
5
|
+
import { CannotCommitError, DatabaseError } from './errors.js'
|
|
6
6
|
|
|
7
7
|
export class Transaction implements Connection.TransactionLike {
|
|
8
8
|
public get isClosed(): boolean {
|
package/src/client/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export * from './Client'
|
|
2
|
-
export * from './errorCodes'
|
|
3
|
-
export * from './errors'
|
|
4
|
-
export * from './Connection'
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
1
|
+
export * from './Client.js'
|
|
2
|
+
export * from './errorCodes.js'
|
|
3
|
+
export * from './errors.js'
|
|
4
|
+
export * from './Connection.js'
|
|
5
|
+
export * from './ScopeLimitedConnection.js'
|
|
6
|
+
export * from './EventManager.js'
|
|
7
|
+
export * from './Pool.js'
|
|
8
|
+
export * from './Transaction.js'
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { With } from './builders/internal/With'
|
|
2
|
-
import { Where } from './builders/internal/Where'
|
|
3
|
-
import { Returning } from './builders/internal/Returning'
|
|
1
|
+
import { With } from './builders/internal/With.js'
|
|
2
|
+
import { Where } from './builders/internal/Where.js'
|
|
3
|
+
import { Returning } from './builders/internal/Returning.js'
|
|
4
4
|
|
|
5
5
|
export type WithAware = With.Aware
|
|
6
6
|
export type WhereAware = Where.Aware
|
|
7
7
|
export type ReturningAware = Returning.Aware
|
|
8
8
|
|
|
9
|
-
export * from './client'
|
|
10
|
-
export * from './builders'
|
|
11
|
-
export * from './metadata'
|
|
12
|
-
export * from './queryable'
|
|
13
|
-
export * from './Literal'
|
|
14
|
-
export * from './types'
|
|
9
|
+
export * from './client/index.js'
|
|
10
|
+
export * from './builders/index.js'
|
|
11
|
+
export * from './metadata/index.js'
|
|
12
|
+
export * from './queryable/index.js'
|
|
13
|
+
export * from './Literal.js'
|
|
14
|
+
export * from './types.js'
|
|
15
15
|
export {
|
|
16
16
|
AcquiringListener,
|
|
17
17
|
asyncIterableTransaction,
|
|
@@ -21,6 +21,7 @@ export {
|
|
|
21
21
|
formatColumnIdentifier,
|
|
22
22
|
Listener,
|
|
23
23
|
retryTransaction,
|
|
24
|
+
Semaphore,
|
|
24
25
|
withDatabaseAdvisoryLock,
|
|
25
26
|
wrapIdentifier,
|
|
26
|
-
} from './utils'
|
|
27
|
+
} from './utils/index.js'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { UniqueConstraintMetadata, UniqueConstraintMetadataSet } from './UniqueConstraintMetadata'
|
|
2
|
-
import { ForeignKeyConstraintMetadata, ForeignKeyConstraintMetadataSet } from './ForeignKeyConstraintMetadata'
|
|
3
|
-
import { IndexMetadata, IndexMetadataSet } from './IndexMetadata'
|
|
1
|
+
import { UniqueConstraintMetadata, UniqueConstraintMetadataSet } from './UniqueConstraintMetadata.js'
|
|
2
|
+
import { ForeignKeyConstraintMetadata, ForeignKeyConstraintMetadataSet } from './ForeignKeyConstraintMetadata.js'
|
|
3
|
+
import { IndexMetadata, IndexMetadataSet } from './IndexMetadata.js'
|
|
4
4
|
|
|
5
5
|
export interface DatabaseMetadata {
|
|
6
6
|
uniqueConstraints: UniqueConstraintMetadataSet
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Connection } from '../client'
|
|
2
|
-
import { createDatabaseMetadata, DatabaseMetadata } from './DatabaseMetadata'
|
|
3
|
-
import { ForeignKeyConstraintMetadata, ForeignKeyDeleteAction } from './ForeignKeyConstraintMetadata'
|
|
4
|
-
import { UniqueConstraintMetadata } from './UniqueConstraintMetadata'
|
|
5
|
-
import { IndexMetadata } from './IndexMetadata'
|
|
1
|
+
import { Connection } from '../client/index.js'
|
|
2
|
+
import { createDatabaseMetadata, DatabaseMetadata } from './DatabaseMetadata.js'
|
|
3
|
+
import { ForeignKeyConstraintMetadata, ForeignKeyDeleteAction } from './ForeignKeyConstraintMetadata.js'
|
|
4
|
+
import { UniqueConstraintMetadata } from './UniqueConstraintMetadata.js'
|
|
5
|
+
import { IndexMetadata } from './IndexMetadata.js'
|
|
6
6
|
|
|
7
7
|
export class DatabaseMetadataResolver {
|
|
8
8
|
async resolveMetadata(db: Connection.Queryable, contentSchema: string): Promise<DatabaseMetadata> {
|
package/src/metadata/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './DatabaseMetadata'
|
|
2
|
-
export * from './DatabaseMetadataResolver'
|
|
3
|
-
export * from './ForeignKeyConstraintMetadata'
|
|
4
|
-
export * from './IndexMetadata'
|
|
5
|
-
export * from './UniqueConstraintMetadata'
|
|
1
|
+
export * from './DatabaseMetadata.js'
|
|
2
|
+
export * from './DatabaseMetadataResolver.js'
|
|
3
|
+
export * from './ForeignKeyConstraintMetadata.js'
|
|
4
|
+
export * from './IndexMetadata.js'
|
|
5
|
+
export * from './UniqueConstraintMetadata.js'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Query } from '@contember/queryable'
|
|
2
|
-
import { DatabaseQueryable } from '../'
|
|
3
|
-
import { ImplementationException } from '../exceptions'
|
|
2
|
+
import { DatabaseQueryable } from '../index.js'
|
|
3
|
+
import { ImplementationException } from '../exceptions.js'
|
|
4
4
|
|
|
5
5
|
export abstract class DatabaseQuery<T> implements Query<DatabaseQueryable, T> {
|
|
6
6
|
abstract fetch(queryable: DatabaseQueryable): Promise<T>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Queryable, QueryHandler, QueryHandlerAccessor } from '@contember/queryable'
|
|
2
|
-
import { Client, SelectBuilder } from '../'
|
|
2
|
+
import { Client, SelectBuilder } from '../index.js'
|
|
3
3
|
|
|
4
4
|
export class DatabaseQueryable implements Queryable<DatabaseQueryable> {
|
|
5
5
|
constructor(public readonly db: Client, private readonly handlerAccessor: QueryHandlerAccessor<DatabaseQueryable>) {}
|
package/src/queryable/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './DatabaseQuery'
|
|
2
|
-
export * from './DatabaseQueryable'
|
|
1
|
+
export * from './DatabaseQuery.js'
|
|
2
|
+
export * from './DatabaseQueryable.js'
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Client } from '../client'
|
|
2
|
-
import { wrapIdentifier } from './sql'
|
|
3
|
-
import { DatabaseMetadata } from '../metadata'
|
|
1
|
+
import { Client } from '../client/index.js'
|
|
2
|
+
import { wrapIdentifier } from './sql.js'
|
|
3
|
+
import { DatabaseMetadata } from '../metadata/index.js'
|
|
4
4
|
|
|
5
5
|
export type ConstraintType = 'foreignKey' | 'unique'
|
|
6
6
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A counting semaphore with a FIFO waiting queue.
|
|
3
|
+
*
|
|
4
|
+
* Limits the number of concurrently running operations to a fixed number of slots.
|
|
5
|
+
* Callers acquire a slot via {@link Semaphore.execute}; if all slots are taken, the
|
|
6
|
+
* caller waits until a slot is released.
|
|
7
|
+
*/
|
|
8
|
+
export class Semaphore {
|
|
9
|
+
private available: number
|
|
10
|
+
|
|
11
|
+
private readonly queue: (() => void)[] = []
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
private readonly maxConcurrency: number,
|
|
15
|
+
) {
|
|
16
|
+
if (maxConcurrency < 1) {
|
|
17
|
+
throw new Error('Semaphore concurrency must be at least 1')
|
|
18
|
+
}
|
|
19
|
+
this.available = maxConcurrency
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public async execute<T>(cb: () => T | Promise<T>): Promise<T> {
|
|
23
|
+
await this.acquire()
|
|
24
|
+
try {
|
|
25
|
+
return await cb()
|
|
26
|
+
} finally {
|
|
27
|
+
this.release()
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private acquire(): Promise<void> {
|
|
32
|
+
if (this.available > 0) {
|
|
33
|
+
this.available--
|
|
34
|
+
return Promise.resolve()
|
|
35
|
+
}
|
|
36
|
+
return new Promise<void>(resolve => {
|
|
37
|
+
this.queue.push(resolve)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private release(): void {
|
|
42
|
+
const next = this.queue.shift()
|
|
43
|
+
if (next) {
|
|
44
|
+
next()
|
|
45
|
+
} else {
|
|
46
|
+
this.available++
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DatabaseConfig } from '../types'
|
|
2
|
-
import { ClientError, ClientErrorCodes, Connection } from '../client'
|
|
3
|
-
import { wrapIdentifier } from './sql'
|
|
1
|
+
import { DatabaseConfig } from '../types.js'
|
|
2
|
+
import { ClientError, ClientErrorCodes, Connection } from '../client/index.js'
|
|
3
|
+
import { wrapIdentifier } from './sql.js'
|
|
4
4
|
|
|
5
5
|
export const createDatabaseIfNotExists = async (db: DatabaseConfig, log: (message: string | Error) => void) => {
|
|
6
6
|
try {
|
package/src/utils/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export * from './assertNever'
|
|
2
|
-
export * from './asyncIterableTransaction'
|
|
3
|
-
export * from './ConstraintHelper'
|
|
4
|
-
export * from './createDatabaseIfNotExists'
|
|
5
|
-
export * from './listen'
|
|
6
|
-
export * from './Mutex'
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './
|
|
10
|
-
export * from './
|
|
1
|
+
export * from './assertNever.js'
|
|
2
|
+
export * from './asyncIterableTransaction.js'
|
|
3
|
+
export * from './ConstraintHelper.js'
|
|
4
|
+
export * from './createDatabaseIfNotExists.js'
|
|
5
|
+
export * from './listen.js'
|
|
6
|
+
export * from './Mutex.js'
|
|
7
|
+
export * from './Semaphore.js'
|
|
8
|
+
export * from './sql.js'
|
|
9
|
+
export * from './retryTransaction.js'
|
|
10
|
+
export * from './withDatabaseAdvisoryLock.js'
|
|
11
|
+
export * from './pgClientFactory.js'
|
package/src/utils/listen.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import pg from 'pg'
|
|
2
|
-
import { DatabaseConfig } from '../types'
|
|
3
|
-
import { PgClient } from '../client/PgClient'
|
|
2
|
+
import { DatabaseConfig } from '../types.js'
|
|
3
|
+
import { PgClient } from '../client/PgClient.js'
|
|
4
4
|
|
|
5
5
|
export type PgClientFactory = () => PgClient
|
|
6
|
-
export const createPgClientFactory =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
export const createPgClientFactory =
|
|
7
|
+
({ queryTimeoutMs, statementTimeoutMs, lockTimeoutMs, connectionTimeoutMs, ...config }: DatabaseConfig) => () => {
|
|
8
|
+
// `lock_timeout` is supported by pg at runtime but missing from the installed @types/pg version,
|
|
9
|
+
// so it is typed explicitly here.
|
|
10
|
+
const clientConfig: pg.ClientConfig & { lock_timeout?: number } = {
|
|
11
|
+
query_timeout: queryTimeoutMs,
|
|
12
|
+
statement_timeout: statementTimeoutMs,
|
|
13
|
+
lock_timeout: lockTimeoutMs,
|
|
14
|
+
connectionTimeoutMillis: connectionTimeoutMs,
|
|
15
|
+
...config,
|
|
16
|
+
}
|
|
17
|
+
return new pg.Client(clientConfig)
|
|
18
|
+
}
|
package/src/utils/sql.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { expect, it } from 'bun:test'
|
|
2
|
-
import { MutexDeadlockError } from '../../../src/utils'
|
|
3
|
-
import { createConnectionMockAlt } from './createConnectionMockAlt'
|
|
2
|
+
import { MutexDeadlockError } from '../../../src/utils/index.js'
|
|
3
|
+
import { createConnectionMockAlt } from './createConnectionMockAlt.js'
|
|
4
4
|
|
|
5
5
|
it('support nested scope', async () => {
|
|
6
6
|
const [connection, end] = createConnectionMockAlt(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Connection, Pool } from '../../../src'
|
|
2
|
-
import { PgClient } from '../../../src/client/PgClient'
|
|
1
|
+
import { Connection, Pool } from '../../../src/index.js'
|
|
2
|
+
import { PgClient } from '../../../src/client/PgClient.js'
|
|
3
3
|
import EventEmitter from 'node:events'
|
|
4
4
|
import { expect } from 'bun:test'
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { EventManager } from '../../../src'
|
|
1
|
+
import { EventManager } from '../../../src/index.js'
|
|
2
2
|
import { expect, test } from 'bun:test'
|
|
3
|
-
import { createConnectionMockAlt } from './createConnectionMockAlt'
|
|
3
|
+
import { createConnectionMockAlt } from './createConnectionMockAlt.js'
|
|
4
4
|
|
|
5
5
|
test('event manager: connection and client', async () => {
|
|
6
6
|
const [connection, end] = createConnectionMockAlt([
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { beforeAll, expect, it } from 'bun:test'
|
|
2
|
-
import { ClientErrorCodes, Pool, PoolLogger } from '../../../src'
|
|
2
|
+
import { ClientErrorCodes, Pool, PoolLogger } from '../../../src/index.js'
|
|
3
3
|
import { Client as PgClient } from 'pg'
|
|
4
4
|
import EventEmitter from 'node:events'
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Client, ConflictActionType, EventManager, LimitByGroupWrapper, LockType, Operator } from '../../../src'
|
|
1
|
+
import { Client, ConflictActionType, EventManager, LimitByGroupWrapper, LockType, Operator } from '../../../src/index.js'
|
|
2
2
|
import { createConnectionMock } from '@contember/database-tester'
|
|
3
|
-
import { SQL } from '../../src/tags'
|
|
3
|
+
import { SQL } from '../../src/tags.js'
|
|
4
4
|
import { test } from 'bun:test'
|
|
5
5
|
|
|
6
6
|
interface Test {
|
|
@@ -43,6 +43,8 @@ test('query builder: constructs condition', async () => {
|
|
|
43
43
|
.compare('j', Operator.containsCI, 'X')
|
|
44
44
|
.compare('k', Operator.startsWithCI, 'Y')
|
|
45
45
|
.compare('l', Operator.endsWithCI, 'Z')
|
|
46
|
+
.compare('m2', Operator.similar, 'hello')
|
|
47
|
+
.compare('m3', Operator.wordSimilar, 'world')
|
|
46
48
|
.compareColumns('z', Operator.eq, ['foo', 'x'])
|
|
47
49
|
.in('o', [1, 2, 3])
|
|
48
50
|
.in('x', values, 'int')
|
|
@@ -59,8 +61,10 @@ test('query builder: constructs condition', async () => {
|
|
|
59
61
|
where "a" = ? and "b" != ? and "c" < ? and "d" <= ? and "e" > ? and "f" >= ?
|
|
60
62
|
and "g" like '%' || ? || '%' and "h" like ? || '%' and "i" like '%' || ?
|
|
61
63
|
and "j" ilike '%' || ? || '%' and "k" ilike ? || '%' and "l" ilike '%' || ?
|
|
64
|
+
and "m2" % ?
|
|
65
|
+
and ? <% "m3"
|
|
62
66
|
and "z" = "foo"."x" and "o" in (?, ?, ?) and "x" = any(?::int[]) and "m" in (select ?) and exists (select ?::int) and "n" is null and false`,
|
|
63
|
-
parameters: [1, 2, 3, 4, 5, 6, 'foo\\\\\\%bar', 'lorem\\_ipsum', 'dolor\\%sit', 'X', 'Y', 'Z', 1, 2, 3, values, 1, 1],
|
|
67
|
+
parameters: [1, 2, 3, 4, 5, 6, 'foo\\\\\\%bar', 'lorem\\_ipsum', 'dolor\\%sit', 'X', 'Y', 'Z', 'hello', 'world', 1, 2, 3, values, 1, 1],
|
|
64
68
|
})
|
|
65
69
|
})
|
|
66
70
|
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { expect, it } from 'bun:test'
|
|
2
|
+
import { Connection, Pool } from '../../../src/index.js'
|
|
3
|
+
import { PgClient } from '../../../src/client/PgClient.js'
|
|
4
|
+
import EventEmitter from 'node:events'
|
|
5
|
+
|
|
6
|
+
const timeout = async (ms = 1) => await new Promise<void>(resolve => setTimeout(resolve, ms))
|
|
7
|
+
|
|
8
|
+
const createConnection = (): Connection => {
|
|
9
|
+
const pool = new Pool(() => {
|
|
10
|
+
return new class extends EventEmitter {
|
|
11
|
+
connect() {
|
|
12
|
+
return Promise.resolve()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
end() {
|
|
16
|
+
return Promise.resolve()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async query() {
|
|
20
|
+
await timeout(5)
|
|
21
|
+
return { rows: [], rowCount: 0 }
|
|
22
|
+
}
|
|
23
|
+
}() as unknown as PgClient
|
|
24
|
+
}, { maxConnections: 10, logError: () => null })
|
|
25
|
+
return new Connection(pool)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A connection whose query() can be told to throw, used to verify the slot is released on error/rollback.
|
|
29
|
+
const createFailingConnection = (shouldThrow: (sql: string) => boolean): Connection => {
|
|
30
|
+
const pool = new Pool(() => {
|
|
31
|
+
return new class extends EventEmitter {
|
|
32
|
+
connect() {
|
|
33
|
+
return Promise.resolve()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
end() {
|
|
37
|
+
return Promise.resolve()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async query(sql: string) {
|
|
41
|
+
await timeout(1)
|
|
42
|
+
if (shouldThrow(sql)) {
|
|
43
|
+
throw new Error('boom')
|
|
44
|
+
}
|
|
45
|
+
return { rows: [], rowCount: 0, command: sql === 'COMMIT' ? 'COMMIT' : undefined }
|
|
46
|
+
}
|
|
47
|
+
}() as unknown as PgClient
|
|
48
|
+
}, { maxConnections: 10, logError: () => null })
|
|
49
|
+
return new Connection(pool)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
it('limits concurrent connections per scope', async () => {
|
|
53
|
+
const connection = createConnection()
|
|
54
|
+
const limited = connection.withMaxConnections(2)
|
|
55
|
+
|
|
56
|
+
let active = 0
|
|
57
|
+
let maxActive = 0
|
|
58
|
+
|
|
59
|
+
const work = async () => {
|
|
60
|
+
await limited.scope(async c => {
|
|
61
|
+
active++
|
|
62
|
+
maxActive = Math.max(maxActive, active)
|
|
63
|
+
await c.query('SELECT 1')
|
|
64
|
+
active--
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await Promise.all([work(), work(), work(), work(), work()])
|
|
69
|
+
|
|
70
|
+
expect(maxActive).toBe(2)
|
|
71
|
+
await connection.end()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('does not limit when used without a wrapper', async () => {
|
|
75
|
+
const connection = createConnection()
|
|
76
|
+
|
|
77
|
+
let active = 0
|
|
78
|
+
let maxActive = 0
|
|
79
|
+
|
|
80
|
+
const work = async () => {
|
|
81
|
+
await connection.scope(async c => {
|
|
82
|
+
active++
|
|
83
|
+
maxActive = Math.max(maxActive, active)
|
|
84
|
+
await c.query('SELECT 1')
|
|
85
|
+
active--
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await Promise.all([work(), work(), work(), work(), work()])
|
|
90
|
+
|
|
91
|
+
expect(maxActive).toBe(5)
|
|
92
|
+
await connection.end()
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('nested scopes do not count against the limit (no deadlock)', async () => {
|
|
96
|
+
const connection = createConnection()
|
|
97
|
+
const limited = connection.withMaxConnections(1)
|
|
98
|
+
|
|
99
|
+
const result = await limited.scope(async c1 => {
|
|
100
|
+
// nested scope reuses the already-acquired connection, so it must not block
|
|
101
|
+
return await c1.scope(async c2 => {
|
|
102
|
+
await c2.query('SELECT 1')
|
|
103
|
+
return 'ok'
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
expect(result).toBe('ok')
|
|
108
|
+
await connection.end()
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('queues work beyond the limit and eventually completes all', async () => {
|
|
112
|
+
const connection = createConnection()
|
|
113
|
+
const limited = connection.withMaxConnections(2)
|
|
114
|
+
|
|
115
|
+
let completed = 0
|
|
116
|
+
const work = async () => {
|
|
117
|
+
await limited.query('SELECT 1')
|
|
118
|
+
completed++
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
await Promise.all(Array.from({ length: 6 }, () => work()))
|
|
122
|
+
|
|
123
|
+
expect(completed).toBe(6)
|
|
124
|
+
await connection.end()
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('the (limit+1)th acquisition waits until a slot is released', async () => {
|
|
128
|
+
const connection = createConnection()
|
|
129
|
+
const limited = connection.withMaxConnections(1)
|
|
130
|
+
|
|
131
|
+
let firstReleased = false
|
|
132
|
+
let secondStartedBeforeFirstReleased = false
|
|
133
|
+
|
|
134
|
+
// hold the only slot open until we explicitly let go
|
|
135
|
+
let releaseFirst!: () => void
|
|
136
|
+
const firstGate = new Promise<void>(resolve => {
|
|
137
|
+
releaseFirst = resolve
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
const first = limited.scope(async () => {
|
|
141
|
+
await firstGate
|
|
142
|
+
firstReleased = true
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
const second = limited.scope(async () => {
|
|
146
|
+
// if the cap worked, the first slot was already released by the time we get here
|
|
147
|
+
if (!firstReleased) {
|
|
148
|
+
secondStartedBeforeFirstReleased = true
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
// give the event loop a chance to (incorrectly) start the second scope
|
|
153
|
+
await timeout(5)
|
|
154
|
+
expect(firstReleased).toBe(false)
|
|
155
|
+
|
|
156
|
+
releaseFirst()
|
|
157
|
+
await Promise.all([first, second])
|
|
158
|
+
|
|
159
|
+
expect(secondStartedBeforeFirstReleased).toBe(false)
|
|
160
|
+
await connection.end()
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('releases the slot when the scope callback throws', async () => {
|
|
164
|
+
const connection = createConnection()
|
|
165
|
+
const limited = connection.withMaxConnections(1)
|
|
166
|
+
|
|
167
|
+
await expect(limited.scope(async () => {
|
|
168
|
+
throw new Error('boom')
|
|
169
|
+
})).rejects.toThrow('boom')
|
|
170
|
+
|
|
171
|
+
// if the slot had leaked, this would hang until the test times out
|
|
172
|
+
const result = await limited.scope(async () => 'ok')
|
|
173
|
+
expect(result).toBe('ok')
|
|
174
|
+
await connection.end()
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('releases the slot when a query inside the scope throws', async () => {
|
|
178
|
+
const connection = createFailingConnection(sql => sql === 'FAIL')
|
|
179
|
+
const limited = connection.withMaxConnections(1)
|
|
180
|
+
|
|
181
|
+
await expect(limited.query('FAIL')).rejects.toThrow('boom')
|
|
182
|
+
|
|
183
|
+
const result = await limited.query('SELECT 1')
|
|
184
|
+
expect(result.rowCount).toBe(0)
|
|
185
|
+
await connection.end()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it('releases the slot when a transaction rolls back', async () => {
|
|
189
|
+
// fail the statement inside the transaction so executeTransaction issues a ROLLBACK and rethrows
|
|
190
|
+
const connection = createFailingConnection(sql => sql === 'FAIL')
|
|
191
|
+
const limited = connection.withMaxConnections(1)
|
|
192
|
+
|
|
193
|
+
await expect(limited.transaction(async trx => {
|
|
194
|
+
await trx.query('FAIL')
|
|
195
|
+
})).rejects.toThrow('boom')
|
|
196
|
+
|
|
197
|
+
// the slot (and the only allowed connection) must be free again for the next request
|
|
198
|
+
const result = await limited.transaction(async trx => {
|
|
199
|
+
await trx.query('SELECT 1')
|
|
200
|
+
return 'ok'
|
|
201
|
+
})
|
|
202
|
+
expect(result).toBe('ok')
|
|
203
|
+
await connection.end()
|
|
204
|
+
})
|