Jsoning

1.0.1 · active · verified Wed Apr 22

Jsoning is a lightweight, key-value JSON-based persistent database library designed for Node.js environments. It currently maintains version 1.0.1 and has a moderate release cadence, with significant updates like the v1.0.0 TypeScript rewrite. The library focuses on ease of use and beginner-friendliness, providing a simple API for common database operations such as setting, getting, pushing, and deleting data within JSON files. Key differentiators include atomic file writing to prevent data corruption, built-in TypeScript support, and EventEmitter integration for reacting to database changes, making it suitable for small projects, prototyping, and educational purposes. It requires Node.js v16 or greater for operation.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates basic CRUD operations using Jsoning, including setting and getting key-value pairs, manipulating arrays with push and remove, performing arithmetic operations, and clearing the entire database. It ensures a clean start and uses explicit file path handling for clarity.

import { Jsoning, MathOps } from 'jsoning';
import { createRequire } from 'module';
import path from 'path';
import { fileURLToPath } from 'url';

const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const dbPath = path.join(__dirname, 'temp-database.json');
const db = new Jsoning(dbPath);

async function runExample() {
  console.log('--- Initializing Database ---');
  await db.clear(); // Ensure a clean start

  // Set some values with a key
  await db.set('birthday', '07-aug');
  await db.set('age', '13');
  console.log('Set birthday and age.');

  // Push stuff to an array for a particular key
  await db.push('transformers', 'optimus prime');
  await db.push('transformers', 'bumblebee');
  await db.push('transformers', 'iron hide');
  console.log('Pushed transformers.');

  // Get the value of a key
  console.log('Transformers:', await db.get('transformers'));

  // Get all the values
  console.log('All data:', await db.all());

  // does such a value exist?
  console.log('Has "value2"?', await db.has('value2'));

  // My age keeps changing, so I'm deleting it
  console.log('Deleted age:', await db.delete('age'));

  // I got $100 for my birthday
  await db.set('money', 100);

  // and someone gave me $200 more
  await db.math('money', MathOps.Add, 200);
  console.log('Performed math operation on money.');

  // Just wanna make sure how much money I got
  console.log('Current money:', await db.get('money'));

  // RIP iron hide, he died
  await db.remove('transformers', 'iron hide');
  console.log('Removed iron hide from transformers:', await db.get('transformers'));

  // I'm getting bored, so I'm clearing the whole database
  await db.clear();
  console.log('Cleared database.');
  console.log('All data after clear:', await db.all());
}

runExample().catch(console.error);

view raw JSON →