Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

5.1 KB
Raw Blame
/** Console API: the Database tab, overview with table counts, the row browser, the SQL console, restore. */
import type { Hono } from 'hono'
import { actorLabel } from '../../console-activity'
import type { AppRow, EnvRow } from '../../db'
import { quoteIdent, quoteLiteral, TABLES_QUERY } from '../../services/db'
import type { ConsoleKit } from './kit'

/** Exact row counts of the given tables in one statement (the SQL guard refuses dynamic SQL, so it is built here). */
const countsQuery = (tables: string[]) =>
  tables.map((t) => `SELECT ${quoteLiteral(t)}, count(*) FROM public.${quoteIdent(t)}`).join(' UNION ALL ')

/** Neon free plan: point-in-time restore reaches 6 hours back. */
const RESTORE_WINDOW_HOURS = 6

/** JSON-safe cells: dates and json columns would otherwise render as [object Object]. */
function cells(rows: unknown[][]): unknown[][] {
  return rows.map((r) =>
    r.map((v) => (v === null || typeof v !== 'object' ? v : v instanceof Date ? v.toISOString() : JSON.stringify(v))),
  )
}

export function consoleDatabaseRoutes(api: Hono, k: ConsoleKit): void {
  const { cp, appOf, envOf, v1 } = k

  // ---- database -------------------------------------------------------------------

  async function tableCounts(app: AppRow, env: EnvRow): Promise<{ schema: string; name: string; rows: number }[]> {
    const names = (await cp<{ rows: unknown[][] }>('POST', `${v1(app, env)}/db/sql`, { query: TABLES_QUERY })).rows.map(
      ([name]) => String(name),
    )
    if (!names.length) return []
    const counts = await cp<{ rows: unknown[][] }>('POST', `${v1(app, env)}/db/sql`, { query: countsQuery(names) })
    const byName = new Map(counts.rows.map(([name, n]) => [String(name), Number(n)]))
    return names.map((name) => ({ schema: 'public', name, rows: byName.get(name) ?? 0 }))
  }

  api.get('/apps/:id/envs/:env/db/view', async (c) => {
    const app = await appOf(c)
    const env = await envOf(c, app)
    if (!env.db_url) return c.json({ status: 'none' })
    // Each call opens its own connection (Neon), so they run side by side.
    const [metaResult, tables, migrations, backups] = await Promise.allSettled([
      cp<{
        status: string
        host?: string
        database?: string
        user?: string
        branch?: string
        size_bytes?: number | null
      }>('GET', `${v1(app, env)}/db`),
      await tableCounts(app, env),
      cp<{ file: string; status: string; deployment?: number | null; applied_at?: string }[]>(
        'GET',
        `${v1(app, env)}/db/migrations`,
      ),
      cp<{ id: string; message: string | null; ts: string; by: string }[]>('GET', `${v1(app, env)}/db/snapshots`),
    ])
    const errors = [metaResult, tables, migrations, backups]
      .filter((r): r is PromiseRejectedResult => r.status === 'rejected')
      .map((r) => (r.reason as Error).message)
    const meta = metaResult.status === 'fulfilled' ? metaResult.value : { status: 'unknown' }
    if (meta.status === 'none') return c.json({ status: 'none' })
    return c.json({
      status: meta.status,
      engine: 'Postgres',
      host: meta.host,
      name: meta.database,
      user: meta.user,
      branch: meta.branch,
      sizeBytes: meta.size_bytes ?? null,
      tables: tables.status === 'fulfilled' ? tables.value : null,
      migrations: migrations.status === 'fulfilled' ? migrations.value : null,
      backups: backups.status === 'fulfilled' ? backups.value.map((b) => ({ ...b, by: actorLabel(b.by) })) : null,
      restoreWindowHours: RESTORE_WINDOW_HOURS,
      errors,
    })
  })

  api.get('/apps/:id/envs/:env/db/rows/:table/view', async (c) => {
    const app = await appOf(c)
    const env = await envOf(c, app)
    const table = c.req.param('table')
    const limit = Math.min(Math.max(Number(c.req.query('limit') ?? 20) || 20, 1), 200)
    const offset = Math.max(Number(c.req.query('offset') ?? 0) || 0, 0)
    const search = (c.req.query('search') ?? '').trim()
    // Whole-row text match; the literal is escaped for both SQL and LIKE.
    const where = search
      ? `${quoteIdent(table)}::text ILIKE '%${search.replace(/[\\%_]/g, '\\$&').replace(/'/g, "''")}%'`
      : ''
    const q = new URLSearchParams({ limit: String(limit), offset: String(offset), ...(where ? { where } : {}) })
    // Each call opens its own database connection, so run them side by side.
    const [page, count, types] = await Promise.all([
      cp<{ columns: string[]; rows: unknown[][] }>('GET', `${v1(app, env)}/db/rows/${encodeURIComponent(table)}?${q}`),
      cp<{ rows: unknown[][] }>('POST', `${v1(app, env)}/db/sql`, {
        query: `SELECT count(*) FROM public.${quoteIdent(table)}${where ? ` WHERE ${where}` : ''}`,
      }),
      cp<{ rows: unknown[][] }>('POST', `${v1(app, env)}/db/sql`, {
        query: `SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = 'public' AND table_name = ${quoteLiteral(table)}`,
      }),
    ])
    const typeOf = new Map(types.rows.map(([name, type]) => [String(name), String(type)]))
    return c.json({
      columns: page.columns,
      types: page.columns.map((col) => typeOf.get(col) ?? null),
      rows: cells(page.rows),
      total: Number(count.rows[0]?.[0] ?? 0),
      limit,
      offset,
    })
  })
}