Skip to content

E-Commerce Schema

A full e-commerce schema demonstrating product catalog and order management.

Self-referential parent allows nested category hierarchies.

schemas/catalog/category.js
export default table({
id: col.id(),
name: col.name(),
slug: col.slug().unique(),
parent: col.belongs_to("catalog.category").optional(),
description: col.text().optional(),
}).timestamps();
schemas/catalog/product.js
export default table({
id: col.id(),
name: col.name(),
slug: col.slug().unique(),
description: col.text().optional(),
price: col.money(),
stock: col.quantity(),
category: col.belongs_to("catalog.category"),
is_active: col.flag(true),
})
.timestamps()
.soft_delete();
schemas/orders/order.js
export default table({
id: col.id(),
user: col.belongs_to("auth.user"),
status: col
.enum(["pending", "paid", "shipped", "delivered", "cancelled"])
.default("pending"),
total: col.money(),
shipping_address: col.text(),
notes: col.text().optional(),
}).timestamps();

Line items join orders and products with quantity and price snapshot.

schemas/orders/order_item.js
export default table({
id: col.id(),
order: col.belongs_to("orders.order"),
product: col.belongs_to("catalog.product"),
quantity: col.quantity(),
unit_price: col.money(),
subtotal: col.money(),
}).timestamps();