Skip to content
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datadrop",
"version": "1.11.0",
"version": "1.11.1",
"type": "module",
"main": "./build/index.js",
"scripts": {
Expand Down Expand Up @@ -28,7 +28,7 @@
"@sendgrid/mail": "8.1.3",
"discord.js": "^14.16.2",
"dotenv": "^16.4.5",
"ts-postgres": "^1.9.0"
"ts-postgres": "1.3.0"
},
"devDependencies": {
"@types/node": "^20.12.7",
Expand Down
19 changes: 11 additions & 8 deletions src/services/PostgresDatabaseService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Snowflake } from 'discord.js';
import { Client, DatabaseError, PreparedStatement } from 'ts-postgres';
import { Client, DatabaseError, PreparedStatement, Value } from 'ts-postgres';

import { ConsoleLogger } from '@hunteroi/advanced-logger';

Expand All @@ -24,17 +24,14 @@ export default class PostgresDatabaseService implements IDatabaseService {
port: Number(process.env.DATABASE_PORT),
database: process.env.POSTGRES_DB
});
this.#database.on('error', (error: DatabaseError) => this.#logger.error(`Une erreur est survenue lors de l'utilisation de la base de données: \n${error.message}`));
this.#listenToDatabaseEvents();
}

/**
* @inherited
*/
public async start(): Promise<void> {
const connectionInfo = await this.#database.connect();
if (!connectionInfo) throw new Error('Impossible de se connecter à la base de données.');

this.#logger.info(`Connecté à la base de données avec les données suivantes: ${JSON.stringify(connectionInfo)}`);
await this.#database.connect();

await this.#database.query(`CREATE TABLE IF NOT EXISTS Migrations (
id serial PRIMARY KEY,
Expand Down Expand Up @@ -166,6 +163,12 @@ export default class PostgresDatabaseService implements IDatabaseService {
}
}

#listenToDatabaseEvents() {
this.#database.on('connect', () => this.#logger.info('Connexion établie avec la base de données!'));
this.#database.on('end', () => this.#logger.info('Connexion fermée avec la base de données!'));
this.#database.on('error', (error: DatabaseError) => this.#logger.error(`Une erreur est survenue lors de l'utilisation de la base de données: \n${error.message}`));
}

#ascendingSort(string1: string, string2: string): number {
if (string1 > string2) return 1;
if (string1 < string2) return -1;
Expand All @@ -184,11 +187,11 @@ export default class PostgresDatabaseService implements IDatabaseService {
return values.map(([prop], index) => prop + ' = $' + (index + offset)).join(', ');
}

#deconstructValues(values: [string, any][]): any[] {
#deconstructValues(values: [string, any][]): Value[] {
return values.flatMap(([, v]) => v instanceof Object && typeof v !== 'bigint' ? JSON.stringify(v) : (v ?? null));
}

async #executeStatement(statement: PreparedStatement, values: any[] = [], isSelect = true): Promise<User | undefined | null> {
async #executeStatement(statement: PreparedStatement, values: Value[] = [], isSelect = true): Promise<User | undefined | null> {
const notFoundMessage = 'User not found';
try {
const entities = await statement.execute(values);
Expand Down
Loading