Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
morten-holm committed Mar 1, 2024
1 parent 7ec1c90 commit 2f34639
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@
*/

import {ConfigProvider} from "@kapeta/sdk-config";

export const RESOURCE_TYPE = 'kapeta/resource-type-mongodb';
export const PORT_TYPE = 'mongodb';
export async function createDBURI(provider:ConfigProvider, resourceName: string) {
const dbInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
console.log("resourceName", resourceName, dbInfo);
if (!dbInfo) {
throw new Error(`Resource ${resourceName} not found`);
}
const dbName =
dbInfo.options && dbInfo.options.dbName
? dbInfo.options.dbName
: resourceName;
const protocol = dbInfo.options && dbInfo.options.protocol ? dbInfo.options.protocol : 'mongodb';
const dbName = dbInfo.options?.dbName ?? resourceName;
const protocol = dbInfo?.options?.protocol ?? 'mongodb';

let credentials = ''
if (dbInfo.credentials?.username) {
Expand All @@ -28,9 +25,7 @@ export async function createDBURI(provider:ConfigProvider, resourceName: string)
}

if (protocol == 'mongodb+srv') {
const result = `mongodb+srv://${credentials}@${dbInfo.host}/${encodeURIComponent(dbName)}?authSource=admin`;
console.log("mongo db url", result);
return result;
return `mongodb+srv://${credentials}@${dbInfo.host}/${encodeURIComponent(dbName)}?authSource=admin`;
} else {
return `mongodb://${credentials}@${dbInfo.host}:${dbInfo.port}/${encodeURIComponent(dbName)}?authSource=admin&directConnection=true`;
}
Expand Down

0 comments on commit 2f34639

Please sign in to comment.