simple-json-db-lite
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Simple JSON DB Lite

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.

Features

  • 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

Installation

npm install simple-json-db-lite

Usage

ES Modules

import { JsonDB } from 'simple-json-db-lite';

const db = new JsonDB('./data/database.json');

CommonJS

const { JsonDB } = require('simple-json-db-lite');

const db = new JsonDB('./data/database.json');

Basic Operations

// 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' });

Pagination

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
// }

Sorting

const sortedUsers = db.find('users', {
  orderBy: {
    field: 'age',
    direction: 'desc'
  }
});

API Reference

Constructor

new JsonDB(filePath: string)

Methods

  • 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: { ... } }

Development

# Install dependencies
npm install

# Run tests
npm test

# Build the package
npm run build

License

MIT

Package Sidebar

Install

npm i simple-json-db-lite

Weekly Downloads

6

Version

1.0.0

License

MIT

Unpacked Size

51.1 kB

Total Files

8

Last publish

Collaborators

  • zobayerhossainmamun