Skip to content

Commit 6dd550e

Browse files
committed
fix: Moved uri creation into util to ensure its always the same
1 parent 3172874 commit 6dd550e

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

src/PostgresDB.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Config, { ConfigProvider, ResourceInfo } from '@kapeta/sdk-config';
2-
const RESOURCE_TYPE = 'kapeta/resource-type-postgresql';
3-
const PORT_TYPE = 'postgres';
2+
import {createDBURI} from "./utils";
43

54
interface PrismaClient {
65
$connect(): Promise<void>;
@@ -11,8 +10,6 @@ interface PrismaClient {
1110
export abstract class PostgresDB<T extends PrismaClient> {
1211
private readonly _resourceName: string;
1312
private _ready: boolean = false;
14-
private _postgresInfo?: ResourceInfo;
15-
private _dbName?: string;
1613
private _prisma?: T;
1714
constructor(resourceName:string) {
1815
this._resourceName = resourceName;
@@ -24,22 +21,7 @@ export abstract class PostgresDB<T extends PrismaClient> {
2421
abstract createClient(opts: any): T;
2522

2623
async init(provider: ConfigProvider) {
27-
this._postgresInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, this._resourceName);
28-
this._dbName =
29-
this._postgresInfo.options && this._postgresInfo.options.dbName
30-
? this._postgresInfo.options.dbName
31-
: this._resourceName;
32-
33-
let credentials = '';
34-
if (this._postgresInfo?.credentials?.username) {
35-
credentials += this._postgresInfo.credentials.username;
36-
37-
if (this._postgresInfo.credentials.password) {
38-
credentials += ':' + this._postgresInfo.credentials.password;
39-
}
40-
}
41-
42-
const url = `postgresql://${credentials}@${this._postgresInfo.host}:${this._postgresInfo.port}/${this._dbName}`;
24+
const url = createDBURI(provider, this._resourceName);
4325
console.log('Connecting to postgres database: %s', url);
4426

4527
this._prisma = this.createClient({

src/cmd-database-url.ts

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import Config from '@kapeta/sdk-config';
2+
import {createDBURI} from "./utils";
23

3-
const RESOURCE_TYPE = 'kapeta/resource-type-postgresql';
4-
const PORT_TYPE = 'postgres';
54

65
//Disable any logging from the SDK
76
console.log = function() {}
87

98
async function resolveUrl(resourceName: string) {
109
const provider = await Config.init(process.cwd(), '' );
11-
const postgresInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
12-
const dbName =
13-
postgresInfo.options && postgresInfo.options.dbName
14-
? postgresInfo.options.dbName
15-
: resourceName;
16-
17-
let credentials = ''
18-
if (postgresInfo.credentials?.username) {
19-
credentials += encodeURIComponent(postgresInfo.credentials.username);
20-
21-
if (postgresInfo.credentials.password) {
22-
credentials += ':' + encodeURIComponent(postgresInfo.credentials.password);
23-
}
24-
}
25-
26-
return `postgresql://${credentials}@${postgresInfo.host}:${postgresInfo.port}/${encodeURIComponent(dbName)}`;
10+
return createDBURI(provider, resourceName);
2711
}
2812

2913
if (!process.argv[2]) {

src/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {ConfigProvider} from "@kapeta/sdk-config";
2+
3+
export const RESOURCE_TYPE = 'kapeta/resource-type-postgresql';
4+
export const PORT_TYPE = 'postgres';
5+
6+
export async function createDBURI(provider:ConfigProvider, resourceName: string) {
7+
const dbInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
8+
const dbName =
9+
dbInfo.options && dbInfo.options.dbName
10+
? dbInfo.options.dbName
11+
: resourceName;
12+
13+
let credentials = ''
14+
if (dbInfo.credentials?.username) {
15+
credentials += encodeURIComponent(dbInfo.credentials.username);
16+
17+
if (dbInfo.credentials.password) {
18+
credentials += ':' + encodeURIComponent(dbInfo.credentials.password);
19+
}
20+
}
21+
22+
return `postgresql://${credentials}@${dbInfo.host}:${dbInfo.port}/${encodeURIComponent(dbName)}`;
23+
}

0 commit comments

Comments
 (0)