ndb

module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: AGPL-3.0

README

ndb

ndb

The API of Elasticsearch. The filters of MongoDB. One Go binary.

ndb takes the routes developers know from Elasticsearch (POST /logs/_search, _count, _bulk, _update_by_query, index patterns like logs-2026-*, date math like now-7d/d) and the query language they know from MongoDB ($in, $gte, $regex, $elemMatch, $inc, $group), and runs them on plain NDJSON. No JVM, no cluster, no schema: one daemon on a unix socket (and optional TCP with a bearer token). Go, Ruby, Rails and TypeScript SDKs speak the same API, so the same query gives the same result in every language and from curl.

curl --unix-socket /run/ndb/ndb.sock http://ndb/calls/_search -d '{
  "query": { "status": { "$in": ["missed", "busy"] }, "ts": { "$gte": "now-1d/d" } },
  "sort": { "ts": -1 },
  "limit": 50
}'

Your apps keep NDJSON as the source of truth: either they append to files (direct mode) or ndb keeps a write-ahead log for them (engine mode). ndb builds indexes and columnar segments from that data. If you delete the derived data, ndb rebuilds it.

Status: 1.0. The HTTP API is stable: routes and keys change only in a major release. See the HTTP API.

Run

Install the binary (Linux and macOS, amd64 and arm64; the script checks the sha256):

curl -fsSL https://ndb.clowk.in/install.sh | bash

On a Linux server, add --systemd: it also creates the ndb user, /var/lib/ndb, /etc/ndb/ndb.yml (never overwritten) and starts ndb.service. The socket is /run/ndb/ndb.sock.

curl -fsSL https://ndb.clowk.in/install.sh | sudo bash -s -- --systemd

Or build from source. You need Go 1.26 or newer.

git clone https://github.com/thadeu/ndb && cd ndb
make build                                  # static binary in bin/ndb

mkdir -p /tmp/ndb
bin/ndb -data-dir /tmp/ndb/data -unix /tmp/ndb/ndb.sock

Quick start (5 minutes)

Keep the daemon running, and open a second terminal.

alias nq='curl -s --unix-socket /tmp/ndb/ndb.sock -H "Content-Type: application/json"'

# 1. Is it ready?
nq http://ndb/_health
# {"status":"ok"}

# 2. Insert. The first insert creates the collection.
nq -X POST http://ndb/calls/_bulk -d '{"docs":[
  {"_id":"c1","status":"answered","dnis":"1133","duration":42,"ts":"2026-11-04T12:00:00Z"},
  {"_id":"c2","status":"failed","dnis":"1144","duration":0,"ts":"2026-11-04T12:30:00Z"},
  {"_id":"c3","status":"answered","dnis":"1133","duration":90,"ts":"2026-11-04T13:10:00Z"}]}'
# {"results":[{"_id":"c1","_v":1},{"_id":"c2","_v":1},{"_id":"c3","_v":1}]}

# 3. Search. The answer is a JSON array of documents.
nq -X POST http://ndb/calls/_search -d '{"query":{"status":"answered"},"sort":{"ts":-1},"limit":10}'
# [{"_id":"c3","status":"answered",...},{"_id":"c1","status":"answered",...}]

# 4. Aggregate: calls by status and hour.
nq -X POST http://ndb/calls/_aggregate -d '{"pipeline":[
  {"$group":{"_id":{"status":"$status","hour":{"$dateTrunc":{"date":"$ts","unit":"hour"}}},
             "n":{"$count":{}},"avg":{"$avg":"$duration"}}},
  {"$sort":{"_id":1}}]}'

# 5. Declare an index (optional), then look at the collection.
nq -X PUT http://ndb/calls -d '{"time":"ts","indexes":["status"]}'
nq http://ndb/calls/_stats

Stop the daemon with Ctrl-C. It finishes the running requests, then closes. Start it again with the same -data-dir and the data is there.

Next: docs/dev/query.md for the query language, and docs/dev/clients.md for Go, Rails and TypeScript.

Docker

docker run -d --name ndb \
  -v ndb-data:/var/lib/ndb -v ndb-run:/run/ndb \
  ghcr.io/thadeu/ndb:latest

