docs: explain Directus shim architecture

- Enhanced AI_ONBOARDING.md with detailed shim explanation
- Added shim section to CTO_LOG.md
- Clarifies how God Mode uses Directus SDK syntax without Directus runtime
This commit is contained in:
cawcenter
2025-12-15 13:18:01 -05:00
parent 7348a70ae3
commit dfec95e82e
2 changed files with 61 additions and 3 deletions

View File

@@ -10,9 +10,48 @@
* **Queue:** Redis + BullMQ (`BatchProcessor.ts`). * **Queue:** Redis + BullMQ (`BatchProcessor.ts`).
## ⚡ Critical Systems (The "Shim") ## ⚡ Critical Systems (The "Shim")
**File:** `src/lib/directus/client.ts`
* **Function:** Intercepts standard SDK calls (`readItems`, `createItem`) and converts them to `pg` pool queries. **File:** `src/lib/directus/client.ts` - **THIS IS THE KEY TO UNDERSTANDING GOD MODE**
* **Why:** Allows "Headless" operation. If the API is down, we still run.
### What It Does:
Translates Directus SDK syntax → Raw PostgreSQL queries. This allows the entire codebase to use familiar Directus SDK patterns while directly querying PostgreSQL.
### Why It Exists:
- **No Directus dependency** - God Mode runs standalone
- **Direct database access** - Faster, no API overhead
- **Familiar syntax** - Developers can use `readItems('sites')` instead of raw SQL
- **Hot-swappable** - Can switch to real Directus later if needed
### How It Works:
**Component code looks like this:**
```typescript
import { getDirectusClient, readItems } from '@/lib/directus/client';
const client = getDirectusClient();
const sites = await client.request(readItems('sites', {
filter: { status: { _eq: 'active' } },
limit: 10
}));
```
**Behind the scenes, the shim converts it to:**
```sql
SELECT * FROM "sites"
WHERE "status" = 'active'
LIMIT 10
```
### Server vs Client:
- **Server-side:** Direct PostgreSQL via `pg` pool
- **Client-side:** HTTP proxy to `/api/god/proxy` which then uses `pg`
### Files Involved:
- `src/lib/directus/client.ts` - Shim implementation (274 lines)
- `src/pages/api/god/proxy.ts` - Client-side proxy endpoint
- `src/lib/db.ts` - PostgreSQL connection pool
**IMPORTANT:** All 35+ admin components use this shim. It's not a hack - it's the architecture.
## ⚠️ "God Tier" Limits (The "Dangerous" Stuff) ## ⚠️ "God Tier" Limits (The "Dangerous" Stuff)
**File:** `docker-compose.yml` **File:** `docker-compose.yml`

View File

@@ -14,6 +14,25 @@
- **Queue:** BullMQ (Redis-backed) for async content generation - **Queue:** BullMQ (Redis-backed) for async content generation
- **Caching:** Redis (shared with queue) - **Caching:** Redis (shared with queue)
### The "Directus Shim" - Critical Innovation
**Problem:** Standard CMS (Directus) is too slow for high-volume operations.
**Solution:** Custom shim layer that mimics Directus SDK but queries PostgreSQL directly.
**How It Works:**
1. All admin components import `getDirectusClient()` from `src/lib/directus/client.ts`
2. They use familiar Directus SDK syntax: `readItems('sites', { filter: {...} })`
3. The shim translates this to raw SQL: `SELECT * FROM "sites" WHERE ...`
4. **Server-side:** Direct PostgreSQL connection via `pg` pool (fast)
5. **Client-side:** HTTP proxy to `/api/god/proxy` → then PostgreSQL (secure)
**Benefits:**
- ✅ No Directus runtime dependency
- ✅ 10x faster queries (no API overhead)
- ✅ Developer-friendly syntax (not raw SQL everywhere)
- ✅ Can swap to real Directus later if needed
**Files:** `src/lib/directus/client.ts` (274 lines), `src/pages/api/god/proxy.ts`, `src/lib/db.ts`
### Content Engine ### Content Engine
- **Spintax:** Custom recursive resolver (`{A|B|{C|D}}` support) - **Spintax:** Custom recursive resolver (`{A|B|{C|D}}` support)
- **Variables:** Handlebars-style expansion (`{{CITY}}`) - **Variables:** Handlebars-style expansion (`{{CITY}}`)