File size: 1,335 Bytes
62c3fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//General
const path = require('path');

//SQLite
const sqlite3 = require('sqlite3').verbose();

const fs = require('fs');

// Database connection
console.log(__dirname);

const dbFilePath = path.resolve(__dirname,'../database/data.db');
console.log('The dbpath is', dbFilePath);

const db = new sqlite3.Database(dbFilePath, (err) => {
  if (err) {
    console.error('Error connecting to SQLite database:', err.message);
    console.log("Current working directory:", process.cwd());
  } else {
    console.log('Connected to SQLite database');
  }
});

// Helper function to promisify db.run for INSERT, UPDATE, CREATE, etc.
function runQuery(query, params = []) {
    return new Promise((resolve, reject) => {
        db.run(query, params, function (err) {
            if (err) {
                reject(err);
            } else {
                resolve(this); // `this` contains metadata like `lastID` or `changes`
            }
        });
    });
}

// Helper function to promisify db.all for SELECT queries
function runSelectQuery(query, params = []) {
    return new Promise((resolve, reject) => {
        db.all(query, params, (err, rows) => {
            if (err) {
                reject(err);
            } else {
                resolve(rows);
            }
        });
    });
}

module.exports = {runQuery, runSelectQuery}