package sanddb

  1. Overview
  2. Docs
A simple immutable database for the masses

Install

Dune Dependency

Authors

Maintainers

Sources

sanddb-0.3.0.tbz
sha256=3452ba21c6b448b58a12f4da42ca66d8dedd3a4105807786d1ba60a7464e1bf1
sha512=a9a412ffbff8eb13ca38144df44571074d9e6402bb5b0539b5bf04ceebb03ce45c66432792783045277309b3ca1d037e8792578e3e26c9573dcda91066d1a47e

Description

SandDB is a simple easy async immutable database

Published: 23 Mar 2022

README

SandDB: A simple immutable database for the masses

SandDB is:

  • Simple: It only does one thing, which is persisting data in a file.

  • Easy to use: SandDB's API is extremely small, so you only need to know few functions to use it.

  • Type safe: Every common dangerous operation (like parsing) is covered by the Result type, so you will know where to expect errors.

  • Immutable: Database is based on the immutable stack idea, where you can only push onto the stack.

  • Crud capable: Even though the database is immutable you still can update and delete records, by shadowing them.

  • Version keeping: Every update and delete operation will produce a new version of the affected record, without modifying the original, so you will have all versions of your data.

  • Concurrent: SandDB is based on lwt, so every database operation is asynchronous.

  • Supports multiple serializers: SandDB supports both json and biniou serialization format thanks to the atdgen library.

Documentation

API documentation

How to use it?

This how to is based on the example that you can find in the examples directory.

  1. Install SandDB

    opam install sanddb
    
  2. Define your record's type with atd

    One of the most important component of SandDB is the atd library, so you must be a little bit familiar with it to be able to use the database.

    You can see below we defined a record which contains a date fields and a data field. It's important to notice that your record's root type's name must be t, because otherwise SandDB can't recognize which type to use. You can learn more about atd here.

    type t = {
        year : int;
        month : int;
        day : int;
        data: string;
    }
    
  3. Generate atd serializers for your record's type

    a. Generate with dune

    This will generate the serializers in the build directory, so it will keep your work directory clean of generated files.

    ;This rule generates your records type file
    (rule
        (targets record_t.ml record_t.mli)
        (deps record.atd)
        (action (run atdgen -t %{deps})))
       
    ;This rule generates the json serializer
    (rule
        (targets record_j.ml record_j.mli)
        (deps record.atd)
        (action (run atdgen -j %{deps})))
    
    ;This rule generates the biniou serializer
    (rule
        (targets record_b.ml record_b.mli)
        (deps record.atd)
        (action (run atdgen -b %{deps})))
    

    b. Generate maualy by using atdgen

    In this case you will generate the serializers in your working directory.

    $ atdgen -t record.atd     # produces OCaml type definitions
    $ atdgen -j record.atd     # produces OCaml code dealing with JSON
    $ atdgen -b record.atd     # produces OCaml code dealing with Biniou
    

    Notice: You don't need to generate both json and biniou serializer if you are only using one of them.

  4. Create the database

    Database creation only needs two things:

    • Database file, which in our case will be test.txt

    • Data serializer, which we generated in the previous steps

    (*Creating a json based database with test.txt file and Record_j generated serializer*)
    let database = Sanddb.create_json_database "test.txt" (module Record_j)
    
    (*or*)
    
    (*Creating a biniou based database with test.txt file and Record_b generated serializer*)
    let database = Sanddb.create_biniou_database "test.txt" (module Record_b)
    
  5. Insert record

    When you are inserting a record into the database you basically appending the record into the database file with a generated uuid, which will be the record's id.

    let record = { year = 2018; month = 4; day = 30; data="Some data 1"}
    Sanddb.insert_record database record
    
  6. Insert shadowing record

    One of the main feature of SandDb is that it's immutable, which is a good thing, but sometimes you want to update or delete a record. That's the time when you want to use a shadowing insert, because you can insert with it a record, which will overshadow the older record, so the older record won't be visible. This is achived by using the old record's id for the new record.

    let shadowing_record = { year = 2018; month = 5; day = 1; data="Some data 2"}
    Sanddb.insert_shadowing_record database id shadowing_record
    
  7. Read all records

    This will read out every record in the database both visible and shadowed record. You will get a list of tuples, where the first item is the oldest and the last item is the newest in the list. The tuple will consist of a record id and the record's content.

    Sanddb.read_all_records database ()
    
  8. Read visible records

    This will only read out the visible records in the database and will give back a list of tuples, where the first item is the newest and the last item is the oldest. So the order of the list items will be different in this case.

    Sanddb.read_visible_records database ()
    

How to run example

Install opam: https://opam.ocaml.org/doc/Install.html

Install dependencies: opam install . --deps-only --with-test

Build repository: dune build

Run example: dune exec examples/main.exe

How to for contributors

Test it: dune test

Run repl: dune utop

Generate Doc: dune doc

Update docs folder: make update-doc

Dependencies (7)

  1. alcotest
  2. uuidm
  3. lwt
  4. base < "v0.17"
  5. atdgen
  6. dune >= "3.0"
  7. ocaml >= "4.06.0"

Dev Dependencies (1)

  1. odoc with-doc

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.