Master PocketBase patterns for Google Antigravity IDE rapid backend development
# PocketBase Backend Patterns for Google Antigravity IDE
Build backends rapidly with PocketBase using Google Antigravity IDE. This guide covers collection schemas, real-time subscriptions, hooks, custom APIs, and deployment patterns for prototypes to production.
## Collection Schema Design
```javascript
// pb_migrations/1234567890_create_collections.js
migrate((db) => {
// Users collection (extends built-in auth)
const users = new Collection({
name: "users",
type: "auth",
schema: [
{ name: "name", type: "text", required: true, options: { min: 2, max: 100 } },
{ name: "avatar", type: "file", options: { maxSelect: 1, maxSize: 5242880, thumbs: ["100x100", "200x200"] } },
{ name: "bio", type: "text", options: { max: 500 } },
{ name: "role", type: "select", options: { values: ["user", "moderator", "admin"] } },
{ name: "preferences", type: "json" },
],
listRule: "@request.auth.id != ''",
viewRule: "@request.auth.id != ''",
createRule: "",
updateRule: "@request.auth.id = id",
deleteRule: "@request.auth.id = id && @request.auth.role = 'admin'",
});
db.save(users);
// Posts collection
const posts = new Collection({
name: "posts",
type: "base",
schema: [
{ name: "title", type: "text", required: true, options: { min: 5, max: 200 } },
{ name: "slug", type: "text", required: true, options: { pattern: "^[a-z0-9-]+$" } },
{ name: "content", type: "editor", required: true },
{ name: "excerpt", type: "text", options: { max: 300 } },
{ name: "coverImage", type: "file", options: { maxSelect: 1, mimeTypes: ["image/jpeg", "image/png", "image/webp"] } },
{ name: "author", type: "relation", required: true, options: { collectionId: users.id, cascadeDelete: false } },
{ name: "tags", type: "relation", options: { collectionId: "_tags", maxSelect: 10 } },
{ name: "published", type: "bool", options: { default: false } },
{ name: "publishedAt", type: "date" },
{ name: "viewCount", type: "number", options: { min: 0 } },
],
indexes: ["CREATE UNIQUE INDEX idx_posts_slug ON posts (slug)", "CREATE INDEX idx_posts_published ON posts (published, publishedAt)"],
listRule: "published = true || @request.auth.id = author.id",
viewRule: "published = true || @request.auth.id = author.id",
createRule: "@request.auth.id != ''",
updateRule: "@request.auth.id = author.id",
deleteRule: "@request.auth.id = author.id || @request.auth.role = 'admin'",
});
db.save(posts);
// Comments collection
const comments = new Collection({
name: "comments",
type: "base",
schema: [
{ name: "post", type: "relation", required: true, options: { collectionId: posts.id, cascadeDelete: true } },
{ name: "author", type: "relation", required: true, options: { collectionId: users.id } },
{ name: "content", type: "text", required: true, options: { min: 1, max: 2000 } },
{ name: "parent", type: "relation", options: { collectionId: "_self" } },
],
listRule: "@request.auth.id != ''",
viewRule: "@request.auth.id != ''",
createRule: "@request.auth.id != ''",
updateRule: "@request.auth.id = author.id",
deleteRule: "@request.auth.id = author.id || @request.auth.role ?= 'moderator'",
});
db.save(comments);
});
```
## TypeScript Client Integration
```typescript
// src/lib/pocketbase.ts
import PocketBase, { RecordService } from "pocketbase";
export interface User {
id: string;
email: string;
name: string;
avatar?: string;
bio?: string;
role: "user" | "moderator" | "admin";
preferences: Record<string, unknown>;
}
export interface Post {
id: string;
title: string;
slug: string;
content: string;
excerpt?: string;
coverImage?: string;
author: string;
tags: string[];
published: boolean;
publishedAt?: string;
viewCount: number;
expand?: {
author?: User;
tags?: Tag[];
};
}
export interface TypedPocketBase extends PocketBase {
collection(idOrName: "users"): RecordService<User>;
collection(idOrName: "posts"): RecordService<Post>;
collection(idOrName: "comments"): RecordService<Comment>;
}
export const pb = new PocketBase(process.env.NEXT_PUBLIC_POCKETBASE_URL) as TypedPocketBase;
// Real-time subscriptions
export function subscribeToCollection<T>(
collection: string,
callback: (data: { action: string; record: T }) => void
) {
return pb.collection(collection).subscribe("*", callback);
}
// File URL helper
export function getFileUrl(record: { id: string; collectionId: string }, filename: string, thumb?: string) {
return pb.files.getUrl(record, filename, { thumb });
}
```
## React Hooks
```typescript
// src/hooks/usePocketBase.ts
import { useEffect, useState } from "react";
import { pb, type Post, type User } from "@/lib/pocketbase";
export function usePosts(options?: { filter?: string; sort?: string }) {
const [posts, setPosts] = useState<Post[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let unsubscribe: (() => void) | undefined;
async function fetchAndSubscribe() {
try {
const records = await pb.collection("posts").getFullList<Post>({
filter: options?.filter ?? "published = true",
sort: options?.sort ?? "-publishedAt",
expand: "author,tags",
});
setPosts(records);
unsubscribe = await pb.collection("posts").subscribe<Post>("*", (e) => {
setPosts((prev) => {
if (e.action === "create") return [e.record, ...prev];
if (e.action === "update") return prev.map((p) => (p.id === e.record.id ? e.record : p));
if (e.action === "delete") return prev.filter((p) => p.id !== e.record.id);
return prev;
});
});
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
}
fetchAndSubscribe();
return () => {
unsubscribe?.();
};
}, [options?.filter, options?.sort]);
return { posts, loading, error };
}
```
## Best Practices for Google Antigravity IDE
When using PocketBase with Google Antigravity, define strict API rules for security. Use relation fields for data normalization. Implement file handling with thumbnails. Set up real-time subscriptions for live updates. Create typed client wrappers. Let Gemini 3 generate collection schemas from your data models.
Google Antigravity excels at generating PocketBase migrations and typed SDK wrappers.This PocketBase prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their pocketbase implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For PocketBase projects, consider mentioning your framework version, coding style, and any specific libraries you're using.