Socket
Socket
Sign inDemoInstall

slonik

Package Overview
Dependencies
Maintainers
1
Versions
395
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slonik - npm Package Compare versions

Comparing version 14.9.0 to 14.10.0

47

dist/binders/bindPool.js

@@ -25,5 +25,20 @@ "use strict";

const bindPool = (parentLog, pool, clientConfiguration) => {
const connect = async connectionRoutine => {
const poolId = getPoolId(parentLog);
const internalConnect = async (connectionRoutine, query = null) => {
for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.beforePoolConnection) {
const maybeNewPool = await interceptor.beforePoolConnection({
log: parentLog,
poolId,
query
});
if (maybeNewPool) {
return maybeNewPool.connect(connectionRoutine);
}
}
}
const connection = await pool.connect();
const poolId = getPoolId(parentLog);
const connectionId = connection.connection.slonik.connectionId;

@@ -40,6 +55,11 @@ const connectionLog = parentLog.child({

for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.afterPoolConnection) {
await interceptor.afterPoolConnection(connectionContext, boundConnection);
try {
for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.afterPoolConnection) {
await interceptor.afterPoolConnection(connectionContext, boundConnection);
}
}
} catch (error) {
await connection.release();
throw error;
}

@@ -51,3 +71,8 @@

result = await connectionRoutine(boundConnection);
} finally {
} catch (error) {
await connection.release();
throw error;
}
try {
for (const interceptor of clientConfiguration.interceptors) {

@@ -58,3 +83,3 @@ if (interceptor.beforePoolConnectionRelease) {

}
} finally {
await connection.release();

@@ -72,5 +97,5 @@ }

return connect(connection => {
return internalConnect(connection => {
return connection[targetMethodName](query);
});
}, query);
};

@@ -82,3 +107,5 @@ };

anyFirst: mapConnection('anyFirst'),
connect,
connect: connectionRoutine => {
return internalConnect(connectionRoutine);
},
many: mapConnection('many'),

@@ -85,0 +112,0 @@ manyFirst: mapConnection('manyFirst'),

@@ -97,3 +97,3 @@ {

},
"version": "14.9.0"
"version": "14.10.0"
}

@@ -331,2 +331,3 @@ <a name="slonik"></a>

* [Using `sql.raw` to generate dynamic queries](#slonik-recipes-using-sql-raw-to-generate-dynamic-queries)
* [Routing queries to different connections](#slonik-recipes-routing-queries-to-different-connections)
* [Conventions](#slonik-conventions)

@@ -547,2 +548,5 @@ * [No multiline values](#slonik-conventions-no-multiline-values)

) => MaybePromiseType<QueryResultType<QueryResultRowType>>,
+beforePoolConnection?: (
connectionContext: ConnectionContextType
) => MaybePromiseType<?DatabasePoolType>,
+beforePoolConnectionRelease?: (

@@ -903,3 +907,3 @@ connectionContext: ConnectionContextType,

```js
const pool = await createPool('postgres://localhost', {
const pool = createPool('postgres://localhost', {
interceptors: [

@@ -1018,3 +1022,32 @@ {

<a name="slonik-recipes-routing-queries-to-different-connections"></a>
### Routing queries to different connections
If connection is initiated by a query (as opposed to a obtained explicitly using `pool#connect()`), then `beforePoolConnection` interceptor can be used to change the pool that will be used to execute the query, e.g.
```js
const slavePool = createPool('postgres://slave');
const masterPool = createPool('postgres://master', {
interceptors: [
{
beforePoolConnection: (connectionContext, pool) => {
if (connectionContext.query && connectionContext.query.sql.includes('SELECT')) {
return slavePool;
}
return pool;
}
}
]
});
// This query will use `postgres://slave` connection..
masterPool.query(sql`SELECT 1`);
// This query will use `postgres://master` connection.
masterPool.query(sql`UPDATE 1`);
```
<a name="slonik-conventions"></a>

@@ -1021,0 +1054,0 @@ ## Conventions

@@ -31,7 +31,21 @@ // @flow

): DatabasePoolType => {
const connect = async (connectionRoutine) => {
const poolId = getPoolId(parentLog);
const internalConnect = async (connectionRoutine, query = null) => {
for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.beforePoolConnection) {
const maybeNewPool = await interceptor.beforePoolConnection({
log: parentLog,
poolId,
query
});
if (maybeNewPool) {
return maybeNewPool.connect(connectionRoutine);
}
}
}
const connection: InternalDatabaseConnectionType = await pool.connect();
const poolId = getPoolId(parentLog);
const connectionId = connection.connection.slonik.connectionId;

@@ -51,6 +65,12 @@

for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.afterPoolConnection) {
await interceptor.afterPoolConnection(connectionContext, boundConnection);
try {
for (const interceptor of clientConfiguration.interceptors) {
if (interceptor.afterPoolConnection) {
await interceptor.afterPoolConnection(connectionContext, boundConnection);
}
}
} catch (error) {
await connection.release();
throw error;
}

@@ -62,3 +82,9 @@

result = await connectionRoutine(boundConnection);
} finally {
} catch (error) {
await connection.release();
throw error;
}
try {
for (const interceptor of clientConfiguration.interceptors) {

@@ -69,3 +95,3 @@ if (interceptor.beforePoolConnectionRelease) {

}
} finally {
await connection.release();

@@ -83,5 +109,5 @@ }

return connect((connection) => {
return internalConnect((connection) => {
return connection[targetMethodName](query);
});
}, query);
};

@@ -93,3 +119,5 @@ };

anyFirst: mapConnection('anyFirst'),
connect,
connect: (connectionRoutine) => {
return internalConnect(connectionRoutine);
},
many: mapConnection('many'),

@@ -96,0 +124,0 @@ manyFirst: mapConnection('manyFirst'),

@@ -125,2 +125,13 @@ // @flow

/**
* @property log Instance of Roarr logger with bound connection context parameters.
* @property poolId Unique connection pool ID.
* @property query The query that is initiating the connection.
*/
export type PoolContextType = {|
+log: LoggerType,
+poolId: string,
+query: TaggedTemplateLiteralInvocationType | null
|};
/**
* @property connectionId Unique connection ID.

@@ -300,2 +311,5 @@ * @property log Instance of Roarr logger with bound connection context parameters.

) => MaybePromiseType<QueryResultType<QueryResultRowType>>,
+beforePoolConnection?: (
connectionContext: PoolContextType
) => MaybePromiseType<?DatabasePoolType>,
+beforePoolConnectionRelease?: (

@@ -302,0 +316,0 @@ connectionContext: ConnectionContextType,

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc