explaingit

tonsky/datascript

5,755ClojureAudience · developerComplexity · 3/5Setup · moderate

TLDR

DataScript is a lightweight in-memory database you embed in a browser app or Clojure program, it stores all your app state in one place, lets you query it with Datalog patterns, and tracks full change history for undo/redo.

Mindmap

mindmap
  root((DataScript))
    Core Concepts
      Immutable DB
      Datalog queries
      Entity model
      History tracking
    Use Cases
      App state storage
      Undo redo
      Relational queries
      Sync layer
    Platforms
      ClojureScript
      Clojure
      JavaScript
    Ecosystem
      Datomic sync
      SQL persistence
      Roam Research
      LogSeq
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Store all your web app state in one DataScript database and query it with Datalog instead of scattering data across component-level variables.

USE CASE 2

Build an undo/redo system by keeping snapshots of immutable DataScript database versions across user actions.

USE CASE 3

Replace a client-side Redux store with DataScript to run richer relational queries across your application data.

USE CASE 4

Sync a browser DataScript state to a server-side Datomic database for persistence using a compatible library.

Tech stack

ClojureClojureScriptJavaScript

Getting it running

Difficulty · moderate Time to first run · 30min

In plain English

DataScript is an in-memory database and query engine designed to run inside a web browser or a Clojure application. The core idea is that creating a DataScript database should be about as simple and cheap as creating an ordinary data structure, like a dictionary or map. You create one on page load, fill it with your application's data, query it as needed, and discard it when the user closes the tab. The database is immutable, meaning that every change produces a new version rather than modifying the original in place. This makes it possible to track the full history of how your application's data changed over time, rewind to earlier states for undo/redo features, and always show a consistent snapshot of your data to the UI, even while updates are happening in the background. Instead of SQL, DataScript uses a query language called Datalog. Datalog lets you express questions about your data as patterns. For example, you can ask for all entities that share a certain attribute value, or follow relationships across multiple entities in a single query. Under the hood, a DataScript query is essentially a series of hashmap lookups and index scans, so it stays lightweight even without the machinery of a traditional database server. The library was built for client-side applications that track a lot of state. By storing everything in one DataScript database rather than scattered across component-local variables, different parts of an application, such as the renderer, a sync layer, or an undo stack, can each read the same state independently without stepping on each other. DataScript is available for Clojure, ClojureScript, and plain JavaScript. It is used by several well-known tools including Roam Research (a graph-based note-taking app), LogSeq (a local-first outliner notebook), and Athens Research (a networked thought tool). The README includes usage examples, links to tutorials, and a list of related libraries for things like syncing DataScript with a server-side Datomic database or persisting it to SQL storage.

Copy-paste prompts

Prompt 1
Show me how to create a DataScript database in ClojureScript, add a few entities with attributes, and run a Datalog query to find entities by a specific attribute value.
Prompt 2
I have a to-do app with tasks and tags. Write a DataScript schema and the Datalog queries needed to find all tasks that share a given tag.
Prompt 3
How do I implement undo/redo in a ClojureScript app using DataScript immutable database snapshots, walk me through the data model and state management pattern.
Prompt 4
Migrate my React and Redux app state to DataScript: how do I structure the schema, create entities from existing Redux state, and write equivalent queries?
Prompt 5
Explain how DataScript Datalog queries work under the hood, what are the hashmap lookups and index scans it uses to stay lightweight without a database server?
Open on GitHub → Explain another repo

← tonsky on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.