A high-performance, file-based JSON database for TypeScript with advanced querying, schema validation, and memory-safe operations.
- π High Performance: One-file-per-record for optimal speed
- π Memory-Safe Locking: Thread-safe operations without filesystem artifacts
- π― Advanced Filtering: MongoDB-style query operators (
$and,$or,$not,$exists,$regex,$in,$arraySize) - π Schema Validation: Built-in Zod integration for type safety
- π Atomic Writes: Crash-safe operations with temporary file swapping
- π Path-Based: Organize data in directories and files, not just collections
- π‘οΈ Type Safety: Full TypeScript support with generics
- ποΈ Directory Management: Create and delete directories programmatically
bun add @kyvrixon/json-dbimport Database from '@kyvrixon/json-db';
import { z } from 'zod';
// Initialize database
const db = new Database('./data');
// Define schema
const UserSchema = z.object({
name: z.string(),
email: z.string().email(),
age: z.number(),
tags: z.array(z.string()).optional()
});
type User = z.infer<typeof UserSchema>;
// Create a user (writes to ./data/users/john.json)
await db.write('users/john', {
name: 'John Doe',
email: '[email protected]',
age: 30,
tags: ['developer', 'typescript']
}, UserSchema);
// Read a user
const user = await db.read<User>('users/john', UserSchema);
console.log(user); // { name: 'John Doe', email: 'john@example.com', ... }
// Find users with advanced filtering
const developers = await db.find('users', {
tags: { $in: ['developer'] },
age: { $greaterThanOrEqual: 25 }
}, UserSchema);
console.log(developers); // Map<string, User>
// Create a directory (e.g. for grouping)
await db.create('users/staff');
// Delete a user
await db.delete('users/john');
// Delete a whole directory (and all its contents)
await db.drop('users');Write a document to a path (e.g. users/123).
await db.write('users/123', { name: 'Alice', age: 25 }, UserSchema);Read a single document by path.
const user = await db.read('users/123', UserSchema);Read all documents in a directory. Returns a Map<string, T>.
const allUsers = await db.readAll('users', UserSchema);Delete a single document by path.
const deleted = await db.delete('users/123'); // Returns booleanCreate an empty directory (and parents if needed).
await db.create('users/staff');Delete a directory and all its contents.
await db.drop('users');Find documents with advanced filtering. Returns a Map<string, T>.
const adults = await db.find('users', {
age: { $greaterThanOrEqual: 18 }
}, UserSchema);
const developers = await db.find('users', {
$and: [
{ tags: { $in: ['developer'] } },
{ age: { $lessThan: 40 } }
]
}, UserSchema);Find the first document matching a filter.
const firstAdmin = await db.findOne('users', {
role: { $equals: 'admin' }
}, UserSchema);
// Returns: { id: string, data: T } | nullSimilar to MongoDB, but easier to understand.
| Operator | Description | Example |
|---|---|---|
$equals |
Exact match | { age: { $equals: 25 } } |
$notEquals |
Not equal | { status: { $notEquals: 'inactive' } } |
$greaterThan |
Greater than | { age: { $greaterThan: 18 } } |
$greaterThanOrEqual |
Greater than or equal | { age: { $greaterThanOrEqual: 21 } } |
$lessThan |
Less than | { age: { $lessThan: 65 } } |
$lessThanOrEqual |
Less than or equal | { age: { $lessThanOrEqual: 30 } } |
$in |
Value in array | { role: { $in: ['admin', 'user'] } } |
$notIn |
Value not in array | { status: { $notIn: ['banned', 'suspended'] } } |
$exists |
Field exists | { email: { $exists: true } } |
$regex |
Regular expression | { name: { $regex: /^John/ } } |
$arraySize |
Array length | { tags: { $arraySize: 3 } } |
$and |
Logical AND | { $and: [{ age: { $gt: 18 } }, { status: 'active' }] } |
$or |
Logical OR | { $or: [{ role: 'admin' }, { role: 'moderator' }] } |
$not |
Logical NOT | { $not: { status: 'inactive' } } |
const db = new Database('./data', {
createDirectory: true, // Auto-create database directory
validateOnRead: false // Validate data against schema on read
});The database is fully typed with TypeScript generics:
interface User {
name: string;
email: string;
age: number;
}
// Typed operations
const user = await db.read<User>('users/123');
const users = await db.find<User>('users', { age: { $greaterThan: 18 } });MIT