Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.cocobase.buzz/llms.txt

Use this file to discover all available pages before exploring further.

Examples

Complete working examples for common use cases.

E-Commerce

Product Search with Filters

def main():
    category = req.get("category")
    min_price = req.get("min_price", "0")
    max_price = req.get("max_price", "10000")
    search = req.get("search", "")

    filters = {
        "status": "active",
        "price_gte": min_price,
        "price_lte": max_price
    }

    if search:
        filters["[or]name_contains"] = search
        filters["[or]description_contains"] = search

    if category:
        filters["category_id"] = category

    products = db.query("products", **filters,
        populate=["category", "brand"],
        sort="popularity",
        order="desc",
        limit=24
    )

    return {
        "products": products["data"],
        "total": products["total"],
        "has_more": products["has_more"]
    }

Order History

def main():
    user_id = req.get("user_id")
    status = req.get("status")

    filters = {"customer_id": user_id}
    if status:
        filters["status"] = status

    orders = db.query("orders", **filters,
        populate=["products", "shipping_address"],
        sort="created_at",
        order="desc",
        limit=20
    )

    return {"orders": orders["data"], "total": orders["total"]}

Social Media

User Feed

def main():
    user_id = req.get("user_id")
    page = int(req.get("page", "1"))
    per_page = 20

    following = db.get_user_relationships(user_id, "following")
    following_ids = [u["id"] for u in following["data"]]
    following_ids.append(user_id)

    filters = {"status": "published"}
    for idx, followed_id in enumerate(following_ids[:50]):
        filters[f"[or:authors]author_id_{idx}"] = followed_id

    feed = db.query("posts", **filters,
        populate=["author", "attachments"],
        sort="created_at",
        order="desc",
        limit=per_page,
        offset=(page - 1) * per_page
    )

    return {"feed": feed["data"], "page": page, "has_more": feed["has_more"]}

Follow/Unfollow

def main():
    action = req.get("action")
    user_id = req.get("user_id")
    target_id = req.get("target_id")

    if action == "follow":
        db.add_user_relationship(user_id, target_id, "following")
        return {"success": True, "message": "Followed"}

    elif action == "unfollow":
        db.remove_user_relationship(user_id, target_id, "following")
        return {"success": True, "message": "Unfollowed"}

    elif action == "is_following":
        following = db.get_user_relationships(user_id, "following")
        is_following = target_id in [u["id"] for u in following["data"]]
        return {"is_following": is_following}

Blog/CMS

Blog Post Search

def main():
    keyword = req.get("keyword", "")
    category = req.get("category")
    page = int(req.get("page", "1"))
    per_page = 20

    filters = {"status": "published"}

    if keyword:
        filters["[or:search]title_contains"] = keyword
        filters["[or:search]content_contains"] = keyword

    if category:
        filters["category_id"] = category

    posts = db.query("posts", **filters,
        populate=["author", "category", "tags"],
        sort="published_at",
        order="desc",
        limit=per_page,
        offset=(page - 1) * per_page
    )

    return {
        "posts": posts["data"],
        "total": posts["total"],
        "page": page
    }

Post with Comments

def main():
    post_id = req.get("post_id")

    post = db.find_one("posts",
        id=post_id,
        populate=["author", "category", "tags"]
    )

    if not post:
        return {"error": "Post not found"}, 404

    comments = db.query("comments",
        post_id=post_id,
        populate=["author"],
        sort="created_at",
        order="asc",
        limit=50
    )

    return {
        "post": post,
        "comments": comments["data"],
        "comment_count": comments["total"]
    }

Authentication Flows

User Registration with Welcome Email

def main():
    email = req.get("email")
    password = req.get("password")
    name = req.get("name")

    if not email or not password:
        return {"error": "Email and password required"}, 400

    # Queue welcome email in background
    queue.add("send_welcome", email=email, name=name)

    return {"success": True}

def send_welcome(email, name):
    await email.send_welcome_email(
        to_email=email,
        user_name=name,
        app_name="MyApp"
    )

Error Handling

def main():
    try:
        user_id = req.get("user_id")

        if not user_id:
            return {"error": "user_id required"}, 400

        user = db.find_one("users", id=user_id)

        if not user:
            return {"error": "User not found"}, 404

        return {"user": user}

    except Exception as e:
        print(f"Error: {e}")
        return {"error": "Internal server error"}, 500