Skip to content

Commit 2cb105a

Browse files
committed
Added SQL exectuor to PongoDB and listing currently known Pongo Collections
1 parent b05f30c commit 2cb105a

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/packages/pongo/src/core/collection/pongoCollection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const enlistIntoTransactionIfActive = async <
6666
return await transaction.enlistDatabase(db);
6767
};
6868

69-
const transactionExecutorOrDefault = async <
69+
export const transactionExecutorOrDefault = async <
7070
ConnectorType extends string = string,
7171
>(
7272
db: PongoDb<ConnectorType>,
@@ -467,7 +467,7 @@ export const pongoCollection = <
467467
): Promise<Result[]> {
468468
await ensureCollectionCreated(options);
469469

470-
const result = await query<Result>(sql);
470+
const result = await query<Result>(sql, options);
471471
return result.rows;
472472
},
473473
async command<Result extends QueryResultRow = QueryResultRow>(
@@ -476,7 +476,7 @@ export const pongoCollection = <
476476
): Promise<QueryResult<Result>> {
477477
await ensureCollectionCreated(options);
478478

479-
return command(sql);
479+
return command(sql, options);
480480
},
481481
},
482482
schema: {

src/packages/pongo/src/core/typing/operations.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,21 @@ export interface PongoDb<ConnectorType extends string = string>
6767
connect(): Promise<void>;
6868
close(): Promise<void>;
6969
collection<T extends PongoDocument>(name: string): PongoCollection<T>;
70+
collections(): ReadonlyArray<PongoCollection<PongoDocument>>;
7071
readonly schema: Readonly<{
7172
component: SchemaComponent;
7273
migrate(): Promise<void>;
7374
}>;
75+
sql: {
76+
query<Result extends QueryResultRow = QueryResultRow>(
77+
sql: SQL,
78+
options?: CollectionOperationOptions,
79+
): Promise<Result[]>;
80+
command<Result extends QueryResultRow = QueryResultRow>(
81+
sql: SQL,
82+
options?: CollectionOperationOptions,
83+
): Promise<QueryResult<Result>>;
84+
};
7485
}
7586

7687
export type CollectionOperationOptions = {

src/packages/pongo/src/postgres/dbClient.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
NodePostgresConnectorType,
55
runPostgreSQLMigrations,
66
schemaComponent,
7+
SQL,
78
type PostgresConnector,
89
type PostgresPoolOptions,
10+
type QueryResult,
11+
type QueryResultRow,
912
type SchemaComponent,
1013
} from '@event-driven-io/dumbo';
1114
import type { Document } from 'mongodb';
@@ -14,6 +17,8 @@ import {
1417
pongoCollection,
1518
pongoCollectionSchemaComponent,
1619
proxyPongoDbWithSchema,
20+
transactionExecutorOrDefault,
21+
type CollectionOperationOptions,
1722
type PongoCollection,
1823
type PongoDb,
1924
type PongoDbClientOptions,
@@ -40,11 +45,29 @@ export const postgresDb = (
4045

4146
const collections = new Map<string, PongoCollection<Document>>();
4247

48+
const command = async <Result extends QueryResultRow = QueryResultRow>(
49+
sql: SQL,
50+
options?: CollectionOperationOptions,
51+
) =>
52+
(
53+
await transactionExecutorOrDefault(db, options, pool.execute)
54+
).command<Result>(sql);
55+
56+
const query = async <T extends QueryResultRow>(
57+
sql: SQL,
58+
options?: CollectionOperationOptions,
59+
) =>
60+
(await transactionExecutorOrDefault(db, options, pool.execute)).query<T>(
61+
sql,
62+
);
63+
4364
const db: PongoDb<PostgresConnector> = {
4465
connectorType: options.connectorType,
4566
databaseName,
4667
connect: () => Promise.resolve(),
4768
close: () => pool.close(),
69+
70+
collections: () => [...collections.values()],
4871
collection: (collectionName) =>
4972
pongoCollection({
5073
collectionName,
@@ -72,6 +95,22 @@ export const postgresDb = (
7295
),
7396
),
7497
},
98+
99+
sql: {
100+
async query<Result extends QueryResultRow = QueryResultRow>(
101+
sql: SQL,
102+
options?: CollectionOperationOptions,
103+
): Promise<Result[]> {
104+
const result = await query<Result>(sql, options);
105+
return result.rows;
106+
},
107+
async command<Result extends QueryResultRow = QueryResultRow>(
108+
sql: SQL,
109+
options?: CollectionOperationOptions,
110+
): Promise<QueryResult<Result>> {
111+
return command(sql, options);
112+
},
113+
},
75114
};
76115

77116
const dbsSchema = options?.schema?.definition?.dbs;

0 commit comments

Comments
 (0)