2.0.3 • Published 2 months ago

json-file-database v2.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

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.

2.0.3

2 months ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.6.16

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.6.15

2 years ago

1.6.14

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.6.11

2 years ago

1.6.10

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.15

2 years ago

1.3.14

2 years ago

1.3.13

2 years ago

1.3.12

2 years ago

1.3.11

2 years ago

1.3.10

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago