2023-11-07 01:15:02 +03:00
|
|
|
import console from 'console';
|
|
|
|
|
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
|
|
|
|
export const connectionSource = new DataSource({
|
|
|
|
type: 'postgres',
|
|
|
|
logging: false,
|
2024-04-05 19:15:47 +03:00
|
|
|
url: process.env.PG_DATABASE_URL,
|
2023-11-07 01:15:02 +03:00
|
|
|
});
|
|
|
|
|
2023-12-08 16:43:52 +03:00
|
|
|
export const camelToSnakeCase = (str) =>
|
|
|
|
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
|
|
|
2023-11-07 01:15:02 +03:00
|
|
|
export const performQuery = async (
|
|
|
|
query: string,
|
|
|
|
consoleDescription: string,
|
|
|
|
withLog = true,
|
2023-12-08 16:43:52 +03:00
|
|
|
ignoreAlreadyExistsError = false,
|
2023-11-07 01:15:02 +03:00
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const result = await connectionSource.query(query);
|
2023-12-08 16:43:52 +03:00
|
|
|
|
2023-11-07 01:15:02 +03:00
|
|
|
withLog && console.log(`Performed '${consoleDescription}' successfully`);
|
2023-12-08 16:43:52 +03:00
|
|
|
|
2023-11-07 01:15:02 +03:00
|
|
|
return result;
|
|
|
|
} catch (err) {
|
2023-12-08 16:43:52 +03:00
|
|
|
let message = '';
|
|
|
|
|
|
|
|
if (ignoreAlreadyExistsError && `${err}`.includes('already exists')) {
|
|
|
|
message = `Performed '${consoleDescription}' successfully`;
|
|
|
|
} else {
|
|
|
|
message = `Failed to perform '${consoleDescription}': ${err}`;
|
|
|
|
}
|
|
|
|
withLog && console.error(message);
|
2023-11-07 01:15:02 +03:00
|
|
|
}
|
|
|
|
};
|