npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

niodb

v0.1.1

Published

The simplest local JSON database

Downloads

6

Readme

🐬 Niodb

The simplest high performance local JSON database.

Installation

$ npm i niodb --save

Quick Example

The following code creates a Nio database on an empty .json file example_data.json, and adds some key-value pairs to it.

import { Nio } from 'niodb' // OR const { Nio } = require('niodb')
const db = await new Nio('example_data.json')
db.name = 'NioDB'
db.message = 'Hello NioDB!'
db.users = {
  count: 100
}
db.users.count++

The example_data.json file after running this code is going to be:

{
  "name": "NioDB",
  "message": "Hello NioDB!",
  "users": {
    "count": 101
  }
}

Getting Started

Setting / Getting the value of a key

Setting a key in the database to hold a value is like assigning a value to a JavaScript object:

import { Nio } from 'niodb'
const db = await new Nio()

db.key = 'value'

If filepath is defined, changes to the data will be stored on disk asynchronously and atomically.

Getting the value of a key is also very simple:

console.log(db.key)

🌟 Just think of the Nio instance as a normal JavaScript object.

Wrapper methods

Alternatively, you can use wrapper methods $set and $get to do the same thing:

db.$set(key, value)
db.$get(key)

The choice is yours.

Deleting / Checking if a key exists

Just like deleting and checking for keys in JavaScript objects:

delete db.key
console.log(key in db)

Wrapper methods

db.$delete(key)
db.$exists(key)

API

Nio

  • new Nio(filepath, config):

Each Nio instance is a database binding to a .json file:

const database = await new Nio(filepath, config);

Filepath

If filepath is a string, new Nio(filepath) returns a Promise object that will return a Nio instance, so await must be used to get the instance.

If filepath is not defined, it will return a Nio instance, so no await is needed. However, for consistency, you should always use await when initializing the database.

Config

config is optional, it should be an object.

All options are:

await new Nio(filepath, {
  // this method is called when the .json file on your disk has been updated
  transactionUpdated: () => {}
})

Wrapper methods

All wrapper methods are:

  • $set(key, value): Set the value of a key. Setting the value to undefined will lead to a TypeError.
  • $get(key): Get the value of a key.
  • $delete(key): Delete a key.
  • $exists(key): Return if a key exists.
  • $randomKey(): Return a random key.
  • $rename(key, newKey): Rename key to newKey, replacing the new key if it already exists.
  • $type(key): Return the data type of the value stored in key. Possible return values are: array, object, null, number, string, boolean, and undefined.

Chaining

You can chain together wrapper methods:

const db = await new Nio()
db.content = {
  content1: 'hello',
  content2: 'this is NioDB',
  content3: 'you will like it'
}
db.content.$delete('content1').$rename('content2', 'introduction').$set('content3', true)

console.log(db)

You will get:

{
  content: {
    content3: true,
    introduction: 'this is NioDB'
  }
}

Error handling

import { DatabaseError } from 'niodb'

Test & Build

$ npm test
$ npm build

License

MIT