Giter VIP home page Giter VIP logo

jsoning's Introduction

jsoning

✨ A simple key-value JSON-based lightweight database. ✨


CodeCov Build Status Latest Stable Version NPM Downloads node-current FOSSA Status


View Demo · Report Bug · Request Feature


Jsoning is a simplified wrapper for Node.js that lets you write and read data to and from JSON files. It's designed to be beginner-friendly and easy to use, with a simple API that makes it easy to get started with. It's perfect for small projects, prototyping, and learning how to work with databases.

Features

  • Use existing JSON files to read and write key-value pairs
  • EventEmitters to listen to changes in the database
  • Atomic file writing to prevent data corruption
  • Easier to use than a toaster
  • TypeScript support for all the fixed-type addicts out there

Install

Node.js v16.x or greater is required for this package to work.

npm i jsoning

# pnpm if you're feeling fast
pnpm i jsoning

# yarn if you're feeling fancy
yarn add jsoning

View the full documentation here.

Basic Usage

import { Jsoning, MathOps } from 'jsoning';
const db = new Jsoning('database.json');

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

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

// Get the value of a key
console.log(await db.get('transformers')); // [ 'optimus prime', 'bumblebee', 'iron hide' ]

// Get all the values
console.log(await db.all()); // { Record<string, JSONValue> of the whole database contents }

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

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

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

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

// Just wanna make sure how much money I got
console.log(await db.get<number>('money')); // 300

// RIP iron hide, he died
await db.remove('transformers', 'iron hide');

// I'm getting bored, so I'm clearing the whole database
await db.clear();

Contributing

Please see CONTRIBUTING.md for more details on contributing!

Contributors

All Contributors

Thanks goes to these wonderful people (emoji key):

Khaleel Gibran
Khaleel Gibran

💻 📖 🎨 🚇 ⚠️
David
David

📖
Jonyk56
Jonyk56

💻
ayntee
ayntee

💻
undefine
undefine

💻 🐛 🛡️
Aditya Gupta
Aditya Gupta

💻
Manuel Maly
Manuel Maly

💻 🐛
wh0
wh0

💻
akpi816218
akpi816218

💻 📖 💡 🚧 ⚠️ 🔧

This project follows the all-contributors specification. Contributions of any kind are welcome!

License

This package is open sourced under the MIT License.

FOSSA Status

jsoning's People

Contributors

adi-g15 avatar akpi816218 avatar allcontributors[bot] avatar dependabot[bot] avatar fossabot avatar khalby786 avatar manmal avatar ygorperez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jsoning's Issues

Typescript Declarations

Is your feature request related to a problem? Please describe.
I want to use your package in a Typescript project

Describe the solution you'd like
Add Typings for jsoning

Describe alternatives you've considered
Only alternative would be using another package

Prototype Pollution Vulnerability

Describe the bug

Prototype pollution by setting a value with a key of "hasOwnProperty"

Reproducible code sample (if applicable)

await db.set("hasOwnProperty", "something");
await db.has("hasOwnProperty"); // Error is thrown

Further details

  • Jsoning version: 0.8.16

Additional context

This vulnerability causes the addition or modification of an existing property that will exist on all objects and may lead to Denial of Service or Code Execution under specific circumstances.

i also already made a PR to patch this vulnerability #8

`has` test doesn't exercise existing key case

Describe the bug

the test Jsoning#has - existing element checks for key bar, but an earlier test of clear has deleted it. and the test checks for false instead of true

Reproducible code sample (if applicable)

n/a

Expected behavior

the test exercises a case where the key exists

Further details

  • Jsoning version: master branch
  • Node.js version: n/a
  • Operating system: n/a

Additional context

n/a

async methods should be declared as returning Promise

Describe the bug

the async-ness is an internal implementation detail. as far as callers and typescript are concerned, they're methods that return a Promise.

Reproducible code sample (if applicable)

const c = db.clear();
// c is Promise, not boolean

Expected behavior

n/a

Further details

  • Jsoning version: master branch
  • Node.js version: n/a
  • Operating system: n/a

Additional context

in jsoning.d.ts

Remove from array

Following the instruction, we can add to the array. But I could not find a way how to remove an element from array.

// 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");

A jsoning question - PHP related ( although this IS a npm package ) - not to the language itself

Is it possible to include just the source file directly?
I've got a PHP project called anonupload ( supernova3339/anonupload )
The kicker is it doesn't use a database. I want to add in multi-user support and finish off plugins. Instead of going crazy, I found your kick-ass project and would like to know if this is possible.

If yes, I'll integrate your project into anonupload as a JsonDB
If no, I'll finish off what I already started.
If confused, other, whatever, feel free to ask me questions.
If implementable, I'll write code to make it possible, then I'll send a PR for said code

Add type parameter to Jsoning class

Is your feature request related to a problem? Please describe.
No problem per se

Describe the solution you'd like
I would like for the class to take a type parameter upon creation, restricting the database to that specific type and thus simplifying the programming.

Describe alternatives you've considered
I ended up doing this in my code, but it would be helpful if the class itself already accepted the type parameter.

class Database<T> extends Jsoning {
	constructor(path: string) {
		super(path);
	}

	async set(key: string, value: T): Promise<boolean> {
		return await super.set(key, value);
	}

	get(key: string): Promise<T> {
		return super.get(key);
	}

	all(): { [key: string]: T } {
		return super.all();
	}
}

Additional context
TypeScript v4.9.5

set(key, value) does not read the DB from the file system and overwrites file system changes

Describe the bug

If jsoning has written at least one value, and the json file is changed in the file system by a different process, and then set() is called with some value, then jsoning overwrites the json file with the previously known values.

Reproducible code sample (if applicable)

let db = new jsoning("database.json");
db.set("one", "one");
// Now remove database.json from the file system
db.set("two", "two");

Expected behavior

After executing the sample code (note the comment about removing the file), the expected behavior would be that database.json contains {"two": "two"}.

Faulty behavior: The database.json contains {"one": "one", "two": "two"}.

Further details

  • Jsoning version: 0.11.21
  • Node.js version: v14.17.1
  • Operating system: macOS 11.2.3

suggestions

Hello, im dream.
so i would like suggest some function and modules that you can add.

  • clone option - to clone the whole db to given path
  • events - add entry/exit/update events
  • backup - eventually update backup db

im thinking of adding these functions to your npm package so i was just asking for permission if its okay!

if its okay then i can start the code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.