Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Adding the ability to set a mysql.config. #705

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/src/enums/service-definition-tag.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ServiceDefinitionTagEnum {
IdentityProvider = "IdentityProviderInterface",
Logger = "LoggerInterface",
MethodParameterDecoratorResolver = "METHOD_PARAMETER_DECORATOR_RESOLVER",
MysqlConfig = "MysqlConfig",
RequestInterceptor = "RequestInterceptorInterface",
ScheduledTask = "ScheduledTaskInterface",
Tracer = "TracerInterface",
Expand Down
2 changes: 1 addition & 1 deletion packages/mysql-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions packages/mysql/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions packages/mysql/src/clients/mysql.client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('MySQL Client', () => {
}

it("should retrieve the table metadata", () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -48,7 +48,7 @@ describe('MySQL Client', () => {
})

it("should retrieve the columns metadata", () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -67,7 +67,7 @@ describe('MySQL Client', () => {
})

it("should retrieve the column metadata", () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -86,7 +86,7 @@ describe('MySQL Client', () => {
})

it("should retrieve the primary key column name", () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -101,7 +101,7 @@ describe('MySQL Client', () => {
})

it("should retrieve the column names", () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -118,7 +118,7 @@ describe('MySQL Client', () => {
})

it("should properly retrieve an object in the db based on the id", async () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('MySQL Client', () => {
})

it("should properly create an object in the db", async () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -179,7 +179,7 @@ describe('MySQL Client', () => {
})

it("should properly update an object in the db", async () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -206,7 +206,7 @@ describe('MySQL Client', () => {
})

it("should properly delete an object in the db", async () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand All @@ -228,7 +228,7 @@ describe('MySQL Client', () => {
})

it("should properly search an object in the db", async () => {
const mysqlClient = new MysqlClient("", 0, "", "", 0, false, {
const mysqlClient = new MysqlClient([],{
critical(message: string, extra?: any, module?: string): void {
}, debug(message: string, extra?: any, module?: string): void {
}, error(message: string, extra?: any, module?: string): void {
Expand Down
97 changes: 53 additions & 44 deletions packages/mysql/src/clients/mysql.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {inject, injectable, singleton} from "tsyringe";
import {inject, injectable, singleton, injectAll} from "tsyringe";
import {MysqlClientInterface} from "../interfaces/mysql-client.interface";
import {ClassMetadata, PropertyMetadata} from "@pristine-ts/metadata";
import {DecoratorMetadataKeynameEnum} from "../enums/decorator-metadata-keyname.enum";
Expand All @@ -9,51 +9,60 @@ import {createPool, Pool} from "mysql2/promise";
import {LogHandlerInterface} from "@pristine-ts/logging";
import {DataMapper} from "@pristine-ts/data-mapping-common";
import {SearchQuery, SearchResult} from "@pristine-ts/mysql-common";
import {tag} from "@pristine-ts/common";
import {ServiceDefinitionTagEnum, tag} from "@pristine-ts/common";
import {MysqlConfig} from "../configs/mysql.config";

@tag("MysqlClientInterface")
@injectable()
@singleton()
export class MysqlClient implements MysqlClientInterface {
private pools: Map<string, Pool> = new Map<string, Pool>();

constructor(@inject(`%${MysqlModuleKeyname}.address%`) private readonly address: string,
@inject(`%${MysqlModuleKeyname}.port%`) private readonly port: number,
@inject(`%${MysqlModuleKeyname}.user%`) private readonly user: string,
@inject(`%${MysqlModuleKeyname}.password%`) private readonly password: string,
@inject(`%${MysqlModuleKeyname}.connection_limit%`) private readonly connectionLimit: number,
@inject(`%${MysqlModuleKeyname}.debug%`) private readonly debug: boolean,
constructor(
@injectAll(ServiceDefinitionTagEnum.MysqlConfig) private readonly mysqlConfigs: MysqlConfig[],
@inject('LogHandlerInterface') private readonly logHandler: LogHandlerInterface,
private readonly dataMapper: DataMapper,
) {
}

/**
* This method returns the mysql config corresponding to the unique keyname.
* @param configUniqueKeyname
*/
private getMysqlConfig(configUniqueKeyname: string): MysqlConfig {
const mysqlConfig = this.mysqlConfigs.find(mysqlConfig => mysqlConfig.uniqueKeyname === configUniqueKeyname) as MysqlConfig;

if(!mysqlConfig) {
throw new Error(`The mysql config with the keyname ${configUniqueKeyname} does not exist.`);
}

return mysqlConfig;
}

/**
* This method returns a pool of connections to the database.
* @param databaseName
* @param configUniqueKeyname
* @param force
*/
async getPool(databaseName: string, force: boolean = false): Promise<Pool> {
if (!this.pools.has(databaseName) && !force) {
async getPool(configUniqueKeyname: string, force: boolean = false): Promise<Pool> {
if (!this.pools.has(configUniqueKeyname) && !force) {
try {
const mysqlConfig = this.getMysqlConfig(configUniqueKeyname);

const pool = createPool({
connectionLimit: this.connectionLimit,
host: this.address,
port: this.port,
user: this.user,
password: this.password,
database: databaseName,
debug: this.debug,
connectionLimit: mysqlConfig.connectionLimit,
host: mysqlConfig.host,
port: mysqlConfig.port,
user: mysqlConfig.user,
password: mysqlConfig.password,
database: mysqlConfig.database,
debug: mysqlConfig.debug,
});

this.pools.set(databaseName, pool);
this.pools.set(configUniqueKeyname, pool);

this.logHandler.debug('MySql Adapter Pool generated successfully', {
connectionLimit: this.connectionLimit,
host: this.address,
user: this.user,
password: this.password,
database: databaseName,
mysqlConfig,
pool,
});
} catch (error) {
Expand All @@ -63,7 +72,7 @@ export class MysqlClient implements MysqlClientInterface {
}
}

return this.pools.get(databaseName) as Pool;
return this.pools.get(configUniqueKeyname) as Pool;
}

/**
Expand Down Expand Up @@ -174,12 +183,12 @@ export class MysqlClient implements MysqlClientInterface {

/**
* This method returns the column name for a given class and property name.
* @param databaseName
* @param configUniqueKeyname
* @param sqlStatement
* @param values
*/
async executeSql(databaseName: string, sqlStatement: string, values: any[]): Promise<any> {
const pool = await this.getPool(databaseName);
async executeSql(configUniqueKeyname: string, sqlStatement: string, values: any[]): Promise<any> {
const pool = await this.getPool(configUniqueKeyname);

this.logHandler.debug("Executing SQL Statement", {sqlStatement, values});

Expand Down Expand Up @@ -224,24 +233,24 @@ export class MysqlClient implements MysqlClientInterface {

/**
* This method returns a single element from the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param primaryKey
*/
async get<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, primaryKey: string | number): Promise<T | null> {
async get<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, primaryKey: string | number): Promise<T | null> {
const sql = `SELECT * FROM ${this.getTableMetadata(classType).tableName} WHERE ${this.getPrimaryKeyColumnName(classType)} = ?`;

const values = await this.executeSql(databaseName, sql, [primaryKey]);
const values = await this.executeSql(configUniqueKeyname, sql, [primaryKey]);

return (await this.mapResults(classType, values))[0];
}

/**
* This method creates a new element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param element
*/
async create<T extends { [key: string]: any; }>(databaseName: string, element: T): Promise<void> {
async create<T extends { [key: string]: any; }>(configUniqueKeyname: string, element: T): Promise<void> {
const columns = this.getColumnsMetadata(element.constructor as { new(): T; });

const columnNames = Object.keys(columns).map(column => this.getColumnName(element.constructor as { new(): T; }, column));
Expand All @@ -250,15 +259,15 @@ export class MysqlClient implements MysqlClientInterface {
// Generate update SQL statement:
const sql = `INSERT INTO ${this.getTableMetadata(element.constructor as { new(): T; }).tableName} (${columnNames.join(", ")}) VALUES (${columnValues.map(() => "?").join(", ")})`;

await this.executeSql(databaseName, sql, columnValues);
await this.executeSql(configUniqueKeyname, sql, columnValues);
}

/**
* This method updates an element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param element
*/
async update<T extends { [key: string]: any; }>(databaseName: string, element: T): Promise<void> {
async update<T extends { [key: string]: any; }>(configUniqueKeyname: string, element: T): Promise<void> {
const columns = this.getColumnsMetadata(element.constructor as { new(): T; });

const primaryKeyColumnName = this.getPrimaryKeyColumnName(element.constructor as { new(): T; });
Expand All @@ -275,28 +284,28 @@ export class MysqlClient implements MysqlClientInterface {

const sql = `UPDATE ${this.getTableMetadata(element.constructor as { new(): T; }).tableName} SET ${columnNames.join(" = ?, ")} = ? WHERE ${primaryKeyColumnName} = ?`;

await this.executeSql(databaseName, sql, columnValues);
await this.executeSql(configUniqueKeyname, sql, columnValues);
}

/**
* This method deletes an element in the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param primaryKey
*/
async delete<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, primaryKey: string | number): Promise<void> {
async delete<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, primaryKey: string | number): Promise<void> {
const sql = `DELETE FROM ${this.getTableMetadata(classType).tableName} WHERE ${this.getPrimaryKeyColumnName(classType)} = ?`;

await this.executeSql(databaseName, sql, [primaryKey]);
await this.executeSql(configUniqueKeyname, sql, [primaryKey]);
}

/**
* This method searches the database.
* @param databaseName
* @param configUniqueKeyname
* @param classType
* @param query
*/
async search<T extends { [key: string]: any; }>(databaseName: string, classType: { new(): T; }, query: SearchQuery): Promise<SearchResult<T>> {
async search<T extends { [key: string]: any; }>(configUniqueKeyname: string, classType: { new(): T; }, query: SearchQuery): Promise<SearchResult<T>> {
let sql = "";
const columns = this.getColumnsMetadata(classType);
const defaultSearchableFields = Object.keys(columns).filter(column => columns[column].isSearchable).map(column => this.getColumnName(classType, column));
Expand Down Expand Up @@ -349,7 +358,7 @@ export class MysqlClient implements MysqlClientInterface {
sql += " ORDER BY " + orderBy.join(", ");
}

const totalNumberOfResults = (await this.executeSql(databaseName, "SELECT COUNT(*) as total_number_of_results FROM `" + tableName + "` WHERE 1=1 " + sql, sqlValues))[0]["total_number_of_results"];
const totalNumberOfResults = (await this.executeSql(configUniqueKeyname, "SELECT COUNT(*) as total_number_of_results FROM `" + tableName + "` WHERE 1=1 " + sql, sqlValues))[0]["total_number_of_results"];

//
// PAGING
Expand All @@ -358,7 +367,7 @@ export class MysqlClient implements MysqlClientInterface {
// If there's a page, add the limit and offset
sql += " LIMIT " + query.maximumNumberOfResultsPerPage + " OFFSET " + (query.page - 1) * query.maximumNumberOfResultsPerPage;

const response = await this.executeSql(databaseName, "SELECT * FROM `" + tableName + "` WHERE 1=1 " + sql, sqlValues);
const response = await this.executeSql(configUniqueKeyname, "SELECT * FROM `" + tableName + "` WHERE 1=1 " + sql, sqlValues);

const searchResult = new SearchResult<any>();
searchResult.page = query.page;
Expand Down
Loading
Loading