|
| 1 | +import { workingDirectory } from '../helpers/conf'; |
| 2 | +import * as sqlite3 from 'sqlite3'; |
| 3 | + |
| 4 | +const dbPath = `${workingDirectory}/redisinsight.db`; |
| 5 | + |
| 6 | +/** |
| 7 | + * Update table column value into local DB |
| 8 | + * @param tableName The name of table in DB |
| 9 | + * @param columnName The name of column in table |
| 10 | + * @param value Value to update in table |
| 11 | + */ |
| 12 | +export async function updateColumnValueInDBTable(tableName: string, columnName: string, value: number | string): Promise<void> { |
| 13 | + const db = new sqlite3.Database(dbPath); |
| 14 | + const query = `UPDATE ${tableName} SET ${columnName} = ${value}`; |
| 15 | + |
| 16 | + return new Promise<void>((resolve, reject) => { |
| 17 | + db.run(query, (err: { message: string }) => { |
| 18 | + if (err) { |
| 19 | + reject(new Error(`Error during changing ${columnName} column value: ${err.message}`)); |
| 20 | + } else { |
| 21 | + db.close(); |
| 22 | + resolve(); |
| 23 | + } |
| 24 | + }); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Get Column value from table in local Database |
| 30 | + * @param tableName The name of table in DB |
| 31 | + * @param columnName The name of column in table |
| 32 | + */ |
| 33 | +export async function getColumnValueFromTableInDB(tableName: string, columnName: string): Promise<any> { |
| 34 | + const db = new sqlite3.Database(dbPath); |
| 35 | + const query = `SELECT ${columnName} FROM ${tableName}`; |
| 36 | + |
| 37 | + return new Promise<void>((resolve, reject) => { |
| 38 | + db.get(query, (err: { message: string }, row: any) => { |
| 39 | + if (err) { |
| 40 | + reject(new Error(`Error during getting ${columnName} column value: ${err.message}`)); |
| 41 | + } else { |
| 42 | + const columnValue = row[columnName]; |
| 43 | + db.close(); |
| 44 | + resolve(columnValue); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Delete all rows from table in local DB |
| 52 | + * @param tableName The name of table in DB |
| 53 | + */ |
| 54 | +export async function deleteRowsFromTableInDB(tableName: string): Promise<void> { |
| 55 | + const db = new sqlite3.Database(dbPath); |
| 56 | + const query = `DELETE FROM ${tableName}`; |
| 57 | + |
| 58 | + return new Promise<void>((resolve, reject) => { |
| 59 | + |
| 60 | + |
| 61 | + db.run(query, (err: { message: string }) => { |
| 62 | + if (err) { |
| 63 | + reject(new Error(`Error during ${tableName} table rows deletion: ${err.message}`)); |
| 64 | + } else { |
| 65 | + db.close(); |
| 66 | + resolve(); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | +} |
0 commit comments