Files
PromptTech/backend/seed_categories.py

41 lines
1.6 KiB
Python
Raw Normal View History

2026-01-27 18:07:00 -06:00
import asyncio
from database import AsyncSessionLocal
from models import Category
def create_slug(name: str) -> str:
"""Convert name to slug"""
return name.lower().replace(" ", "-").replace("&", "and")
async def seed_categories():
"""Seed initial categories"""
default_categories = [
{"name": "Phones", "slug": "phones", "description": "Smartphones and mobile devices"},
{"name": "Laptops", "slug": "laptops", "description": "Portable computers and notebooks"},
{"name": "Tablets", "slug": "tablets", "description": "Tablet devices and e-readers"},
{"name": "Wearables", "slug": "wearables", "description": "Smartwatches and fitness trackers"},
{"name": "Accessories", "slug": "accessories", "description": "Tech accessories and peripherals"},
{"name": "Gaming", "slug": "gaming", "description": "Gaming consoles and accessories"},
{"name": "Audio", "slug": "audio", "description": "Headphones, speakers, and audio equipment"},
]
async with AsyncSessionLocal() as db:
# Check if categories already exist
from sqlalchemy import select
result = await db.execute(select(Category))
existing = result.scalars().all()
if existing:
print(f"✓ Categories already exist ({len(existing)} found)")
return
# Add categories
for cat_data in default_categories:
category = Category(**cat_data)
db.add(category)
await db.commit()
print(f"✓ Seeded {len(default_categories)} categories successfully")
if __name__ == "__main__":
asyncio.run(seed_categories())