i-darrshan's picture
initial update of the vite site
62c3fe0
//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}