Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@chieforz/json-file-database

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chieforz/json-file-database

Lightweight Database on NodeJS by JSON Files

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

JSON File Database

It's not necessary to use a real database in some small projects, and I often use JSON files as databases. However, just using JSON and fs is very inconvenient, so this project is made.

Documentations

Click here.

Features

  • Pure TypeScript. This helps you make fewer mistakes about types.

  • Whenever you change the collection, it will start a timer to save the data. If some changes happen during specified delay, it will restart the timer. This is so-called debouncing.

  • It uses binary-search(for collections typed array) or AVL-tree(for collections typed avl) to maintain the data. This means their best time complexity would be O(log n).

Usage

First, download it by npm(or yarn, pnpm, etc.).

npm i json-file-database

And then use it in your project like this:

import { connect } from 'json-file-database'

/**
 * The type of elements must have an `id` property
 * to make them unique and sorted in the collection.
 */
type User = { id: number, name: string }

/**
 * Connect to the database.
 * If there is not specified file yet, it will create one after running this program.
 */
const db = connect({
    file: './db.json',
    init: {
        users: [
            { id: 1, name: 'San Zhang' },
            { id: 2, name: 'Si Li' },
            { id: 3, name: 'Wu Wang' },
        ]
    }
})

/**
 * Specify the type of elements is `User`.
 * 
 * You can go to the documentations to see how to customize all
 * options, including the `comparator` to compare the elements.
 */
const users = db<User>('users')

/**
 * Find the element with its id.
 */
console.log('The user whose id is 1:', users.find(1))


/**
 * Find all elements that match given condition.
 */
console.log('All users whose id <= 2 are:', users.findAll(u => u.id <= 2))


/**
 * Check whether this collection has the element.
 */
console.log('Whether the collection has a user whose id is 5:', users.has(5))


/**
 * Insert an element and return whether it has been inserted.
 */
console.log('Insert a user whose id is 2:', users.insert({ id: 2, name: 'Liu Zhao' }))


/**
 * List all elements.
 * 
 * You can also use `[...users]` or `for...of` because
 * it has implemented Iterable.
 */
console.log('All users are:', Array.from(users))


/**
 * Remove the element and return whether it has been removed.
 */
console.log('Remove the user whose id is 1:', users.remove(1))


/**
 * Remove all elements that match the condition, and return the number of them.
 */
console.log('Remove all users whose id < 3, the number of them is:',
        users.removeAll(u => u.id < 3)) 


/**
 * Update the element with id, and return whether it has been updated.
 */
console.log('Update the user whose id is 3:', users.update(3, { name: 'Liu Zhao' }))

At the first time you run, it will create a new file db.json and all outputs are expected. However, when you try to run it again, the outputs are not the same.

This is because if there is no file, it will use the init property when you try to connect the database; otherwise it will read it directly.

FAQs

When and How to Use AVL-based Collection?

You should use it only when you have to insert data very frequently.

Here is an example to use it:

const users = db<User>({
    name: 'users',
    type: 'avl'
})

Normally, using array-based collection is enough, because saving AVL-based collection needs to convert it to an array(in JSON), which may be expensive cost when there is large data.

The Specific Time Complexity?

Like the previous question, inserting or removing data is not friendly to array-based collection. However, find is usually what happens the most.

OperationArray-basedAVL-based
InsertO(n)O(log n)
UpdateO(log n)O(log n)
FindO(log n)O(log n)
Has (id)O(log n)O(log n)
Has (condition)O(n)O(n)
RemoveO(n)O(log n)
Remove AllO(n^2)O(n*log n)
Find AllO(n)O(n)
SaveO(1)O(n)

Besides, the space complexity of AVL-based collection to be saved is S(n) because a new array will be created(as it should be stored to a JSON file).

Any Test for Performance?

Yes, there are some tests for performance. Here is the result on my machine:

The Result of Tests

However, performance depends on many factors, and we can't judge it simply by the running time. But we can see that array-based collection is not good at removing elements for sure :(.

Additionally, we can see that linear search(search with condition) is really slow, so you should avoid using them if it is not necessary.

Keywords

FAQs

Package last updated on 14 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc