Files
app/lib/db.ts
Albert e1ee79a386 feat: Step 11 - 3D Thought Galaxy Visualization
Implements interactive 3D visualization of user's thought network using
React Three Fiber and UMAP dimensionality reduction.

Key components:
- /api/calculate-graph: UMAP projection from 768-D embeddings to 3-D coords
- /galaxy page: UI with "Calculate My Graph" button and 3D canvas
- ThoughtGalaxy component: Interactive R3F scene with nodes and links
- Magnitude tests: Comprehensive test coverage for galaxy features

Technical implementation:
- Uses umap-js for dimensionality reduction (768-D → 3-D)
- React Three Fiber for WebGL 3D rendering
- CameraControls for smooth navigation
- Client-side SurrealDB connection for fetching nodes/links
- Hackathon workaround: API uses root credentials with user DID filtering

Note: Authentication fix applied - API route uses root SurrealDB credentials
with JWT-extracted user DID filtering to maintain security while working
around JWT authentication issues in hackathon timeframe.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 02:40:50 +00:00

38 lines
1.1 KiB
TypeScript

import Surreal from 'surrealdb';
/**
* Connects to the SurrealDB instance and authenticates with the user's JWT.
* This enforces row-level security defined in the schema.
*
* @param token - The user's app-specific (SurrealDB) JWT
* @returns The authenticated SurrealDB instance
*/
export async function connectToDB(token: string): Promise<Surreal> {
const SURREALDB_URL = process.env.SURREALDB_URL;
const SURREALDB_NAMESPACE = process.env.SURREALDB_NS;
const SURREALDB_DATABASE = process.env.SURREALDB_DB;
if (!SURREALDB_URL || !SURREALDB_NAMESPACE || !SURREALDB_DATABASE) {
throw new Error('SurrealDB configuration is missing');
}
// Create a new instance for each request to avoid connection state issues
const db = new Surreal();
// Connect to SurrealDB
await db.connect(SURREALDB_URL);
// Authenticate as the user for this request.
// This enforces the row-level security (PERMISSIONS)
// defined in the schema for all subsequent queries.
await db.authenticate(token);
// Use the correct namespace and database
await db.use({
namespace: SURREALDB_NAMESPACE,
database: SURREALDB_DATABASE,
});
return db;
}