EJDB2

Pure C11 Embeddable JSON Database engine

Star

 Get

git clone https://github.com/Softmotions/ejdb.git
mkdir ./build && cd ./build
cmake .. -DCMAKE_BUILD_TYPE=Release
make package
sudo add-apt-repository ppa:adamansky/ejdb2
sudo apt-get install ejdb2

# Display installed files
dpkg -L ejdb2
# Using Homebrew https://brew.sh

brew install ejdb
# Note: nodejs binding is not supported on Windows
# Prerequisites: Clang or GCC, CMake, Make

yarn add ejdb2_node
Get for other platforms

 Embed

/// Sample records put/query example
#include <ejdb2/ejdb2.h>

#define CHECK(rc_)          \
  if (rc_) {                 \
    iwlog_ecode_error3(rc_); \
    return 1;                \
  }

static iwrc documents_visitor(EJDB_EXEC *ctx, const EJDB_DOC doc, int64_t *step) {
  // Print document to stderr
  return jbl_as_json(doc->raw, jbl_fstream_json_printer, stderr, JBL_PRINT_PRETTY);
}

int main() {

  EJDB_OPTS opts = {
    .kv = {
      .path = "example.db",
      .oflags = IWKV_TRUNC
    }
  };
  EJDB db;     // EJDB2 storage handle
  int64_t id;  // Document id placeholder
  JQL q = 0;   // Query instance
  JBL jbl = 0; // Json document

  iwrc rc = ejdb_init();
  CHECK(rc);

  rc = ejdb_open(&opts, &db);
  CHECK(rc);

  // First record
  rc = jbl_from_json(&jbl, "{\"name\":\"Bianca\", \"age\":4}");
  RCGO(rc, finish);
  rc = ejdb_put_new(db, "parrots", jbl, &id);
  RCGO(rc, finish);
  jbl_destroy(&jbl);

  // Second record
  rc = jbl_from_json(&jbl, "{\"name\":\"Darko\", \"age\":8}");
  RCGO(rc, finish);
  rc = ejdb_put_new(db, "parrots", jbl, &id);
  RCGO(rc, finish);
  jbl_destroy(&jbl);

  // Now execute query
  rc =  jql_create(&q, "parrots", "/[age > :age]");
  RCGO(rc, finish);

  EJDB_EXEC ux = {
    .db = db,
    .q = q,
    .visitor = documents_visitor
  };

  // Set query placeholder value.
  // Actual query will be /[age > 3]
  rc = jql_set_i64(q, "age", 0, 3);
  RCGO(rc, finish);
  
  rc = ejdb_exec(&ux); // Execute query

finish:
  jql_destroy(&q);
  jbl_destroy(&jbl);
  ejdb_close(&db);
  CHECK(rc);
  return 0;
}
/// myapp.c
#include <ejdb2/ejdb2.h>
#include <stdlib.h>

int main(int argc, char const *argv[]) {
  EJDB db;
  EJDB_OPTS opts = {
    .kv = { .path = "myapp.db" },
    .http = {
      .enabled = true,
      .blocking = true,
      .port = 9191,
      .bind = "localhost"
    }
  };
  iwrc rc = ejdb_open(&opts, &db); // blocked until http server shutdown
  if (rc) {
   iwlog_ecode_error3(rc);
   exit(1);
  }
  ejdb_close(&db);
  return 0;
}
import { EJDB2 } from 'ejdb2_node';

async function run() {
  const db = await EJDB2.open('example.db', { truncate: true });

  var id = await db.put('parrots', {'name': 'Bianca', 'age': 4});
  console.log(`Bianca record: ${id}`);

  id = await db.put('parrots', {'name': 'Darko', 'age': 8});
  console.log(`Darko record: ${id}`);

  const q = db.createQuery('/[age > :age]', 'parrots');

  for await (const doc of q.setNumber('age', 3).stream()) {
    console.log(`Found ${doc}`);
  }

  await db.close();
}

run();
package com.softmotions.ejdb2.example;

import com.softmotions.ejdb2.EJDB2;
import com.softmotions.ejdb2.EJDB2Builder;

public class EJDB2Example {

  public static void main(String[] args) {
    try (EJDB2 db = new EJDB2Builder("example.db").truncate().open()) {
      long id = db.put("parrots", "{\"name\":\"Bianca\", \"age\": 4}");
      System.out.println("Bianca record: " + id);

      id = db.put("parrots", "{\"name\":\"Darko\", \"age\": 8}");
      System.out.println("Darko record: " + id);

      db.createQuery("@parrots/[age > :age]").setLong("age", 3).execute((docId, doc) -> {
        System.out.println(String.format("Found %d %s", docId, doc));
        return 1;
      });
    }
  }
}    
import "package:ejdb2_dart/ejdb2_dart.dart";

void main() async {
  final db = await EJDB2.open('example.db', truncate: true);

  var id = await db.put('parrots', {'name': 'Bianca', 'age': 4});
  print('Bianca record: ${id}');

  id = await db.put('parrots', {'name': 'Darko', 'age': 8});
  print('Darko record: ${id}');

  final q = db.createQuery('/[age > :age]', 'parrots');
  await for (final doc in q.setInt('age', 3).execute()) {
    print('Found $doc');
  }
  await db.close();
}  
import EJDB2

let db = try EJDB2Builder("example.db").withTruncate().open()

var id = try db.put("parrots", ["name": "Bianca", "age": 4])
print("Bianca record: \(id)")

id = try db.put("parrots", ["name": "Darko", "age": 8])
print("Bianca record: \(id)")

try db.createQuery("@parrots/[age > :?]").setInt64(0, 3).list().forEach({
  print("Found \($0)")
})

try? db.close()

 Run

# EJDB 2 standalone REST/Websocket server
./jbs&

# Save JSON document
curl --data-raw '{"name": "Andy"}'      http://localhost:9191/persons

# Query document
curl --data-raw '@persons/[name=Andy]'  http://localhost:9191
1	{"name": "Andy"}

      

      

Documentation

MIT License

Copyright (c) 2012-2022 Softmotions Ltd <info@softmotions.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.