Basics
1
Define a Table
// src/config/products.ts
import { defineTable } from '@tablecraft/engine';
import { products } from '../db/schema'; // Your Drizzle schema
export const productConfig = defineTable(products)
// --- Display ---
.label('name', 'Product Name') // Rename column in UI/API
.hide('internal_id', 'supplier_code') // Hide sensitive/useless columns
// --- Filtering & Search ---
.search('name', 'description') // Allow search on these fields
.filter('category', 'status') // Allow exact match filtering
// --- Sorting ---
.sort('-createdAt') // Default sort: Newest first
.sortable('price', 'name') // Allow user to sort by these
// --- Pagination ---
.pageSize(20) // Default page size
.toConfig(); // Export the final config2
Create the Engine
// src/routes/products.ts
import { createTableEngine } from '@tablecraft/engine';
import { db } from '../db';
import { productConfig } from '../config/products';
// In your route handler (e.g., Hono, Express, Next.js)
app.get('/products', async (c) => {
const engine = createTableEngine({
db,
config: productConfig,
});
const query = c.req.query(); // Get URL params
const result = await engine.query(query);
return c.json(result);
});Date Range Filtering
export const orders = defineTable(schema.orders)
.filter('createdAt', 'status') // Enable filtering on 'createdAt'
.toConfig();Response Format
{
"data": [
{
"id": 1,
"name": "iPhone 15",
"price": 999,
"category": "electronics"
}
],
"meta": {
"total": 150, // Total rows matching filter
"page": 2, // Current page
"pageSize": 50, // Current page size
"totalPages": 3, // Total pages
"nextCursor": "..." // Cursor for next page
}
}Next Steps
Last updated
Was this helpful?
