Leaf is a lightweight NoSQL database designed for being simple and with few dependencies.
Fixes Insert persistence indexing bug
Added tag v1.0.2 for changeset 33df76e12226


browse log
browse .tar.gz




Lightweight document-oriented NoSQL Database

License: MIT

Leaf is a lightweight NoSQL database designed for being simple and easily maintainable, thanks to its small code base. It has been inspired by tinyDB.

Leaf is a fork of Clover. On 2022-03-02, Clover changed backeds from flat file to Badger, which exploded the dependencies from 13 to 148. Leaf was forked to preserve the original goal of being small and light.


  • Document oriented
  • Written in pure Golang
  • Simple and intuitive api
  • Easily maintainable

#Why Leaf?

Leaf has been written for being easily maintenable. As such, it trades performance with simplicity, and is not intented to be an alternative to more performant databases such as mongoDB or mySQL. However, there are projects where running a separate database server may result overkilled, and, for simple queries, network delay may be the major performance bottleneck. For there scenario, leafDB may be a more suitable alternative.

#Database layout

Leaf abstracts the way collections are stored on disk through the StorageEngine interface. The default implementation stores each collection in a separate text file, with each line corresponding to a different document. Each insert, update or delete operation rewrites from scratch the file corresponding to a given collection. Thus, the cost of such operations increase as the amount of data grows. In return, the absence of dead records in each file speed-ups iteration at query time. Also, to allow for fast document retrieval by id, the size and the location of each document in the corresponding file are stored in an in-memory table.

If you are really concerned about performance, you could write your own implementation.

#API usage

import (
	c "ser1.net/leaf"


#Create a new collection

db, _ := c.Open("leaf-db")

doc := c.NewDocument()
doc.Set("hello", "leaf!")

docId, _ := db.InsertOne("myCollection", doc)

doc, _ = db.Query("myCollection").FindById(docId)

#Query an existing database

db, _ := c.Open("../test-data/todos")

// find all completed todos belonging to users with id 5 and 8
docs, _ := db.Query("todos").Where(c.Field("completed").Eq(true).And(c.Field("userId").In(5, 8))).FindAll()

todo := &struct {
    Completed bool   `json:"completed"`
    Title     string `json:"title"`
    UserId    int    `json:"userId"`

for _, doc := range docs {

#Update and delete documents

db, _ := c.Open("../test-data/todos")

// mark all todos belonging to user with id 1 as completed
updates := make(map[string]interface{})
updates["completed"] = true


// delete all todos belonging to users with id 5 and 8