/// 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()