An Astro-based shop API with SQLite backend and Zod validation.
npm install
npm run dev/
├── data/ # SQLite database (auto-created)
│ └── shop.db
├── public/
├── src/
│ ├── lib/
│ │ ├── db.ts # SQLite connection & schema
│ │ ├── errors.ts # Error response helpers
│ │ └── schema.ts # Zod validation schemas
│ └── pages/
│ ├── api/
│ │ └── products.ts # Products CRUD API
│ └── index.astro
├── seed.ts # Database seeding script
└── package.json
SQLite with better-sqlite3 (WAL mode enabled).
- Default path:
./data/shop.db - Override: Set
DATABASE_PATHenv variable
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY AUTOINCREMENT |
| name | TEXT | NOT NULL |
| description | TEXT | DEFAULT '' |
| price | REAL | NOT NULL, CHECK > 0 |
| category | TEXT | DEFAULT 'uncategorized' |
| image_url | TEXT | DEFAULT '' |
| stock | INTEGER | DEFAULT 0, CHECK >= 0 |
| created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP |
| updated_at | DATETIME | DEFAULT CURRENT_TIMESTAMP |
npx tsx seed.tsAll endpoints return JSON. Base URL: http://localhost:4321/api/products
| Method | Description | Query Params | Body (JSON) |
|---|---|---|---|
GET |
List all products | page, limit |
— |
GET |
Search products | q, category, page, limit |
— |
POST |
Create product | — | name, price, stock, description, category, image_url |
PUT |
Update product | id |
Partial product fields |
DELETE |
Delete product | id |
— |
All commands run from the root of the project:
| Command | Action |
|---|---|
npm install |
Installs dependencies |
npm run dev |
Starts local dev server at localhost:4321 |
npm run build |
Build your production site to ./dist/ |
npm run preview |
Preview your build locally, before deploying |
npm run astro ... |
Run CLI commands like astro add, astro check |