File tree 5 files changed +23
-17
lines changed
5 files changed +23
-17
lines changed Original file line number Diff line number Diff line change 1
- import { postgresClient } from '../postgres' ;
1
+ import { postgresClient , type PongoClientOptions } from '../postgres' ;
2
2
import type { PongoCollection } from './typing/operations' ;
3
3
4
4
export interface DbClient {
@@ -7,10 +7,7 @@ export interface DbClient {
7
7
collection : < T > ( name : string ) => PongoCollection < T > ;
8
8
}
9
9
10
- export const getDbClient = (
11
- connectionString : string ,
12
- database ?: string ,
13
- ) : DbClient => {
10
+ export const getDbClient = ( options : PongoClientOptions ) : DbClient => {
14
11
// This is the place where in the future could come resolution of other database types
15
- return postgresClient ( connectionString , database ) ;
12
+ return postgresClient ( options ) ;
16
13
} ;
Original file line number Diff line number Diff line change 1
- export * from './client' ;
2
1
export * from './dbClient' ;
2
+ export * from './pongoClient' ;
3
3
export * from './typing' ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
6
6
const defaultDbName = getDatabaseNameOrDefault ( connectionString ) ;
7
7
const dbClients : Map < string , DbClient > = new Map ( ) ;
8
8
9
- const dbClient = getDbClient ( connectionString ) ;
9
+ const dbClient = getDbClient ( { connectionString } ) ;
10
10
dbClients . set ( defaultDbName , dbClient ) ;
11
11
12
12
const pongoClient : PongoClient = {
@@ -25,7 +25,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
25
25
return (
26
26
dbClients . get ( dbName ) ??
27
27
dbClients
28
- . set ( dbName , getDbClient ( connectionString , dbName ) )
28
+ . set ( dbName , getDbClient ( { connectionString, database : dbName } ) )
29
29
. get ( dbName ) !
30
30
) ;
31
31
} ,
Original file line number Diff line number Diff line change 1
1
import { endPool , getPool } from '@event-driven-io/dumbo' ;
2
+ import pg from 'pg' ;
2
3
import { type DbClient } from '../main' ;
3
4
import { postgresCollection } from './postgresCollection' ;
4
5
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 } ) ;
10
16
11
17
return {
12
18
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 ) ,
15
24
} ;
16
25
} ;
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ import { buildUpdateQuery } from './update';
17
17
18
18
export const postgresCollection = < T > (
19
19
collectionName : string ,
20
- pool : pg . Pool ,
20
+ pool : pg . Pool | pg . PoolClient ,
21
21
) : PongoCollection < T > => {
22
22
const execute = ( sql : SQL ) => executeSQL ( pool , sql ) ;
23
23
const SqlFor = collectionSQLBuilder ( collectionName ) ;
You can’t perform that action at this time.
0 commit comments