Skip to main content
Version: 1.3.2

Quickstart

flowchart LR
subgraph App
A[JsonDatabase.open] --> B[Collection API]
B --> C[insert/find/update/delete]
B --> D[Hooks & Schemas]
end
App -- change events --> E[(Change Log)]
App -- CLI --> F{{JSONVault}}

1. Open the database

Create a JsonDatabase instance pointing at a folder on disk:

db.js
const { JsonDatabase } = require("jsonvault");

async function openDb() {
return JsonDatabase.open({
path: "./data",
changeLog: { path: "./data/changelog.jsonl" },
ttlIntervalMs: 30_000,
});
}

2. Define (optional) schema and hooks

users.js
const { createSchema } = require("jsonvault");

const userSchema = createSchema({
fields: {
name: { type: "string", required: true },
email: {
type: "string",
required: true,
pattern: ".+@.+\\..+",
transform: (value) => value.toLowerCase(),
},
roles: { type: "array", items: "string", default: () => [] },
},
allowAdditional: false,
});

function attachUsers(db) {
return db.collection("users", {
schema: userSchema,
hooks: {
beforeInsert(doc) {
doc.slug = doc.name.toLowerCase().replace(/\s+/g, "-");
},
afterInsert(doc) {
console.log("new user", doc._id);
},
},
});
}

3. Run queries

app.js
const db = await openDb();
const users = attachUsers(db);

await users.insertOne({ name: "Ada Lovelace", email: "ADA@example.com" });
await users.updateOne({ email: "ada@example.com" }, { $set: { active: true } });

const admins = await users.find({ roles: { $contains: "admin" } });
const activeTotal = await users.count({ active: true });

await db.save();
await db.close();

4. Explore other entry points

const highValue = await db.sql`
SELECT orders.id AS orderId, users.email
FROM orders
JOIN users ON orders.userId = users._id
WHERE orders.total > 1000
ORDER BY orderId
`;

What’s next?