Skip to content

Commit 7be06a1

Browse files
committed
refactor(pgsql-test): store opts in PgTestClient to avoid type casting
- Store opts as class property in PgTestClient - Make PgTestClientOpts extend Partial<PgTestConnectionOptions> - Refactor auth() to use this.opts instead of this.config as any - Refactor clearContext() to use this.opts instead of casting config - Update manager.ts to accept and pass through opts parameter - Remove all 'as any' type casts from authentication code Co-Authored-By: Dan Lynch <pyramation@gmail.com>
1 parent f17a6f3 commit 7be06a1

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

packages/pgsql-test/src/connect.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ export const getConnections = async (
105105
const dbConfig = {
106106
...config,
107107
user: connOpts.connection.user,
108-
password: connOpts.connection.password,
109-
auth: connOpts.auth
108+
password: connOpts.connection.password
110109
} as PgConfig;
111110

112-
const db = manager.getClient(dbConfig);
111+
const db = manager.getClient(dbConfig, {
112+
auth: connOpts.auth,
113+
roles: connOpts.roles
114+
});
113115
db.setContext({ role: getDefaultRole(connOpts) });
114116

115117
return { pg, db, teardown, manager, admin };

packages/pgsql-test/src/manager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ export class PgTestConnector {
8484
return this.pgPools.get(key)!;
8585
}
8686

87-
getClient(config: PgConfig): PgTestClient {
87+
getClient(config: PgConfig, opts: any = {}): PgTestClient {
8888
if (this.shuttingDown) {
8989
throw new Error('PgTestConnector is shutting down; no new clients allowed');
9090
}
91-
const client = new PgTestClient(config, { trackConnect: (p) => this.registerConnect(p) });
91+
const client = new PgTestClient(config, {
92+
trackConnect: (p) => this.registerConnect(p),
93+
...opts
94+
});
9295
this.clients.add(client);
9396

9497
const key = this.dbKey(config);

packages/pgsql-test/src/test-client.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { Client, QueryResult } from 'pg';
22
import { PgConfig } from 'pg-env';
3-
import { AuthOptions } from '@launchql/types';
3+
import { AuthOptions, PgTestConnectionOptions } from '@launchql/types';
44
import { getRoleName } from './roles';
55

66
type PgTestClientOpts = {
77
deferConnect?: boolean;
88
trackConnect?: (p: Promise<any>) => void;
9-
auth?: AuthOptions;
10-
};
9+
} & Partial<PgTestConnectionOptions>;
1110

1211
export class PgTestClient {
1312
public config: PgConfig;
1413
public client: Client;
14+
private opts: PgTestClientOpts;
1515
private ctxStmts: string = '';
1616
private contextSettings: Record<string, string | null> = {};
1717
private _ended: boolean = false;
1818
private connectPromise: Promise<void> | null = null;
1919

2020
constructor(config: PgConfig, opts: PgTestClientOpts = {}) {
21+
this.opts = opts;
2122
this.config = config;
2223
this.client = new Client({
2324
host: this.config.host,
@@ -91,15 +92,15 @@ export class PgTestClient {
9192

9293
/**
9394
* Set authentication context for the current session.
94-
* Configures role and user ID using cascading defaults from options → config.auth → RoleMapping.
95+
* Configures role and user ID using cascading defaults from options → opts.auth → RoleMapping.
9596
*/
9697
async auth(options: AuthOptions = {}): Promise<void> {
9798
const role =
98-
options.role ?? (this.config as any).auth?.role ?? getRoleName('authenticated', (this.config as any).roles);
99+
options.role ?? this.opts.auth?.role ?? getRoleName('authenticated', this.opts);
99100
const userIdKey =
100-
options.userIdKey ?? (this.config as any).auth?.userIdKey ?? 'jwt.claims.user_id';
101+
options.userIdKey ?? this.opts.auth?.userIdKey ?? 'jwt.claims.user_id';
101102
const userId =
102-
options.userId ?? (this.config as any).auth?.userId ?? null;
103+
options.userId ?? this.opts.auth?.userId ?? null;
103104

104105
this.setContext({
105106
role,
@@ -122,7 +123,7 @@ export class PgTestClient {
122123
* Clear all session context variables and reset to default anonymous role.
123124
*/
124125
clearContext(): void {
125-
const defaultRole = getRoleName('anonymous', (this.config as any).roles);
126+
const defaultRole = getRoleName('anonymous', this.opts);
126127

127128
const nulledSettings: Record<string, string | null> = {};
128129
Object.keys(this.contextSettings).forEach(key => {

0 commit comments

Comments
 (0)