managarten/packages/shared-hono/src/db.ts
Till JS 32939fbfb5 refactor(infra): remove zitare + clock NestJS backends, add shared-hono package
Both apps are fully local-first via Dexie.js + mana-sync. Their NestJS
backends were pure CRUD wrappers (20 + 31 source files) that are no
longer needed.

Changes:
- Add packages/shared-hono: JWT auth via JWKS (jose), Drizzle DB factory,
  health route, generic GDPR admin handler, error middleware
- Migrate zitare lists page from fetch() to listsStore (local-first)
- Rewrite clock timers store from API-based to timerCollection (Dexie)
- Update clock +layout.svelte CommandBar search to use local collections
- Remove zitare-backend + clock-backend from docker-compose, CI/CD,
  Prometheus, env generation, setup scripts
- Add docs/TECHNOLOGY_AUDIT_2026_03.md with full repo analysis

Net result: -2 Docker containers, -2 ports, -2728 lines of code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 22:43:46 +01:00

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';
const connection = postgres(url, {
max: opts?.maxConnections ?? 5,
idle_timeout: opts?.idleTimeout ?? 20,
});
return drizzle(connection, {
schema: opts?.schema as TSchema,
});
}