feat: refactor createDatabase method to remove databaseNodeId parameter and implement findOptimalNode in DatabaseNodeService for improved database node selection
This commit is contained in:
@ -14,11 +14,8 @@ export class DatabaseManagerController {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Post("create")
|
@Post("create")
|
||||||
createDatabase(@Body() body: { projectId: string; databaseNodeId: string }) {
|
createDatabase(@Body() body: { projectId: string }) {
|
||||||
return this.databaseManagerService.createDatabase(
|
return this.databaseManagerService.createDatabase(body.projectId);
|
||||||
body.databaseNodeId,
|
|
||||||
body.projectId
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("node/create")
|
@Post("node/create")
|
||||||
|
|||||||
@ -84,11 +84,8 @@ export class DatabaseManagerService extends DatabaseEncryptionService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async createDatabase(
|
async createDatabase(projectId: string): Promise<Database> {
|
||||||
databaseNodeId: string,
|
const node = await this.databaseNodeService.findOptimalNode();
|
||||||
projectId: string
|
|
||||||
): Promise<Database> {
|
|
||||||
const node = await this.databaseNodeService.findById(databaseNodeId);
|
|
||||||
|
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw new Error("Database node not found");
|
throw new Error("Database node not found");
|
||||||
@ -114,7 +111,7 @@ export class DatabaseManagerService extends DatabaseEncryptionService {
|
|||||||
q_username,
|
q_username,
|
||||||
password: this.decryptPassword(password),
|
password: this.decryptPassword(password),
|
||||||
},
|
},
|
||||||
databaseNodeId
|
node.id
|
||||||
);
|
);
|
||||||
|
|
||||||
const database = this.databaseRepository.create({
|
const database = this.databaseRepository.create({
|
||||||
|
|||||||
@ -18,6 +18,22 @@ export class DatabaseNodeService extends DatabaseEncryptionService {
|
|||||||
return this.databaseNodeRepository.findOne({ where: { id } });
|
return this.databaseNodeRepository.findOne({ where: { id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findOptimalNode(): Promise<DatabaseNode | null> {
|
||||||
|
const nodes = await this.databaseNodeRepository.find({
|
||||||
|
relations: ["databases"],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (nodes.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes.reduce((optimalNode, currentNode) => {
|
||||||
|
const currentCount = currentNode.databases?.length || 0;
|
||||||
|
const optimalCount = optimalNode.databases?.length || 0;
|
||||||
|
return currentCount < optimalCount ? currentNode : optimalNode;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async initDatabase(
|
async initDatabase(
|
||||||
data: {
|
data: {
|
||||||
database: string;
|
database: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user