Skip to content

Commit 73942e8

Browse files
committed
fix: Moved uri creation into util to ensure its always the same
1 parent 88820b4 commit 73942e8

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

src/MongoDB.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-mongodb';
3-
const PORT_TYPE = 'mongodb';
2+
import {createDBURI} from "./utils";
43

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

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

4527
this._prisma = this.createClient({

src/cmd-database-url.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
import Config from '@kapeta/sdk-config';
2-
3-
const RESOURCE_TYPE = 'kapeta/resource-type-mongodb';
4-
const PORT_TYPE = 'mongodb';
2+
import {createDBURI} from "./utils";
53

64
//Disable any logging from the SDK
75
console.log = function() {}
86

97
async function resolveUrl(resourceName: string) {
108
const provider = await Config.init(process.cwd(), '' );
11-
const dbInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
12-
const dbName =
13-
dbInfo.options && dbInfo.options.dbName
14-
? dbInfo.options.dbName
15-
: resourceName;
16-
17-
let credentials = ''
18-
if (dbInfo.credentials?.username) {
19-
credentials += encodeURIComponent(dbInfo.credentials.username);
20-
21-
if (dbInfo.credentials.password) {
22-
credentials += ':' + encodeURIComponent(dbInfo.credentials.password);
23-
}
24-
}
25-
26-
return `mongodb://${credentials}@${dbInfo.host}:${dbInfo.port}/${encodeURIComponent(dbName)}?authSource=admin&directConnection=true`;
9+
return createDBURI(provider, resourceName);
2710
}
2811

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

src/utils.ts

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

0 commit comments

Comments
 (0)