1
- import { workingDirectory } from '../helpers/conf' ;
2
1
import * as sqlite3 from 'sqlite3' ;
2
+ import { workingDirectory } from '../helpers/conf' ;
3
3
4
4
const dbPath = `${ workingDirectory } /redisinsight.db` ;
5
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 } ` ;
6
+ export class DatabaseScripts {
7
+ /**
8
+ * Find the row index in a table based on a condition in another column
9
+ * @param dbTableParameters The sqlite database table parameters
10
+ * @returns The index of the row matching the condition
11
+ */
12
+ static async getRowIndexFromTableInDB ( dbTableParameters : DbTableParameters ) : Promise < number | null > {
13
+ const db = new sqlite3 . Database ( dbPath ) ;
14
+ const query = `SELECT rowid FROM ${ dbTableParameters . tableName } WHERE ${ dbTableParameters . conditionColumnName } = ? ` ;
15
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
- }
16
+ return new Promise < number | null > ( ( resolve , reject ) => {
17
+ db . get ( query , [ dbTableParameters . conditionColumnValue ] , ( err : { message : string } , row : any ) => {
18
+ if ( err ) {
19
+ reject ( new Error ( `Error during finding row index: ${ err . message } ` ) ) ;
20
+ }
21
+ else {
22
+ db . close ( ) ;
23
+ resolve ( row ? row . rowid : null ) ; // Return the rowid if a row matches the condition
24
+ }
25
+ } ) ;
24
26
} ) ;
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 } ` ;
27
+ }
28
+ /**
29
+ * Update table column value into local DB for a specific row
30
+ * @param dbTableParameters The sqlite database table parameters
31
+ */
32
+ static async updateColumnValueInDBTable ( dbTableParameters : DbTableParameters ) : Promise < void > {
33
+ const rowIndex = await this . getRowIndexFromTableInDB ( dbTableParameters ) ;
34
+ const db = new sqlite3 . Database ( dbPath ) ;
35
+ const query = `UPDATE ${ dbTableParameters . tableName } SET ${ dbTableParameters . columnName } = ? WHERE rowid = ?` ;
36
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
- }
37
+ return new Promise < void > ( ( resolve , reject ) => {
38
+ db . run ( query , [ dbTableParameters . rowValue , rowIndex ] , ( err : { message : string } ) => {
39
+ if ( err ) {
40
+ reject ( new Error ( `Error during changing ${ dbTableParameters . columnName } column value: ${ err . message } ` ) ) ;
41
+ }
42
+ else {
43
+ db . close ( ) ;
44
+ resolve ( ) ;
45
+ }
46
+ } ) ;
46
47
} ) ;
47
- } ) ;
48
- }
48
+ }
49
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 } ` ;
50
+ /**
51
+ * Get Column value from table in local Database
52
+ * @param dbTableParameters The sqlite database table parameters
53
+ */
54
+ static async getColumnValueFromTableInDB ( dbTableParameters : DbTableParameters ) : Promise < any > {
55
+ const rowIndex = await DatabaseScripts . getRowIndexFromTableInDB ( dbTableParameters ) ;
56
+ if ( rowIndex !== null ) {
57
+ const db = new sqlite3 . Database ( dbPath ) ;
58
+ const query = `SELECT ${ dbTableParameters . columnName } FROM ${ dbTableParameters . tableName } WHERE rowid = ?` ;
59
+
60
+ return new Promise < void > ( ( resolve , reject ) => {
61
+ db . get ( query , [ rowIndex ] , ( err : { message : string } , row : any ) => {
62
+ if ( err ) {
63
+ reject ( new Error ( `Error during getting ${ dbTableParameters . columnName } column value: ${ err . message } ` ) ) ;
64
+ }
65
+ else {
66
+ const columnValue = row [ dbTableParameters . columnName ! ] ;
67
+ db . close ( ) ;
68
+ resolve ( columnValue ) ;
69
+ }
70
+ } ) ;
71
+ } ) ;
72
+ }
73
+ throw new Error ( `No matching row found for the given condition in ${ dbTableParameters . tableName } ` ) ;
57
74
58
- return new Promise < void > ( ( resolve , reject ) => {
75
+ }
59
76
77
+ /**
78
+ * Delete all rows from table in local DB
79
+ * @param dbTableParameters The sqlite database table parameters
80
+ */
81
+ static async deleteRowsFromTableInDB ( dbTableParameters : DbTableParameters ) : Promise < void > {
82
+ const db = new sqlite3 . Database ( dbPath ) ;
83
+ const query = `DELETE FROM ${ dbTableParameters . tableName } ` ;
60
84
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
- }
85
+ return new Promise < void > ( ( resolve , reject ) => {
86
+
87
+ db . run ( query , ( err : { message : string } ) => {
88
+ if ( err ) {
89
+ reject ( new Error ( `Error during ${ dbTableParameters . tableName } table rows deletion: ${ err . message } ` ) ) ;
90
+ }
91
+ else {
92
+ db . close ( ) ;
93
+ resolve ( ) ;
94
+ }
95
+ } ) ;
68
96
} ) ;
69
- } ) ;
97
+ }
70
98
}
99
+ /**
100
+ * Add new database parameters
101
+ * @param tableName The name of table in DB
102
+ * @param columnName The name of column in table
103
+ * @param rowValue Value to update in table
104
+ * @param conditionColumnName The name of the column to search
105
+ * @param conditionColumnValue The value to match in the column
106
+ */
107
+ export type DbTableParameters = {
108
+ tableName : string ,
109
+ columnName ?: string ,
110
+ rowValue ?: string | number ,
111
+ conditionColumnName ?: string ,
112
+ conditionColumnValue ?: string
113
+ } ;
0 commit comments