New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tanstack-filesystem-collection

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tanstack-filesystem-collection

Filesystem collection adapter for TanStack DB

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

TanStack Filesystem Collection

A filesystem-based collection adapter for TanStack DB. Stores your data as JSON or CSV files on disk.

⚠️ Heads up: This is a proof-of-concept. The sync engine is a bit flaky and definitely not production-ready. Great for testing, CLI tools, and local dev though.

Why?

TanStack DB is runtime-agnostic, so why not use the filesystem as a backend? This lets you:

  • Persist data to JSON/CSV files
  • Use TanStack DB in Node.js or Bun environments
  • Build CLI apps with reactive data (think OpenTUI or similar React-in-terminal renderers)
  • Prototype without setting up a database

Note: This only works in Node.js and Bun. No browser support (obviously), and Deno isn't planned.

Install

npm install tanstack-filesystem-collection @tanstack/db
# or
bun add tanstack-filesystem-collection @tanstack/db

Quick Start

import { createCollection } from "@tanstack/db"
import { filesystemCollectionOptions } from "tanstack-filesystem-collection"
import { z } from "zod"

const todoSchema = z.object({
  id: z.string(),
  text: z.string(),
  done: z.boolean(),
})

const todos = createCollection(
  filesystemCollectionOptions({
    id: "todos",
    schema: todoSchema,
    getKey: (item) => item.id,
  })
)

This creates a todos.json file in .tanstack-collection-cache/ and keeps it in sync with your collection.

Config Options

OptionDefaultWhat it does
idrequiredCollection name (also the filename)
getKeyrequiredFunction to get unique key from an item
schema-Zod/Standard Schema for validation & types
format"json""json" or "csv"
cacheDir".tanstack-collection-cache"Where to store files
rowUpdateMode"partial""partial" merges changes, "full" replaces
runtimeauto-detectedForce "bun" or "node"
codec-Transform data on read/write
enableFileWatchfalseWatch file for external changes
fileWatchDebounce100Debounce ms for file watching

Persistence Handler Options

You can hook into mutations for backend sync:

OptionDefaultWhat it does
onInsert-Called after filesystem write on insert
onUpdate-Called after filesystem write on update
onDelete-Called after filesystem write on delete
awaitPersistencefalseWait for handlers to complete
persistenceTimeoutMs5000Timeout for handlers
swallowPersistenceErrorstrueLog errors instead of throwing

Examples

CSV Format

const logs = createCollection(
  filesystemCollectionOptions({
    id: "logs",
    format: "csv",
    getKey: (item) => item.timestamp,
  })
)

Custom Directory

const config = createCollection(
  filesystemCollectionOptions({
    id: "config",
    cacheDir: "./data",
    getKey: (item) => item.key,
  })
)

Data Transformation

const events = createCollection(
  filesystemCollectionOptions({
    id: "events",
    getKey: (item) => item.id,
    codec: {
      parse: (raw) => ({ ...raw, date: new Date(raw.date) }),
      serialize: (item) => ({ ...item, date: item.date.toISOString() }),
    },
  })
)

Utility Methods

The collection exposes some handy utils via collection.utils:

// Get the file path
todos.utils.getFilePath() // ".tanstack-collection-cache/todos.json"

// Clear the cache file
await todos.utils.clearCache()

// Local operations (bypass user handlers)
await todos.utils.insertLocally(item)
await todos.utils.updateLocally(id, item)
await todos.utils.deleteLocally(id)

// Bulk operations
await todos.utils.bulkInsertLocally(items)
await todos.utils.bulkUpdateLocally(items)
await todos.utils.bulkDeleteLocally(ids)

Using with React (CLI)

Works great with @tanstack/react-db in custom React renderers like OpenTUI:

import { useLiveQuery } from "@tanstack/react-db"

function TodoList() {
  const { data: todos } = useLiveQuery(todosCollection)
  
  return (
    <box>
      {todos.map(todo => (
        <text key={todo.id}>{todo.text}</text>
      ))}
    </box>
  )
}

Limitations

  • Node/Bun only - No browser, no Deno
  • Sync is experimental - File watching works but can be flaky
  • Not for production - This is a proof-of-concept
  • Single process - No multi-process locking (yet)

License

MIT

Keywords

filesystem

FAQs

Package last updated on 25 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts