|
| 1 | +# @tanstack/db-devtools |
| 2 | + |
| 3 | +Developer tools for TanStack DB that provide real-time insights into your collections, live queries, and transactions. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Collection Monitoring**: View all active collections with real-time status updates |
| 8 | +- **Live Query Insights**: Special handling for live queries with performance metrics |
| 9 | +- **Transaction Tracking**: Monitor all database transactions and their states |
| 10 | +- **WeakRef Architecture**: Collections are tracked without preventing garbage collection |
| 11 | +- **Framework Agnostic**: Core devtools built with Solid.js, with React and Vue wrappers |
| 12 | +- **Development Only**: Automatically tree-shaken in production builds |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +# Core devtools (built with Solid.js) |
| 18 | +npm install @tanstack/db-devtools |
| 19 | + |
| 20 | +# React wrapper |
| 21 | +npm install @tanstack/react-db-devtools |
| 22 | + |
| 23 | +# Vue wrapper |
| 24 | +npm install @tanstack/vue-db-devtools |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Core Devtools (Solid.js) |
| 30 | + |
| 31 | +```typescript |
| 32 | +import { DbDevtools } from '@tanstack/db-devtools' |
| 33 | + |
| 34 | +// Initialize devtools (must be called before creating collections) |
| 35 | +import { initializeDbDevtools } from '@tanstack/db-devtools' |
| 36 | +initializeDbDevtools() |
| 37 | + |
| 38 | +// Use the devtools component |
| 39 | +function App() { |
| 40 | + return ( |
| 41 | + <div> |
| 42 | + <h1>My App</h1> |
| 43 | + <DbDevtools /> |
| 44 | + </div> |
| 45 | + ) |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### React |
| 50 | + |
| 51 | +```tsx |
| 52 | +import { ReactDbDevtools } from '@tanstack/react-db-devtools' |
| 53 | + |
| 54 | +function App() { |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <h1>My App</h1> |
| 58 | + <ReactDbDevtools /> |
| 59 | + </div> |
| 60 | + ) |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Vue |
| 65 | + |
| 66 | +```vue |
| 67 | +<template> |
| 68 | + <div> |
| 69 | + <h1>My App</h1> |
| 70 | + <VueDbDevtools /> |
| 71 | + </div> |
| 72 | +</template> |
| 73 | +
|
| 74 | +<script setup> |
| 75 | +import { VueDbDevtools } from '@tanstack/vue-db-devtools' |
| 76 | +</script> |
| 77 | +``` |
| 78 | + |
| 79 | +## Configuration |
| 80 | + |
| 81 | +The devtools accept the following configuration options: |
| 82 | + |
| 83 | +```typescript |
| 84 | +interface DbDevtoolsConfig { |
| 85 | + initialIsOpen?: boolean |
| 86 | + position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative' |
| 87 | + panelProps?: Record<string, any> |
| 88 | + closeButtonProps?: Record<string, any> |
| 89 | + toggleButtonProps?: Record<string, any> |
| 90 | + storageKey?: string |
| 91 | + panelState?: 'open' | 'closed' |
| 92 | + onPanelStateChange?: (isOpen: boolean) => void |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Architecture |
| 97 | + |
| 98 | +### Auto-Registration |
| 99 | + |
| 100 | +Collections automatically register themselves with the devtools when created: |
| 101 | + |
| 102 | +```typescript |
| 103 | +// When devtools are initialized, this creates a window global |
| 104 | +window.__TANSTACK_DB_DEVTOOLS__ |
| 105 | + |
| 106 | +// Collections check for this global and register themselves |
| 107 | +const collection = createCollection({ |
| 108 | + // ... config |
| 109 | +}) |
| 110 | +// Collection is now visible in devtools |
| 111 | +``` |
| 112 | + |
| 113 | +### WeakRef Design |
| 114 | + |
| 115 | +The devtools use WeakRef to track collections without preventing garbage collection: |
| 116 | + |
| 117 | +- **Metadata polling**: Basic info (size, status) is polled every second |
| 118 | +- **Hard references**: Only created when viewing collection details |
| 119 | +- **Automatic cleanup**: Dead references are garbage collected |
| 120 | + |
| 121 | +### Live Query Detection |
| 122 | + |
| 123 | +Live queries are automatically detected and shown separately: |
| 124 | + |
| 125 | +```typescript |
| 126 | +// This will be marked as a live query in devtools |
| 127 | +const liveQuery = createLiveQueryCollection({ |
| 128 | + query: (q) => q.from(collection).select() |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | +## What You Can See |
| 133 | + |
| 134 | +### Collections View |
| 135 | +- Collection ID and type (regular vs live query) |
| 136 | +- Status (idle, loading, ready, error, cleaned-up) |
| 137 | +- Current size and transaction count |
| 138 | +- Creation and last update timestamps |
| 139 | +- Garbage collection settings |
| 140 | + |
| 141 | +### Live Queries View |
| 142 | +- All the above plus: |
| 143 | +- Initial run time |
| 144 | +- Total incremental runs |
| 145 | +- Average incremental run time |
| 146 | +- Last incremental run time |
| 147 | + |
| 148 | +### Transactions View |
| 149 | +- Transaction ID and state |
| 150 | +- Associated collection |
| 151 | +- Mutation details (insert, update, delete) |
| 152 | +- Optimistic vs confirmed operations |
| 153 | +- Creation and update timestamps |
| 154 | + |
| 155 | +### Collection Details |
| 156 | +- Full collection metadata |
| 157 | +- Live data with real-time updates |
| 158 | +- Individual item inspection |
| 159 | +- JSON view of all items |
| 160 | + |
| 161 | +## Performance |
| 162 | + |
| 163 | +The devtools are designed to have minimal impact on your application: |
| 164 | + |
| 165 | +- **Development only**: Automatically removed in production |
| 166 | +- **WeakRef tracking**: No memory leaks from collection references |
| 167 | +- **Efficient polling**: Only basic metadata is polled, detailed data is fetched on-demand |
| 168 | +- **Lazy loading**: UI components are loaded only when needed |
| 169 | + |
| 170 | +## Browser Support |
| 171 | + |
| 172 | +Requires modern browsers that support WeakRef (Chrome 84+, Firefox 79+, Safari 14.1+). |
| 173 | +Since this is development-only, this should not be a concern for production applications. |
0 commit comments