Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3882298
Added initial types definition for Pongo Sessions and transactions
oskardudycz Jul 30, 2024
4244779
Added typed DbClient options making the first step towards supporting…
oskardudycz Jul 30, 2024
1ce277e
Added abstractions for connection and transaction handling
oskardudycz Jul 31, 2024
d872d8a
Refactored connection and execution to allow injecting custom impleme…
oskardudycz Jul 31, 2024
5b6372f
Finished the first draft the new connection management
oskardudycz Aug 1, 2024
02f3216
Refactored file structure to reflect database types and providers
oskardudycz Aug 2, 2024
7604a78
Moved query related types and functions to the dedicated folder
oskardudycz Aug 2, 2024
02459dc
Added explicit batch queries and commands
oskardudycz Aug 2, 2024
9116fc5
Added typing for NodePostgres pool options
oskardudycz Aug 2, 2024
db712ad
Generalised pool settings
oskardudycz Aug 2, 2024
02b478b
Added dummy session implementation
oskardudycz Aug 2, 2024
3cd2629
Extended PongoTransaction to have reference to current db and databas…
oskardudycz Aug 2, 2024
01dc3a0
Exposed SQLExecturo that's not aware of client existence
oskardudycz Aug 3, 2024
8ebecb0
Refactored the executor setup
oskardudycz Aug 3, 2024
4e68d09
Simplified the executor creation by using reusable factory with new c…
oskardudycz Aug 3, 2024
15806e0
Renamed existing to ambient in pool setup to make code intention more…
oskardudycz Aug 3, 2024
1a3f9a9
Injected SQLExecutor instead of connection pool to Pongo PostgresColl…
oskardudycz Aug 3, 2024
96ae0f8
Renamed main => core in Pongo to align with introduced in Dumbo conve…
oskardudycz Aug 3, 2024
ccf17fe
Made pongoCollection generic by introducing injectable sql builder
oskardudycz Aug 3, 2024
1a9a9ee
Nested sql building in Pongo postgres provider
oskardudycz Aug 3, 2024
b68edb1
Generalised typing for query operators
oskardudycz Aug 3, 2024
1755469
Made possible to inject Session to collection operations
oskardudycz Aug 3, 2024
11c4036
Used database transaction for Pongo
oskardudycz Aug 3, 2024
12f6e29
Implemented Pongo Transaction handling
oskardudycz Aug 3, 2024
04c24b1
Merged DbClient into PongoDb
oskardudycz Aug 3, 2024
4062dad
Made dumbo types like Connection, Pool, Transaction interfaces
oskardudycz Aug 3, 2024
d5fd6f5
Added and used consistently Pongo connector type across Dumbo and Pongo
oskardudycz Aug 3, 2024
ac438e6
Used transaction from options in Pongo Operations
oskardudycz Aug 3, 2024
e24b870
Added Postgres Pongo e2e tests
oskardudycz Aug 3, 2024
2db3bc6
Fixed commiting transaction
oskardudycz Aug 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/pongo-core",
"version": "0.7.0",
"version": "0.8.0",
"description": "Pongo - Mongo with strong consistency on top of Postgres",
"type": "module",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/dumbo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@event-driven-io/dumbo",
"version": "0.5.0",
"version": "0.6.0",
"description": "Dumbo - tools for dealing with PostgreSQL",
"type": "module",
"scripts": {
Expand Down
19 changes: 0 additions & 19 deletions src/packages/dumbo/src/connections/client.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/packages/dumbo/src/connections/index.ts

This file was deleted.

82 changes: 0 additions & 82 deletions src/packages/dumbo/src/connections/pool.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/packages/dumbo/src/core/connections/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { WithSQLExecutor } from '../execute';
import type { DatabaseTransactionFactory } from './transaction';

export interface Connection<
ConnectorType extends string = string,
DbClient = unknown,
> extends WithSQLExecutor,
DatabaseTransactionFactory<ConnectorType> {
type: ConnectorType;
connect: () => Promise<DbClient>;
close: () => Promise<void>;
}
3 changes: 3 additions & 0 deletions src/packages/dumbo/src/core/connections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './connection';
export * from './pool';
export * from './transaction';
16 changes: 16 additions & 0 deletions src/packages/dumbo/src/core/connections/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { WithSQLExecutor } from '../execute';
import { type Connection } from './connection';
import type { DatabaseTransactionFactory } from './transaction';

export interface ConnectionPool<ConnectionType extends Connection = Connection>
extends WithSQLExecutor,
DatabaseTransactionFactory<ConnectionType['type']> {
type: ConnectionType['type'];
open: () => Promise<ConnectionType>;
close: () => Promise<void>;
}

export type ConnectionPoolProvider<
ConnectionPoolType extends ConnectionPool = ConnectionPool,
ConnectionPoolOptions = unknown,
> = (options: ConnectionPoolOptions) => ConnectionPoolType;
99 changes: 99 additions & 0 deletions src/packages/dumbo/src/core/connections/transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { WithSQLExecutor } from '../execute';
import { type Connection } from './connection';

export interface DatabaseTransaction<ConnectorType extends string = string>
extends WithSQLExecutor {
type: ConnectorType;
begin: () => Promise<void>;
commit: () => Promise<void>;
rollback: (error?: unknown) => Promise<void>;
}

export interface DatabaseTransactionFactory<
ConnectorType extends string = string,
> {
transaction: () => DatabaseTransaction<ConnectorType>;

withTransaction: <Result = unknown>(
handle: (
transaction: DatabaseTransaction<ConnectorType>,
) => Promise<{ success: boolean; result: Result }>,
) => Promise<Result>;
}

export const executeInTransaction = async <
ConnectorType extends string = string,
Result = unknown,
>(
transaction: DatabaseTransaction<ConnectorType>,
handle: (
transaction: DatabaseTransaction<ConnectorType>,
) => Promise<{ success: boolean; result: Result }>,
): Promise<Result> => {
await transaction.begin();

try {
const { success, result } = await handle(transaction);

if (success) await transaction.commit();
else await transaction.rollback();

return result;
} catch (e) {
await transaction.rollback();
throw e;
}
};

export const transactionFactoryWithDbClient = <
ConnectorType extends string = string,
DbClient = unknown,
>(
connect: () => Promise<DbClient>,
initTransaction: (
client: Promise<DbClient>,
) => DatabaseTransaction<ConnectorType>,
): DatabaseTransactionFactory<ConnectorType> => ({
transaction: () => initTransaction(connect()),
withTransaction: (handle) =>
executeInTransaction(initTransaction(connect()), handle),
});

const wrapInConnectionClosure = async <
ConnectionType extends Connection = Connection,
Result = unknown,
>(
connection: ConnectionType,
handle: () => Promise<Result>,
) => {
try {
return await handle();
} finally {
await connection.close();
}
};

export const transactionFactoryWithNewConnection = <
ConnectionType extends Connection = Connection,
>(
connect: () => ConnectionType,
): DatabaseTransactionFactory<ConnectionType['type']> => ({
transaction: () => {
const connection = connect();
const transaction = connection.transaction();

return {
...transaction,
commit: () =>
wrapInConnectionClosure(connection, () => transaction.commit()),
rollback: () =>
wrapInConnectionClosure(connection, () => transaction.rollback()),
};
},
withTransaction: (handle) => {
const connection = connect();
return wrapInConnectionClosure(connection, () =>
connection.withTransaction(handle),
);
},
});
Loading