Skip to content

Commit da27f62

Browse files
committed
Made pg code fully separated from the core one
1 parent 99072b1 commit da27f62

File tree

24 files changed

+137
-118
lines changed

24 files changed

+137
-118
lines changed

src/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"setup": "cat .nvmrc | nvm install; nvm use",
1212
"build": "run-s build:ts build:dumbo build:pongo",
1313
"build:ts": "tsc -b",
14+
"build:ts:clean": "tsc --build --clean",
1415
"build:ts:watch": "tsc -b --watch",
1516
"build:dumbo": "npm run build -w packages/dumbo",
1617
"build:pongo": "npm run build -w packages/pongo",

src/packages/dumbo/src/benchmarks/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import 'dotenv/config';
22

33
import Benchmark from 'benchmark';
44
import pg from 'pg';
5-
import { defaultPostgreSQLConenctionString, dumbo, rawSql, single } from '..';
5+
import { rawSql, single } from '..';
6+
import { defaultPostgreSQLConenctionString, dumbo } from '../pg';
67

78
const connectionString =
89
process.env.BENCHMARK_POSTGRESQL_CONNECTION_STRING ??

src/packages/dumbo/src/core/connections/connection.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ConnectorType } from '../connectors';
12
import {
23
sqlExecutor,
34
type DbSQLExecutor,
@@ -10,11 +11,11 @@ import {
1011
} from './transaction';
1112

1213
export interface Connection<
13-
ConnectorType extends string = string,
14+
Connector extends ConnectorType = ConnectorType,
1415
DbClient = unknown,
1516
> extends WithSQLExecutor,
16-
DatabaseTransactionFactory<ConnectorType> {
17-
connector: ConnectorType;
17+
DatabaseTransactionFactory<Connector> {
18+
connector: Connector;
1819
open: () => Promise<DbClient>;
1920
close: () => Promise<void>;
2021
}
@@ -30,36 +31,34 @@ export interface ConnectionFactory<
3031
}
3132

3233
export type CreateConnectionOptions<
33-
ConnectorType extends string = string,
34+
Connector extends ConnectorType = ConnectorType,
3435
DbClient = unknown,
35-
ConnectionType extends Connection<ConnectorType, DbClient> = Connection<
36-
ConnectorType,
36+
ConnectionType extends Connection<Connector, DbClient> = Connection<
37+
Connector,
3738
DbClient
3839
>,
3940
Executor extends DbSQLExecutor = DbSQLExecutor,
4041
> = {
41-
connector: ConnectorType;
42+
connector: Connector;
4243
connect: Promise<DbClient>;
4344
close: (client: DbClient) => Promise<void>;
4445
initTransaction: (
4546
connection: () => ConnectionType,
46-
) => (
47-
client: Promise<DbClient>,
48-
) => DatabaseTransaction<ConnectorType, DbClient>;
47+
) => (client: Promise<DbClient>) => DatabaseTransaction<Connector, DbClient>;
4948
executor: () => Executor;
5049
};
5150

