-
Notifications
You must be signed in to change notification settings - Fork 0
database.js
Edward edited this page Nov 21, 2024
·
1 revision
The database.js file serves as the backend layer of the application, providing methods to interact with an SQLite database. It facilitates data fetching and manipulation, enabling the frontend to query and display relevant information. Below is a detailed analysis of its components and their roles.
import sqlite3 from 'sqlite3'; // ES module import
import path from 'path';
const dbPath = path.resolve('DMS-Project.sqlite');-
sqlite3: The SQLite library used to interface with the database. -
path: Ensures a consistent file path for the SQLite database file, regardless of the operating system.
Function: Resolves the path to the SQLite file (DMS-Project.sqlite) that will store the application data.
const db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('Connected to the DMS-Project database.');
}
});- Purpose: Establishes a connection to the SQLite database.
- Error Handling: Logs errors if the database cannot be opened.
- Success Message: Confirms successful connection in the console.
export function getPlayersBySeason(season, filter = '') {
return new Promise((resolve, reject) => {
let query = 'SELECT Player, Tm FROM NFL_Player_Stats_1922 WHERE Season = ?';
let params = [season];
if (filter) {
query += ' AND Player LIKE ?';
params.push('%' + filter + '%');
}
db.all(query, params, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
}-
season:The season to query for. -
filter:An optional filter to narrow results based on player names.
- Selects
PlayerandTm(team) columns for the specified season. - Adds a
LIKEclause if a filter is provided.
Usage in Frontend: Enables components like Players.vue to fetch and display player data dynamically.
export function getPlayerStats(player, sortBy = 'Season', sortOrder = 'ASC', filter = '') {
return new Promise((resolve, reject) => {
let query = 'SELECT * FROM NFL_Player_Stats_1922 WHERE Player = ?';
let params = [player];
if (filter) {
query += ' AND Player LIKE ?';
params.push('%' + filter + '%');
}
query += ` ORDER BY ${sortBy} ${sortOrder}`;
db.all(query, params, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
}-
player:The player's name to query. -
sortBy:Column to sort the results (default:Season). -
sortOrder:Sorting order (ASCorDESC). -
filter:Optional filter to refine results.
- Selects all columns for the specified player.
- Applies sorting and optional filtering.
export default {
getPlayersBySeason,
getPlayerStats
};- Purpose: Groups the exported functions for simpler import and usage.
- Frontend Interaction: These methods are accessed via window.api in the frontend (e.g., Players.vue).
The Players.vue component interacts with database.js using the exposed getPlayersBySeason function:
-
Fetching Players: The component calls
window.api.getPlayersBySeasonto retrieve data for the specified season. - Displaying Data: Player data is grouped by teams and rendered dynamically.
- User Interaction: Features like toggling team visibility and filtering players enhance user experience.
- Database Connection: Logs connection errors to the console.
- SQL Queries: Rejects promises on query failures, ensuring error propagation to the calling component for appropriate handling.