Extending
1. Using manualResult
manualResultExample: Complex Analytics Report
1
2
Write the Route Handler
// src/routes/analytics.ts
import { db } from '@/db';
import { orders } from '@/db/schema';
import { sql, desc, count, sum } from 'drizzle-orm';
import { manualResult } from '@tablecraft/engine';
import { reportConfig } from '../config/reports';
export async function GET(request: Request) {
const page = 1;
const pageSize = 25;
// 1. Your Complex Query (Raw Drizzle)
const data = await db
.select({
month: sql<string>`DATE_TRUNC('month', ${orders.createdAt})`,
totalRevenue: sum(orders.total),
orderCount: count(orders.id),
})
.from(orders)
.groupBy(sql`DATE_TRUNC('month', ${orders.createdAt})`)
.orderBy(desc(sql`DATE_TRUNC('month', ${orders.createdAt})`))
.limit(pageSize)
.offset((page - 1) * pageSize);
// 2. Count Total (for pagination metadata)
const [{ total }] = await db
.select({ total: count() })
.from(orders);
// 3. Format Response
// Wraps data + meta into the standard { data, meta } shape.
return Response.json(
manualResult(data, total, reportConfig, { page, pageSize })
);
}{
"data": [
{ "month": "2023-10-01", "totalRevenue": 5000, "orderCount": 10 }
],
"meta": {
"total": 100,
"page": 1,
"pageSize": 25,
"totalPages": 4
}
}2. Manual Exports
3. Hybrid Approach: Wrapping the Engine
Last updated
Was this helpful?
