├── .changeset └── config.json ├── .github └── workflows │ ├── autofix.yml │ ├── pr.yml │ └── release.yml ├── .gitignore ├── .husky └── pre-commit ├── .npmrc ├── .nvmrc ├── .prettierignore ├── .prettierrc ├── LICENSE ├── README.md ├── docs ├── config.json ├── framework │ ├── react │ │ └── adapter.md │ ├── solid │ │ └── adapter.md │ ├── svelte │ │ └── adapter.md │ └── vue │ │ └── adapter.md ├── index.md ├── installation.md ├── overview.md ├── quick-start.md ├── unidirectional-data-flow.lg.png └── unidirectional-data-flow.png ├── eslint.config.mjs ├── examples └── react │ └── todo │ ├── CHANGELOG.md │ ├── README.md │ ├── docker-compose.yml │ ├── drizzle.config.ts │ ├── drizzle │ ├── 0000_whole_sprite.sql │ ├── 0001_sturdy_titania.sql │ ├── 0002_update_timestamps_trigger.sql │ └── meta │ │ ├── 0000_snapshot.json │ │ ├── 0001_snapshot.json │ │ ├── 0002_snapshot.json │ │ └── _journal.json │ ├── index.html │ ├── package.json │ ├── pnpm-lock.yaml │ ├── postgres.conf │ ├── public │ └── vite.svg │ ├── scripts │ └── migrate.ts │ ├── src │ ├── App.tsx │ ├── DevTools.tsx │ ├── DiffView.tsx │ ├── api │ │ ├── server.ts │ │ └── write-to-pg.ts │ ├── assets │ │ └── react.svg │ ├── db │ │ ├── index.ts │ │ ├── postgres.ts │ │ ├── schema.ts │ │ └── validation.ts │ ├── index.css │ ├── main.tsx │ └── vite-env.d.ts │ └── vite.config.ts ├── media └── repo-header.jpg ├── package.json ├── packages ├── db-collections │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ │ ├── electric.ts │ │ ├── index.ts │ │ └── query.ts │ ├── tests │ │ ├── electric.test.ts │ │ ├── query.test.ts │ │ └── test-setup.ts │ ├── tsconfig.json │ └── vite.config.ts ├── db │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ │ ├── SortedMap.ts │ │ ├── collection.ts │ │ ├── deferred.ts │ │ ├── errors.ts │ │ ├── index.ts │ │ ├── proxy.ts │ │ ├── query │ │ │ ├── compiled-query.ts │ │ │ ├── evaluators.ts │ │ │ ├── extractors.ts │ │ │ ├── functions.ts │ │ │ ├── group-by.ts │ │ │ ├── index.ts │ │ │ ├── joins.ts │ │ │ ├── key-by.ts │ │ │ ├── order-by.ts │ │ │ ├── pipeline-compiler.ts │ │ │ ├── query-builder.ts │ │ │ ├── schema.ts │ │ │ ├── select.ts │ │ │ ├── types.ts │ │ │ └── utils.ts │ │ ├── transactions.ts │ │ ├── types.ts │ │ └── utils.ts │ ├── tests │ │ ├── SortedMap.test.ts │ │ ├── collection-getters.test.ts │ │ ├── collection-subscribe-changes.test.ts │ │ ├── collection.test.ts │ │ ├── deferred.test.ts │ │ ├── errors.test.ts │ │ ├── object-key-map.test.ts │ │ ├── preloadCollection.test.ts │ │ ├── proxy.test.ts │ │ ├── query │ │ │ ├── compiler.test.ts │ │ │ ├── conditions.test.ts │ │ │ ├── function-integration.test.ts │ │ │ ├── functions.test.ts │ │ │ ├── group-by.test.ts │ │ │ ├── having.test.ts │ │ │ ├── in-operator.test.ts │ │ │ ├── join.test.ts │ │ │ ├── key-by.test.ts │ │ │ ├── like-operator.test.ts │ │ │ ├── nested-conditions.test.ts │ │ │ ├── order-by.test.ts │ │ │ ├── query-builder │ │ │ │ ├── from.test.ts │ │ │ │ ├── group-by.test.ts │ │ │ │ ├── having.test.ts │ │ │ │ ├── join.test.ts │ │ │ │ ├── key-by.test.ts │ │ │ │ ├── order-by.test.ts │ │ │ │ ├── select-functions.test.ts │ │ │ │ ├── select.test.ts │ │ │ │ ├── where.test.ts │ │ │ │ └── with.test.ts │ │ │ ├── query-collection.test.ts │ │ │ ├── query-types.test.ts │ │ │ ├── table-alias.test.ts │ │ │ ├── types.test.ts │ │ │ ├── wildcard-select.test.ts │ │ │ └── with.test.ts │ │ ├── test-setup.ts │ │ ├── transaction-types.test.ts │ │ ├── transactions.test.ts │ │ └── utils.test.ts │ ├── tsconfig.json │ └── vite.config.ts ├── react-db │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ │ ├── index.ts │ │ ├── useLiveQuery.ts │ │ └── useOptimisticMutation.ts │ ├── tests │ │ ├── test-setup.ts │ │ └── useLiveQuery.test.tsx │ ├── tsconfig.json │ └── vite.config.ts └── vue-db │ ├── CHANGELOG.md │ ├── README.md │ ├── package.json │ ├── src │ ├── index.ts │ ├── useLiveQuery.ts │ └── useOptimisticMutation.ts │ ├── tests │ └── useLiveQuery.test.ts │ ├── tsconfig.json │ └── vite.config.ts ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── todo.md └── tsconfig.json /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@3.1.1/schema.json", 3 | "changelog": [ 4 | "@svitejs/changesets-changelog-github-compact", 5 | { "repo": "TanStack/optimistic" } 6 | ], 7 | "commit": false, 8 | "access": "public", 9 | "baseBranch": "main", 10 | "updateInternalDependencies": "patch", 11 | "fixed": [], 12 | "linked": [], 13 | "ignore": [] 14 | } 15 | -------------------------------------------------------------------------------- /.github/workflows/autofix.yml: -------------------------------------------------------------------------------- 1 | name: autofix.ci # needed to securely identify the workflow 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: [main, alpha, beta] 7 | 8 | concurrency: 9 | group: ${{ github.workflow }}-${{ github.event.number || github.ref }} 10 | cancel-in-progress: true 11 | 12 | permissions: 13 | contents: read 14 | 15 | jobs: 16 | autofix: 17 | name: autofix 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@v4.2.2 22 | - name: Setup Tools 23 | uses: tanstack/config/.github/setup@main 24 | - name: Fix formatting 25 | run: pnpm prettier --ignore-unknown . --check 26 | - name: Apply fixes 27 | uses: autofix-ci/action@551dded8c6cc8a1054039c8bc0b8b48c51dfc6ef 28 | with: 29 | commit-message: "ci: apply automated fixes" 30 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: PR 2 | 3 | on: 4 | pull_request: 5 | 6 | concurrency: 7 | group: ${{ github.workflow }}-${{ github.event.number || github.ref }} 8 | cancel-in-progress: true 9 | 10 | env: 11 | NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }} 12 | 13 | permissions: 14 | contents: read 15 | 16 | jobs: 17 | test: 18 | name: Test 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v4.2.2 23 | with: 24 | fetch-depth: 0 25 | - name: Setup Tools 26 | uses: tanstack/config/.github/setup@main 27 | - name: Get base and head commits for `nx affected` 28 | uses: nrwl/nx-set-shas@v4.3.0 29 | with: 30 | main-branch-name: main 31 | - name: Run Checks 32 | run: pnpm run lint && pnpm run build && pnpm run test 33 | preview: 34 | name: Preview 35 | runs-on: ubuntu-latest 36 | steps: 37 | - name: Checkout 38 | uses: actions/checkout@v4.2.2 39 | with: 40 | fetch-depth: 0 41 | - name: Setup Tools 42 | uses: tanstack/config/.github/setup@main 43 | - name: Build Packages 44 | run: pnpm run build 45 | - name: Publish Previews 46 | run: pnpx pkg-pr-new publish --pnpm --compact './packages/*' --template './examples/*/*' 47 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: [main, alpha, beta] 6 | 7 | concurrency: 8 | group: ${{ github.workflow }}-${{ github.event.number || github.ref }} 9 | cancel-in-progress: true 10 | 11 | env: 12 | NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }} 13 | 14 | permissions: 15 | contents: write 16 | id-token: write 17 | pull-requests: write 18 | 19 | jobs: 20 | release: 21 | name: Release 22 | if: github.repository_owner == 'TanStack' 23 | runs-on: ubuntu-latest 24 | steps: 25 | - name: Checkout 26 | uses: actions/checkout@v4.2.2 27 | with: 28 | fetch-depth: 0 29 | - name: Setup Tools 30 | uses: tanstack/config/.github/setup@main 31 | - name: Run Tests 32 | run: pnpm run lint && pnpm run build && pnpm run test 33 | - name: Run Changesets (version or publish) 34 | uses: changesets/action@v1.4.9 35 | with: 36 | version: pnpm run changeset:version 37 | publish: pnpm run changeset:publish 38 | commit: "ci: Version Packages" 39 | title: "ci: Version Packages" 40 | env: 41 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 42 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | .DS_Store 132 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | . "$(dirname -- "$0")/_/husky.sh" 3 | 4 | pnpm lint-staged 5 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | link-workspace-packages=true 2 | prefer-workspace-packages=true 3 | provenance=true 4 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 22.13.1 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | **/.next 2 | **/.nx/cache 3 | **/.svelte-kit 4 | **/coverage 5 | **/dist 6 | **/docs 7 | **/snap 8 | pnpm-lock.yaml 9 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "semi": false, 4 | "tabWidth": 2 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Kyle Mathews 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TanStack DB 2 | 3 | 4 | 5 | **A reactive client store for building super fast apps on sync** 6 | 7 | TanStack DB extends TanStack Query with collections, live queries and optimistic mutations that keep your UI reactive, consistent and blazing fast 🔥 8 | 9 |

