Lewati ke konten utama

BAB 64. MULTI-SCHOOL ARCHITECTURE

64.1 Tujuan​

Memungkinkan satu instalasi aplikasi TemuBelajar digunakan oleh banyak sekolah sekaligus dengan isolasi data yang aman, efisien, dan mudah dikelola tanpa perlu deployment terpisah per sekolah.


64.2 Pendekatan​

Model yang digunakan: Shared Database – Shared Schema (Soft Multi-Tenant)

Setiap record data memiliki kolom school_id yang secara otomatis memfilter akses data hanya ke sekolah yang bersangkutan.

users → school_id
students → school_id
teachers → school_id
requests → school_id
bookings → school_id
meetings → school_id
leaderboards → school_id

64.3 Arsitektur Multi-School​

flowchart TD
SA["🏫 SMA Negeri 1\n(school_id: 1)"] --> API["🌐 TemuBelajar\nAPI Gateway"]
SB["🏫 SMA Negeri 2\n(school_id: 2)"] --> API
SC["🏫 SMA Kartini\n(school_id: 3)"] --> API

API --> TR["🔑 Tenant Resolver\n(Resolve school dari token)"]
TR --> DB[("💾 MySQL\n(Shared Database)")]

DB --> D1["📊 Data Sekolah 1"]
DB --> D2["📊 Data Sekolah 2"]
DB --> D3["📊 Data Sekolah 3"]

style TR fill:#7c3aed,color:#fff
style API fill:#4f63d2,color:#fff

64.4 Implementasi Isolasi Data​

Global Scope di Model​

// app/Shared/Traits/HasSchool.php

trait HasSchool
{
protected static function bootHasSchool(): void
{
static::addGlobalScope('school', function (Builder $builder) {
if (auth()->check()) {
$builder->where(
static::getTable() . '.school_id',
auth()->user()->school_id
);
}
});
}
}
// app/Modules/Requests/Models/Request.php

class Request extends Model
{
use HasSchool; // Semua query otomatis difilter by school_id

protected $fillable = [
'school_id',
'student_id',
'subject_id',
// ...
];
}

Middleware Tenant Resolver​

// app/Http/Middleware/VerifiedSchoolMiddleware.php

public function handle(Request $request, Closure $next): Response
{
$school = auth()->user()->school;

if (!$school || !$school->is_active) {
abort(403, 'Sekolah tidak aktif.');
}

// Inject ke app container untuk dipakai di seluruh request
app()->instance('current_school', $school);

return $next($request);
}

64.5 Admin Multi-School​

flowchart TD
SA["👑 Super Admin"] --> PORTAL["🖥️ School Portal\n(Manage semua sekolah)"]
PORTAL --> S1["🏫 SMA N 1\n(Admin Sekolah 1)"]
PORTAL --> S2["🏫 SMA N 2\n(Admin Sekolah 2)"]
PORTAL --> S3["🏫 SMA Kartini\n(Admin Sekolah 3)"]

style SA fill:#7c3aed,color:#fff
style PORTAL fill:#4f63d2,color:#fff

64.6 Keuntungan Model Shared Database​

KeuntunganPenjelasan
Biaya rendahSatu server untuk banyak sekolah
Maintenance sederhanaDeploy sekali untuk semua
Backup mudahSatu database untuk di-backup
Deployment cepatTambah sekolah baru cukup insert data
Analytics lintas sekolahMudah query statistik nasional

64.7 Roadmap Pengembangan Lanjutan​

Apabila skala sangat besar (>500 sekolah):

ModelDeskripsi
Database per sekolahIsolasi penuh, migrasi lebih kompleks
Schema per sekolahPostgreSQL schema separation
Region per provinsiServer berbeda per wilayah
Microservice per domainPisah service AI, Chat, Meeting