Skip to content

Commit

Permalink
feat(postgressql-session-storage): make connection string extensible …
Browse files Browse the repository at this point in the history
…with any pg config

Signed-off-by: Octanium <[email protected]>
  • Loading branch information
0ctanium committed Nov 8, 2023
1 parent 794cdb9 commit 462b039
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import pg from 'pg';
import pg, {type PoolConfig} from 'pg';
import {RdbmsConnection} from '@shopify/shopify-app-session-storage';

export class PostgresConnection implements RdbmsConnection {
sessionStorageIdentifier: string;
private ready: Promise<void>;
private pool: pg.Pool;
private dbUrl: URL;
private config: PoolConfig;

constructor(dbUrl: string, sessionStorageIdentifier: string) {
this.dbUrl = new URL(dbUrl);
constructor(config: string | PoolConfig, sessionStorageIdentifier: string) {
if (typeof config === 'string') {
const dbUrl = new URL(config);

this.config = {
host: dbUrl.hostname,
user: decodeURIComponent(dbUrl.username),
password: decodeURIComponent(dbUrl.password),
database: decodeURIComponent(dbUrl.pathname.slice(1)),
port: Number(dbUrl.port),
};
} else {
this.config = config;
}
this.ready = this.init();
this.sessionStorageIdentifier = sessionStorageIdentifier;
}
Expand Down Expand Up @@ -60,7 +72,7 @@ export class PostgresConnection implements RdbmsConnection {
}

public getDatabase(): string | undefined {
return decodeURIComponent(this.dbUrl.pathname.slice(1));
return this.config.database;
}

async hasTable(tablename: string): Promise<boolean> {
Expand All @@ -83,12 +95,6 @@ export class PostgresConnection implements RdbmsConnection {
}

private async init(): Promise<void> {
this.pool = new pg.Pool({
host: this.dbUrl.hostname,
user: decodeURIComponent(this.dbUrl.username),
password: decodeURIComponent(this.dbUrl.password),
database: this.getDatabase(),
port: Number(this.dbUrl.port),
});
this.pool = new pg.Pool(this.config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {migrationList} from './migrations';
import {PostgresConnection} from './postgres-connection';
import {PostgresSessionStorageMigrator} from './postgres-migrator';
import type {PoolConfig} from 'pg';

export interface PostgreSQLSessionStorageOptions
extends RdbmsSessionStorageOptions {
Expand Down Expand Up @@ -47,12 +48,12 @@ export class PostgreSQLSessionStorage implements SessionStorage {
private migrator: PostgresSessionStorageMigrator;

constructor(
dbUrl: URL | string,
config: string | URL | PoolConfig,
opts: Partial<PostgreSQLSessionStorageOptions> = {},
) {
this.options = {...defaultPostgreSQLSessionStorageOptions, ...opts};
this.internalInit = this.init(
typeof dbUrl === 'string' ? dbUrl : dbUrl.toString(),
config instanceof URL ? config.toString() : config,
);
this.migrator = new PostgresSessionStorageMigrator(
this.client,
Expand Down Expand Up @@ -144,8 +145,8 @@ export class PostgreSQLSessionStorage implements SessionStorage {
return this.client.disconnect();
}

private async init(dbUrl: string) {
this.client = new PostgresConnection(dbUrl, this.options.sessionTableName);
private async init(config: string | PoolConfig) {
this.client = new PostgresConnection(config, this.options.sessionTableName);
await this.connectClient();
await this.createTable();
}
Expand Down

0 comments on commit 462b039

Please sign in to comment.