Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

699 B
Raw Blame
import { drizzle } from 'drizzle-orm/mysql2'
import { createConnection } from 'mysql2/promise'
import { configLoader } from '../../../configuration/config.loader.js'
import * as schema from '../schema.js'

export class DatabaseConfigurationError extends Error {}

export async function getDB() {
  const mysqlUrl = configLoader().mysql?.url
  if (typeof mysqlUrl !== 'string' || !mysqlUrl.trim()) {
    throw new DatabaseConfigurationError(
      'MySQL URL is not configured. Define "mysql.url" in environment.yaml or set the SYNCIN_MYSQL_URL environment variable.'
    )
  }

  const client = await createConnection(mysqlUrl)
  return drizzle({ client, schema: { ...schema }, mode: 'default' })
}