Git Versioned SQLite VFS

0.0.20 · active · verified Wed Apr 22

git-sqlite-vfs (version 0.0.20) provides a unique solution for versioning SQLite databases using Git. It achieves this by implementing a custom Virtual File System (VFS) extension for SQLite, sharding database files into deterministic 4KB binary pages stored in a specified directory (e.g., `.my-db`). This approach resolves the common issue of binary merge conflicts in Git when trying to version a monolithic SQLite database file, enabling effective diffing and merging of database changes. The library integrates seamlessly with popular tools like Drizzle ORM and libSQL clients, and is compatible with both Node.js (v22.5+) and Deno environments. While still in early development, it offers CLI tools to replace standard `drizzle-kit` commands for schema management and migrations within the VFS context, ensuring database changes are correctly applied to the sharded structure. Its primary differentiator is making SQLite a first-class citizen in Git-based version control workflows, enabling collaborative database schema and data evolution.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a VFS-enabled libSQL client, integrating it with Drizzle ORM, and performing basic database operations. It also highlights the automatic configuration for database compaction.

import { createVFSClient } from 'git-sqlite-vfs';
import { drizzle } from 'drizzle-orm/libsql';
import { sql } from 'drizzle-orm';

const run = async () => {
  // Initialize the VFS-enabled client, automatically loading the VFS extension
  // and configuring PRAGMAs for compaction.
  const client = await createVFSClient({
      url: 'file:.db/main.db' // Database path within the VFS sharded directory
  });

  // Integrate the VFS client with Drizzle ORM
  const db = drizzle(client);

  // Example: Create a table and insert data using Drizzle
  await db.execute(sql`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);`);
  await db.execute(sql`INSERT INTO users (name) VALUES ('Alice'), ('Bob');`);
  const result = await db.execute(sql`SELECT * FROM users;`);
  console.log('Users:', result);

  // To ensure VFS shards are compacted, trigger a VACUUM periodically
  // or rely on auto_vacuum=FULL; and journal_mode=DELETE; which createVFSClient sets.
  // await db.execute(sql`VACUUM;`);

  await client.close();
};

run().catch(console.error);

view raw JSON →