@devmoods/express-extras 0.54.0 → 0.54.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.
@@ -1,4 +1,5 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { type RequestHandler } from 'express';
2
3
  import pg, { type PoolClient, type PoolConfig, type QueryResult, type QueryResultRow } from 'pg';
3
4
  import { type SqlStatement } from './sql.js';
4
5
  export declare const escapePostgresIdentifier: typeof pg.escapeIdentifier;
@@ -31,7 +32,7 @@ export declare class Connection {
31
32
  private options;
32
33
  client: PoolClient;
33
34
  constructor(client: PoolClient, options: ConnectionOptions);
34
- private onLogStatement;
35
+ private logStatement;
35
36
  query<T extends QueryResultRow>(text: string | SqlStatement, values?: Value[]): Promise<QueryResult<T>>;
36
37
  all<T extends QueryResultRow>(text: string | SqlStatement, values?: Value[]): Promise<T[]>;
37
38
  insert<T extends QueryResultRow>(text: string | SqlStatement, values?: Value[]): Promise<QueryResult<T>>;
@@ -66,4 +67,5 @@ export declare function createPostgres({ onError, url, ...postgresOptions }?: Po
66
67
  clear: (force?: boolean) => Promise<void>;
67
68
  end(): Promise<void>;
68
69
  };
70
+ export declare function withTransaction(postgres: ReturnType<typeof createPostgres>): RequestHandler;
69
71
  export {};
package/dist/postgres.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import {} from 'express';
2
3
  import pg, {} from 'pg';
3
4
  import { config } from './config.js';
4
5
  import { formatSql } from './sql.js';
@@ -29,7 +30,7 @@ export class Connection {
29
30
  this.options = options;
30
31
  this.client = client;
31
32
  }
32
- onLogStatement(text, values, duration) {
33
+ logStatement(text, values, duration) {
33
34
  if (this.options.onLogStatement) {
34
35
  return this.options.onLogStatement(text, values, duration);
35
36
  }
@@ -49,7 +50,7 @@ export class Connection {
49
50
  throw error;
50
51
  }
51
52
  finally {
52
- this.onLogStatement?.(text, values, timer.getDuration());
53
+ this.logStatement?.(text, values, timer.getDuration());
53
54
  }
54
55
  }
55
56
  async all(text, values) {
@@ -166,11 +167,11 @@ export function createPostgres({ onError, url = config.value.POSTGRES_URL, ...po
166
167
  }
167
168
  // Start a new top-level transaction
168
169
  const client = await pool.connect();
170
+ const txConnection = new Transaction(client, {
171
+ onLogStatement: postgresOptions.onLogStatement,
172
+ });
169
173
  try {
170
- await client.query('BEGIN');
171
- const txConnection = new Transaction(client, {
172
- onLogStatement: postgresOptions.onLogStatement,
173
- });
174
+ await txConnection.query('BEGIN');
174
175
  // Create transaction context
175
176
  const txContext = {
176
177
  connection: txConnection,
@@ -182,12 +183,12 @@ export function createPostgres({ onError, url = config.value.POSTGRES_URL, ...po
182
183
  const result = await transactionStorage.run(txContext, async () => {
183
184
  return await callback();
184
185
  });
185
- await client.query('COMMIT');
186
+ await txConnection.query('COMMIT');
186
187
  txContext.isCommitted = true;
187
188
  return result;
188
189
  }
189
190
  catch (error) {
190
- await client.query('ROLLBACK');
191
+ await txConnection.query('ROLLBACK');
191
192
  throw error;
192
193
  }
193
194
  finally {
@@ -243,3 +244,19 @@ export function createPostgres({ onError, url = config.value.POSTGRES_URL, ...po
243
244
  },
244
245
  };
245
246
  }
247
+ export function withTransaction(postgres) {
248
+ return async (req, res, next) => {
249
+ try {
250
+ await postgres.transaction(async () => {
251
+ await new Promise((resolve, reject) => {
252
+ res.on('finish', resolve);
253
+ res.on('error', reject);
254
+ next();
255
+ });
256
+ });
257
+ }
258
+ catch (error) {
259
+ next(error);
260
+ }
261
+ };
262
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.54.0",
3
+ "version": "0.54.1",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",