The socket is /run/ndb/ndb.sock. Share the /run/ndb volume with your app containers. See docs/dev/deploy/docker.md.

Download

curl -fsSL https://ndb.clowk.in/install.sh | bash installs the latest release (NDB_VERSION=0.1.0 pins one; uninstall.sh removes it). Each v* tag publishes static binaries (no CGO) for Linux amd64/arm64 and macOS arm64, with a checksums.txt, at Releases. The binary also has a CLI: ndb stats, ndb size, ndb collections, ndb query, ndb compact, ndb truncate, ndb drop, ndb quarantine, ndb restore, ndb backup, ndb help (docs/dev/clients/cli.md). A systemd unit is in deploy/systemd/. The image is ghcr.io/thadeu/ndb. Client SDKs: Go github.com/thadeu/ndb/sdk/go, Ruby gems ndb (plain client) and ndb-rails (Rails), npm @thadeu/ndb. See CHANGELOG.md.

UI (console)

ndb-ui is a self-hosted web console: connections (local socket or TCP with a token), Overview, a Browser with a JSON tree editor, Dev Tools for raw requests, a live Watch, and Backups. It runs on your side, with no account.

curl -fsSL https://ndb.clowk.in/install.sh | bash -s -- --ui && ndb-ui     # http://127.0.0.1:4321
docker run -p 127.0.0.1:4321:4321 -e NDB_URL=tcp://10.10.0.1:7070 -e NDB_TOKEN=... ghcr.io/thadeu/ndb-ui

A hosted version is planned for later. See docs/dev/ui.md.

Features

  • Search with $eq $ne $gt $gte $lt $lte $in $nin $exists $regex $elemMatch $and $or $not, date math (now-7d/d), sort, offset and cursor pagination, and collection patterns (logs-2026-*).
  • aggregate with $match $group $sort $limit $skip $project, $dateTrunc for time buckets.
  • Two write modes: direct (apps append NDJSON files, ndb follows them) and engine (insert, atomic update and delete by id or by query, with a WAL).
  • watch and ack for outbox-style consumers, with an optional query filter and wal_retention to trim the WAL.
  • Optional bearer token on the TCP listener (-token-file); the unix socket needs none.
  • Secondary and unique indexes, built in the background.
  • TTL per document, retention, compaction, and a blob store for large values.
  • Quarantine for bad lines in direct mode. Online backup. Prometheus /_metrics.
  • One static binary, one data directory.

Documentation

Contributing

Bug reports and pull requests are welcome. Read CONTRIBUTING.md first. It explains the tests, the code style and how to open a pull request. Maintainers: the release steps are in docs/dev/releasing.md. Report security problems as described in SECURITY.md, not in a public issue.

Community

Use GitHub Issues for bugs and feature requests. Everyone who takes part follows the Code of Conduct.

License

The daemon is licensed under AGPL-3.0. The SDKs in sdk/ are MIT (see the gemspec; the Go and npm packages will carry the same license).

Directories

