Google Antigravity Directory

The #1 directory for Google Antigravity prompts, rules, workflows & MCP servers. Optimized for Gemini 3 agentic development.

Resources

PromptsMCP ServersAntigravity RulesGEMINI.md GuideBest Practices

Company

Submit PromptAntigravityAI.directory

Popular Prompts

Next.js 14 App RouterReact TypeScriptTypeScript AdvancedFastAPI GuideDocker Best Practices

Legal

Privacy PolicyTerms of ServiceContact Us
Featured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver ToolsFeatured on FazierFeatured on WayfindioAntigravity AI - Featured on Startup FameFeatured on Wired BusinessFeatured on Twelve ToolsListed on Turbo0Featured on findly.toolsFeatured on Aura++That App ShowAI ToolzShinyLaunchMillion Dot HomepageSolver Tools

© 2026 Antigravity AI Directory. All rights reserved.

The #1 directory for Google Antigravity IDE

This website is not affiliated with, endorsed by, or associated with Google LLC. "Google" and "Gemini" are trademarks of Google LLC.

Antigravity AI Directory
PromptsMCPBest PracticesUse CasesLearn
Home
Prompts
IndexedDB Offline Storage Patterns

IndexedDB Offline Storage Patterns

Build offline-first applications with IndexedDB storage patterns for Google Antigravity projects.

indexeddbofflinestoragepwa
by antigravity-team
⭐0Stars
.antigravity
# IndexedDB Offline Storage for Google Antigravity

IndexedDB provides a powerful client-side database for building offline-first applications. This guide covers modern patterns for using IndexedDB in Google Antigravity projects.

## Database Wrapper Class

```typescript
// lib/indexed-db.ts
export class IndexedDBStore<T extends { id: string }> {
    private db: IDBDatabase | null = null;
    private dbName: string;
    private storeName: string;
    private version: number;

    constructor(dbName: string, storeName: string, version = 1) {
        this.dbName = dbName;
        this.storeName = storeName;
        this.version = version;
    }

    async init(): Promise<void> {
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(this.dbName, this.version);
            request.onerror = () => reject(request.error);
            request.onsuccess = () => { this.db = request.result; resolve(); };
            request.onupgradeneeded = (event) => {
                const db = (event.target as IDBOpenDBRequest).result;
                if (!db.objectStoreNames.contains(this.storeName)) {
                    const store = db.createObjectStore(this.storeName, { keyPath: "id" });
                    store.createIndex("updatedAt", "updatedAt", { unique: false });
                    store.createIndex("syncStatus", "syncStatus", { unique: false });
                }
            };
        });
    }

    async put(item: T): Promise<void> {
        return new Promise((resolve, reject) => {
            const transaction = this.db!.transaction(this.storeName, "readwrite");
            const store = transaction.objectStore(this.storeName);
            const request = store.put({ ...item, updatedAt: Date.now(), syncStatus: "pending" });
            request.onerror = () => reject(request.error);
            request.onsuccess = () => resolve();
        });
    }

    async get(id: string): Promise<T | undefined> {
        return new Promise((resolve, reject) => {
            const transaction = this.db!.transaction(this.storeName, "readonly");
            const request = transaction.objectStore(this.storeName).get(id);
            request.onerror = () => reject(request.error);
            request.onsuccess = () => resolve(request.result);
        });
    }

    async getAll(): Promise<T[]> {
        return new Promise((resolve, reject) => {
            const transaction = this.db!.transaction(this.storeName, "readonly");
            const request = transaction.objectStore(this.storeName).getAll();
            request.onerror = () => reject(request.error);
            request.onsuccess = () => resolve(request.result);
        });
    }

    async delete(id: string): Promise<void> {
        return new Promise((resolve, reject) => {
            const transaction = this.db!.transaction(this.storeName, "readwrite");
            const request = transaction.objectStore(this.storeName).delete(id);
            request.onerror = () => reject(request.error);
            request.onsuccess = () => resolve();
        });
    }

    async getPendingSync(): Promise<T[]> {
        return new Promise((resolve, reject) => {
            const transaction = this.db!.transaction(this.storeName, "readonly");
            const index = transaction.objectStore(this.storeName).index("syncStatus");
            const request = index.getAll("pending");
            request.onerror = () => reject(request.error);
            request.onsuccess = () => resolve(request.result);
        });
    }
}
```

