Skip to content

Commit

Permalink
fix: Moved uri creation into util to ensure its always the same
Browse files Browse the repository at this point in the history
  • Loading branch information
hofmeister committed Jun 29, 2023
1 parent 88820b4 commit 73942e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
22 changes: 2 additions & 20 deletions src/MongoDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Config, { ConfigProvider, ResourceInfo } from '@kapeta/sdk-config';
const RESOURCE_TYPE = 'kapeta/resource-type-mongodb';
const PORT_TYPE = 'mongodb';
import {createDBURI} from "./utils";

interface PrismaClient {
$connect(): Promise<void>;
Expand All @@ -11,8 +10,6 @@ interface PrismaClient {
export abstract class MongoDB<T extends PrismaClient> {
private readonly _resourceName: string;
private _ready: boolean = false;
private _dbInfo?: ResourceInfo;
private _dbName?: string;
private _prisma?: T;
constructor(resourceName:string) {
this._resourceName = resourceName;
Expand All @@ -24,22 +21,7 @@ export abstract class MongoDB<T extends PrismaClient> {
abstract createClient(opts: any): T;

async init(provider: ConfigProvider) {
this._dbInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, this._resourceName);
this._dbName =
this._dbInfo.options && this._dbInfo.options.dbName
? this._dbInfo.options.dbName
: this._resourceName;

let credentials = '';
if (this._dbInfo?.credentials?.username) {
credentials += this._dbInfo.credentials.username;

if (this._dbInfo.credentials.password) {
credentials += ':' + this._dbInfo.credentials.password;
}
}

const url = `mongodb://${credentials}@${this._dbInfo.host}:${this._dbInfo.port}/${this._dbName}?authSource=admin&directConnection=true`;
const url = createDBURI(provider, this._resourceName);
console.log('Connecting to mongodb database: %s', url);

this._prisma = this.createClient({
Expand Down
21 changes: 2 additions & 19 deletions src/cmd-database-url.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import Config from '@kapeta/sdk-config';

const RESOURCE_TYPE = 'kapeta/resource-type-mongodb';
const PORT_TYPE = 'mongodb';
import {createDBURI} from "./utils";

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

async function resolveUrl(resourceName: string) {
const provider = await Config.init(process.cwd(), '' );
const dbInfo = await provider.getResourceInfo(RESOURCE_TYPE, PORT_TYPE, resourceName);
const dbName =
dbInfo.options && dbInfo.options.dbName
? dbInfo.options.dbName
: resourceName;

let credentials = ''
if (dbInfo.credentials?.username) {
credentials += encodeURIComponent(dbInfo.credentials.username);

if (dbInfo.credentials.password) {
credentials += ':' + encodeURIComponent(dbInfo.credentials.password);
}
}

return `mongodb://${credentials}@${dbInfo.host}:${dbInfo.port}/${encodeURIComponent(dbName)}?authSource=admin&directConnection=true`;
return createDBURI(provider, resourceName);
}

if (!process.argv[2]) {
Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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);
const dbName =
dbInfo.options && dbInfo.options.dbName
? dbInfo.options.dbName
: resourceName;

let credentials = ''
if (dbInfo.credentials?.username) {
credentials += encodeURIComponent(dbInfo.credentials.username);

if (dbInfo.credentials.password) {
credentials += ':' + encodeURIComponent(dbInfo.credentials.password);
}
}

return `mongodb://${credentials}@${dbInfo.host}:${dbInfo.port}/${encodeURIComponent(dbName)}?authSource=admin&directConnection=true`;
}

0 comments on commit 73942e8

Please sign in to comment.