From 871c4d6be70d79e124b8e7d73259fd038d639cee Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 25 Feb 2023 19:02:48 +0100 Subject: [PATCH 1/2] port --- src/db.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/db.ts b/src/db.ts index 47935fd..fb07ef7 100644 --- a/src/db.ts +++ b/src/db.ts @@ -15,10 +15,10 @@ export class DB { type: dbType; mariaPool!: mariadb.Pool; //pgPool!: pg.Pool; - constructor({ host, user, password, database, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, connectionLimit?: number, databaseType?: dbType }) { + constructor({ host, user, password, database, port = 3306, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, port?: number, connectionLimit?: number, databaseType?: dbType }) { this.type = databaseType; if (databaseType == dbType.mariadb) { - this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true }); + this.mariaPool = mariadb.createPool({ host, user, password, port, database, connectionLimit, multipleStatements: true }); this.handler = new Handler(this); } /*else if (databaseType == dbType.postgres) { @@ -30,7 +30,7 @@ export class DB { this.name = database; } async query(query: Query, printQuery = false) { - if(printQuery)console.log(query); + if (printQuery) console.log(query); if (this.type == dbType.mariadb) return await this.mariaPool.query(query); /*else if (this.type == dbType.postgres) { let res = await this.pgPool.query(query.sql, query.values); @@ -115,8 +115,8 @@ export class Attribute { toStringFunc(handler: Handler) { return this.table.serialize(handler) + "(" + this.serialize(handler) + ")"; } - toString(){ - return this.table.dbLangTableName + "_" + this.name; + toString() { + return this.table.dbLangTableName + "_" + this.name; } } -- 2.39.5 From e78e90d83c24c30669ff316826dfac8930b2748d Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 25 Feb 2023 19:11:34 +0100 Subject: [PATCH 2/2] later structure --- src/db.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/db.ts b/src/db.ts index fb07ef7..bb89b4f 100644 --- a/src/db.ts +++ b/src/db.ts @@ -10,15 +10,20 @@ import { insertResponse, readResponse, writeResponse, singleResponse } from "./r export class DB { tables: Table[] = []; - handler: Handler; + handler!: Handler; name: string; - type: dbType; + type!: dbType; mariaPool!: mariadb.Pool; + private connected = false; //pgPool!: pg.Pool; - constructor({ host, user, password, database, port = 3306, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, port?: number, connectionLimit?: number, databaseType?: dbType }) { + constructor(database: string) { + this.name = database; + } + connect({ host, user, password, port = 3306, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, port?: number, connectionLimit?: number, databaseType?: dbType }) { + this.connected = true; this.type = databaseType; if (databaseType == dbType.mariadb) { - this.mariaPool = mariadb.createPool({ host, user, password, port, database, connectionLimit, multipleStatements: true }); + this.mariaPool = mariadb.createPool({ host, user, password, port, database: this.name, connectionLimit, multipleStatements: true }); this.handler = new Handler(this); } /*else if (databaseType == dbType.postgres) { @@ -27,9 +32,10 @@ export class DB { }*/ else throw new Error("Unsuported Database type!"); - this.name = database; + } async query(query: Query, printQuery = false) { + if(!this.connected) throw new Error("Not connected yet!"); if (printQuery) console.log(query); if (this.type == dbType.mariadb) return await this.mariaPool.query(query); /*else if (this.type == dbType.postgres) { @@ -40,10 +46,12 @@ export class DB { else throw new Error("Invalid Database Type!"); } getHandler() { + if(!this.connected) throw new Error("Not connected yet!"); return this.handler; } async sync(force = false) { + if(!this.connected) throw new Error("Not connected yet!"); let handler = this.getHandler(); await handler.syncDB(this, handler, force); } @@ -59,6 +67,7 @@ export class DB { return null; } async close() { + if(!this.connected) throw new Error("Not connected yet!"); if (this.type == dbType.mariadb && this.mariaPool) await this.mariaPool.end(); //if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end(); } -- 2.39.5