I need to create a separate database for each entity in my client's app. I'm going to determine the database name according to the subdomain from which the request is coming from. How can I achieve that and connect dynamically to a database using nestjs and mongoose?
async findOneByEmail(email: string, subdomain: string): Promise<User | any> {
const liveConnections = await Databases.getConnection(subdomain)
const user = await liveConnections.model(User.name, UserSchema).find()
// { 'securityInfo.email': email }
if (!user)
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'user not found',
field: 'user',
},
HttpStatus.BAD_REQUEST
)
return user
}
class DataBases extends Mongoose {
private clientOption = {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
}
private databases: { [key: string]: Connection } = {}
private getConnectionUri = (companyName = '') =>
`mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@cluster0.2lukt.mongodb.net/${companyName}?retryWrites=true&w=majority`
public async getConnection(companyName = ''): Promise<Connection> {
const connection = this.databases[companyName]
return connection ? connection : await this.createDataBase(companyName)
}
private async createDataBase(comapnyName = ''): Promise<Connection> {
// create new connection and if the database not exists just create new one
const newConnection = await this.createConnection(
this.getConnectionUri(comapnyName),
this.clientOption
)
this.databases[comapnyName] = newConnection
return newConnection
}
}
I fix it. the DataBases Class that I create works really great and if you know a better way please tell me . what I had to change is the way I use the connection to MongoDB. and now I can connect to different databases depends on the subdomain.
I hope it will help someone!
async findOneByEmail(email: string, subdomain: string): Promise<User | any> {
const liveConnections = await Databases.getConnection(subdomain)
const user = await liveConnections
.model(User.name, UserSchema)
.findOne({ 'securityInfo.email': email })
.exec()
if (!user)
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'user not found',
field: 'user',
},
HttpStatus.BAD_REQUEST
)
return user
}