feat(api): M4 - API Service with better-sqlite3 and robust type safety#3
Merged
reckziegelwilliam merged 2 commits intomainfrom Dec 2, 2025
Merged
feat(api): M4 - API Service with better-sqlite3 and robust type safety#3reckziegelwilliam merged 2 commits intomainfrom
reckziegelwilliam merged 2 commits intomainfrom
Conversation
…pe safety - Replace Prisma with better-sqlite3 for simpler, faster database access - Implement comprehensive TypeScript type system with branded types - Create type-safe database client with prepared statements - Build migration system for schema versioning - Implement all API endpoints: - GET /v1/config (SDK config fetching) - CRUD endpoints for flags (GET/POST/PUT/DELETE /v1/flags) - POST /v1/evaluate (server-side evaluation) - Add API key authentication middleware - Create comprehensive integration tests (17 tests, all passing) - Full type safety maintained throughout with no 'any' leaks Technical highlights: - Branded types prevent mixing ProjectId, EnvironmentId, FlagId - Type-safe mappers convert between DB rows and domain models - Prepared statements for performance - In-memory SQLite for blazing fast tests - Zero magic - explicit SQL queries - ~1MB bundle vs Prisma's ~3MB
- Create standalone ESLint config (root config didn't exist) - Remove unused ApiKey import from db/client.ts - Remove unused Environment import from types/api.ts - All linting now passes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Implements M4 - API Service using better-sqlite3 instead of Prisma for maximum simplicity, performance, and control while maintaining robust type safety.
What's New
Core Implementation
API Endpoints
GET /v1/config?apiKey=<key>- SDK config fetching (returns FlagConfig)GET /v1/flags?apiKey=<key>- List all flags in environmentPOST /v1/flags- Create a new flagGET /v1/flags/:id- Get specific flagPUT /v1/flags/:id- Update flag configurationDELETE /v1/flags/:id- Delete flagPOST /v1/evaluate- Server-side flag evaluationTesting
Technical Highlights
Type Safety
Database Client
Why better-sqlite3 over Prisma?
For Togglekit's simple schema (3 tables) and read-heavy workload, better-sqlite3 provides better control and performance without the overhead of Prisma's code generation.
Database Schema
Test Results
Files Changed
apps/api/package.json- Dependencies and scriptsapps/api/src/db/types.ts- Comprehensive type definitionsapps/api/src/db/client.ts- Type-safe database clientapps/api/src/db/migrations.ts- Migration systemapps/api/src/middleware/auth.ts- API key authenticationapps/api/src/routes/*.ts- All API endpointsapps/api/src/app.ts- Fastify app factoryapps/api/src/index.ts- Server entry pointapps/api/test/api.test.ts- Integration testsReady for Review
Next Steps (M5)
After merging, the next milestone will be the Dashboard (Next.js UI) for managing flags visually.
Closes #M4