I'm having a little problem trying to create the database connection url. I am using the AWS Secrets Manager service and with the returned data I create my connection URL. The problem is that when I want to create the URL it gives me an error. If someone can help me I would really appreciate it
I created a module with functions to create the URL but, the return is undefined. What seems to happen is that the execution does not wait for the promise to resolve. because the Secrets if I am receiving them but later
import { Injectable } from '@nestjs/common'
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager'
@Injectable({})
export class SecretsService{
public final: string
async getSecret() {
const secret_name = 'Secret'
let lastURL
const client = new SecretsManagerClient({
region: 'us-east-1',
})
client.send(
new GetSecretValueCommand({
SecretId: secret_name,
VersionStage: 'AWSCURRENT'// VersionStage defaults to AWSCURRENT if unspecified
})
).then((response)=>{
const data = response.SecretString
const secret=JSON.parse(data)
lastURL= `${secret.engine}://${secret.username}:${secret.password}@${secret.host}:${secret.port}/dbname?schema=public`
this.final=lastURL
console.log(this.final)
}).catch((error)=>{
console.log(error)
})
}
}
import { Injectable } from '@nestjs/common'
import { PrismaClient } from '@prisma/client'
import { SecretsService } from 'src/secrets/secrets.service'
const asd = new SecretsService()
asd.getSecret()
const urlFinal=asd.final
@Injectable()
export class PrismaService extends PrismaClient {
constructor(){
super({
datasources: {
db:{
url: asd.final
}
}
})
}
}