Adding interfacing for sqlite
This commit is contained in:
7
src/utilties/storage/interfaces.ts
Normal file
7
src/utilties/storage/interfaces.ts
Normal 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[]>
|
||||
}
|
||||
|
||||
68
src/utilties/storage/sqlite.ts
Normal file
68
src/utilties/storage/sqlite.ts
Normal 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
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user