kuadratic
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v22View latest version
A Val Town voting widget that provides embeddable HTML voting components with SQLite persistence and vote limiting.
- Multiple Options: Display multiple voteable options in a single poll
- Persistent Storage: All votes stored in Val Town's SQLite database
- Vote Limiting: Configurable limits for total votes and votes per user
- Embeddable: Export HTML components for easy integration
- Real-time Updates: Vote counts update immediately after voting
import { createVotingWidget, initializeOptions } from "./index.ts";
const config = {
pollId: "my-poll-2024",
maxTotalVotes: 1000,
maxVotesPerUser: 3,
options: [
{ id: "option1", title: "Option 1", description: "First choice" },
{ id: "option2", title: "Option 2", description: "Second choice" },
{ id: "option3", title: "Option 3" }
]
};
// Initialize the poll options in the database
await initializeOptions(config);
// Generate HTML widget
const widgetHTML = createVotingWidget(config);
The component provides these API endpoints:
POST /api/vote
- Submit a voteGET /api/results/:pollId
- Get current results
{ "pollId": "my-poll-2024", "optionId": "option1", "voterId": "unique-voter-id", "maxTotalVotes": 1000, "maxVotesPerUser": 3 }
kuadratic_votes_1
id
- Auto-increment primary keypoll_id
- Poll identifieroption_id
- Selected option IDvoter_id
- Unique voter identifiertimestamp
- Vote timestamp
kuadratic_options_1
id
- Option ID (primary key)poll_id
- Poll identifiertitle
- Option titledescription
- Optional descriptionvote_count
- Current vote count
pollId
: Unique identifier for the pollmaxTotalVotes
: Maximum total votes allowed across all usersmaxVotesPerUser
: Maximum votes per individual useroptions
: Array of voting options
The component enforces two types of limits:
- Global Limit: Total votes across all users
- Per-User Limit: Votes per individual (tracked by voter ID)
The widget generates self-contained HTML with embedded CSS and JavaScript. Simply insert the generated HTML into any webpage to enable voting functionality.
The component automatically:
- Loads current vote counts on page load
- Updates counts in real-time after voting
- Shows success/error messages
- Prevents voting when limits are reached