## React Hook for IndexedDB

```typescript
// hooks/useIndexedDB.ts
import { useState, useEffect, useCallback } from "react";
import { IndexedDBStore } from "@/lib/indexed-db";

export function useIndexedDB<T extends { id: string }>(dbName: string, storeName: string) {
    const [store, setStore] = useState<IndexedDBStore<T> | null>(null);
    const [data, setData] = useState<T[]>([]);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState<Error | null>(null);

    useEffect(() => {
        const initStore = async () => {
            try {
                const dbStore = new IndexedDBStore<T>(dbName, storeName);
                await dbStore.init();
                setStore(dbStore);
                const items = await dbStore.getAll();
                setData(items);
            } catch (err) {
                setError(err instanceof Error ? err : new Error("Init failed"));
            } finally {
                setLoading(false);
            }
        };
        initStore();
    }, [dbName, storeName]);

    const addItem = useCallback(async (item: T) => {
        if (!store) return;
        await store.put(item);
        setData((prev) => [...prev.filter((i) => i.id !== item.id), item]);
    }, [store]);

    const removeItem = useCallback(async (id: string) => {
        if (!store) return;
        await store.delete(id);
        setData((prev) => prev.filter((i) => i.id !== id));
    }, [store]);

    return { data, loading, error, addItem, removeItem };
}
```

## Sync Manager

```typescript
// lib/sync-manager.ts
export class SyncManager<T extends { id: string }> {
    private store: IndexedDBStore<T>;
    private syncEndpoint: string;
    private syncInProgress = false;

    constructor(store: IndexedDBStore<T>, syncEndpoint: string) {
        this.store = store;
        this.syncEndpoint = syncEndpoint;
        window.addEventListener("online", () => this.sync());
    }

    async sync(): Promise<void> {
        if (this.syncInProgress || !navigator.onLine) return;
        this.syncInProgress = true;
        try {
            const pendingItems = await this.store.getPendingSync();
            for (const item of pendingItems) {
                const response = await fetch(this.syncEndpoint, {
                    method: "POST",
                    headers: { "Content-Type": "application/json" },
                    body: JSON.stringify(item),
                });
                if (response.ok) await this.store.put({ ...item, syncStatus: "synced" } as T);
            }
        } finally {
            this.syncInProgress = false;
        }
    }
}
```

## Best Practices

1. **Versioning**: Implement proper database versioning for schema migrations
2. **Error Recovery**: Handle quota exceeded errors and provide user feedback
3. **Background Sync**: Use Service Workers for background synchronization
4. **Transactions**: Use transactions for atomic operations on multiple records
5. **Cleanup**: Implement data expiration policies to manage storage limits

When to Use This Prompt

This indexeddb prompt is ideal for developers working on:

  • indexeddb applications requiring modern best practices and optimal performance
  • Projects that need production-ready indexeddb code with proper error handling
  • Teams looking to standardize their indexeddb development workflow
  • Developers wanting to learn industry-standard indexeddb patterns and techniques

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 indexeddb implementations.

How to Use

  1. Copy the prompt - Click the copy button above to copy the entire prompt to your clipboard
  2. Paste into your AI assistant - Use with Claude, ChatGPT, Cursor, or any AI coding tool
  3. Customize as needed - Adjust the prompt based on your specific requirements
  4. Review the output - Always review generated code for security and correctness
💡 Pro Tip: For best results, provide context about your project structure and any specific constraints or preferences you have.

Best Practices

  • ✓ Always review generated code for security vulnerabilities before deploying
  • ✓ Test the indexeddb code in a development environment first
  • ✓ Customize the prompt output to match your project's coding standards
  • ✓ Keep your AI assistant's context window in mind for complex requirements
  • ✓ Version control your prompts alongside your code for reproducibility

Frequently Asked Questions

Can I use this indexeddb prompt commercially?

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.

Which AI assistants work best with this prompt?

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.

How do I customize this prompt for my specific needs?

You can modify the prompt by adding specific requirements, constraints, or preferences. For indexeddb projects, consider mentioning your framework version, coding style, and any specific libraries you're using.

Related Prompts

💬 Comments

Loading comments...