Skip to main content

Overview

The ElectionRepository class handles all database operations for elections in the Consensus e-voting platform. It implements the IElectionRepository interface and uses SQLite for data persistence.

Constructor

constructor(db?: Database.Database)
db
Database.Database
Optional database instance. If not provided, uses the singleton DatabaseConnection instance.

Methods

save

Persists a new election to the database.
save(election: Election): void
election
Election
required
The election entity to save to the database.
return
void
No return value. Throws an error if the save operation fails.
Example:
const election = new Election(
  "elec-123",
  "Presidential Election 2024",
  ElectionType.PREFERENTIAL,
  new Date("2024-11-01"),
  new Date("2024-11-30"),
  "Annual presidential election"
);

electionRepository.save(election);

findById

Retrieves an election by its unique identifier.
findById(electionID: string): Election | null
electionID
string
required
The unique identifier of the election to retrieve.
return
Election | null
Returns the Election entity if found, or null if no election exists with the given ID.
Example:
const election = electionRepository.findById("elec-123");
if (election) {
  console.log(election.name);
}

findByStatus

Retrieves all elections with a specific status.
findByStatus(status: ElectionStatus): Election[]
status
ElectionStatus
required
The status to filter elections by (DRAFT, ACTIVE, CLOSED).
return
Election[]
Array of elections matching the specified status. Returns empty array if no matches found.
Example:
const activeElections = electionRepository.findByStatus(ElectionStatus.ACTIVE);
console.log(`Found ${activeElections.length} active elections`);

findActive

Retrieves all currently active elections (status is ACTIVE and current date is between start and end dates).
findActive(): Election[]
return
Election[]
Array of currently active elections. Returns empty array if no active elections found.
Example:
const activeElections = electionRepository.findActive();
activeElections.forEach(election => {
  console.log(`Active: ${election.name}`);
});

update

Updates an existing election in the database.
update(election: Election): void
election
Election
required
The election entity with updated values. The electionID must match an existing election.
return
void
No return value. Throws an error if the update operation fails.
Example:
const election = electionRepository.findById("elec-123");
if (election) {
  election.status = ElectionStatus.ACTIVE;
  electionRepository.update(election);
}

delete

Deletes an election from the database.
delete(electionID: string): void
electionID
string
required
The unique identifier of the election to delete.
return
void
No return value. Throws an error if the delete operation fails.
Example:
electionRepository.delete("elec-123");

findAll

Retrieves all elections from the database.
findAll(): Election[]
return
Election[]
Array of all elections in the database. Returns empty array if no elections exist.
Example:
const allElections = electionRepository.findAll();
console.log(`Total elections: ${allElections.length}`);

Election

The Election entity with the following properties:
  • electionID: string - Unique identifier
  • name: string - Election name
  • electionType: ElectionType - Type of election (FPTP or PREFERENTIAL)
  • status: ElectionStatus - Current status (DRAFT, ACTIVE, or CLOSED)
  • startDate: Date - Election start date
  • endDate: Date - Election end date
  • description: string - Election description

ElectionStatus

Enum with values:
  • DRAFT - Election is being prepared
  • ACTIVE - Election is currently running
  • CLOSED - Election has ended

Build docs developers (and LLMs) love