Skip to content

Commit 8cef6b5

Browse files
committed
Added option to inject PoolClient to PongoClient
That will enable running transactions and sharing connection with Emmett projections
1 parent 27fb123 commit 8cef6b5

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed
+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { postgresClient } from '../postgres';
1+
import { postgresClient, type PongoClientOptions } from '../postgres';
22
import type { PongoCollection } from './typing/operations';
33

44
export interface DbClient {
@@ -7,10 +7,7 @@ export interface DbClient {
77
collection: <T>(name: string) => PongoCollection<T>;
88
}
99

10-
export const getDbClient = (
11-
connectionString: string,
12-
database?: string,
13-
): DbClient => {
10+
export const getDbClient = (options: PongoClientOptions): DbClient => {
1411
// This is the place where in the future could come resolution of other database types
15-
return postgresClient(connectionString, database);
12+
return postgresClient(options);
1613
};

src/packages/pongo/src/main/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './client';
21
export * from './dbClient';
2+
export * from './pongoClient';
33
export * from './typing';

src/packages/pongo/src/main/client.ts src/packages/pongo/src/main/pongoClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
66
const defaultDbName = getDatabaseNameOrDefault(connectionString);
77
const dbClients: Map<string, DbClient> = new Map();
88

9-
const dbClient = getDbClient(connectionString);
9+
const dbClient = getDbClient({ connectionString });
1010
dbClients.set(defaultDbName, dbClient);
1111

1212
const pongoClient: PongoClient = {
@@ -25,7 +25,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
2525
return (
2626
dbClients.get(dbName) ??
2727
dbClients
28-
.set(dbName, getDbClient(connectionString, dbName))
28+
.set(dbName, getDbClient({ connectionString, database: dbName }))
2929
.get(dbName)!
3030
);
3131
},
+16-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { endPool, getPool } from '@event-driven-io/dumbo';
2+
import pg from 'pg';
23
import { type DbClient } from '../main';
34
import { postgresCollection } from './postgresCollection';
45

5-
export const postgresClient = (
6-
connectionString: string,
7-
database?: string,
8-
): DbClient => {
9-
const pool = getPool({ connectionString, database });
6+
export type PongoClientOptions = {
7+
connectionString: string;
8+
database?: string | undefined;
9+
client?: pg.PoolClient;
10+
};
11+
12+
export const postgresClient = (options: PongoClientOptions): DbClient => {
13+
const { connectionString, database, client } = options;
14+
const managesPoolLifetime = !client;
15+
const clientOrPool = client ?? getPool({ connectionString, database });
1016

1117
return {
1218
connect: () => Promise.resolve(),
13-
close: () => endPool({ connectionString, database }),
14-
collection: <T>(name: string) => postgresCollection<T>(name, pool),
19+
close: () =>
20+
managesPoolLifetime
21+
? endPool({ connectionString, database })
22+
: Promise.resolve(),
23+
collection: <T>(name: string) => postgresCollection<T>(name, clientOrPool),
1524
};
1625
};

src/packages/pongo/src/postgres/postgresCollection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { buildUpdateQuery } from './update';
1717

1818
export const postgresCollection = <T>(
1919
collectionName: string,
20-
pool: pg.Pool,
20+
pool: pg.Pool | pg.PoolClient,
2121
): PongoCollection<T> => {
2222
const execute = (sql: SQL) => executeSQL(pool, sql);
2323
const SqlFor = collectionSQLBuilder(collectionName);

0 commit comments

Comments
 (0)