jsonvault
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

JsonVault

JsonVault is a lightweight library for managing JSON-based databases in Node.js applications. It provides simple and efficient methods to interact with JSON data, including storing, retrieving, updating, and deleting records.

Installation

You can install JsonVault via npm:

npm install jsonvault

Usage

To use JsonVault, follow these steps:

Initialize JsonVault

Initialize a database with a specified file name and optional initial data:

import { JsonVault } from 'jsonvault';

const dbFileName = './db.json';
const initialData = {
    users: [],
    posts: []
};

const db = new JsonVault(dbFileName, initialData);

Basic Operations

Get Data

Retrieve data from the JSON database using property paths:

// Get users data
const users = db.get('users');

// Get the first user's name
const firstName = db.get('users.0.name');

Set Data

Set or update data in the JSON database:

// Add a new user
db.set('users.1', { name: 'Bob' });

// Update a user's name
db.set('users.0.name', 'Alice');

Delete Data

Delete data from the JSON database:

// Delete a user
db.delete('users.1');

// Clear all users
db.set('users', []);

Advanced Operations

Push to Array

Append new elements to an array in the JSON database:

// Add a new post
db.get('posts').push({ title: 'New Post' });

Nested Operations

Perform chained operations on nested data:

// Update a nested property
db.get('users.0').set('name', 'Eve');

// Delete a nested property
db.get('posts.0').delete('title');

Automatic Cleanup

JsonVault automatically removes empty objects from arrays after deletions:

// Before deletion
// { "users": [{}, { "name": "Bob" }], "posts": [{ "title": "First Post" }] }

// Delete the first user
db.delete('users.0');

// After deletion (empty object removed)
// { "users": [{ "name": "Bob" }], "posts": [{ "title": "First Post" }] }

Contributing

Contributions are welcome! If you have suggestions, feature requests, or bug reports, please open an issue.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i jsonvault

Weekly Downloads

6

Version

0.0.3

License

MIT

Unpacked Size

23.5 kB

Total Files

12

Last publish

Collaborators

  • nikolapavlovic994