Skip to content

Commit

Permalink
Added option to inject PoolClient to PongoClient
Browse files Browse the repository at this point in the history
That will enable running transactions and sharing connection with Emmett projections
  • Loading branch information
oskardudycz committed Jul 12, 2024
1 parent 27669bd commit de94c41
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
9 changes: 3 additions & 6 deletions src/packages/pongo/src/main/dbClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { postgresClient } from '../postgres';
import { postgresClient, type PongoClientOptions } from '../postgres';
import type { PongoCollection } from './typing/operations';

export interface DbClient {
Expand All @@ -7,10 +7,7 @@ export interface DbClient {
collection: <T>(name: string) => PongoCollection<T>;
}

export const getDbClient = (
connectionString: string,
database?: string,
): DbClient => {
export const getDbClient = (options: PongoClientOptions): DbClient => {
// This is the place where in the future could come resolution of other database types
return postgresClient(connectionString, database);
return postgresClient(options);
};
2 changes: 1 addition & 1 deletion src/packages/pongo/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './client';
export * from './dbClient';
export * from './pongoClient';
export * from './typing';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
const defaultDbName = getDatabaseNameOrDefault(connectionString);
const dbClients: Map<string, DbClient> = new Map();

const dbClient = getDbClient(connectionString);
const dbClient = getDbClient({ connectionString });
dbClients.set(defaultDbName, dbClient);

const pongoClient: PongoClient = {
Expand All @@ -25,7 +25,7 @@ export const pongoClient = (connectionString: string): PongoClient => {
return (
dbClients.get(dbName) ??
dbClients
.set(dbName, getDbClient(connectionString, dbName))
.set(dbName, getDbClient({ connectionString, database: dbName }))
.get(dbName)!
);
},
Expand Down
23 changes: 16 additions & 7 deletions src/packages/pongo/src/postgres/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { endPool, getPool } from '@event-driven-io/dumbo';
import pg from 'pg';
import { type DbClient } from '../main';
import { postgresCollection } from './postgresCollection';

export const postgresClient = (
connectionString: string,
database?: string,
): DbClient => {
const pool = getPool({ connectionString, database });
export type PongoClientOptions = {
connectionString: string;
database?: string | undefined;
client?: pg.PoolClient;
};

export const postgresClient = (options: PongoClientOptions): DbClient => {
const { connectionString, database, client } = options;
const managesPoolLifetime = !client;
const clientOrPool = client ?? getPool({ connectionString, database });

return {
connect: () => Promise.resolve(),
close: () => endPool({ connectionString, database }),
collection: <T>(name: string) => postgresCollection<T>(name, pool),
close: () =>
managesPoolLifetime
? endPool({ connectionString, database })
: Promise.resolve(),
collection: <T>(name: string) => postgresCollection<T>(name, clientOrPool),
};
};
2 changes: 1 addition & 1 deletion src/packages/pongo/src/postgres/postgresCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { buildUpdateQuery } from './update';

export const postgresCollection = <T>(
collectionName: string,
pool: pg.Pool,
pool: pg.Pool | pg.PoolClient,
): PongoCollection<T> => {
const execute = (sql: SQL) => executeSQL(pool, sql);
const SqlFor = collectionSQLBuilder(collectionName);
Expand Down

0 comments on commit de94c41

Please sign in to comment.