Path Synopsis
cmd
ndb command
Command ndb is the daemon.
Command ndb is the daemon.
ndbgen command
ndbgen writes deterministic NDJSON test data.
ndbgen writes deterministic NDJSON test data.
internal
agg
Package agg parses and runs the aggregation pipeline (plan §10.2, §11.3).
Package agg parses and runs the aggregation pipeline (plan §10.2, §11.3).
backup
Package backup writes and reads the backup file: one NDJSON stream with the extension .ndb (or .ndb.gz when compressed).
Package backup writes and reads the backup file: one NDJSON stream with the extension .ndb (or .ndb.gz when compressed).
bench
Package bench is the ESL outbox benchmark (ndb bench): an app that talks to FreeSWITCH writes 7 docs per call (6 events and a finishcall) and workers consume them.
Package bench is the ESL outbox benchmark (ndb bench): an app that talks to FreeSWITCH writes 7 docs per call (6 events and a finishcall) and workers consume them.
blob
Package blob is the content-addressed store of plan §7.5.
Package blob is the content-addressed store of plan §7.5.
catalog
Package catalog stores collection definitions and merges declarations from code, ndb.yml and implicit creation (plan §5).
Package catalog stores collection definitions and merges declarations from code, ndb.yml and implicit creation (plan §5).
collection
Package collection is one engine-mode collection: WAL writer, recovery, and the in-memory state (memtable) with CRUD.
Package collection is one engine-mode collection: WAL writer, recovery, and the in-memory state (memtable) with CRUD.
config
Package config loads the optional ndb.yml.
Package config loads the optional ndb.yml.
crashtest
Package crashtest kills a child process with SIGKILL at random times and checks invariants after each kill.
Package crashtest kills a child process with SIGKILL at random times and checks invariants after each kill.
cursors
Package cursors keeps the position of each watch consumer in <data_dir>/cursors.bolt: collection + consumer name -> position token.
Package cursors keeps the position of each watch consumer in <data_dir>/cursors.bolt: collection + consumer name -> position token.
diskusage
Package diskusage measures the bytes of a data dir, split by what they are for.
Package diskusage measures the bytes of a data dir, split by what they are for.
engine
Package engine opens a data dir: it takes the lock, loads the catalog and opens one collection per catalog entry.
Package engine opens a data dir: it takes the lock, loads the catalog and opens one collection per catalog entry.
feed
Package feed is the change feed of a collection: the events a consumer reads with watch, and the hub that fans live commits out to the watchers without ever blocking the writer (plan §10.4).
Package feed is the change feed of a collection: the events a consumer reads with watch, and the hub that fans live commits out to the watchers without ever blocking the writer (plan §10.4).
fs
Package fs is the only door to the disk.
Package fs is the only door to the disk.
gen
Package gen writes deterministic NDJSON test data.
Package gen writes deterministic NDJSON test data.
index
Package index has the order-preserving key encoding and the in-memory secondary index (a B-tree of key + id).
Package index has the order-preserving key encoding and the in-memory secondary index (a B-tree of key + id).
jsondoc
Package jsondoc decodes the documents that ndb has stored, on the hot read paths: queries, aggregations, seals and compactions.
Package jsondoc decodes the documents that ndb has stored, on the hot read paths: queries, aggregations, seals and compactions.
lock
Package lock makes sure only one ndb process uses a data dir.
Package lock makes sure only one ndb process uses a data dir.
metrics
Package metrics has the small pieces of the Prometheus text format that ndb needs: histograms built on atomics, and the observer of a WAL writer.
Package metrics has the small pieces of the Prometheus text format that ndb needs: histograms built on atomics, and the observer of a WAL writer.
query
Package query parses, validates and evaluates the JSON query language (plan §10.1, §10.6).
Package query parses, validates and evaluates the JSON query language (plan §10.1, §10.6).
recovery
Package recovery replays a collection WAL after a start or a crash.
Package recovery replays a collection WAL after a start or a crash.
segment
Package segment holds the immutable columnar files that a sealed memtable becomes (plan §7.3, §7.4).
Package segment holds the immutable columnar files that a sealed memtable becomes (plan §7.3, §7.4).
server
Package server serves the HTTP API of plan §12.1 on one or more listeners.
Package server serves the HTTP API of plan §12.1 on one or more listeners.
spec
Package spec loads the shared cases in spec/cases and runs them against any Engine: the library, the HTTP API, or (through their own runners) the SDKs.
Package spec loads the shared cases in spec/cases and runs them against any Engine: the library, the HTTP API, or (through their own runners) the SDKs.
tailer
Package tailer follows the NDJSON files of a direct-mode collection and feeds each line to a Sink (plan §9).
Package tailer follows the NDJSON files of a direct-mode collection and feeds each line to a Sink (plan §9).
tui
Package tui renders command output for people: tables that fit the terminal, with byte sizes and numbers a person can read.
Package tui renders command output for people: tables that fit the terminal, with byte sizes and numbers a person can read.
wal
writer
Package writer is the single writer of one collection WAL: it serializes commits, groups them into one write + one fsync, and rotates segments.
Package writer is the single writer of one collection WAL: it serializes commits, groups them into one write + one fsync, and rotates segments.
sdk
go module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL