A lightweight JSON database with CRUD operations and pagination. This package allows you to use a JSON file as a simple database with collections and documents.
- Simple API for CRUD operations
- Support for collections (similar to tables in SQL)
- Query filtering with
where
conditions - Pagination support
- Sorting capabilities
- Works with ES modules, CommonJS, and TypeScript
- MIT licensed
npm install simple-json-db-lite
import { JsonDB } from 'simple-json-db-lite';
const db = new JsonDB('./data/database.json');
const { JsonDB } = require('simple-json-db-lite');
const db = new JsonDB('./data/database.json');
// Create a collection (if it doesn't exist)
db.createCollection('users');
// Insert a document
const user = db.insert('users', {
name: 'John Doe',
email: 'john@example.com',
age: 30
});
console.log(user.id); // Automatically generated ID
// Find documents
const allUsers = db.find('users');
const youngUsers = db.find('users', {
where: { age: 30 }
});
// Find by ID
const john = db.findById('users', user.id);
// Update documents
const updatedCount = db.update('users',
{ status: 'active' },
{ where: { age: 30 } }
);
// Delete documents
const deletedCount = db.delete('users', {
where: { status: 'inactive' }
});
// Delete by ID
const deleted = db.deleteById('users', user.id);
// Count documents
const totalUsers = db.count('users');
const activeUsers = db.count('users', { status: 'active' });
const { data, pagination } = db.paginate('users', 1, 10, { status: 'active' });
console.log(data); // Array of users for page 1
console.log(pagination);
// {
// total: 25,
// page: 1,
// pageSize: 10,
// totalPages: 3,
// hasNext: true,
// hasPrev: false
// }
const sortedUsers = db.find('users', {
orderBy: {
field: 'age',
direction: 'desc'
}
});
new JsonDB(filePath: string)
createCollection(collectionName: string): void
insert<T>(collectionName: string, document: T): T & { id: string }
find<T>(collectionName: string, options?: QueryOptions): T[]
findById<T>(collectionName: string, id: string): T | null
update(collectionName: string, update: Record<string, any>, options?: UpdateOptions): number
delete(collectionName: string, options?: DeleteOptions): number
deleteById(collectionName: string, id: string): boolean
count(collectionName: string, where?: Record<string, any>): number
paginate<T>(collectionName: string, page?: number, pageSize?: number, where?: Record<string, any>): { data: T[], pagination: { ... } }
# Install dependencies
npm install
# Run tests
npm test
# Build the package
npm run build
MIT