Switch to postgres + promises

This commit is contained in:
Ajay Ramachandran
2021-03-01 20:40:31 -05:00
parent 9a9038d5e0
commit ff4af3786e
37 changed files with 424 additions and 291 deletions

View File

@@ -1,13 +1,7 @@
export interface IDatabase {
init(): void;
prepare(type: QueryType, query: string, params: any[]): any;
get<TModel>(query: string, params: any[]): TModel;
getAll<TModel>(query: string, params: any[]): TModel[];
run(query: string, params: any[]): void;
exec(query: string): any;
prepare(type: QueryType, query: string, params: any[]): Promise<any | any[] | void>;
}
export type QueryType = 'get' | 'all' | 'run';

View File

@@ -9,14 +9,10 @@ export class Mysql implements IDatabase {
constructor(private config: any) {
}
init() {
init(): void {
this.connection = new MysqlInterface(this.config);
}
exec(query: string) {
this.prepare('run', query, []);
}
prepare(type: QueryType, query: string, params: any[]) {
Logger.debug(`prepare (mysql): type: ${type}, query: ${query}, params: ${params}`);
const queryResult = this.connection.query(query, params);
@@ -34,16 +30,5 @@ export class Mysql implements IDatabase {
}
}
public get<TModel>(query: string, params: any[]): TModel {
return this.prepare('get', query, params);
}
public getAll<TModel>(query: string, params: any[]): TModel[] {
return this.prepare('all', query, params);
}
public run(query: string, params: any[]): void {
this.prepare('run', query, params);
}
}

31
src/databases/Postgres.ts Normal file
View File

@@ -0,0 +1,31 @@
import { Logger } from '../utils/logger';
import { IDatabase, QueryType } from './IDatabase';
import { Pool } from 'pg';
export class Mysql implements IDatabase {
private pool: Pool;
constructor(private config: any) {}
init(): void {
this.pool = new Pool();
}
async prepare(type: QueryType, query: string, params: any[]) {
Logger.debug(`prepare (postgres): type: ${type}, query: ${query}, params: ${params}`);
const queryResult = await this.pool.query(query, params);
switch (type) {
case 'get': {
return queryResult.rows[0];
}
case 'all': {
return queryResult.rows;
}
case 'run': {
break;
}
}
}
}

View File

@@ -12,7 +12,7 @@ export class Sqlite implements IDatabase {
{
}
prepare(type: QueryType, query: string, params: any[]) {
async prepare(type: QueryType, query: string, params: any[]) {
const preparedQuery = this.db.prepare(query);
switch (type) {
@@ -29,20 +29,6 @@ export class Sqlite implements IDatabase {
}
}
get<TModel>(query: string, params: any[]): TModel {
return this.prepare('get', query, params);
}
getAll<TModel>(query: string, params: any[]): TModel[] {
return this.prepare('all', query, params);
}
run(query: string, params: any[]): void {
this.prepare('run', query, params);
}
exec(query: string) {
return this.db.exec(query);
}
init() {
// Make dirs if required
if (!fs.existsSync(path.join(this.config.dbPath, "../"))) {

View File

@@ -9,8 +9,7 @@ let privateDB: IDatabase;
if (config.mysql) {
db = new Mysql(config.mysql);
privateDB = new Mysql(config.privateMysql);
}
else {
} else {
db = new Sqlite({
dbPath: config.db,
dbSchemaFileName: config.dbSchema,