Compare commits
No commits in common. "a501f86ef6842534d435384f16907efffbe099fc" and "85921d64933da5a0fc1910492602694fbae12105" have entirely different histories.
a501f86ef6
...
85921d6493
1 changed files with 8 additions and 17 deletions
25
src/db.ts
25
src/db.ts
|
@ -10,20 +10,15 @@ import { insertResponse, readResponse, writeResponse, singleResponse } from "./r
|
||||||
|
|
||||||
export class DB {
|
export class DB {
|
||||||
tables: Table[] = [];
|
tables: Table[] = [];
|
||||||
handler!: Handler;
|
handler: Handler;
|
||||||
name: string;
|
name: string;
|
||||||
type!: dbType;
|
type: dbType;
|
||||||
mariaPool!: mariadb.Pool;
|
mariaPool!: mariadb.Pool;
|
||||||
private connected = false;
|
|
||||||
//pgPool!: pg.Pool;
|
//pgPool!: pg.Pool;
|
||||||
constructor(database: string) {
|
constructor({ host, user, password, database, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, connectionLimit?: number, databaseType?: dbType }) {
|
||||||
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;
|
this.type = databaseType;
|
||||||
if (databaseType == dbType.mariadb) {
|
if (databaseType == dbType.mariadb) {
|
||||||
this.mariaPool = mariadb.createPool({ host, user, password, port, database: this.name, connectionLimit, multipleStatements: true });
|
this.mariaPool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
||||||
this.handler = new Handler(this);
|
this.handler = new Handler(this);
|
||||||
}
|
}
|
||||||
/*else if (databaseType == dbType.postgres) {
|
/*else if (databaseType == dbType.postgres) {
|
||||||
|
@ -32,11 +27,10 @@ export class DB {
|
||||||
}*/
|
}*/
|
||||||
else throw new Error("Unsuported Database type!");
|
else throw new Error("Unsuported Database type!");
|
||||||
|
|
||||||
|
this.name = database;
|
||||||
}
|
}
|
||||||
async query(query: Query, printQuery = false) {
|
async query(query: Query, printQuery = false) {
|
||||||
if(!this.connected) throw new Error("Not connected yet!");
|
if(printQuery)console.log(query);
|
||||||
if (printQuery) console.log(query);
|
|
||||||
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
||||||
/*else if (this.type == dbType.postgres) {
|
/*else if (this.type == dbType.postgres) {
|
||||||
let res = await this.pgPool.query(query.sql, query.values);
|
let res = await this.pgPool.query(query.sql, query.values);
|
||||||
|
@ -46,12 +40,10 @@ export class DB {
|
||||||
else throw new Error("Invalid Database Type!");
|
else throw new Error("Invalid Database Type!");
|
||||||
}
|
}
|
||||||
getHandler() {
|
getHandler() {
|
||||||
if(!this.connected) throw new Error("Not connected yet!");
|
|
||||||
return this.handler;
|
return this.handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sync(force = false) {
|
async sync(force = false) {
|
||||||
if(!this.connected) throw new Error("Not connected yet!");
|
|
||||||
let handler = this.getHandler();
|
let handler = this.getHandler();
|
||||||
await handler.syncDB(this, handler, force);
|
await handler.syncDB(this, handler, force);
|
||||||
}
|
}
|
||||||
|
@ -67,7 +59,6 @@ export class DB {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
async close() {
|
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.mariadb && this.mariaPool) await this.mariaPool.end();
|
||||||
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
|
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
|
||||||
}
|
}
|
||||||
|
@ -124,8 +115,8 @@ export class Attribute {
|
||||||
toStringFunc(handler: Handler) {
|
toStringFunc(handler: Handler) {
|
||||||
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
}
|
}
|
||||||
toString() {
|
toString(){
|
||||||
return this.table.dbLangTableName + "_" + this.name;
|
return this.table.dbLangTableName + "_" + this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue