Skip to content

Optional Connections

Technomantus Corvi edited this page Sep 5, 2026 · 1 revision

Optional Connections: Redis and MongoDB

Tanuki ships with three independent, lazy-loaded connection classes — Database, Redis, and Mongo. They don't share an interface on purpose: each exposes its native driver API directly, so the framework never grows into a hidden ORM/ODM. Use whichever your project needs; unused ones never connect.

// Relational (PDO) — see Models for the full API
Database::connect();

// Redis (requires the native php-redis extension)
Redis::connect()->set('key', 'value');
Redis::connect()->get('key');
Redis::connect()->expire('key', 3600);

// MongoDB (requires the mongodb extension + composer require mongodb/mongodb)
Mongo::connect()->selectCollection('posts')->find(['status' => 'published']);
Mongo::connect()->selectCollection('posts')->insertOne(['title' => 'Hello']);

Installing the drivers

# Redis
sudo apt install php-redis

# MongoDB
sudo apt install php-mongodb
composer require mongodb/mongodb

Both are entirely optional — a project that never calls Redis::connect() or Mongo::connect() never opens those connections.

.env variables

# Redis (optional)
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DATABASE=0

# MongoDB (optional)
MONGO_URI=mongodb://127.0.0.1:27017
MONGO_DATABASE=

Why no shared interface

A unified query API across a relational database and a document store (Model::where() working identically for both) is essentially building a lightweight ORM/ODM — exactly what this framework avoids. Each class stays a thin, honest wrapper around its native driver, so you're never guessing what a call actually does under the hood.

Clone this wiki locally