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

json-file-database

v2.0.3

Published

Lightweight Database on NodeJS by JSON Files

Downloads

86

Readme

JSON File Database

In small projects, it's not necessary to use actual databases.

I often use JSON files to store data.

However, using JSON and fs is very inconvenient, so this project is designed for this demand.

Features

  • Pure TypeScript, helping you make fewer mistakes about types.

  • Whenever you change the collection, it will start a debounced timer.

  • Uses binary-search to maintain the data, meaning the best time complexity would be O(log n).

Note

There are breaking changes in version 2.x, aiming to provide a customizable primary key rather than id only.

Usage

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

pnpm add 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 no 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 element as `User`.
 * 
 * You can go to the documentation to see how to customize all
 * options, including the `comparator` to compare the elements.
 */
const users = db<User>({
  name: 'users',
  primaryKey: 'id',
})

/**
 * Find the element with its id.
 */
console.log('The user whose id is 1 is:', users.find({ id: 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({ id: 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({ id: 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({ id: 3, name: 'Liu Zhao' }))

The first time you run it, 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.