mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-17 00:39:41 +02:00
Mirrors the frontend unification (single IndexedDB) on the backend. All services now use pgSchema() for isolation within one shared database, enabling cross-schema JOINs, simplified ops, and zero DB setup for new apps. - Migrate 7 services from pgTable() to pgSchema(): mana-user (usr), mana-media (media), todo, traces, presi, uload, cards - Update all DATABASE_URLs in .env.development, docker-compose, configs - Rewrite init-db scripts for 2 databases + 12 schemas - Rewrite setup-databases.sh for consolidated architecture - Update shared-drizzle-config default to mana_platform - Update CLAUDE.md with new database architecture docs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
/**
|
|
* Drizzle ORM database connection factory for Hono servers.
|
|
*
|
|
* Provides a lightweight connection with sensible defaults.
|
|
* Each server defines its own minimal schema (only tables it needs).
|
|
*/
|
|
|
|
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
import postgres from 'postgres';
|
|
|
|
export interface DbOptions {
|
|
/** PostgreSQL connection URL */
|
|
url?: string;
|
|
/** Max connections (default: 5) */
|
|
maxConnections?: number;
|
|
/** Idle timeout in seconds (default: 20) */
|
|
idleTimeout?: number;
|
|
}
|
|
|
|
/**
|
|
* Create a Drizzle database instance with postgres.js driver.
|
|
*
|
|
* Usage:
|
|
* ```ts
|
|
* import { createDb } from '@manacore/shared-hono/db';
|
|
* import { tasks, projects } from './schema';
|
|
*
|
|
* const db = createDb({
|
|
* schema: { tasks, projects },
|
|
* });
|
|
* ```
|
|
*/
|
|
export function createDb<TSchema extends Record<string, unknown>>(
|
|
opts?: DbOptions & { schema?: TSchema }
|
|
) {
|
|
const url =
|
|
opts?.url ??
|
|
process.env.DATABASE_URL ??
|
|
'postgresql://manacore:devpassword@localhost:5432/mana_platform';
|
|
|
|
const connection = postgres(url, {
|
|
max: opts?.maxConnections ?? 5,
|
|
idle_timeout: opts?.idleTimeout ?? 20,
|
|
});
|
|
|
|
return drizzle(connection, {
|
|
schema: opts?.schema as TSchema,
|
|
});
|
|
}
|