Adding interfacing for sqlite

This commit is contained in:
2025-06-22 22:08:01 -04:00
parent b307630642
commit d9561d9179
7 changed files with 1755 additions and 14 deletions

View File

@@ -0,0 +1,7 @@
export interface sql_common {
run(query: string) : Promise<number>
runParameterized(query: string, parameters: any[]): Promise<number>
getAll(query: string) : Promise<Object[]>
getAllParameterized(query: string, parameters: any[]) : Promise<Object[]>
}

View File

@@ -0,0 +1,68 @@
import * as sqlite3 from 'sqlite3'
import { sql_common } from "./interfaces"
export class SqliteDB implements sql_common {
private db : sqlite3.Database;
public constructor(private readonly dbName: string) {
this.db = new sqlite3.Database(this.dbName);
}
async run(query: string): Promise<number> {
return new Promise(() => {
this.db.run(query, (result : sqlite3.RunResult, err: Error) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return result.changes
}
})
})
}
async runParameterized(query: string, parameters: any[]): Promise<number> {
return new Promise(() => {
this.db.run(query, parameters, (result : sqlite3.RunResult, err: Error) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return result.changes
}
})
})
}
async getAll(query: string): Promise<Object[]> {
return new Promise(() => {
this.db.all(query, (err: Error, rows: Object[]) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return rows
}
})
})
}
getAllParameterized(query: string, parameters: any[]): Promise<Object[]> {
return new Promise(() => {
this.db.all(query, parameters, (err: Error, rows: Object[]) => {
if (err) {
// TODO Winston should handle this
console.log(err)
throw err
} else {
return rows
}
})
})
}
}