Skip to content

Database

Overview

Database menggunakan Neon Postgres (Serverless PostgreSQL dengan edge connectivity) dengan Drizzle ORM. Semua tables — termasuk auth tables — berada dalam satu database.

Stack Versions

PackageVersionNotes
drizzle-orm0.45.2 (latest stable)Neon HTTP adapter via drizzle-orm/neon-http
drizzle-kit0.31.10 (latest stable)Schema generation + migration tooling
Neon dialectpostgresqldialect: "postgresql" in drizzle.config.ts

Schema Domains

Schema diorganisir dalam file terpisah di packages/db/src/schema/:

Auth Tables (managed by better-auth)

better-auth Drizzle adapter mengelola tables berikut:

TablePurpose
userExtended dengan role, phone, profilePictureUrl
sessionDatabase sessions
accountOAuth accounts (Google)
verificationEmail verification dan magic link tokens

Jangan modify auth tables secara manual. Gunakan better-auth APIs.

App Tables

DomainTablesPurpose
Ordersorders, payment_proofsOrder dari murid, bukti bayar
Lessonslesson_sessionsJadwal les, status, assignment
Confirmationslesson_confirmationsTriple-confirmation tracking
Presencepresence_reportsLaporan guru setelah les
Evidencesession_evidenceFoto evidence les
Profile / KYCprofileUser profile, KTP data, KYC review
Teacher Applicationsteacher_applicationsTeacher application tracking

Key Relationships

user (student) → orders → lesson_sessions → lesson_confirmations

                                 presence_reports

                                 session_evidence

user (teacher) ───────→ lesson_sessions (assigned_teacher_id)

Conventions

  • Table names: snake_case, plural (e.g., lesson_sessions, payment_proofs)
  • Column names: snake_case (e.g., student_id, teacher_id, created_at)
  • Foreign key columns: {table}_id (e.g., order_id, session_id)
  • Timestamps: Setiap table wajib punya created_at dan updated_at
  • Primary keys: text dengan UUID (gunakan uuidv7)

Important Constraints

EntityFieldValues
ordersstatuspending, paid, verified, cancelled
lesson_sessionsstatusscheduled, ongoing, completed, cancelled
lesson_confirmationstypeteacher_start, student_start, teacher_end, student_end
userroleuser, student, teacher, admin, owner
teacher_applicationsstatuspending, approved, rejected

Migrations

  • Generate: drizzle-kit generate
  • Apply (local/dev): drizzle-kit push
  • Apply production: drizzle-kit migrate
  • JANGAN edit migration file setelah di-apply ke shared database.

Query Patterns

  • Gunakan Drizzle query builder untuk semua DB operations.
  • Prefer db.select().from() over raw SQL.
  • Gunakan database transactions untuk operasi yang memerlukan konsistensi.
  • Neon Postgres provides full transactional support dan advanced features.

Released under the MIT License.