Giter Site home page Giter Site logo

kifuan / json-file-database Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 430 KB

Lightweight Database on NodeJS by JSON Files

Home Page: https://kifuan.github.io/json-file-database/

License: MIT License

TypeScript 100.00%
ts json lightweight database typescript

json-file-database's Introduction

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.

json-file-database's People

Contributors

chieforz avatar dependabot[bot] avatar kifuan avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-file-database's Issues

[Suggestion] Indexing other fields

Hi, I'm benchmarking typescript json databases, and your project is really well thought out.
Your codebase is also easy to read, which is a huge plus in case of eventual abandonment, which happens a lot in the open source world.
So congrats !

I was wondering if another option could be added to CollectionOptions to allow another field than id to be indexed ?
And if this index could be made unique or not ?

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.