This is the backend API repository for NumberNinja, an arithmetic practice game built with React Native and Expo by Adam Richard Turner.
NumberNinja API is a robust backend service that powers the NumberNinja arithmetic practice game. It handles user sessions, question generation, performance tracking, and authentication services.
- Node.js & Express.js: Server framework
- TypeScript: Programming language
- PostgreSQL: Database
- Firebase Admin SDK: Authentication
- Jest & Supertest: Testing
- Firebase authentication integration
- JWT token validation
- Protected routes
- Dynamic session creation and tracking
- Customizable practice parameters
- Multiple operation support (Addition, Subtraction, Multiplication, Division)
- Dynamic question generation based on difficulty levels
- Support for various number ranges
- Multiple operation modes
- Customizable time limits
- Detailed session statistics
- Operation-specific performance metrics
- Progress tracking over time
- Common mistake analysis
- Secure user data storage
- Account deletion functionality
- Performance history
The application includes comprehensive test coverage using Jest and Supertest:
- Integration tests for all API endpoints
- Authentication testing
- Session management testing
- Question generation testing
- Performance tracking testing
Test files can be found in the src/__tests__
directory.
The application uses a PostgreSQL database with tables for:
- Users
- Sessions
- Questions
- User Answers
- Modes
- Operations
- Difficulty Levels
- Number Ranges
/api/auth
: Authentication endpoints/api/sessions
: Session management/api/questions
: Question generation and submission/api/performance
: Performance tracking and statistics/api/user
: User data management
- Clone the repository
- Install dependencies:
npm install
- Set up environment variables:
DATABASE_URL=your_database_url
PORT=3000
- Run development server:
npm run dev
- Run tests:
npm test
- Node.js (v16 or higher)
- PostgreSQL (v14 or higher)
- Firebase Admin SDK credentials
- Clone the repository:
git clone <repository-url>
cd numberninja-api
- Install dependencies:
npm install
- Set up environment variables:
Create two files in the project root:
.env.development.local
(for development).env.production.local
(for production)
Add the following variables to both files:
# Database
DATABASE_URL=postgresql://username:password@localhost:5432/numberninja
# Server
PORT=3000
NODE_ENV=development # or production
# Firebase Admin SDK
FIREBASE_PROJECT_ID=your-project-id
FIREBASE_PRIVATE_KEY=your-private-key
FIREBASE_CLIENT_EMAIL=your-client-email
- Set up Firebase Admin SDK:
- Create a Firebase project at Firebase Console
- Generate a new private key from Project Settings > Service Accounts
- Save the generated JSON as
src/config/firebase-adminsdk-key.json
- Database Setup:
a. Create PostgreSQL database:
createdb numberninja
b. Run the schema file:
psql -d numberninja -f src/database/schema.sql
The schema creates the following tables:
- difficulty_levels
- modes
- number_ranges
- operations
- users
- sessions
- questions
- user_answers
- Ensure the Frontend Repository references the correct API URL in the relevant config and API service files, alternatively test the API locally with cURL or Postman.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request