5251
export const createConnection = <
53-
ConnectorType extends string = string,
52+
Connector extends ConnectorType = ConnectorType,
5453
DbClient = unknown,
55-
ConnectionType extends Connection<ConnectorType, DbClient> = Connection<
56-
ConnectorType,
54+
ConnectionType extends Connection<Connector, DbClient> = Connection<
55+
Connector,
5756
DbClient
5857
>,
5958
Executor extends DbSQLExecutor = DbSQLExecutor,
6059
>(
6160
options: CreateConnectionOptions<
62-
ConnectorType,
61+
Connector,
6362
DbClient,
6463
ConnectionType,
6564
Executor
@@ -71,7 +70,7 @@ export const createConnection = <
7170

7271
const getClient = async () => client ?? (client = await connect);
7372

74-
const connection: Connection<ConnectorType, DbClient> = {
73+
const connection: Connection<Connector, DbClient> = {
7574
connector,
7675
open: getClient,
7776
close: () => (client ? close(client) : Promise.resolve()),

src/packages/dumbo/src/core/connections/transaction.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import type { ConnectorType } from '../connectors';
12
import type { WithSQLExecutor } from '../execute';
23
import { type Connection } from './connection';
34

45
export interface DatabaseTransaction<
5-
ConnectorType extends string = string,
6+
Connector extends ConnectorType = ConnectorType,
67
DbClient = unknown,
78
> extends WithSQLExecutor {
8-
connector: ConnectorType;
9-
connection: Connection<ConnectorType, DbClient>;
9+
connector: Connector;
10+
connection: Connection<Connector, DbClient>;
1011
begin: () => Promise<void>;
1112
commit: () => Promise<void>;
1213
rollback: (error?: unknown) => Promise<void>;
1314
}
1415

1516
export interface DatabaseTransactionFactory<
16-
ConnectorType extends string = string,
17+
Connector extends ConnectorType = ConnectorType,
1718
DbClient = unknown,
1819
> {
19-
transaction: () => DatabaseTransaction<ConnectorType, DbClient>;
20+
transaction: () => DatabaseTransaction<Connector, DbClient>;
2021

2122
withTransaction: <Result = never>(
2223
handle: (
23-
transaction: DatabaseTransaction<ConnectorType, DbClient>,
24+
transaction: DatabaseTransaction<Connector, DbClient>,
2425
) => Promise<TransactionResult<Result> | Result>,
2526
) => Promise<Result>;
2627
}
@@ -38,13 +39,13 @@ const toTransactionResult = <Result>(
3839
: { success: true, result: transactionResult };
3940

4041
export const executeInTransaction = async <
41-
ConnectorType extends string = string,
42+
Connector extends ConnectorType = ConnectorType,
4243
DbClient = unknown,
4344
Result = void,
4445
>(
45-
transaction: DatabaseTransaction<ConnectorType, DbClient>,
46+
transaction: DatabaseTransaction<Connector, DbClient>,
4647
handle: (
47-
transaction: DatabaseTransaction<ConnectorType, DbClient>,
48+
transaction: DatabaseTransaction<Connector, DbClient>,
4849
) => Promise<TransactionResult<Result> | Result>,
4950
): Promise<Result> => {
5051
await transaction.begin();
@@ -63,14 +64,14 @@ export const executeInTransaction = async <
6364
};
6465

6566
export const transactionFactoryWithDbClient = <
66-
ConnectorType extends string = string,
67+
Connector extends ConnectorType = ConnectorType,
6768
DbClient = unknown,
6869
>(
6970
connect: () => Promise<DbClient>,
7071
initTransaction: (
7172
client: Promise<DbClient>,
72-
) => DatabaseTransaction<ConnectorType, DbClient>,
73-
): DatabaseTransactionFactory<ConnectorType, DbClient> => ({
73+
) => DatabaseTransaction<Connector, DbClient>,
74+
): DatabaseTransactionFactory<Connector, DbClient> => ({
7475
transaction: () => initTransaction(connect()),
7576
withTransaction: (handle) =>
7677
executeInTransaction(initTransaction(connect()), handle),

src/packages/dumbo/src/core/execute/execute.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { Connection } from '../connections';
2+
import type { ConnectorType } from '../connectors';
23
import type { QueryResult, QueryResultRow } from '../query';
34
import { type SQL } from '../sql';
45

56
export type SQLQueryOptions = { timeoutMs?: number };
67
export type SQLCommandOptions = { timeoutMs?: number };
78

89
export interface DbSQLExecutor<
9-
ConnectorType extends string = string,
10+
Connector extends ConnectorType = ConnectorType,
1011
DbClient = unknown,
1112
> {
12-
connector: ConnectorType;
13+
connector: Connector;
1314
query<Result extends QueryResultRow = QueryResultRow>(
1415
client: DbClient,
1516
sql: SQL,

src/packages/dumbo/src/core/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
import type { Connection, ConnectionPool } from './connections';
2+
import type { ConnectorType } from './connectors';
3+
14
export * from './connections';
25
export * from './connectors';
36
export * from './execute';
7+
export * from './locks';
48
export * from './query';
59
export * from './schema';
610
export * from './serializer';
711
export * from './sql';
812
export * from './tracing';
13+
14+
export type DumboOptions<Connector extends ConnectorType = ConnectorType> = {
15+
connector?: Connector;
16+
};
17+
18+
export type Dumbo<
19+
Connector extends ConnectorType = ConnectorType,
20+
ConnectionType extends Connection<Connector> = Connection<Connector>,
21+
> = ConnectionPool<ConnectionType>;

src/packages/dumbo/src/index.ts

-27
Original file line numberDiff line numberDiff line change
@@ -1,28 +1 @@
1-
import {
2-
postgresPool,
3-
type PostgresPool,
4-
type PostgresPoolOptions,
5-
} from './storage/postgresql';
6-
71
export * from './core';
8-
export * from './storage/postgresql';
9-
10-
// TODO: Merge with ConnectorTypeName
11-
export type SupportedConnector = `PostgreSQL:pg` | `SQLite:sqlite3`;
12-
13-
export type PoolOptions = {
14-
connector?: SupportedConnector;
15-
};
16-
17-
export type DumboOptions = PoolOptions & PostgresPoolOptions;
18-
export type Dumbo = PostgresPool;
19-
20-
export const connectionPool = <PoolOptionsType extends DumboOptions>(
21-
options: PoolOptionsType,
22-
) =>
23-
// TODO: this should have the pattern matching and verification
24-
postgresPool(options as unknown as PostgresPoolOptions);
25-
26-
export const dumbo = <DumboOptionsType extends DumboOptions = DumboOptions>(
27-
options: DumboOptionsType,
28-
): Dumbo => connectionPool(options);

src/packages/dumbo/src/pg.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './core';
12
export * from './storage/postgresql/core';
23
export * from './storage/postgresql/pg';

src/packages/dumbo/src/storage/postgresql/core/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ConnectorType } from '../../../core';
22

3-
export * from '../../../core/locks';
43
export * from './connections';
4+
export * from './locks';
55
export * from './schema';
66

77
export type PostgreSQLConnector = 'PostgreSQL';

src/packages/dumbo/src/storage/postgresql/core/locks/advisoryLocks.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
defaultDatabaseLockOptions,
3+
single,
4+
sql,
35
type AcquireDatabaseLockMode,
46
type AcquireDatabaseLockOptions,
57
type DatabaseLock,
68
type DatabaseLockOptions,
79
type ReleaseDatabaseLockOptions,
8-
} from '..';
9-
import { single, sql, type SQLExecutor } from '../../../../core';
10+
type SQLExecutor,
11+
} from '../../../../core';
1012

1113
export const tryAcquireAdvisoryLock = async (
1214
execute: SQLExecutor,

src/packages/dumbo/src/storage/postgresql/core/schema/migrations.int.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
import assert from 'assert';
66
import { after, before, beforeEach, describe, it } from 'node:test';
77
import { tableExists } from '..';
8-
import { type Dumbo, dumbo } from '../../../..';
8+
import { type Dumbo } from '../../../..';
99
import { count, rawSql, sql } from '../../../../core';
1010
import { type SQLMigration, MIGRATIONS_LOCK_ID } from '../../../../core/schema';
11+
import { dumbo } from '../../../../pg';
1112
import { acquireAdvisoryLock, releaseAdvisoryLock } from '../locks';
1213
import { runPostgreSQLMigrations } from './migrations';
1314

src/packages/dumbo/src/storage/postgresql/core/schema/migrations.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { AdvisoryLock, type DatabaseLockOptions } from '../..';
2-
import { rawSql, type Dumbo } from '../../../..';
31
import {
42
MIGRATIONS_LOCK_ID,
3+
rawSql,
54
runSQLMigrations,
65
schemaComponent,
76
sqlMigration,
7+
type DatabaseLockOptions,
8+
type Dumbo,
89
type SQLMigration,
9-
} from '../../../../core/schema';
10+
} from '../../../../core';
11+
import { AdvisoryLock } from '../locks';
1012

1113
export type PostgreSQLMigratorOptions = {
1214
lock?: {

src/packages/dumbo/src/storage/postgresql/index.ts

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
export * from '../core/locks';
1+
import type { Dumbo, DumboOptions } from '../../../core';
2+
import {
3+
type NodePostgresConnection,
4+
type NodePostgresConnector,
5+
type NodePostgresPool,
6+
nodePostgresPool,
7+
type NodePostgresPoolOptions,
8+
} from './connections';
9+
10+
export type PostgresConnector = NodePostgresConnector;
11+
export type PostgresPool = NodePostgresPool;
12+
export type PostgresConnection = NodePostgresConnection;
13+
14+
export type PostgresPoolOptions = DumboOptions<NodePostgresConnector> &
15+
NodePostgresPoolOptions;
16+
export const postgresPool = nodePostgresPool;
17+
18+
export const connectionPool = <PoolOptionsType extends DumboOptions>(
19+
options: PoolOptionsType,
20+
) =>
21+
// TODO: this should have the pattern matching and verification
22+
postgresPool(options as unknown as PostgresPoolOptions);
23+
24+
export const dumbo = <
25+
DumboOptionsType extends PostgresPoolOptions = PostgresPoolOptions,
26+
>(
27+
options: DumboOptionsType,
28+
): Dumbo<NodePostgresConnector> => connectionPool(options);
29+
230
export * from './connections';
331
export * from './execute';
432
export * from './serialization';

src/packages/pongo/src/commandLine/migrate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { combineMigrations } from '@event-driven-io/dumbo';
12
import {
2-
combineMigrations,
33
dumbo,
44
migrationTableSchemaComponent,
55
runPostgreSQLMigrations,
6-
} from '@event-driven-io/dumbo';
6+
} from '@event-driven-io/dumbo/pg';
77
import { Command } from 'commander';
88
import { pongoCollectionSchemaComponent } from '../core';
99
import { loadConfigFile } from './configFile';

src/packages/pongo/src/commandLine/shell.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
2-
checkConnection,
32
color,
43
LogLevel,
54
LogStyle,
65
prettyJson,
76
SQL,
87
type MigrationStyle,
98
} from '@event-driven-io/dumbo';
9+
import { checkConnection } from '@event-driven-io/dumbo/pg';
1010
import Table from 'cli-table3';
1111
import { Command } from 'commander';
1212
import repl from 'node:repl';

0 commit comments

Comments
 (0)