use axum::http::header;
use axum::response::IntoResponse;
use yorishiro_core::ResultExt;
use yorishiro_core::repositories::export;
use crate::error::ApiError;
use crate::http::middleware::auth::{Authorized, ReadScope};
#[utoipa::path(
get,
path = "/api/export.jsonl",
responses(
(status = 200, description = "JSON Lines export of every schema, entity, and relation for the tenant", content_type = "application/x-ndjson"),
(status = 401, description = "Invalid or missing credentials", body = crate::error::ApiErrorBody),
(status = 403, description = "Insufficient scope", body = crate::error::ApiErrorBody),
),
tag = "export",
)]
pub async fn export_jsonl(
mut authorized: Authorized<ReadScope>,
) -> Result<impl IntoResponse, ApiError> {
let workspace_id = authorized.ctx.workspace_id;
let records = export::export_all(authorized.conn(), workspace_id).await?;
let mut body = Vec::new();
for record in &records {
serde_json::to_writer(&mut body, record).internal()?;
body.push(b'\n');
}
Ok(([(header::CONTENT_TYPE, "application/x-ndjson")], body))
}