10 | 11 | #TanStack 12 | 13 | Status - Alpha 14 | 15 | 16 | 17 | 18 | 19 | Join the discussion on Github 20 | 21 | 22 |

23 | 24 | Enjoy this library? Try the entire [TanStack](https://tanstack.com), including [TanStack Query](https://tanstack.com/query), [TanStack Store](https://tanstack.com/store), etc. 25 | 26 | ## 🚀 Why TanStack DB? 27 | 28 | TanStack DB gives you robust support for real-time sync, live queries and local writes. With no stale data, super fast re-rendering and sub-millisecond cross-collection queries — even for large complex apps. 29 | 30 | Built on a TypeScript implementation of differential dataflow ([#](https://github.com/electric-sql/d2ts)), TanStack DB gives you: 31 | 32 | - 🔥 **a blazing fast query engine**
33 | for sub-millisecond live queries — even for complex queries with joins and aggregates 34 | - 🎯 **fine-grained reactivity**
35 | to minimize component re-rendering 36 | - 💪 **robust transaction primitives**
37 | for easy optimistic mutations with sync and lifecycle support 38 | - 🌟 **normalized data**
39 | to keep your backend simple 40 | 41 | TanStack DB is **backend agnostic** and **incrementally adoptable**: 42 | 43 | - plug in any backend: sync engines, REST APIs, GraphQL, polling, custom sources 44 | - builds on [TanStack Store](https://tanstack.com/store), works with and alongside [TanStack Query](https://tanstack.com/query) 45 | 46 | ## 💥 Usage example 47 | 48 | Sync data into collections: 49 | 50 | ```ts 51 | import { createQueryCollection } from "@tanstack/db-collections" 52 | 53 | const todoCollection = createQueryCollection({ 54 | queryKey: ["todos"], 55 | queryFn: async () => fetch("/api/todos"), 56 | getId: (item) => item.id, 57 | schema: todoSchema, // any standard schema 58 | }) 59 | ``` 60 | 61 | Use live queries in your components: 62 | 63 | ```tsx 64 | import { useLiveQuery } from "@tanstack/react-db" 65 | 66 | const Todos = () => { 67 | const { data: todos } = useLiveQuery((query) => 68 | query.from({ todoCollection }).where("@completed", "=", false) 69 | ) 70 | 71 | return 72 | } 73 | ``` 74 | 75 | Apply transactional writes with local optimistic state: 76 | 77 | ```tsx 78 | import { useOptimisticMutation } from "@tanstack/react-db" 79 | 80 | const AddTodo = () => { 81 | const addTodo = useOptimisticMutation({ 82 | mutationFn: async ({ transaction }) => { 83 | const { collection, modified: newTodo } = transaction.mutations[0]! 84 | 85 | await api.todos.create(newTodo) 86 | await collection.refetch() 87 | }, 88 | }) 89 | 90 | return ( 91 |